xarical commited on
Commit
ef60c31
·
verified ·
1 Parent(s): d4d3544

Upload 5 files

Browse files
Files changed (5) hide show
  1. code.txt +4 -0
  2. editor.js +92 -0
  3. index.html +21 -0
  4. main.py +35 -0
  5. requirements.txt +1 -0
code.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ print("This is code lmao")
2
+ while True:
3
+ pass # this is more code lmao
4
+ # this is a comment lmao
editor.js ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ require.config({paths: {'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.33.0/min/vs'}});
2
+
3
+ require(['vs/editor/editor.main'], function() {
4
+ const editor = monaco.editor.create(document.getElementById('editor'), {
5
+ value: '',
6
+ language: 'python',
7
+ theme: 'vs-dark',
8
+ automaticLayout: true
9
+ });
10
+ const myself = Math.random();
11
+ let flag = false;
12
+
13
+ function normalizeContent(content) {
14
+ return content.replace(/\r\n/g, '\n').trim();
15
+ }
16
+
17
+ function fetchContent() {
18
+ fetch('/content', {
19
+ method: 'GET',
20
+ headers: {
21
+ 'Content-Type': 'application/json',
22
+ }
23
+ })
24
+ .then(response => {
25
+ if (!response.ok) {
26
+ throw new Error('Whoops, something went wrong');
27
+ } else {
28
+ return response.json()
29
+ }
30
+ })
31
+ .then(data => {
32
+ const currentContent = editor.getValue();
33
+ let hasChanged = normalizeContent(currentContent) !== normalizeContent(data.content)
34
+
35
+ if (hasChanged && myself != data.lastEditor) {
36
+ flag = true;
37
+ const selection = editor.getSelection();
38
+ const model = editor.getModel();
39
+ model.pushEditOperations(
40
+ [],
41
+ [
42
+ {
43
+ range: model.getFullModelRange(),
44
+ text: data.content,
45
+ }
46
+ ]
47
+ );
48
+ editor.setSelection(selection);
49
+ }
50
+ })
51
+ .catch((error) => {
52
+ console.log(error);
53
+ });
54
+ }
55
+
56
+ const fetchDelay = 1000
57
+ function fetchContentLoop() {
58
+ (function loop() {
59
+ setTimeout(() => {
60
+ fetchContent();
61
+ loop();
62
+ }, fetchDelay);
63
+ })();
64
+ }
65
+ fetchContentLoop();
66
+
67
+ editor.onDidChangeModelContent(function() {
68
+ if (flag){
69
+ flag = false;
70
+ return;
71
+ } else {
72
+ fetch('/update', {
73
+ method: 'POST',
74
+ headers: {
75
+ 'Content-Type': 'application/json',
76
+ },
77
+ body: JSON.stringify({
78
+ code: editor.getValue(),
79
+ iam: myself
80
+ })
81
+ })
82
+ .then(response => {
83
+ if (!response.ok) {
84
+ throw new Error('Whoops, something went wrong');
85
+ }
86
+ })
87
+ .catch((error) => {
88
+ console.log(error);
89
+ });
90
+ }
91
+ });
92
+ });
index.html ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>My Code Editor</title>
7
+ <style>
8
+ #editor {
9
+ width: 100%;
10
+ height: 600px;
11
+ border: 1px solid #000;
12
+ }
13
+ </style>
14
+ </head>
15
+
16
+ <body>
17
+ <div id="editor"></div>
18
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.33.0/min/vs/loader.js"></script>
19
+ <script src="editor.js"></script>
20
+ </body>
21
+ </html>
main.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, abort, request, json, jsonify, send_file
2
+ import os
3
+
4
+ app = Flask(__name__)
5
+ last_editor = "1"
6
+
7
+ @app.route('/', methods=['GET'])
8
+ def home():
9
+ return send_file('index.html')
10
+
11
+ @app.route('/editor.js', methods=['GET'])
12
+ def get_editor():
13
+ return send_file('editor.js')
14
+
15
+ @app.route('/content', methods=['GET'])
16
+ def get_content():
17
+ if os.path.exists('code.txt'):
18
+ with open('code.txt', 'r') as file:
19
+ content = file.read()
20
+ return jsonify({'content': content, 'lastEditor': last_editor})
21
+ else:
22
+ abort(404, description="Whoops! Document not found")
23
+
24
+ @app.route('/update', methods=['POST'])
25
+ def update_file():
26
+ data = json.loads(request.data)
27
+ code = data["code"]
28
+ global last_editor
29
+ last_editor = data["iam"]
30
+ with open('code.txt', 'w') as file:
31
+ file.write(code)
32
+ return ""
33
+
34
+ if __name__ == "__main__":
35
+ app.run(host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ flask