-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
82 lines (76 loc) · 2.49 KB
/
main.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
let worker = null;
document.addEventListener('DOMContentLoaded', (event) => {
});
let deleteWorker = function() {
worker.terminate();
worker = null;
};
let keyupListener = function(event) {
if(event.key === "Enter") {
event.preventDefault();
keyupListener.sendBtn.click();
}
};
let runClicked = function(context, debug_mode, max_lines) {
if (debug_mode === undefined) {
debug_mode = true;
}
if (max_lines === undefined) {
max_lines = 6;
}
if (worker !== null) {
deleteWorker();
}
let inputArea = context.parentNode.parentNode.children[0];
let outputArea = context.parentNode.children[1];
if (context.parentNode.children.length > 3) {
intentInput = context.parentNode.children[2];
let sendBtn = context.parentNode.children[3];
keyupListener.sendBtn = sendBtn;
intentInput.addEventListener("keyup", keyupListener, false);
intentInput.style.display = "";
sendBtn.style.display = "";
sendBtn.onclick = function() {
if (worker !== null) {
worker.postMessage({
type: 'handle-event',
input: intentInput.value
});
intentInput.value = "";
}
};
}
let dmsExample = inputArea.innerText.split('\n');
dmsExample.shift();
let dms = dmsExample.join('\n');
outputArea.innerText = "";
let dmpl = Compiler(dms);
worker = new Worker('./worker.js');
worker.onmessage = function(msg) {
let { data } = msg;
switch(data.type) {
case 'print':
let dataJson = JSON.parse(data.text);
if (dataJson['@act'] !== undefined) {
outputArea.innerText += `${JSON.stringify(dataJson['@act'])}\n`;
} else if (dataJson['@set'] !== undefined) {
outputArea.innerText += `${dataJson['@set']} = ${JSON.stringify(dataJson['val'])}\n`;
}
if (outputArea.innerText.split('\n').length > max_lines) {
outputArea.innerText += `...`;
deleteWorker();
}
break;
default:
throw new Error('Unknown message from worker: ' + JSON.stringify(data));
}
};
worker.postMessage({
type: 'start',
dependencies: {
"__main__": dmpl
},
debug_mode: debug_mode
});
return false;
}