This repository was archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptor.js
More file actions
120 lines (100 loc) · 3.09 KB
/
scriptor.js
File metadata and controls
120 lines (100 loc) · 3.09 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
window.addEventListener("load", () => {
let worker = null;
let output = document.querySelector("#output");
let run = document.querySelector("#run");
let stop = document.querySelector("#stop");
// Initialize code editor
let editor = CodeMirror.fromTextArea(
document.querySelector("#code"),
{
mode: "python",
theme: "neo",
lineNumbers: true,
lineWrapping: true,
styleActiveLine: true,
styleActiveSelected: true,
matchBrackets: true,
keyMap: "sublime",
smartIndent: true,
indentUnit: 4,
indentWithTabs: false,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
foldGutter: true,
autofocus: true,
autorefresh: true,
autoCloseBrackets: true
}
);
// Load a script into the editor?
if (window.location.hash) {
fetch(window.location.hash.substring(1))
.then((response) => response.text())
.then((text) => editor.setValue(text))
}
function writeOutput(items, cls) {
if (items === undefined) {
return;
}
let li = document.createElement("li");
if (cls) {
li.className = cls;
}
li.innerText = items.join(" ");
output.appendChild(li);
}
function eventHandler(event) {
const data = event.data;
console.debug(data);
switch (data.type) {
case "download":
let a = document.createElement("a");
a.href = URL.createObjectURL(data.blob);
a.style["visible"] = "hidden";
a.download = data.filename;
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
break;
case "error":
stop.click();
writeOutput(data.error, "error raw");
break;
case "exit":
stop.click();
break;
case "print":
writeOutput(data.items, data.level);
break;
case "result":
stop.click();
writeOutput(data.result);
break;
default:
writeOutput(`${data.type} not implemented`, "error");
}
}
run.addEventListener(
"click", () => {
if (worker !== null) {
worker.terminate();
}
worker = new Worker("scriptor-worker.js");
worker.onmessage = eventHandler;
worker.onerrormessage = eventHandler;
worker.postMessage({code: editor.getValue()});
stop.disabled = false;
}
);
clear.addEventListener(
"click", () => {
output.innerHTML = "";
}
);
stop.addEventListener(
"click", () => {
worker.terminate();
worker = null;
stop.disabled = true;
}
);
});