Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (143 loc) · 5.96 KB
/
Copy pathscript.js
File metadata and controls
171 lines (143 loc) · 5.96 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
import { Console } from "./utils/console.js";
import { PulsarScript, StepKind } from "./utils/pulsarScript.js";
const myConsole = new Console();
/** @type {HTMLPreElement} */
let $report;
let runningScript = null;
/** @returns {Promise<PulsarScript>} */
async function loadNewScript(fileName, buffer) {
myConsole.clearAll();
$report.innerText = "";
while (runningScript != null) {
const thisScript = runningScript;
await thisScript.stop();
// if no other script was ran or this signal is still active
if (runningScript == null || runningScript === thisScript) {
// clear the script and resume execution
runningScript = null;
}
}
const $windows = document.getElementById("windows");
$windows.innerHTML = "";
try {
runningScript = new PulsarScript(fileName, buffer);
const scriptHooks = runningScript.bindNatives(
(...data) => myConsole.write(...data),
(stopSignal) => myConsole.read(stopSignal));
runningScript.onReport(message => {
$report.innerText = message;
});
$windows.append(...scriptHooks.windows);
} catch (error) {
$report.innerText = `${error.constructor.name}: ${error.message}`;
runningScript = null;
}
return runningScript;
}
window.addEventListener("load", async () => {
window.addEventListener("keydown", async ev => await runningScript?.hooks?.sendKeyDown(ev));
window.addEventListener("keyup", async ev => await runningScript?.hooks?.sendKeyUp(ev));
window.addEventListener("click", async ev => await runningScript?.hooks?.sendClick(ev));
const $console = document.getElementById("console");
$console.replaceWith(myConsole.$element);
$report = document.getElementById("report");
/** @type {HTMLSelectElement} */
const $examplePicker = document.getElementById("example-picker");
const $exampleShare = document.getElementById("example-share");
/** @type {HTMLLabelElement} */
const $scriptLabel = document.getElementById("script-label");
const NO_SCRIPT_TEXT = $scriptLabel.innerText;
/** @type {HTMLInputElement} */
const $scriptPicker = document.getElementById("script-picker");
let debugStep;
/** @type {HTMLInputElement} */
const $debug = document.getElementById("debug");
const $stepButtons = document.getElementById("step-buttons");
$stepButtons.classList.add("collapsed");
const attachStepFunction = (elementId, stepKind) => {
const $step = document.getElementById(elementId);
$step.addEventListener("click", () => {
if (debugStep != null) {
debugStep(stepKind).catch(console.warn);
}
});
};
attachStepFunction("debug-step", StepKind.Instruction);
attachStepFunction("debug-step-over", StepKind.StepOver);
attachStepFunction("debug-step-into", StepKind.StepInto);
attachStepFunction("debug-step-out", StepKind.StepOut);
attachStepFunction("debug-step-continue", StepKind.Continue);
attachStepFunction("debug-step-pause", StepKind.Pause);
const runScript = async (fileName, buffer) => {
debugStep = undefined;
const script = await loadNewScript(fileName, buffer);
if ($debug.checked) {
$stepButtons.classList.remove("collapsed");
debugStep = script.runDebug({ linesBefore: 2, linesAfter: 2 });
} else {
$stepButtons.classList.add("collapsed");
script.run(undefined, { frameTime: 250 }).catch(console.warn);
}
};
const clearExamplePicker = () => {
$examplePicker.value = "";
};
const runFromExamplePicker = async () => {
const filePath = $examplePicker.value;
if (filePath.length <= 0) return;
const file = await fetch(filePath);
const fileBytes = await file.bytes();
clearScriptPicker();
runScript(filePath, fileBytes.buffer);
};
const clearScriptPicker = () => {
$scriptLabel.innerText = NO_SCRIPT_TEXT;
$scriptPicker.value = "";
};
const runFromScriptPicker = async () => {
if ($scriptPicker.files.length <= 0) {
clearScriptPicker();
return;
}
const file = $scriptPicker.files[0];
const fileBytes = await file.bytes();
$scriptLabel.innerText = `Script: '${file.name}'`;
clearExamplePicker();
runScript(file.name, fileBytes.buffer);
};
clearExamplePicker();
clearScriptPicker();
$examplePicker.addEventListener("change", () => runFromExamplePicker());
$scriptPicker.addEventListener("input", () => runFromScriptPicker());
/** @param {HTMLOptionElement} $option */
const getExampleName = ($option) => {
return $option.innerText.replaceAll(/\s+/g, "").toLowerCase();
};
$exampleShare.addEventListener("click", () => {
if ($examplePicker.value.length <= 0) return;
const $option = $examplePicker.options.item($examplePicker.selectedIndex);
const url = new URL(document.location);
url.searchParams.set("example", getExampleName($option));
window.location.replace(url);
});
{
const url = new URL(document.location);
const example = url.searchParams.get("example")?.toLowerCase();
if (example != null) {
console.log(`Loading '${example}' from URL`);
let exampleFound = false;
for (let index = 0; index < $examplePicker.options.length; index++) {
const $option = $examplePicker.options.item(index);
if (getExampleName($option) === example) {
exampleFound = true;
$examplePicker.selectedIndex = index;
runFromExamplePicker();
break;
}
}
if (!exampleFound) {
console.warn(`Could not load '${example}', it's not a valid example`);
}
}
}
});