Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
1583 lines (1487 loc) · 69.7 KB
/
Copy pathscript.js
File metadata and controls
1583 lines (1487 loc) · 69.7 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ---------------- file type helpers ---------------- */
const EXT_MAP = {
html:{color:'var(--c-html)',lang:'html',runnable:true},
htm:{color:'var(--c-html)',lang:'html',runnable:true},
css:{color:'var(--c-css)',lang:'css',runnable:true},
js:{color:'var(--c-js)',lang:'javascript',runnable:true},
mjs:{color:'var(--c-js)',lang:'javascript',runnable:true},
jsx:{color:'var(--c-js)',lang:'javascript',runnable:false},
ts:{color:'var(--c-ts)',lang:'typescript',runnable:false},
tsx:{color:'var(--c-ts)',lang:'typescript',runnable:false},
json:{color:'var(--c-json)',lang:'json',runnable:false},
md:{color:'var(--c-md)',lang:'markdown',runnable:false},
markdown:{color:'var(--c-md)',lang:'markdown',runnable:false},
php:{color:'var(--c-php)',lang:'php',runnable:false},
py:{color:'var(--c-py)',lang:'python',runnable:false},
txt:{color:'var(--c-other)',lang:'text',runnable:false},
xml:{color:'var(--c-other)',lang:'xml',runnable:false},
yml:{color:'var(--c-other)',lang:'yaml',runnable:false},
yaml:{color:'var(--c-other)',lang:'yaml',runnable:false},
svg:{color:'var(--c-img)',lang:'image',runnable:false,binary:true},
png:{color:'var(--c-img)',lang:'image',runnable:false,binary:true},
jpg:{color:'var(--c-img)',lang:'image',runnable:false,binary:true},
jpeg:{color:'var(--c-img)',lang:'image',runnable:false,binary:true},
gif:{color:'var(--c-img)',lang:'image',runnable:false,binary:true},
webp:{color:'var(--c-img)',lang:'image',runnable:false,binary:true}
};
function basename(path){
const parts = path.split('/');
return parts[parts.length-1];
}
function dirOf(path){
const idx = path.lastIndexOf('/');
return idx === -1 ? '' : path.slice(0, idx);
}
function extOf(path){
const b = basename(path);
const parts = b.split('.');
return parts.length > 1 ? parts.pop().toLowerCase() : '';
}
function typeInfo(path){
return EXT_MAP[extOf(path)] || {color:'var(--c-other)',lang:'text',runnable:false};
}
/* ---------------- virtual file system ---------------- */
let files = {}; // path -> { content, binary }
let activeFile = null;
let collapsedFolders = new Set();
let openTabs = [];
let savedSettings = {};
const defaults = {
'index.html': `<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="card">
<h1>Build with KM Digital Labs</h1>
<p>Write HTML, CSS and JavaScript. Your live result appears in the preview panel.</p>
<button id="btn">Click me</button>
</div>
<script src="script.js"><\/script>
</body>
</html>`,
'style.css': `:root{
--paper:#f5f2e9;
--paper-alt:#eeeae0;
--charcoal:#171916;
--ink:#10110f;
--orange:#f36a2d;
--orange-deep:#b84216;
--green:#2f5d45;
--green-soft:#cdd9c8;
}
*{box-sizing:border-box;}
body{
margin:0;
min-height:100vh;
display:flex;
align-items:center;
justify-content:center;
padding:24px;
font-family:Arial,sans-serif;
background:
radial-gradient(circle at 80% 15%,rgba(243,106,45,.13),transparent 24%),
var(--charcoal);
}
.card{
width:min(520px,100%);
background:var(--paper);
border:1px solid var(--orange);
border-radius:20px;
padding:34px;
color:var(--ink);
text-align:center;
box-shadow:0 24px 70px rgba(0,0,0,.34);
}
.card::before{
content:'KM DIGITAL LABS · CODESPACE';
display:inline-block;
margin-bottom:18px;
padding:7px 11px;
border-radius:999px;
background:var(--green-soft);
color:var(--green);
font-size:11px;
font-weight:700;
letter-spacing:.08em;
}
h1{color:var(--orange-deep);margin:0 0 9px;}
p{color:#6d7169;line-height:1.6;}
button{
margin-top:16px;
background:var(--orange);
border:1px solid var(--orange);
color:var(--ink);
padding:10px 20px;
border-radius:11px;
cursor:pointer;
font-size:14px;
font-weight:700;
transition:.18s ease;
}
button:hover{background:#df5920;transform:translateY(-2px);}`,
'script.js': `let count = 0;
document.getElementById('btn').addEventListener('click', () => {
count++;
console.log('clicked', count, 'times');
});
console.log('script loaded');`
};
function loadDefaults(){
files = {};
Object.keys(defaults).forEach(name => { files[name] = {content: defaults[name], binary:false}; });
activeFile = 'index.html';
collapsedFolders = new Set();
openTabs = ['index.html','style.css','script.js'];
}
const STORAGE_KEY = 'kmdlabs-codespace-workspace-v4';
const LEGACY_STORAGE_KEY = 'kmdlabs-codespace-workspace-v3';
const saveStatus = document.getElementById('saveStatus');
const cursorStatus = document.getElementById('cursorStatus');
let saveTimer = null;
let editorFontSize = 13;
function setSaveStatus(text, state){
saveStatus.textContent = text;
saveStatus.className = state || '';
}
function saveWorkspace(){
try{
localStorage.setItem(STORAGE_KEY, JSON.stringify({
version:4,
files,
activeFile,
openTabs,
collapsedFolders:[...collapsedFolders],
settings:{
autoRun,
previewOpen,
consoleCollapsed:document.getElementById('consolePanel')?.classList.contains('collapsed') || false,
sidebarCollapsed:document.body.classList.contains('sidebar-collapsed'),
editorFontSize
}
}));
setSaveStatus('saved locally', 'saved');
}catch(error){
setSaveStatus('storage full — download files', 'error');
console.warn('Workspace could not be saved locally:', error);
}
}
function scheduleSave(){
setSaveStatus('saving…', 'saving');
clearTimeout(saveTimer);
saveTimer = setTimeout(saveWorkspace, 260);
}
function restoreWorkspace(){
try{
const raw = localStorage.getItem(STORAGE_KEY) || localStorage.getItem(LEGACY_STORAGE_KEY);
if(!raw) return false;
const saved = JSON.parse(raw);
if(!saved || !saved.files || typeof saved.files !== 'object') return false;
files = saved.files;
activeFile = saved.activeFile && files[saved.activeFile] ? saved.activeFile : Object.keys(files)[0] || null;
collapsedFolders = new Set(Array.isArray(saved.collapsedFolders) ? saved.collapsedFolders : []);
openTabs = Array.isArray(saved.openTabs) ? saved.openTabs.filter(path => files[path]) : (activeFile ? [activeFile] : []);
if(activeFile && !openTabs.includes(activeFile)) openTabs.unshift(activeFile);
savedSettings = saved.settings && typeof saved.settings === 'object' ? saved.settings : {};
return true;
}catch(error){
return false;
}
}
if(!restoreWorkspace()) loadDefaults();
/* ---------------- sidebar tree rendering ---------------- */
const fileList = document.getElementById('fileList');
const fileFilter = document.getElementById('fileFilter');
const workspaceStatus = document.getElementById('workspaceStatus');
const compileStatus = document.getElementById('compileStatus');
const languageStatus = document.getElementById('languageStatus');
const folderIconSVG = '<svg class="folder-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V7z"/></svg>';
const deleteButtonHTML = '<button type="button" class="row-action del" title="Delete" aria-label="Delete"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 6h18M8 6V4h8v2M19 6l-1 14H6L5 6M10 10v6M14 10v6"/></svg></button>';
const renameButtonHTML = '<button type="button" class="row-action rename" title="Rename" aria-label="Rename"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 20h9M16.5 3.5a2.1 2.1 0 0 1 3 3L8 18l-4 1 1-4L16.5 3.5z"/></svg></button>';
function escapeHTML(value){
return String(value).replace(/[&<>'"]/g, char => ({'&':'&','<':'<','>':'>',"'":''','"':'"'}[char]));
}
function normalizeUserPath(value){
return String(value || '').trim().replace(/\\/g,'/').replace(/^\/+|\/+$/g,'').replace(/\/{2,}/g,'/');
}
function buildTree(paths){
const root = {folders:{}, files:[]};
paths.forEach(p => {
const parts = p.split('/');
let node = root;
parts.forEach((part, i) => {
if(i === parts.length - 1){
node.files.push({name: part, path: p});
} else {
if(!node.folders[part]) node.folders[part] = {folders:{}, files:[]};
node = node.folders[part];
}
});
});
return root;
}
function renderFileList(){
fileList.innerHTML = '';
const query = (fileFilter?.value || '').trim().toLowerCase();
const visiblePaths = Object.keys(files).filter(path => !query || path.toLowerCase().includes(query));
const tree = buildTree(visiblePaths);
renderTreeLevel(tree, fileList, 0, '');
if(!visiblePaths.length) fileList.innerHTML = '<div class="editor-empty">no matching files</div>';
workspaceStatus.textContent = Object.keys(files).length + (Object.keys(files).length === 1 ? ' file' : ' files');
}
fileFilter?.addEventListener('input', renderFileList);
function renderTreeLevel(node, container, depth, prefix){
Object.keys(node.folders).sort().forEach(folderName => {
const fullPath = prefix ? prefix + '/' + folderName : folderName;
const expanded = !collapsedFolders.has(fullPath);
const row = document.createElement('div');
row.className = 'file-row folder-row' + (expanded ? ' expanded' : '');
row.style.paddingLeft = (8 + depth*14) + 'px';
row.innerHTML = '<span class="chev">▸</span>' + folderIconSVG + '<span class="fname">'+escapeHTML(folderName)+'</span><span class="row-actions">' + deleteButtonHTML + '</span>';
row.addEventListener('click', e => {
if(e.target.closest('.row-action')) return;
if(collapsedFolders.has(fullPath)) collapsedFolders.delete(fullPath);
else collapsedFolders.add(fullPath);
renderFileList();
scheduleSave();
});
row.querySelector('.del').addEventListener('click', e => {
e.stopPropagation();
requestDelete({kind:'folder', path:fullPath, label:folderName});
});
container.appendChild(row);
if(expanded) renderTreeLevel(node.folders[folderName], container, depth+1, fullPath);
});
node.files.sort((a,b) => a.name.localeCompare(b.name)).forEach(file => {
const info = typeInfo(file.path);
const row = document.createElement('div');
row.className = 'file-row' + (file.path === activeFile ? ' active' : '');
row.style.paddingLeft = (8 + depth*14 + 16) + 'px';
row.title = file.path;
row.innerHTML = '<span class="sq" style="background:'+info.color+'"></span><span class="fname">'+escapeHTML(file.name)+'</span><span class="row-actions">' + renameButtonHTML + deleteButtonHTML + '</span>';
row.addEventListener('click', e => {
if(e.target.closest('.row-action')) return;
if(isMobileLayout() && document.body.classList.contains('file-manage-mode')) return;
openFile(file.path);
if(isMobileLayout()) setMobileView('editor');
});
row.querySelector('.rename').addEventListener('click', e => {
e.stopPropagation();
const requested = prompt('Rename file or move it to a folder:', file.path);
const newPath = normalizeUserPath(requested);
if(!newPath || newPath === file.path) return;
if(files[newPath]){ alert('A file with that path already exists.'); return; }
files[newPath] = files[file.path];
delete files[file.path];
if(activeFile === file.path) activeFile = newPath;
openTabs = openTabs.map(path => path === file.path ? newPath : path);
renderFileList();
renderEditor();
scheduleRun();
scheduleSave();
});
row.querySelector('.del').addEventListener('click', e => {
e.stopPropagation();
requestDelete({kind:'file', path:file.path, label:file.name});
});
container.appendChild(row);
});
}
/* ---------------- mobile file management ---------------- */
const manageFilesBtn = document.getElementById('manageFilesBtn');
const deleteSheet = document.getElementById('deleteSheet');
const deleteSheetMessage = document.getElementById('deleteSheetMessage');
const cancelDeleteBtn = document.getElementById('cancelDeleteBtn');
const confirmDeleteBtn = document.getElementById('confirmDeleteBtn');
const deleteSheetBackdrop = document.getElementById('deleteSheetBackdrop');
let pendingDelete = null;
function setFileManageMode(enabled){
document.body.classList.toggle('file-manage-mode', enabled);
manageFilesBtn?.classList.toggle('active', enabled);
manageFilesBtn?.setAttribute('aria-pressed', String(enabled));
}
manageFilesBtn?.addEventListener('click', () => {
setFileManageMode(!document.body.classList.contains('file-manage-mode'));
});
function closeDeleteSheet(){
deleteSheet.classList.remove('open');
deleteSheet.setAttribute('aria-hidden','true');
pendingDelete = null;
}
function completeDelete(item){
if(!item) return;
if(item.kind === 'folder'){
Object.keys(files).forEach(path => { if(path.startsWith(item.path + '/')) delete files[path]; });
openTabs = openTabs.filter(path => !path.startsWith(item.path + '/'));
if(activeFile && activeFile.startsWith(item.path + '/')) activeFile = Object.keys(files)[0] || null;
}else{
delete files[item.path];
openTabs = openTabs.filter(path => path !== item.path);
if(activeFile === item.path) activeFile = Object.keys(files)[0] || null;
}
renderFileList();
renderEditor();
scheduleRun();
scheduleSave();
if(isMobileLayout()) setFileManageMode(false);
}
function requestDelete(item){
const label = item.kind === 'folder' ? 'folder "' + item.label + '" and everything inside it' : 'file "' + item.path + '"';
if(!isMobileLayout()){
if(confirm('Delete ' + label + '?')) completeDelete(item);
return;
}
pendingDelete = item;
deleteSheetMessage.textContent = 'Delete ' + label + '? This cannot be undone after the workspace is saved.';
deleteSheet.classList.add('open');
deleteSheet.setAttribute('aria-hidden','false');
setTimeout(() => confirmDeleteBtn.focus(), 0);
}
cancelDeleteBtn?.addEventListener('click', closeDeleteSheet);
deleteSheetBackdrop?.addEventListener('click', closeDeleteSheet);
confirmDeleteBtn?.addEventListener('click', () => {
const item = pendingDelete;
closeDeleteSheet();
completeDelete(item);
});
document.addEventListener('keydown', event => {
if(event.key === 'Escape' && deleteSheet.classList.contains('open')) closeDeleteSheet();
});
/* ---------------- editor rendering ---------------- */
const editorWrap = document.getElementById('editorWrap');
const editorTopline = document.getElementById('editorTopline');
const editorTabs = document.getElementById('editorTabs');
let currentTextarea = null;
let currentAutocomplete = null;
function touchTab(path){
if(!path || !files[path]) return;
if(!openTabs.includes(path)) openTabs.push(path);
if(openTabs.length > 10) openTabs.splice(openTabs.findIndex(tab => tab !== activeFile), 1);
}
function openFile(path, options = {}){
if(!files[path]) return;
activeFile = path;
touchTab(path);
renderFileList();
renderEditor();
if(!options.skipSave) scheduleSave();
if(options.line && currentTextarea){
const lines = currentTextarea.value.split('\n');
const position = lines.slice(0, Math.max(0, options.line - 1)).reduce((total, line) => total + line.length + 1, 0);
currentTextarea.focus();
currentTextarea.setSelectionRange(position, position);
currentTextarea.scrollTop = Math.max(0, (options.line - 3) * (parseFloat(getComputedStyle(currentTextarea).lineHeight) || 21));
}
}
function closeTab(path){
const index = openTabs.indexOf(path);
if(index < 0) return;
openTabs.splice(index, 1);
if(activeFile === path){
activeFile = openTabs[index] || openTabs[index - 1] || null;
if(!activeFile) activeFile = Object.keys(files)[0] || null;
}
renderFileList();
renderEditor();
scheduleSave();
}
function renderTabs(){
openTabs = openTabs.filter(path => files[path]);
if(activeFile) touchTab(activeFile);
editorTabs.innerHTML = '';
openTabs.forEach(path => {
const info = typeInfo(path);
const tab = document.createElement('button');
tab.type = 'button';
tab.className = 'editor-tab' + (path === activeFile ? ' active' : '');
tab.setAttribute('role','tab');
tab.setAttribute('aria-selected', String(path === activeFile));
tab.title = path;
tab.innerHTML = '<span class="sq" style="background:'+info.color+'"></span><span class="tab-name">'+escapeHTML(basename(path))+'</span><span class="tab-close" title="Close tab" aria-label="Close tab">×</span>';
tab.addEventListener('click', event => {
if(event.target.closest('.tab-close')) closeTab(path);
else openFile(path);
});
editorTabs.appendChild(tab);
});
}
const HTML_VOID_TAGS = new Set(['area','base','br','col','embed','hr','img','input','link','meta','param','source','track','wbr']);
const HTML_TAGS = ['html','head','body','title','meta','link','style','script','header','nav','main','section','article','aside','footer','div','span','h1','h2','h3','h4','h5','h6','p','a','button','form','label','input','textarea','select','option','ul','ol','li','img','picture','source','video','audio','canvas','table','thead','tbody','tr','th','td','br','hr'];
const CSS_SNIPPETS = [
['display','display: flex;','CSS property'],['display-grid','display: grid;','CSS property'],['position','position: relative;','CSS property'],
['color','color: #10110f;','CSS property'],['background','background: #f5f2e9;','CSS property'],['margin','margin: 0;','CSS property'],
['padding','padding: 16px;','CSS property'],['border','border: 1px solid #dcd8cd;','CSS property'],['radius','border-radius: 12px;','CSS property'],
['width','width: 100%;','CSS property'],['height','height: 100%;','CSS property'],['max-width','max-width: 1200px;','CSS property'],
['font-size','font-size: 16px;','CSS property'],['font-family','font-family: Arial, sans-serif;','CSS property'],['font-weight','font-weight: 600;','CSS property'],
['align-items','align-items: center;','Flexbox'],['justify-content','justify-content: center;','Flexbox'],['gap','gap: 16px;','Layout'],
['grid-columns','grid-template-columns: repeat(3, 1fr);','CSS Grid'],['transition','transition: 0.2s ease;','Animation'],
['media','@media (max-width: 768px) {\n \n}','Responsive rule']
];
const JS_SNIPPETS = [
['console.log','console.log();','JavaScript'],['const','const name = value;','Variable'],['let','let value = 0;','Variable'],
['function','function name() {\n \n}','Function'],['arrow','const name = () => {\n \n};','Arrow function'],
['querySelector','document.querySelector(\'selector\');','DOM'],['querySelectorAll','document.querySelectorAll(\'selector\');','DOM'],
['getElementById','document.getElementById(\'id\');','DOM'],['addEventListener','element.addEventListener(\'click\', () => {\n \n});','Event'],
['if','if (condition) {\n \n}','Condition'],['if-else','if (condition) {\n \n} else {\n \n}','Condition'],
['for','for (let i = 0; i < items.length; i++) {\n \n}','Loop'],['forEach','items.forEach((item) => {\n \n});','Loop'],
['fetch','fetch(url)\n .then(response => response.json())\n .then(data => console.log(data))\n .catch(error => console.error(error));','Network'],
['async','async function name() {\n try {\n \n } catch (error) {\n console.error(error);\n }\n}','Async function']
];
function escCode(value){
return String(value).replace(/[&<>]/g, ch => ({'&':'&','<':'<','>':'>'}[ch]));
}
function token(cls, value){ return '<span class="tok-'+cls+'">'+escCode(value)+'</span>'; }
function highlightJS(code){
const keywords = new Set(['const','let','var','function','return','if','else','for','while','do','switch','case','break','continue','new','class','extends','import','from','export','default','async','await','try','catch','finally','throw','typeof','instanceof','in','of','this','super','static','get','set','yield','delete','void']);
const builtins = new Set(['document','window','console','Math','JSON','Date','Array','Object','String','Number','Boolean','Promise','Set','Map','localStorage','sessionStorage','fetch','URL','RegExp']);
const constants = new Set(['true','false','null','undefined','NaN','Infinity']);
let out='', i=0;
while(i<code.length){
const ch=code[i], next=code[i+1];
if(ch==='/' && next==='/'){
let j=code.indexOf('\n',i); if(j<0) j=code.length;
out+=token('comment',code.slice(i,j)); i=j; continue;
}
if(ch==='/' && next==='*'){
let j=code.indexOf('*/',i+2); j=j<0?code.length:j+2;
out+=token('comment',code.slice(i,j)); i=j; continue;
}
if(ch==='"' || ch==="'" || ch==='`'){
const q=ch; let j=i+1;
while(j<code.length){ if(code[j]==='\\'){j+=2;continue;} if(code[j]===q){j++;break;} j++; }
out+=token('string',code.slice(i,j)); i=j; continue;
}
if(/[0-9]/.test(ch) || (ch==='.' && /[0-9]/.test(next||''))){
let j=i+1; while(j<code.length && /[\w.xobA-Fa-f]/.test(code[j])) j++;
out+=token('number',code.slice(i,j)); i=j; continue;
}
if(/[A-Za-z_$]/.test(ch)){
let j=i+1; while(j<code.length && /[\w$]/.test(code[j])) j++;
const word=code.slice(i,j); let k=j; while(/\s/.test(code[k]||'')) k++;
if(keywords.has(word)) out+=token('keyword',word);
else if(constants.has(word)) out+=token(word==='true'||word==='false'?'boolean':'constant',word);
else if(builtins.has(word)) out+=token('builtin',word);
else if(code[k]==='(') out+=token('function',word);
else out+=escCode(word);
i=j; continue;
}
if(/[+\-*%=!<>?&|~^]/.test(ch)) out+=token('operator',ch); else if(/[{}()[\].,;:]/.test(ch)) out+=token('punctuation',ch); else out+=escCode(ch);
i++;
}
return out;
}
function highlightCSS(code){
let out='', i=0, depth=0;
while(i<code.length){
const ch=code[i], next=code[i+1];
if(ch==='/' && next==='*'){
let j=code.indexOf('*/',i+2); j=j<0?code.length:j+2;
out+=token('comment',code.slice(i,j)); i=j; continue;
}
if(ch==='"' || ch==="'"){
const q=ch; let j=i+1; while(j<code.length){if(code[j]==='\\'){j+=2;continue;}if(code[j]===q){j++;break;}j++;}
out+=token('string',code.slice(i,j));i=j;continue;
}
if(ch==='#' && /[0-9A-Fa-f]/.test(next||'')){
let j=i+1;while(j<code.length && /[0-9A-Fa-f]/.test(code[j]))j++;
out+=token('number',code.slice(i,j));i=j;continue;
}
if(/[0-9]/.test(ch) || (ch==='.' && /[0-9]/.test(next||''))){
let j=i+1;while(j<code.length && /[0-9A-Za-z.%_-]/.test(code[j]))j++;
out+=token('number',code.slice(i,j));i=j;continue;
}
if(ch==='@'){
let j=i+1;while(j<code.length && /[\w-]/.test(code[j]))j++;
out+=token('keyword',code.slice(i,j));i=j;continue;
}
if(/[A-Za-z_-]/.test(ch)){
let j=i+1;while(j<code.length && /[\w-]/.test(code[j]))j++;
const word=code.slice(i,j);let k=j;while(/\s/.test(code[k]||''))k++;
const before=code.slice(0,i).trimEnd().slice(-1);
if(word.startsWith('--')) out+=token('variable',word);
else if(depth>0 && code[k]===':') out+=token('property',word);
else if(depth===0 || before==='.' || before==='#' || before==='>' || before==='+' || before==='~') out+=token('selector',word);
else out+=token('string',word);
i=j;continue;
}
if(ch==='{') depth++; if(ch==='}') depth=Math.max(0,depth-1);
if(/[{}();,:>+~\[\]=]/.test(ch)) out+=token('punctuation',ch); else out+=escCode(ch);
i++;
}
return out;
}
function findTagEnd(code,start){
let quote=null;
for(let i=start+1;i<code.length;i++){
const ch=code[i];
if(quote){if(ch==='\\')i++;else if(ch===quote)quote=null;}
else if(ch==='"'||ch==="'")quote=ch;else if(ch==='>')return i;
}
return code.length-1;
}
const HTML_TAG_COLOUR_GROUPS = {
document:new Set(['html','head','body','main','header','footer','nav','section','article','aside']),
content:new Set(['h1','h2','h3','h4','h5','h6','p','span','strong','em','small','blockquote','pre','code','ul','ol','li']),
interactive:new Set(['a','button','input','textarea','select','option','label','form','details','summary','dialog']),
media:new Set(['img','picture','source','video','audio','canvas','svg','path','iframe','link','script','style','meta'])
};
function htmlTagToken(name){
const normalized = name.toLowerCase();
const group = Object.keys(HTML_TAG_COLOUR_GROUPS).find(key => HTML_TAG_COLOUR_GROUPS[key].has(normalized));
return token(group ? 'tag-'+group : 'tag', name);
}
function highlightHTMLTag(raw){
let out='',i=0;
if(raw[i]==='<'){out+=token('punctuation','<');i++;}
if(raw[i]==='/'){out+=token('punctuation','/');i++;}
if(raw[i]==='!'){
return token('doctype',raw);
}
while(i<raw.length && /\s/.test(raw[i])){out+=escCode(raw[i]);i++;}
let j=i;while(j<raw.length && /[\w:-]/.test(raw[j]))j++;
if(j>i){out+=htmlTagToken(raw.slice(i,j));i=j;}
while(i<raw.length){
const ch=raw[i];
if(ch==='"'||ch==="'"){
const q=ch;let k=i+1;while(k<raw.length){if(raw[k]==='\\')k+=2;else if(raw[k]===q){k++;break;}else k++;}
out+=token('string',raw.slice(i,k));i=k;continue;
}
if(/[A-Za-z_:]/.test(ch)){
let k=i+1;while(k<raw.length && /[\w:.-]/.test(raw[k]))k++;
out+=token('attr',raw.slice(i,k));i=k;continue;
}
if(ch==='=')out+=token('operator',ch);else if(ch==='>'||ch==='/')out+=token('punctuation',ch);else out+=escCode(ch);
i++;
}
return out;
}
function highlightHTML(code){
let out='',i=0;
while(i<code.length){
if(code.startsWith('<!--',i)){
let j=code.indexOf('-->',i+4);j=j<0?code.length:j+3;
out+=token('comment',code.slice(i,j));i=j;continue;
}
if(code[i]==='<'){
const end=findTagEnd(code,i);const raw=code.slice(i,end+1);const match=raw.match(/^<\s*([A-Za-z][\w:-]*)/);const tagName=match?match[1].toLowerCase():'';
out+=highlightHTMLTag(raw);i=end+1;
if(tagName==='style' || tagName==='script'){
const close='</'+tagName+'>';const closeAt=code.toLowerCase().indexOf(close,i);
if(closeAt>=0){
const inner=code.slice(i,closeAt);out+=tagName==='style'?highlightCSS(inner):highlightJS(inner);
out+=highlightHTMLTag(code.slice(closeAt,closeAt+close.length));i=closeAt+close.length;
}
}
continue;
}
let j=code.indexOf('<',i);if(j<0)j=code.length;
out+='<span class="tok-text">'+escCode(code.slice(i,j))+'</span>';i=j;
}
return out;
}
function highlightCode(code,lang){
if(lang==='html') return highlightHTML(code);
if(lang==='css') return highlightCSS(code);
if(lang==='javascript' || lang==='typescript') return highlightJS(code);
if(lang==='json') return highlightJS(code);
return escCode(code);
}
function findUnclosedHTMLTag(source){
const stack=[]; const rx=/<\/?([A-Za-z][\w:-]*)\b[^>]*>/g; let m;
while((m=rx.exec(source))){
const full=m[0],name=m[1].toLowerCase();
if(full.startsWith('</')){
const at=stack.lastIndexOf(name); if(at>=0) stack.splice(at,1);
}else if(!full.endsWith('/>') && !HTML_VOID_TAGS.has(name)) stack.push(name);
}
return stack[stack.length-1]||'';
}
function currentWord(ta){
const before=ta.value.slice(0,ta.selectionStart);const m=before.match(/[A-Za-z_$-][\w$-]*$/);return m?m[0]:'';
}
function suggestionsFor(ta,lang){
const before=ta.value.slice(0,ta.selectionStart);const word=currentWord(ta);const items=[];
if(lang==='html'){
if(/(?:^|\s)html$/i.test(before)) items.push({label:'HTML5 document',detail:'Complete page structure',kind:'snippet',insert:'<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <title>Document</title>\n</head>\n<body>\n \n</body>\n</html>',replace:4,caretBack:17});
if(/!$/.test(before)) items.push({label:'!',detail:'HTML5 boilerplate',kind:'snippet',insert:'<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <title>Document</title>\n</head>\n<body>\n \n</body>\n</html>',replace:1,caretBack:17});
const closeMatch=before.match(/<\/([\w-]*)$/);
if(closeMatch){
const open=findUnclosedHTMLTag(before.slice(0,before.length-closeMatch[0].length));
if(open && open.startsWith(closeMatch[1].toLowerCase())) items.push({label:'</'+open+'>',detail:'Close nearest open tag',kind:'tag',insert:'</'+open+'>',replace:closeMatch[0].length});
}else{
const tagMatch=before.match(/<([\w-]*)$/);
if(tagMatch){
const q=tagMatch[1].toLowerCase();
HTML_TAGS.filter(t=>t.startsWith(q)).slice(0,12).forEach(t=>{
const insert=HTML_VOID_TAGS.has(t)?'<'+t+'>':'<'+t+'></'+t+'>';
items.push({label:insert,detail:HTML_VOID_TAGS.has(t)?'HTML element':'Paired HTML element',kind:'tag',insert,replace:tagMatch[0].length,caretBack:HTML_VOID_TAGS.has(t)?0:t.length+3});
});
}else if(word.length>=2){
HTML_TAGS.filter(t=>t.startsWith(word.toLowerCase())).slice(0,8).forEach(t=>items.push({label:'<'+t+'>',detail:'HTML element',kind:'tag',insert:HTML_VOID_TAGS.has(t)?'<'+t+'>':'<'+t+'></'+t+'>',replace:word.length,caretBack:HTML_VOID_TAGS.has(t)?0:t.length+3}));
}
}
}else if(lang==='css'){
if(word.length>=1) CSS_SNIPPETS.filter(x=>x[0].startsWith(word.toLowerCase())).slice(0,10).forEach(x=>items.push({label:x[0],detail:x[2],kind:'css',insert:x[1],replace:word.length,caretBack:x[1].includes('\n \n')?2:0}));
}else if(lang==='javascript' || lang==='typescript'){
if(word.length>=1) JS_SNIPPETS.filter(x=>x[0].toLowerCase().startsWith(word.toLowerCase())).slice(0,10).forEach(x=>items.push({label:x[0],detail:x[2],kind:'js',insert:x[1],replace:word.length,caretBack:x[1].includes('\n \n')?2:(x[1].endsWith(');')?2:0)}));
}
return items;
}
function renderEditor(){
renderTabs();
editorWrap.innerHTML = '';
currentAutocomplete = null;
if(!activeFile){
editorTopline.textContent = '';
languageStatus.textContent = 'Plain Text';
editorWrap.innerHTML = '<div class="editor-empty">no file open — create or upload one</div>';
return;
}
const info = typeInfo(activeFile);
languageStatus.textContent = info.lang.charAt(0).toUpperCase() + info.lang.slice(1);
editorTopline.innerHTML = '<span class="sq" style="background:'+info.color+'"></span> '+escapeHTML(activeFile)+' <span style="color:var(--text-faint)">· '+info.lang+'</span><span class="editor-help"><strong>Tab/Enter</strong> accept suggestion · <strong>Ctrl/Cmd + Enter</strong> compile</span>';
if(info.binary){
const wrap = document.createElement('div');
wrap.className = 'binary-note';
if(info.lang === 'image') wrap.innerHTML = '<img src="'+files[activeFile].content+'" alt="'+activeFile+'"><span>binary file — preview only</span>';
else wrap.textContent = 'binary file — no text preview';
editorWrap.appendChild(wrap);
return;
}
const editingPath = activeFile;
const ln = document.createElement('div');
ln.className = 'line-numbers';
const stage=document.createElement('div');stage.className='code-stage';
const syntax=document.createElement('div');syntax.className='syntax-layer';
const code=document.createElement('pre');code.className='syntax-code';
syntax.appendChild(code);
const ta = document.createElement('textarea');
ta.className='code-input';
ta.spellcheck = false; ta.autocapitalize = 'off'; ta.autocomplete = 'off'; ta.autocorrect = 'off';
ta.setAttribute('aria-label', 'Code editor for ' + editingPath);
ta.value = files[editingPath].content;
const popup=document.createElement('div');popup.className='autocomplete';popup.setAttribute('role','listbox');
stage.appendChild(syntax);stage.appendChild(ta);stage.appendChild(popup);
currentTextarea = ta; currentAutocomplete=popup;
let suggestionItems=[],selectedSuggestion=0;
let renderedLineCount = 0;
let visualFrame = 0;
function updateLn(){
const lines = ta.value.split('\n').length;
if(lines === renderedLineCount) return;
renderedLineCount = lines;
let out = '';
for(let i=1;i<=lines;i++) out += i + '\n';
ln.textContent = out.trim();
}
function updateCursor(){
const before = ta.value.slice(0, ta.selectionStart); const line = before.split('\n').length;
const col = before.length - before.lastIndexOf('\n'); cursorStatus.textContent = 'Ln ' + line + ', Col ' + col;
}
function updateSyntax(){
const plainMode = ta.value.length > 120000;
stage.classList.toggle('plain-mode', plainMode);
code.innerHTML = plainMode ? '' : highlightCode(ta.value,info.lang)+(ta.value.endsWith('\n')?'\n':'');
syncScroll();
}
function syncScroll(){ code.style.transform='translate3d('+(-ta.scrollLeft)+'px,'+(-ta.scrollTop)+'px,0)'; ln.scrollTop=ta.scrollTop; }
function refreshVisuals(){ visualFrame=0;updateLn();updateCursor();updateSyntax(); }
function persist(){
if(!files[editingPath]) return;
files[editingPath].content=ta.value;
if(!visualFrame) visualFrame=requestAnimationFrame(refreshVisuals);
scheduleRun(editingPath);
scheduleSave();
}
function closeSuggestions(){popup.classList.remove('open');popup.innerHTML='';suggestionItems=[];selectedSuggestion=0;}
function popupPosition(){
const before=ta.value.slice(0,ta.selectionStart);const lines=before.split('\n');const line=lines.length-1,col=lines[lines.length-1].length;
const cs=getComputedStyle(ta);const lh=parseFloat(cs.lineHeight)||21;const charW=(parseFloat(cs.fontSize)||13)*.61;
const x=Math.max(8,Math.min(stage.clientWidth-340,parseFloat(cs.paddingLeft)+col*charW-ta.scrollLeft));
const y=Math.max(8,Math.min(stage.clientHeight-110,parseFloat(cs.paddingTop)+(line+1)*lh-ta.scrollTop));
popup.style.left=x+'px';popup.style.top=y+'px';
}
function drawSuggestions(){
suggestionItems=suggestionsFor(ta,info.lang);selectedSuggestion=0;
if(!suggestionItems.length){closeSuggestions();return;}
popup.innerHTML='';
suggestionItems.forEach((item,index)=>{
const btn=document.createElement('button');btn.type='button';btn.className='suggestion'+(index===0?' active':'');
btn.innerHTML='<span class="suggestion-main"><span class="suggestion-label">'+escapeHTML(item.label)+'</span><span class="suggestion-detail">'+escapeHTML(item.detail)+'</span></span><span class="suggestion-kind">'+escapeHTML(item.kind)+'</span>';
btn.addEventListener('pointerdown',e=>{e.preventDefault();acceptSuggestion(index);});popup.appendChild(btn);
});
popupPosition();popup.classList.add('open');
}
function chooseSuggestion(index){
if(!suggestionItems.length)return;selectedSuggestion=(index+suggestionItems.length)%suggestionItems.length;
[...popup.children].forEach((el,i)=>el.classList.toggle('active',i===selectedSuggestion));
popup.children[selectedSuggestion]?.scrollIntoView({block:'nearest'});
}
function acceptSuggestion(index=selectedSuggestion){
const item=suggestionItems[index];if(!item)return false;
const end=ta.selectionStart,start=Math.max(0,end-(item.replace||0));ta.setRangeText(item.insert,start,end,'end');
if(item.caretBack){const pos=ta.selectionStart-item.caretBack;ta.setSelectionRange(pos,pos);}
closeSuggestions();persist();ta.focus();return true;
}
function insertPair(open,close){
const start=ta.selectionStart,end=ta.selectionEnd,selected=ta.value.slice(start,end);
ta.setRangeText(open+selected+close,start,end,'end');const pos=start+open.length+(selected?selected.length:0);ta.setSelectionRange(pos,pos);persist();
}
function autoCloseHTMLTag(e){
if(info.lang!=='html' || e.key!=='>')return false;
const before=ta.value.slice(0,ta.selectionStart);const m=before.match(/<([A-Za-z][\w:-]*)(?:\s[^<>]*)?$/);
if(!m || before.endsWith('/') || HTML_VOID_TAGS.has(m[1].toLowerCase()))return false;
e.preventDefault();const tag=m[1];const pos=ta.selectionStart;ta.setRangeText('></'+tag+'>',pos,pos,'end');ta.setSelectionRange(pos+1,pos+1);persist();return true;
}
updateLn();updateCursor();updateSyntax();
ta.addEventListener('input',()=>{persist();drawSuggestions();});
ta.addEventListener('scroll',()=>{syncScroll();if(popup.classList.contains('open'))popupPosition();});
['click','keyup','select'].forEach(eventName=>ta.addEventListener(eventName,()=>{updateCursor();if(eventName==='click')drawSuggestions();}));
ta.addEventListener('blur',()=>setTimeout(closeSuggestions,120));
ta.addEventListener('keydown',e=>{
if(popup.classList.contains('open')){
if(e.key==='ArrowDown'){e.preventDefault();chooseSuggestion(selectedSuggestion+1);return;}
if(e.key==='ArrowUp'){e.preventDefault();chooseSuggestion(selectedSuggestion-1);return;}
if(e.key==='Enter' || e.key==='Tab'){e.preventDefault();acceptSuggestion();return;}
if(e.key==='Escape'){e.preventDefault();closeSuggestions();return;}
}
if(autoCloseHTMLTag(e))return;
const pairs={'(' : ')','[':']','{':'}','"':'"',"'":"'",'`':'`'};
if(pairs[e.key] && !e.ctrlKey && !e.metaKey && !e.altKey){
const next=ta.value[ta.selectionStart];
if((e.key==='"'||e.key==="'"||e.key==='`') && next===e.key && ta.selectionStart===ta.selectionEnd){e.preventDefault();ta.setSelectionRange(ta.selectionStart+1,ta.selectionStart+1);return;}
e.preventDefault();insertPair(e.key,pairs[e.key]);return;
}
if([')',']','}'].includes(e.key) && ta.value[ta.selectionStart]===e.key && ta.selectionStart===ta.selectionEnd){e.preventDefault();ta.setSelectionRange(ta.selectionStart+1,ta.selectionStart+1);return;}
if(e.key==='Enter'){
const start=ta.selectionStart,end=ta.selectionEnd;const before=ta.value.slice(0,start);const line=before.slice(before.lastIndexOf('\n')+1);const indent=(line.match(/^\s*/)||[''])[0];
const prev=ta.value[start-1],next=ta.value[start];
if((prev==='{'&&next==='}')||(prev==='['&&next===']')||(prev==='('&&next===')')){
e.preventDefault();const inner=indent+' ';ta.setRangeText('\n'+inner+'\n'+indent,start,end,'end');ta.setSelectionRange(start+1+inner.length,start+1+inner.length);persist();return;
}
if(indent){e.preventDefault();ta.setRangeText('\n'+indent,start,end,'end');persist();return;}
}
if(e.key === 'Tab'){
e.preventDefault();const start=ta.selectionStart,end=ta.selectionEnd;ta.setRangeText(' ',start,end,'end');persist();
}
});
editorWrap.appendChild(ln);editorWrap.appendChild(stage);
}
/* ---------------- new file ---------------- */
document.getElementById('newFileBtn').addEventListener('click', () => {
const name = normalizeUserPath(prompt('New file name (use / for folders, e.g. css/about.css)'));
if(!name) return;
if(files[name]){ alert('A file with that path already exists.'); return; }
files[name] = {content:'', binary:false};
activeFile = name;
touchTab(name);
renderFileList();
renderEditor();
scheduleRun();
scheduleSave();
if(isMobileLayout()) setMobileView('editor');
setTimeout(() => currentTextarea && currentTextarea.focus(), 0);
});
/* ---------------- ingesting files (click upload, folder upload, drag & drop) ---------------- */
function ingestEntries(entries){
if(!entries.length) return;
const workspaceEntry = entries.find(({path}) => extOf(path) === 'kmdspace');
if(workspaceEntry){
importWorkspaceFile(workspaceEntry.file);
return;
}
let remaining = entries.length;
let lastPath = null;
entries.forEach(({file, path}) => {
const info = typeInfo(path);
const reader = new FileReader();
reader.onload = () => {
files[path] = {content: reader.result, binary: !!info.binary};
remaining--;
lastPath = path;
if(remaining === 0){
activeFile = lastPath;
renderFileList();
renderEditor();
scheduleRun();
scheduleSave();
if(isMobileLayout()) setMobileView('editor');
}
};
if(info.binary) reader.readAsDataURL(file);
else reader.readAsText(file);
});
}
function importWorkspaceFile(file){
const reader = new FileReader();
reader.onload = () => {
try{
const bundle = JSON.parse(reader.result);
if(!bundle || bundle.format !== 'kmdlabs-codespace' || !bundle.files || typeof bundle.files !== 'object') throw new Error('Invalid workspace');
files = bundle.files;
collapsedFolders = new Set();
activeFile = bundle.activeFile && files[bundle.activeFile] ? bundle.activeFile : Object.keys(files)[0] || null;
openTabs = Array.isArray(bundle.openTabs) ? bundle.openTabs.filter(path => files[path]) : (activeFile ? [activeFile] : []);
renderFileList();
renderEditor();
scheduleRun();
saveWorkspace();
showToast('Workspace imported — '+Object.keys(files).length+' files ready.');
}catch(error){
alert('That file is not a valid CodeSpace workspace.');
}
};
reader.readAsText(file);
}
function filesToEntries(fileListObj){
return Array.from(fileListObj).map(f => ({
file: f,
path: (f.webkitRelativePath && f.webkitRelativePath.length) ? f.webkitRelativePath : f.name
}));
}
const fileInput = document.getElementById('fileInput');
const folderInput = document.getElementById('folderInput');
document.getElementById('uploadBtn').addEventListener('click', () => fileInput.click());
document.getElementById('uploadFolderBtn').addEventListener('click', () => folderInput.click());
fileInput.addEventListener('change', () => { ingestEntries(filesToEntries(fileInput.files)); fileInput.value=''; });
folderInput.addEventListener('change', () => { ingestEntries(filesToEntries(folderInput.files)); folderInput.value=''; });
/* drag & drop, including whole folders */
function traverseEntry(entry, prefix, out){
return new Promise(resolve => {
if(entry.isFile){
entry.file(file => {
out.push({file, path: prefix ? prefix+'/'+entry.name : entry.name});
resolve();
}, () => resolve());
} else if(entry.isDirectory){
const reader = entry.createReader();
const readAll = () => {
reader.readEntries(async entries => {
if(!entries.length){ resolve(); return; }
const newPrefix = prefix ? prefix+'/'+entry.name : entry.name;
await Promise.all(entries.map(e2 => traverseEntry(e2, newPrefix, out)));
readAll();
}, () => resolve());
};
readAll();
} else {
resolve();
}
});
}
const sidebar = document.getElementById('sidebar');
['dragenter','dragover'].forEach(ev => sidebar.addEventListener(ev, e => {
e.preventDefault();
sidebar.classList.add('dragover');
}));
['dragleave'].forEach(ev => sidebar.addEventListener(ev, e => {
e.preventDefault();
sidebar.classList.remove('dragover');
}));
sidebar.addEventListener('drop', e => {
e.preventDefault();
sidebar.classList.remove('dragover');
const dt = e.dataTransfer;
if(dt.items && dt.items.length && dt.items[0].webkitGetAsEntry){
const rootEntries = [];
for(let i=0;i<dt.items.length;i++){
const entry = dt.items[i].webkitGetAsEntry && dt.items[i].webkitGetAsEntry();
if(entry) rootEntries.push(entry);
}
if(rootEntries.length){
const collected = [];
Promise.all(rootEntries.map(en => traverseEntry(en, '', collected))).then(() => {
ingestEntries(collected);
});
return;
}
}
if(dt.files && dt.files.length) ingestEntries(filesToEntries(dt.files));
});
/* ---------------- workbench navigation and search ---------------- */
const explorerPanel = document.getElementById('explorerPanel');
const searchPanel = document.getElementById('searchPanel');
const projectSearchInput = document.getElementById('projectSearchInput');
const projectSearchForm = document.getElementById('projectSearchForm');
const searchResults = document.getElementById('searchResults');
const searchSummary = document.getElementById('searchSummary');
function setSidePanel(panel){
const searchOpen = panel === 'search';
explorerPanel.classList.toggle('active', !searchOpen);
searchPanel.classList.toggle('active', searchOpen);
document.querySelectorAll('[data-side-panel]').forEach(button => button.classList.toggle('active', button.dataset.sidePanel === panel));
if(isMobileLayout()) setMobileView('files');
if(searchOpen) setTimeout(() => projectSearchInput.focus(), 0);
}
document.querySelectorAll('[data-side-panel]').forEach(button => button.addEventListener('click', () => setSidePanel(button.dataset.sidePanel)));
function highlightedExcerpt(text, query){
const safe = escapeHTML(text.trim());
const index = text.toLowerCase().indexOf(query.toLowerCase());
if(index < 0) return safe;
const start = Math.max(0,index - 32);
const end = Math.min(text.length,index + query.length + 58);
const excerpt = (start ? '…' : '') + text.slice(start,end) + (end < text.length ? '…' : '');
const escaped = escapeHTML(excerpt);
return escaped.replace(new RegExp(escapeRegExp(escapeHTML(query)),'ig'), match => '<mark>'+match+'</mark>');
}
function escapeRegExp(value){ return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }
function searchProject(query){
const needle = query.trim().toLowerCase();
searchResults.innerHTML = '';
if(!needle){ searchSummary.textContent = 'Search filenames and file contents.'; return; }
const matches = [];
Object.keys(files).sort().forEach(path => {
if(path.toLowerCase().includes(needle)) matches.push({path,line:1,text:path,kind:'filename'});
if(files[path].binary) return;
String(files[path].content).split('\n').forEach((line,index) => {
if(matches.length < 100 && line.toLowerCase().includes(needle)) matches.push({path,line:index+1,text:line,kind:'content'});
});
});
searchSummary.textContent = matches.length + (matches.length === 1 ? ' result' : ' results') + (matches.length >= 100 ? ' · showing first 100' : '');
matches.slice(0,100).forEach(match => {
const button = document.createElement('button');
button.type = 'button';
button.className = 'search-result';
button.innerHTML = '<strong>'+escapeHTML(match.path)+(match.kind === 'content' ? ':'+match.line : '')+'</strong><span>'+highlightedExcerpt(match.text,query)+'</span>';
button.addEventListener('click', () => {
openFile(match.path,{line:match.line});
if(isMobileLayout()) setMobileView('editor');
});
searchResults.appendChild(button);
});
if(!matches.length) searchResults.innerHTML = '<div class="editor-empty">no results</div>';
}
projectSearchForm?.addEventListener('submit', event => { event.preventDefault(); searchProject(projectSearchInput.value); });