akborana4 commited on
Commit
002b4a7
·
verified ·
1 Parent(s): 9c24e79

Create static/js/editor.js

Browse files
Files changed (1) hide show
  1. static/js/editor.js +168 -0
static/js/editor.js ADDED
@@ -0,0 +1,168 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // static/js/editor.js
2
+
3
+ let editor;
4
+
5
+ function initEditor() {
6
+ if (!document.getElementById("ace-editor")) return;
7
+
8
+ // Initialize Ace
9
+ editor = ace.edit("ace-editor");
10
+ editor.setTheme("ace/theme/tomorrow_night_eighties"); // Deep dark theme matching mockups
11
+ editor.session.setMode("ace/mode/python");
12
+ editor.setOptions({
13
+ fontSize: "14px",
14
+ fontFamily: "var(--font-mono)",
15
+ showPrintMargin: false,
16
+ enableBasicAutocompletion: true
17
+ });
18
+
19
+ // Load files immediately after init
20
+ loadFiles();
21
+ }
22
+
23
+ async function loadFiles() {
24
+ if (!currentToken) return;
25
+
26
+ try {
27
+ const res = await fetch('/api/files', {
28
+ method: 'POST',
29
+ headers: {'Content-Type': 'application/json'},
30
+ body: JSON.stringify({token: currentToken})
31
+ });
32
+ const data = await res.json();
33
+
34
+ const list = document.getElementById('file-list');
35
+ if(!list) return;
36
+
37
+ list.innerHTML = '';
38
+
39
+ if (data.files && data.files.length > 0) {
40
+ data.files.forEach(f => {
41
+ const div = document.createElement('div');
42
+ div.className = 'file-item';
43
+ div.innerHTML = `📄 ${f}`;
44
+ div.onclick = () => openFile(f);
45
+ list.appendChild(div);
46
+ });
47
+ } else {
48
+ list.innerHTML = `<div style="color: var(--text-muted); font-size: 12px; text-align: center; margin-top: 20px;">No files yet. Click + New</div>`;
49
+ }
50
+ } catch(e) {
51
+ console.error("Failed to load files", e);
52
+ }
53
+ }
54
+
55
+ function newFile() {
56
+ document.getElementById('current-filename').value = "untitled.txt";
57
+ if(editor) {
58
+ editor.setValue("", -1);
59
+ editor.session.setMode("ace/mode/text");
60
+ editor.focus();
61
+ }
62
+ }
63
+
64
+ async function openFile(filename) {
65
+ document.getElementById('current-filename').value = filename;
66
+ try {
67
+ const res = await fetch('/api/file/read', {
68
+ method: 'POST',
69
+ headers: {'Content-Type': 'application/json'},
70
+ body: JSON.stringify({token: currentToken, filename: filename})
71
+ });
72
+ const data = await res.json();
73
+
74
+ if(!data.error && editor) {
75
+ editor.setValue(data.content, -1);
76
+
77
+ // Set Syntax Highlighting
78
+ if(filename.endsWith('.py')) editor.session.setMode("ace/mode/python");
79
+ else if(filename.endsWith('.js')) editor.session.setMode("ace/mode/javascript");
80
+ else if(filename.endsWith('.html')) editor.session.setMode("ace/mode/html");
81
+ else if(filename.endsWith('.css')) editor.session.setMode("ace/mode/css");
82
+ else if(filename.endsWith('.json')) editor.session.setMode("ace/mode/json");
83
+ else editor.session.setMode("ace/mode/text");
84
+ }
85
+ } catch (e) {
86
+ alert("Network error reading file.");
87
+ }
88
+ }
89
+
90
+ async function saveFile() {
91
+ const filename = document.getElementById('current-filename').value;
92
+ const content = editor ? editor.getValue() : "";
93
+
94
+ if(!filename) {
95
+ alert("Please enter a filename");
96
+ return;
97
+ }
98
+
99
+ const saveBtn = document.querySelector('.editor-btn.primary');
100
+ saveBtn.innerText = "Saving...";
101
+
102
+ try {
103
+ const res = await fetch('/api/file/save', {
104
+ method: 'POST',
105
+ headers: {'Content-Type': 'application/json'},
106
+ body: JSON.stringify({token: currentToken, filename: filename, content: content})
107
+ });
108
+ const data = await res.json();
109
+
110
+ if(data.success) {
111
+ saveBtn.innerText = "✓ Saved";
112
+ setTimeout(() => saveBtn.innerText = "💾 Save", 2000);
113
+ loadFiles(); // Refresh list just in case it's a new file
114
+ } else {
115
+ alert("Error saving: " + data.error);
116
+ saveBtn.innerText = "💾 Save";
117
+ }
118
+ } catch(e) {
119
+ alert("Network error while saving.");
120
+ saveBtn.innerText = "💾 Save";
121
+ }
122
+ }
123
+
124
+ async function renameFile() {
125
+ const newName = prompt("Enter new filename:");
126
+ if(!newName) return;
127
+
128
+ const oldName = document.getElementById('current-filename').value;
129
+
130
+ try {
131
+ await fetch('/api/file/rename', {
132
+ method: 'POST',
133
+ headers: {'Content-Type': 'application/json'},
134
+ body: JSON.stringify({token: currentToken, filename: oldName, new_name: newName})
135
+ });
136
+ document.getElementById('current-filename').value = newName;
137
+ loadFiles();
138
+ } catch (e) {
139
+ alert("Error renaming file.");
140
+ }
141
+ }
142
+
143
+ async function aiEdit() {
144
+ const promptStr = prompt("✨ AI Edit: What should the AI do to this code? (e.g., 'Fix the loop bug', 'Add comments')");
145
+ if(!promptStr || !editor) return;
146
+
147
+ const originalCode = editor.getValue();
148
+ editor.setValue("✨ AI is analyzing and rewriting your code... please wait.", -1);
149
+
150
+ try {
151
+ const res = await fetch('/api/ai_edit', {
152
+ method: 'POST',
153
+ headers: {'Content-Type': 'application/json'},
154
+ body: JSON.stringify({prompt: promptStr, content: originalCode})
155
+ });
156
+ const data = await res.json();
157
+
158
+ if(data.code && !data.code.includes("NETWORK_ERROR")) {
159
+ editor.setValue(data.code, -1);
160
+ } else {
161
+ editor.setValue(originalCode, -1);
162
+ alert("AI Edit failed: \n" + (data.code || "Unknown error"));
163
+ }
164
+ } catch(e) {
165
+ editor.setValue(originalCode, -1);
166
+ alert("Network error during AI edit.");
167
+ }
168
+ }