forked from circuitpython/web-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
601 lines (527 loc) · 17.9 KB
/
script.js
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
import { basicSetup } from "codemirror";
import { EditorView, keymap } from "@codemirror/view";
import { EditorState } from "@codemirror/state";
import {indentWithTab} from "@codemirror/commands"
import { python } from "@codemirror/lang-python";
import { syntaxHighlighting, indentUnit } from "@codemirror/language";
import { classHighlighter } from "@lezer/highlight";
import { getFileIcon } from "./common/file_dialog.js";
import { Terminal } from '@xterm/xterm';
import { FitAddon } from '@xterm/addon-fit';
import { WebLinksAddon } from '@xterm/addon-web-links';
import state from './state.js'
import { BLEWorkflow } from './workflows/ble.js';
import { USBWorkflow } from './workflows/usb.js';
import { WebWorkflow } from './workflows/web.js';
import { isValidBackend, getBackendWorkflow, getWorkflowBackendName } from './workflows/workflow.js';
import { ButtonValueDialog, MessageModal } from './common/dialogs.js';
import { isLocal, switchUrl, getUrlParam } from './common/utilities.js';
import { CONNTYPE } from './constants.js';
import './layout.js'; // load for side effects only
import { mainContent, showSerial } from './layout.js';
// Instantiate workflows
let workflows = {};
workflows[CONNTYPE.Ble] = new BLEWorkflow();
workflows[CONNTYPE.Usb] = new USBWorkflow();
workflows[CONNTYPE.Web] = new WebWorkflow();
let workflow = null;
let unchanged = 0;
let connectionPromise = null;
const btnRestart = document.querySelector('.btn-restart');
const btnClear = document.querySelector('.btn-clear');
const btnConnect = document.querySelectorAll('.btn-connect');
const btnNew = document.querySelectorAll('.btn-new');
const btnOpen = document.querySelectorAll('.btn-open');
const btnSave = document.querySelectorAll('.btn-save');
const btnSaveAs = document.querySelectorAll('.btn-save-as');
const btnSaveRun = document.querySelectorAll('.btn-save-run');
const btnInfo = document.querySelector('.btn-info');
const terminalTitle = document.getElementById('terminal-title');
const messageDialog = new MessageModal("message");
const connectionType = new ButtonValueDialog("connection-type");
const editorTheme = EditorView.theme({}, {dark: true});
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('mobile-menu-button').addEventListener('click', handleMobileToggle);
document.querySelectorAll('#mobile-menu-contents li a').forEach((element) => {
element.addEventListener('click', handleMobileToggle);
});
});
function handleMobileToggle(event) {
event.preventDefault();
var menuContainer = document.getElementById('mobile-menu-contents');
menuContainer.classList.toggle('hidden');
var menuIcon = document.querySelector('#mobile-menu-button > i');
if (menuContainer.classList.contains('hidden')) {
menuIcon.classList.replace('fa-times', 'fa-bars');
} else {
menuIcon.classList.replace('fa-bars', 'fa-times');
}
}
// New Link/Button (Mobile and Desktop Layout)
btnNew.forEach((element) => {
element.addEventListener('click', async function(e) {
e.preventDefault();
e.stopPropagation();
await newFile();
});
});
// Open Link/Button (Mobile and Desktop Layout)
btnOpen.forEach((element) => {
element.addEventListener('click', async function(e) {
e.preventDefault();
e.stopPropagation();
await openFile();
});
});
// Save Link/Button (Mobile and Desktop Layout)
btnSave.forEach((element) => {
element.addEventListener('click', async function(e) {
e.preventDefault();
e.stopPropagation();
await saveFile();
});
});
// Save As Link/Button (Mobile and Desktop Layout)
btnSaveAs.forEach((element) => {
element.addEventListener('click', async function(e) {
e.preventDefault();
e.stopPropagation();
if (await checkConnected()) {
let path = await workflow.saveFileAs();
if (path !== null) {
console.log("Current File Changed to: " + workflow.currentFilename);
}
}
});
});
// Save + Run Link/Button (Mobile and Desktop Layout)
btnSaveRun.forEach((element) => {
element.addEventListener('click', async function(e) {
e.preventDefault();
e.stopPropagation();
await saveRunFile();
});
});
// Restart Button
btnRestart.addEventListener('click', async function(e) {
if (await checkConnected()) {
// Perform a device soft restart
await workflow.restartDevice();
}
});
// Clear Button
btnClear.addEventListener('click', async function(e) {
state.terminal.clear();
});
btnInfo.addEventListener('click', async function(e) {
if (await checkConnected()) {
await workflow.showInfo(getDocState());
}
});
// Basic functions used for buttons and hotkeys
async function openFile() {
if (await checkConnected()) {
workflow.openFile();
}
}
async function saveFile() {
if (await checkConnected()) {
await workflow.saveFile();
}
}
async function newFile() {
if (await checkConnected()) {
if (await workflow.checkSaved()) {
loadFileContents(null, "");
}
}
}
async function saveRunFile() {
if (await checkConnected()) {
if (await workflow.saveFile()) {
setSaved(true);
await workflow.runCurrentCode();
}
}
}
function setSaved(saved) {
if (saved) {
mainContent.classList.remove("unsaved");
} else {
mainContent.classList.add("unsaved");
}
}
async function checkConnected() {
if (!workflow || !workflow.connectionStatus()) {
let connType = await chooseConnection();
if (!connType) {
return false;
}
await loadWorkflow(connType);
// Connect if we're local (Web Workflow Only)
if ((isLocal()) && workflow.host) {
if (await workflowConnect()) {
await checkReadOnly();
}
}
if (!workflow.connectionStatus()) {
// Display the appropriate connection dialog
await workflow.showConnect(getDocState());
} else if (workflow.type === CONNTYPE.Web) {
// We're connected, local, and using Web Workflow
await workflow.showInfo(getDocState());
}
}
return true;
}
function getDocState() {
return workflow.makeDocState(editor.state.doc.sliceString(0), unchanged);
}
async function workflowConnect() {
let returnVal;
if (!workflow) return false;
if ((returnVal = await workflow.showBusy(workflow.connect())) instanceof Error) {
await showMessage(`Unable to connect. ${returnVal.message}`);
return false;
}
return true;
}
async function checkReadOnly() {
const readOnly = await workflow.readOnly();
btnSaveAs.forEach((element) => {
element.disabled = readOnly;
});
btnSaveRun.forEach((element) => {
element.disabled = readOnly;
});
if (readOnly instanceof Error) {
await showMessage(readOnly);
return false;
} else if (readOnly) {
await showMessage("Warning: File System is in read only mode. Disable the USB drive to allow write access.");
}
return true;
}
/* Update the filename and update the UI */
function setFilename(path) {
// Use the extension_map to figure out the file icon
let filename = path;
// Prepend an icon to the path
const [style, icon] = getFileIcon(path);
filename = `<i class="${style} ${icon}"></i> ` + filename;
if (path === null) {
filename = "[New Document]";
btnSave.forEach((b) => b.style.display = 'none');
} else if (!workflow) {
throw Error("Unable to set path when no workflow is loaded");
} else {
btnSave.forEach((b) => b.style.display = null);
}
if (workflow) {
workflow.currentFilename = path;
}
document.querySelector('#editor-bar .file-path').innerHTML = filename;
document.querySelector('#mobile-editor-bar .file-path').innerHTML = path === null ? filename : filename.split("/")[filename.split("/").length - 1];
}
async function chooseConnection() {
// Don't allow more than one dialog
if (connectionPromise) return;
// Get the promise first
connectionPromise = connectionType.open();
// Disable any buttons in validBackends, but not in workflows
let modal = connectionType.getModal();
let buttons = modal.querySelectorAll("button");
for (let button of buttons) {
if (!getBackendWorkflow(button.value)) {
button.disabled = true;
}
};
// Wait for the user to click a button
let connType = await connectionPromise;
connectionPromise = null
if (isValidBackend(connType)) {
return getBackendWorkflow(connType);
}
// Outside of dialog was clicked
return null;
}
// Dynamically Load a Workflow (where the magic happens)
async function loadWorkflow(workflowType = null) {
let currentFilename = null;
if (workflow && workflowType == null) {
// Get the last workflow
workflowType = workflow.type;
}
if (!(workflowType in workflows) && workflowType != CONNTYPE.None) {
return false;
}
// Unload anything from the current workflow
if (workflow != null) {
// Update Workflow specific UI elements
await workflow.deinit();
}
if (workflowType != CONNTYPE.None) {
// Is the requested workflow different than the currently loaded one?
if (workflow != workflows[workflowType]) {
console.log("Load different workflow");
if (workflow) {
currentFilename = workflow.currentFilename;
if (isLocal()) {
let url = "https://code.circuitpython.org";
if (location.hostname == "localhost" || location.hostname == "127.0.0.1") {
url = `${location.protocol}//${location.host}`;
}
switchUrl(url, getDocState(), getWorkflowBackendName(workflowType));
}
}
workflow = workflows[workflowType];
// Initialize the workflow
await workflow.init({
terminal: state.terminal,
terminalTitle: terminalTitle,
loadEditorFunc: loadEditor,
debugLogFunc: debugLog,
disconnectFunc: disconnectCallback,
isDirtyFunc: isDirty,
setFilenameFunc: setFilename,
saveFileFunc: saveFileContents,
loadFileFunc: loadFileContents,
loadEditorContentsFunc: loadEditorContents,
showMessageFunc: showMessage,
currentFilename: currentFilename,
showSerialFunc: showSerial,
});
} else {
console.log("Reload workflow");
}
} else {
console.log("Unload workflow");
if (workflow != null) {
// Update Workflow specific UI elements
await workflow.disconnectButtonHandler();
}
// Unload workflow
workflow = null;
}
}
const hotkeyMap = [
{ key: "Mod-s", run: saveFile },
{ key: "Mod-o", run: openFile },
{ key: "Alt-n", run: newFile },
{ key: "Mod-r", run: saveRunFile },
];
const editorExtensions = [
basicSetup,
keymap.of([indentWithTab]),
keymap.of(hotkeyMap),
indentUnit.of(" "),
python(),
editorTheme,
syntaxHighlighting(classHighlighter),
EditorView.updateListener.of(onTextChange)
];
// Use the editor's function to check if anything has changed
function isDirty() {
if (unchanged == editor.state.doc.length) return false;
return true;
}
function loadEditorContents(content) {
editor.setState(EditorState.create({
doc: content,
extensions: editorExtensions
}));
unchanged = editor.state.doc.length;
//console.log("doc length", unchanged);
}
setFilename(null);
async function showMessage(message) {
return await messageDialog.open(message);
}
async function debugLog(msg) {
state.terminal.writeln(''); // get a fresh line without any prior content (a '>>>' prompt might be there without newline)
state.terminal.writeln(`\x1b[93m${msg}\x1b[0m`);
}
function updateUIConnected(isConnected) {
if (isConnected) {
// Set to Connected State
btnConnect.forEach((element) => {
element.innerHTML = "Disconnect";
element.disabled = false;
});
if (workflow.showInfo !== undefined) {
btnInfo.disabled = false;
}
} else {
// Set to Disconnected State
btnConnect.forEach((element) => {
element.innerHTML = "Connect";
element.disabled = false;
});
btnInfo.disabled = true;
}
}
window.onbeforeunload = () => {
if (isDirty()) {
return "You have unsaved changed, exit anyways?";
}
};
async function loadEditor() {
let documentState = loadParameterizedContent();
if (documentState) {
loadFileContents(documentState.path, documentState.contents, null);
unchanged = documentState.pos;
setSaved(!isDirty());
}
updateUIConnected(true);
}
var editor;
var currentTimeout = null;
// Save the File Contents and update the UI
async function saveFileContents(path) {
// If this is a different file, we write everything
if (path !== workflow.currentFilename) {
unchanged = 0;
}
let doc = editor.state.doc;
let offset = 0;
let contents = doc.sliceString(0);
if (workflow.partialWrites) {
offset = unchanged;
console.log("sync starting at", unchanged, "to", editor.state.doc.length);
}
let oldUnchanged = unchanged;
unchanged = doc.length;
try {
if (await workflow.writeFile(path, contents, offset)) {
setFilename(workflow.currentFilename);
setSaved(true);
} else {
await showMessage(`Saving file '${workflow.currentFilename} failed.`);
}
} catch (e) {
console.error("write failed", e, e.stack);
unchanged = Math.min(oldUnchanged, unchanged);
if (currentTimeout != null) {
clearTimeout(currentTimeout);
}
currentTimeout = setTimeout(saveFileContents, 2000);
}
}
// Load the File Contents and Path into the UI
function loadFileContents(path, contents, saved = true) {
setFilename(path);
loadEditorContents(contents);
if (saved !== null) {
setSaved(saved);
}
console.log("Current File Changed to: " + workflow.currentFilename);
}
async function onTextChange(update) {
if (!update.docChanged) {
return;
}
var hasGap = false;
update.changes.desc.iterGaps(function(posA, posB, length) {
// this are unchanged gaps.
hasGap = true;
if (posA != 0 && posB != 0) {
return;
} else if (posA == 0 && posB == 0) {
unchanged = Math.min(length, unchanged);
} else {
unchanged = 0;
}
});
// Everything has changed.
if (!hasGap) {
unchanged = 0;
}
if (currentTimeout != null) {
clearTimeout(currentTimeout);
}
setSaved(false);
}
function disconnectCallback() {
updateUIConnected(false);
}
editor = new EditorView({
state: EditorState.create({
doc: "",
extensions: editorExtensions
}),
parent: document.querySelector('#editor')
});
async function setupXterm() {
state.terminal = new Terminal({
theme: {
background: '#333',
foreground: '#ddd',
cursor: '#ddd',
}
});
state.fitter = new FitAddon();
state.terminal.loadAddon(state.fitter);
state.terminal.loadAddon(new WebLinksAddon());
state.terminal.open(document.getElementById('terminal'));
state.terminal.onData(async (data) => {
if (await checkConnected()) {
workflow.serialTransmit(data);
}
});
}
function getBackend() {
let backend = getUrlParam("backend");
if (backend && isValidBackend(backend)) {
return getBackendWorkflow(backend);
} else if (isLocal()) {
return getBackendWorkflow("web");
}
return null;
}
function loadParameterizedContent() {
let documentState = getUrlParam("state");
if (documentState) {
documentState = JSON.parse(decodeURIComponent(documentState));
}
return documentState;
}
document.addEventListener('DOMContentLoaded', async (event) => {
await setupXterm();
btnConnect.forEach((element) => {
element.addEventListener('click', async function(e) {
e.preventDefault();
e.stopPropagation();
// Check if we have an active connection
if (workflow != null && workflow.connectionStatus()) {
// If so, unload the current workflow
await workflow.disconnectButtonHandler(null);
} else {
// If not, it should display the available connections
await checkConnected();
}
});
});
// Check backend param and load appropriate type if specified
let backend = getBackend();
if (backend) {
await loadWorkflow(backend);
// If we don't have all the info we need to connect
let returnVal = await workflow.parseParams();
if (returnVal === true && await workflowConnect() && workflow.type === CONNTYPE.Web) {
if (await checkReadOnly()) {
// We're connected, local, no errors, and using Web Workflow
await workflow.showInfo(getDocState());
}
} else {
if (returnVal instanceof Error) {
await showMessage(returnVal);
} else {
loadEditor();
await workflow.showConnect(getDocState());
}
}
} else {
//await showMessage("USB Workflow is currently experiencing issues. See <a href=\"https://github.com/circuitpython/web-editor/issues/203\">GitHub issue #203</a> for more details. Please use Web Workflow.");
await checkConnected();
}
});