-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroide.htm
424 lines (369 loc) · 11.8 KB
/
microide.htm
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>IoT-IDE</title>
<link rel="icon" type="image/png" href="/favicon48.png" sizes="48x48" />
<!-- no web site meta data here -->
<link Content-Type="text/css" href="iotstyle.css" rel="stylesheet" />
<script src="micro.js"></script>
<script src="/sfc/loader.js"></script>
<script>
var allSFCLoaded = window.loadComponent('u-toast', '/sfc');
</script>
<style>
#files {
border: solid 1px black;
background-color: white;
color: black;
font-size: 1rem;
overflow-y: scroll;
width: 100%;
height: 100%;
}
#files>div {
display: flex;
}
#files>div:hover {
background-color: #eeeeee;
}
.file-entry,
.file-delete {
cursor: pointer;
font-family: 'Courier New', Courier, monospace;
padding: 0 0.1em;
height: 1.2em;
}
#files>div div.file-entry {
overflow: hidden;
flex-grow: 1;
}
pre.code {
display: block;
margin: 0;
border: solid 1px black;
padding: 0.2em;
font-family: Courier New, Courier, monospace;
font-size: 0.75rem;
line-height: 1.2;
color: black;
background-color: #fffff8;
overflow-x: hidden;
overflow-y: scroll;
height: 100%;
width: 100%;
}
#checker {
min-width: 3rem;
text-align: center;
border: 2px solid silver;
background-color: #eeeeee;
}
button.once {
position: relative;
}
button:disabled.once:after {
content: '';
position: absolute;
height: 0.6em;
width: 0.6em;
left: 50%;
top: 50%;
border: 0.2em solid #203050;
border-bottom-color: transparent;
border-radius: 50%;
animation: 1.5s linear infinite turn;
}
@keyframes turn {
0% {
transform: translate(-50%, -50%) rotate(0turn);
}
100% {
transform: translate(-50%, -50%) rotate(1turn);
}
}
.grid-container {
display: grid;
grid-template-columns: minmax(32ch, max-content) minmax(32ch, auto);
grid-template-rows: min-content calc(100vh - 8em);
column-gap: 0.5rem;
}
</style>
</head>
<body class="sitelayout" style="max-width:100%;--layout-gap: 0;--content-back:none">
<u-toast></u-toast>
<header>
<svg class="icon">
<use href="./icons.svg#ide" />
</svg>
<h1>HomeDing IDE</h1>
</header>
<div class="navbar">
<a href="/">Home</a>
<a href="/board.htm">Board</a>
<a href="/microide.htm">IDE</a>
<a href="/log.htm">Log</a>
<span class="gap"></span>
<button class="once" id="doReboot">reboot</button>
</div>
<main class="grid-container">
<div style="grid-column: 1">
<h3 style="display: inline-block; margin: 0">Files</h3>
<button style="float: right" onclick="handleReloadFS()">reload</button>
</div>
<div style="grid-column: 2">
<h3 style="display: inline-block; margin: 0">
File: <span id="activeFile"></span>
</h3>
<button id="format" class="once secondary" style="float: right">
format
</button>
<span class="btn-group" style="float: right">
<button class="once" onclick="handleSave()">save</button>
<label class="valid" id="checker">ok</label>
</span>
</div>
<!-- <div class="row" style="margin-bottom:0;overflow:hidden"> -->
<div id="files" style="grid-column: 1; flex: 1 1 auto; overflow: scroll" title="drag and drop files here."></div>
<pre style="grid-column: 2" class="code" contenteditable id="content" autocomplete="off" autocorrect="off"
autocapitalize="off" spellcheck="false"></pre>
</main>
<script>
const filesObj = document.getElementById('files');
const contentObj = document.getElementById('content');
const toastObj = document.querySelector('u-toast');
const activeFileObj = document.getElementById('activeFile');
const checkerObj = document.getElementById('checker');
let thisFile;
// load file into editor from the server
function loadEntry(e) {
let path = getPath();
const s = e.target.innerText;
const name = s.split(' ')[0].substring(1);
contentObj.innerText = '';
if (!s.endsWith(')')) {
// a folder
if (s === '..') {
// one level up.
path = path.split('/');
path.splice(path.length - 2, 1);
path = path.join('/');
} else {
path = s + '/';
}
activeFileObj.innerText = thisFile = path;
location.hash = path;
} else {
// load file
fetchText(name)
.then(function(txt) {
contentObj.innerText = txt;
activeFileObj.innerText = thisFile = name;
});
}
} // handleLoadFile()
// General Purpose Promise
function DelayPromise(ms) {
return new Promise(function(resolve) {
setTimeout(resolve, ms);
});
}
// delete file on the server
async function handleDeleteFile(e) {
e.stopPropagation();
e.preventDefault();
let s = e.target.parentElement.firstElementChild.innerText;
s = s.split(' ')[0];
const yn = window.confirm('Delete ' + s + ' ?');
if (yn) {
await fetch(s, { method: 'DELETE' });
window.setTimeout(handleReloadFS, 100);
} // if
} // handleDeleteFile()
// add one file to the directory listing.
function addEntry(f) {
const row = createHTMLElement(filesObj, 'div', {
class: 'row nogutter',
});
row.addEventListener('click', loadEntry);
// add file name and size
const entry = createHTMLElement(row, 'div', {
class: 'col stretch file-entry',
});
if (f.type === 'dir') {
entry.innerText = f.name;
} else {
entry.innerText = f.name + ' (' + f.size + ')';
const de = createHTMLElement(row, 'div', {
class: 'col file-delete',
});
de.innerText = '[X]';
de.addEventListener('click', handleDeleteFile);
}
} // addFileEntry()
// get the path to the current folder to be shown
function getPath() {
let path = '/';
if (location.hash.length > 0) path = location.hash.substring(1);
if (!path.endsWith('/')) path += '/';
return path;
}
function handleReloadFS() {
const path = getPath();
fetchJSON(`/api/list?path=${path}`)
.then(function(x) {
x.sort(function(a, b) {
if (a.type && !b.type) return -1;
if (!a.type && b.type) return 1;
const an = a.name.toLowerCase();
const bn = b.name.toLowerCase();
if (an < bn) return -1;
if (an > bn) return 1;
return 0;
});
filesObj.innerHTML = '';
if (path !== '/') addEntry({ name: '..', type: 'dir' });
x.forEach(function(f) {
addEntry(f);
});
});
} // handleReloadFS()
// some drag events need to call stopPropagation and preventDefault to allow custom interactions
function dragHelper(e) {
e.stopPropagation();
e.preventDefault();
} // dragHelper()
// only paste plain text at cursor position.
contentObj.addEventListener('paste', function(e) {
const sel = window.getSelection();
if (sel.rangeCount) {
const txt = e.clipboardData.getData('text/plain');
sel.deleteFromDocument();
sel.getRangeAt(0).insertNode(document.createTextNode(txt));
sel.collapseToEnd();
e.preventDefault();
}
});
// start uploading file content
function startUpload(filename, contentType, content) {
toastObj.info(`Uploading ${filename}`);
const formData = new FormData();
const blob = new Blob([content], {
type: contentType,
});
formData.append(filename, blob, filename);
fetch('/', { method: 'POST', body: formData }).then(() => {
handleReloadFS();
toastObj.info('done.');
});
}
// save file from editor back to server.
function handleSave() {
thisFile = window.prompt('Save File:', thisFile);
if (thisFile !== undefined)
startUpload(thisFile, 'text/html', contentObj.innerText);
} // handleSave()
// Format JSON file, remove ugly characters...
document.querySelector("#format").addEventListener('click', e => {
const fName = activeFileObj.innerText;
if (fName.toLowerCase().endsWith('.json')) {
let t = contentObj.innerText;
let o = null;
// missing comma in '},{'
t = t.replace(/\}\s*\{/g, '},{');
// missing comma in '},"nextkey"'
t = t.replace(/\}\s*"/g, '},"');
// missing comma in '"value","nextkey"' when there is \n
t = t.replace(/"\s*\n\s*"/g, '","');
// comma before close brackets
t = t.replace(/,\s*([}\]])/g, '$1');
try {
o = JSON.parse(t);
if (o && typeof o === 'object') {
t = JSON.stringify(o, null, 2);
t = t.replace(/\[\s+"([^,[\]]*)"\s+\]/g, '[ "$1" ]');
t = t.replace(/\{\s+"([^,{}}]*)"\s+\}/g, '{ "$1" }');
contentObj.innerText = t;
}
} finally {
// do not report an error
}
}
});
// files was dropped on dropzone
function drop(e) {
e.stopPropagation();
e.preventDefault();
const dtFiles = e.dataTransfer.files;
toastObj.info(`Uploading ${dtFiles.length} files...`);
const formData = new FormData();
const path = getPath();
for (let i = 0; i < dtFiles.length; i++) {
formData.append('file', dtFiles[i], path + dtFiles[i].name);
}
fetch('/', { method: 'POST', body: formData }).then(function() {
handleReloadFS();
toastObj.info('done.');
});
}
filesObj.addEventListener('dragenter', dragHelper, false);
filesObj.addEventListener('dragover', dragHelper, false);
filesObj.addEventListener('drop', drop, false);
window.addEventListener('DOMContentLoaded', handleReloadFS);
window.addEventListener('hashchange', handleReloadFS);
function jsonCheck() {
const fName = activeFileObj.innerText;
if (!fName.toLowerCase().endsWith('.json')) {
if (checkerObj.textContent != '--') {
checkerObj.textContent = '--';
checkerObj.className = '';
}
} else {
let o = null;
let t = contentObj.innerText;
if (t.indexOf('\xA0') >= 0)
contentObj.innerText = t = t.replace(/\u00A0/g, ' ');
try {
o = JSON.parse(t);
if (o && typeof o === 'object') {
// ok
} else {
o = null;
} // if
} catch (e) {
checkerObj.title = e.message;
}
if (o !== null) {
checkerObj.textContent = 'ok';
checkerObj.className = 'valid';
checkerObj.title = '';
} else {
checkerObj.textContent = 'no';
checkerObj.className = 'invalid';
}
window.status = t.length + '--' + (o !== null);
} // if
} // jsonCheck()
window.setInterval(jsonCheck, 500);
// call this to start blocking the button while activity is running.
function runOnce() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
var scope = this;
this.disabled = true;
DelayPromise(20) // give some time to display progress stuff
.then(function() {
return fetch('/api/reboot');
})
.then(function() {
return DelayPromise(20);
})
.finally(function(result) {
scope.disabled = false;
});
} // runOnce
var resetObj = document.getElementById('doReboot');
resetObj.addEventListener('click', runOnce);
</script>
</body>
</html>