-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_editor.html
More file actions
415 lines (375 loc) · 12.5 KB
/
code_editor.html
File metadata and controls
415 lines (375 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>editor</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./lib/dracula.css">
<script src="./lib/highlight.pack.js"></script>
<style>
:root {
--bg-deep: #0b0e14;
--bg-editor: #282a36;
--accent: #bd93f9;
--text-main: #f8f8f2;
--text-dim: #6272a4;
--border: rgba(189, 147, 249, 0.2);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Inter', system-ui, sans-serif;
background: var(--bg-deep);
color: var(--text-main);
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
#app-header {
flex-shrink: 0;
padding: 10px 20px;
background: #1e1f29;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid var(--border);
}
.brand {
font-size: 1.1rem;
font-weight: 700;
color: var(--accent);
text-transform: uppercase;
}
.controls {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
button {
background: rgba(255, 255, 255, 0.05);
border: 1px solid var(--border);
color: var(--text-main);
padding: 6px 12px;
border-radius: 6px;
cursor: pointer;
font-size: 0.85rem;
transition: all 0.2s;
}
button:hover {
background: rgba(189, 147, 249, 0.15);
}
button.primary {
background: var(--accent);
color: var(--bg-deep);
border: none;
}
#main-container {
flex: 1;
display: flex;
flex-direction: column;
min-height: 0;
position: relative;
}
#editor-mode,
#preview-mode {
flex: 1;
display: none;
flex-direction: column;
min-height: 0;
}
#editor-mode.active,
#preview-mode.active {
display: flex;
}
#editor-container {
flex: 1;
display: flex;
min-height: 0;
background: var(--bg-editor);
}
#line-numbers {
flex-shrink: 0;
width: 50px;
background: rgba(0, 0, 0, 0.2);
color: var(--text-dim);
text-align: right;
padding: 15px 8px;
user-select: none;
border-right: 1px solid var(--border);
font-family: monospace;
font-size: 14px;
line-height: 1.6;
overflow-y: auto;
overflow-x: hidden;
}
#line-numbers::-webkit-scrollbar {
width: 0;
}
.editor-wrap {
flex: 1;
position: relative;
min-width: 0;
min-height: 0;
}
#code-input {
width: 100%;
height: 100%;
padding: 15px;
background: var(--bg-editor);
color: var(--text-main);
caret-color: var(--text-main);
border: none;
resize: none;
outline: none;
white-space: pre;
overflow: auto;
font-family: 'Fira Code', monospace;
font-size: 14px;
line-height: 1.6;
tab-size: 4;
}
#toolbar {
flex-shrink: 0;
display: flex;
gap: 6px;
padding: 6px 15px;
background: rgba(0, 0, 0, 0.3);
border-top: 1px solid var(--border);
overflow-x: auto;
}
.overlay-panel {
display: none;
position: absolute;
top: 10px;
right: 20px;
background: #282a36;
border: 1px solid var(--accent);
padding: 10px;
border-radius: 8px;
z-index: 1000;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
gap: 8px;
align-items: center;
}
.overlay-panel input {
background: #1e1f29;
border: 1px solid var(--border);
color: white;
padding: 4px 8px;
border-radius: 4px;
}
#status-bar {
flex-shrink: 0;
padding: 4px 15px;
background: #1e1f29;
font-size: 11px;
color: var(--text-dim);
display: flex;
justify-content: space-between;
border-top: 1px solid rgba(255, 255, 255, 0.05);
}
#preview-frame {
flex: 1;
border: none;
background: white;
}
</style>
</head>
<body>
<header id="app-header">
<div class="brand">editor</div>
<div class="controls">
<input type="file" id="file-open" style="display:none;">
<button onclick="document.getElementById('file-open').click()">📂 Open</button>
<button id="undo-btn">↩️ Undo</button>
<button id="redo-btn">↪️ Redo</button>
<button id="find-btn">🔍 Search</button>
<button id="jump-btn">🔢 Jump</button>
<button id="fullscreen-btn">🖥️ Full</button>
<button id="run-btn" class="primary">▶️ Run</button>
<button id="stop-btn" style="display:none;">🛑 Stop</button>
<button id="export-btn">⬇️ Save</button>
</div>
</header>
<main id="main-container">
<div id="search-panel" class="overlay-panel">
<input type="text" id="search-input" placeholder="Find...">
<button onclick="findNext()">Next</button>
<button onclick="togglePanel('search-panel')">X</button>
</div>
<div id="jump-panel" class="overlay-panel">
<input type="number" id="jump-input" placeholder="Line...">
<button onclick="jumpToLine()">Go</button>
<button onclick="togglePanel('jump-panel')">X</button>
</div>
<div id="editor-mode" class="active">
<div id="editor-container">
<div id="line-numbers"></div>
<div class="editor-wrap">
<textarea id="code-input" spellcheck="false" placeholder="Paste code here..."></textarea>
</div>
</div>
<div id="toolbar">
<button onclick="insert('<div>\n \n</div>')"><div></button>
<button onclick="insert('<span>', '</span>')"><span></button>
<button onclick="insert('<script>\n ', '\n</script>')"><script></button>
<button onclick="insert('style="', '"')">style</button>
<button onclick="insert('class="', '"')">class</button>
<button onclick="insert('(', ')')">( )</button>
<button onclick="insert('{ ', ' }')">{ }</button>
<button onclick="insert(';')">;</button>
</div>
</div>
<div id="preview-mode">
<iframe id="preview-frame" sandbox="allow-scripts allow-modals allow-same-origin"></iframe>
</div>
</main>
<footer id="status-bar">
<div id="file-info">untitled.html</div>
<div id="stats">Line 1, Col 1 | 0 Chars</div>
</footer>
<script>
const codeInput = document.getElementById('code-input');
const lineNumbers = document.getElementById('line-numbers');
const stats = document.getElementById('stats');
let history = [''];
let historyIdx = 0;
function updateEditor() {
const code = codeInput.value;
const lines = code.split('\n');
lineNumbers.innerHTML = lines.map((_, i) => `<div>${i + 1}</div>`).join('');
const cursor = codeInput.selectionStart;
const lineNr = code.substring(0, cursor).split('\n').length;
const colNr = cursor - code.lastIndexOf('\n', cursor - 1);
stats.textContent = `Line ${lineNr}, Col ${colNr} | ${code.length} Chars`;
if (code !== history[historyIdx]) {
history = history.slice(0, historyIdx + 1);
history.push(code);
if (history.length > 50) history.shift();
else historyIdx++;
}
}
codeInput.addEventListener('scroll', () => {
lineNumbers.scrollTop = codeInput.scrollTop;
});
codeInput.addEventListener('input', updateEditor);
codeInput.addEventListener('click', updateEditor);
codeInput.addEventListener('keydown', (e) => {
if (e.key === 'Tab') {
e.preventDefault();
insert(" ");
}
});
function insert(before, after = '') {
const start = codeInput.selectionStart;
const end = codeInput.selectionEnd;
const val = codeInput.value;
const selected = val.substring(start, end);
codeInput.value = val.substring(0, start) + before + selected + after + val.substring(end);
codeInput.selectionStart = start + before.length;
codeInput.selectionEnd = start + before.length + selected.length;
codeInput.focus();
updateEditor();
}
document.getElementById('file-open').onchange = function (e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (ev) => {
codeInput.value = ev.target.result;
document.getElementById('file-info').textContent = file.name;
updateEditor();
};
reader.readAsText(file);
};
function togglePanel(id) {
const panel = document.getElementById(id);
panel.style.display = (panel.style.display === 'flex') ? 'none' : 'flex';
}
document.getElementById('find-btn').onclick = () => togglePanel('search-panel');
document.getElementById('jump-btn').onclick = () => togglePanel('jump-panel');
function jumpToLine() {
const line = parseInt(document.getElementById('jump-input').value);
if (isNaN(line) || line < 1) return;
const lines = codeInput.value.split('\n');
let pos = 0;
for (let i = 0; i < Math.min(line - 1, lines.length); i++) pos += lines[i].length + 1;
codeInput.focus();
codeInput.setSelectionRange(pos, pos);
updateEditor();
togglePanel('jump-panel');
}
function findNext() {
const query = document.getElementById('search-input').value;
if (!query) return;
const text = codeInput.value;
let index = text.indexOf(query, codeInput.selectionEnd);
if (index === -1) index = text.indexOf(query);
if (index !== -1) {
codeInput.setSelectionRange(index, index + query.length);
codeInput.focus();
updateEditor();
}
}
document.getElementById('fullscreen-btn').onclick = () => {
const elem = document.documentElement;
if (!document.fullscreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
if (elem.requestFullscreen) elem.requestFullscreen();
else if (elem.webkitRequestFullscreen) elem.webkitRequestFullscreen();
else if (elem.msRequestFullscreen) elem.msRequestFullscreen();
} else {
if (document.exitFullscreen) document.exitFullscreen();
else if (document.webkitExitFullscreen) document.webkitExitFullscreen();
else if (document.msExitFullscreen) document.msExitFullscreen();
}
};
document.getElementById('run-btn').onclick = () => {
document.getElementById('editor-mode').classList.remove('active');
document.getElementById('preview-mode').classList.add('active');
document.getElementById('run-btn').style.display = 'none';
document.getElementById('stop-btn').style.display = 'flex';
const frame = document.getElementById('preview-frame');
const doc = frame.contentDocument || frame.contentWindow.document;
doc.open();
doc.write(codeInput.value || 'No code.');
doc.close();
};
document.getElementById('stop-btn').onclick = () => {
document.getElementById('preview-mode').classList.remove('active');
document.getElementById('editor-mode').classList.add('active');
document.getElementById('stop-btn').style.display = 'none';
document.getElementById('run-btn').style.display = 'flex';
};
document.getElementById('undo-btn').onclick = () => {
if (historyIdx > 0) {
historyIdx--;
codeInput.value = history[historyIdx];
updateEditor();
}
};
document.getElementById('redo-btn').onclick = () => {
if (historyIdx < history.length - 1) {
historyIdx++;
codeInput.value = history[historyIdx];
updateEditor();
}
};
document.getElementById('export-btn').onclick = () => {
const blob = new Blob([codeInput.value], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = document.getElementById('file-info').textContent || 'project.html';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
updateEditor();
</script>
</body>
</html>