Spaces:
Runtime error
Runtime error
Rename code to code_editor.py
Browse files- code +0 -0
- code_editor.py +69 -0
code
DELETED
File without changes
|
code_editor.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# code_editor.py
|
2 |
+
|
3 |
+
import json
|
4 |
+
|
5 |
+
def code_editor(value: str = "", issue_num: int = 0) -> str:
|
6 |
+
"""
|
7 |
+
Returns an HTML snippet for a CodeMirror-based code editor.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
value (str): Initial content for the code editor.
|
11 |
+
issue_num (int): The issue number to identify the editor instance.
|
12 |
+
|
13 |
+
Returns:
|
14 |
+
str: HTML string that embeds a CodeMirror editor.
|
15 |
+
"""
|
16 |
+
html = f"""
|
17 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.3/codemirror.min.css">
|
18 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.3/theme/material-darker.min.css">
|
19 |
+
|
20 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.3/codemirror.min.js"></script>
|
21 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.3/mode/python/python.min.js"></script>
|
22 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.3/mode/javascript/javascript.min.js"></script>
|
23 |
+
|
24 |
+
<style>
|
25 |
+
/* Basic styling for the editor container */
|
26 |
+
#code-editor-container-{issue_num} {{
|
27 |
+
border: 1px solid #ccc;
|
28 |
+
border-radius: 4px;
|
29 |
+
overflow: hidden;
|
30 |
+
margin: 0 auto;
|
31 |
+
}}
|
32 |
+
.CodeMirror {{
|
33 |
+
height: auto;
|
34 |
+
min-height: 300px;
|
35 |
+
}}
|
36 |
+
</style>
|
37 |
+
|
38 |
+
<div id="code-editor-container-{issue_num}">
|
39 |
+
<textarea id="code-editor-{issue_num}">{value}</textarea>
|
40 |
+
</div>
|
41 |
+
|
42 |
+
<script>
|
43 |
+
// Initialize CodeMirror on the textarea after it is loaded
|
44 |
+
document.addEventListener("DOMContentLoaded", function() {{
|
45 |
+
var editor = CodeMirror.fromTextArea(document.getElementById("code-editor-{issue_num}"), {{
|
46 |
+
lineNumbers: true,
|
47 |
+
mode: "python", // Change this to "javascript" or any other mode as needed
|
48 |
+
theme: "material-darker",
|
49 |
+
tabSize: 4
|
50 |
+
}});
|
51 |
+
|
52 |
+
// Expose the editor globally so we can interact with it elsewhere if needed
|
53 |
+
window._codeEditorInstance{issue_num} = editor;
|
54 |
+
|
55 |
+
// Handle changes and send updates to the server
|
56 |
+
editor.on("change", function() {{
|
57 |
+
const delta = editor.getValue();
|
58 |
+
const message = JSON.stringify({{
|
59 |
+
type: "code_update",
|
60 |
+
issue_num: {issue_num},
|
61 |
+
delta: delta
|
62 |
+
}});
|
63 |
+
// Send the updated code to the WebSocket server
|
64 |
+
collabWs.send(message);
|
65 |
+
}});
|
66 |
+
}});
|
67 |
+
</script>
|
68 |
+
"""
|
69 |
+
return html
|