forked from code4fukui/DNCL3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
207 lines (189 loc) · 5.02 KB
/
index.html
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
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><link rel="icon" href="data:">
<title>Wirth Playground</title>
</head><body>
<h1>Wirth Playground</h1>
examples <select id=selexamples>
<option value="">-</option>
</select>
<button id=btnrun>RUN</button>
<button id=btnstop>STOP</button>
<label><input type=checkbox id=chkautorun checked>auto run</label>
<label><input type=checkbox id=chkvar checked>show vars</label>
<main>
<div class=diveditor id=divprog></div>
<div>
<div class=diveditor id=divrun></div>
<input type=text id=ininput disabled>
</div>
</main>
<a href=https://github.com/code4fukui/Wirth/blob/main/README.md>Wirth Language Reference</a>
<a href=https://github.com/code4fukui/Wirth/>src on GitHub</a>
<style>
body {
font-family: sans-serif;
}
h1 {
margin: 0;
}
main > div {
width: calc(50% - .5em);
padding: .1em;
vertical-align: top;
display: inline-block;
}
.diveditor {
display: inline-block;
height: calc(100vh - 8em);
}
.diveditor#divrun {
width: 100%;
height: calc(100vh - 8em - 3.5em);
}
main input {
box-sizing: border-box;
width: 100%;
font-size: 16px;
height: 2.8em;
margin: .1em 0;
padding: 0.3em 0.3em;
}
a {
color: gray !important;
}
@media only screen and (max-width: 600px) {
.diveditor {
width: calc(100vw - .8em);
height: calc(50dvh - 5em);
}
}
</style>
<script type="module">
import { Wirth } from "./Wirth.js";
import { monaco } from "https://code4fukui.github.io/monaco-editor/monaco.js";
import { CSV } from "https://js.sabae.cc/CSV.js";
import { removeHash } from "https://js.sabae.cc/removeHash.js";
export const makeEditor = (div, language) => {
const editor = monaco.editor.create(div, {
language,
autoIndent: true,
//autoIndent: "none",
//formatOnPaste: true,
//formatOnType: true,
fontSize: 16, // for mobile
lineHeight: 24,
suggestOnTriggerCharacters: false, // トリガーキャラクターでの補完を無効化
acceptSuggestionOnEnter: 'off', // Enterでの補完選択を無効化
parameterHints: false, // パラメータヒントを無効化
quickSuggestions: false, // 自動的に補完候補を表示しない
inlineSuggest: { enabled: false }, // インライン補完を無効化
tabSize: 2,
minimap: { enabled: false },
//overflow: "auto",
automaticLayout: true,
theme: "vs-dark",
});
window.addEventListener("resize", () => {
editor.layout();
});
return editor;
};
const prog = makeEditor(divprog);
const run = makeEditor(divrun);
const removeStartEnd = (json) => {
for (const name in json) {
const v = json[name];
if (typeof v == "object") {
removeStartEnd(v);
} else if (name == "start" || name == "end") {
delete json[name];
}
}
};
const input = (s) => {
if (ininput.reject) {
ininput.reject();
}
ininput.disabled = false;
ininput.value = "";
ininput.placeholder = s || "Please input here.";
const p = new Promise((resolve, reject) => {
ininput.reject = reject;
ininput.onkeyup = (e) => {
if (e.key == "Enter") {
resolve(ininput.value);
ininput.disabled = true;
}
};
});
return p;
};
let running = false;
let abortctrl = null;
const runScript = async () => {
//console.log("onchange", performance.now(), running)
if (running) {
//abortctrl.abort();
return;
}
abortctrl = new AbortController();
running = true;
const src = prog.getValue();
run.setValue("");
run.revealLine(1);
try {
const output = (s) => {
run.setValue(run.getValue() + s + "\n");
};
const runtime = new Wirth(src, output, input);
const maxloop = 0; // chkmaxloop.checked ? inmaxloop.value : 0;
btnstop.onclick = () => {
abortctrl.abort();
};
await runtime.run(maxloop, abortctrl.signal);
if (chkvar.checked) {
run.setValue(run.getValue() + "\n" + runtime.getVars() + "\n");
}
} catch (e) {
console.log(e);
//ast.setValue(JSON.stringify(e, null, 2));
run.setValue(run.getValue() + e.toString() + "\n");
}
running = false;
};
const onchange = async () => {
if (chkautorun.checked) runScript();
};
btnrun.onclick = () => runScript();
//prog.onDidChangeModelContent = onchange; // なぜか初回のみ
let bkval = null;
setInterval(async () => {
const txt = prog.getValue();
if (bkval == txt) return;
bkval = txt;
await onchange();
}, 500);
const data = await CSV.fetchJSON("examples.csv");
for (const item of data) {
const opt = document.createElement("option");
opt.textContent = item.title;
opt.value = item.fn;
selexamples.appendChild(opt);
}
selexamples.oninput = async () => {
const fn = selexamples.value;
if (!fn) {
removeHash();
return;
}
location.hash = fn;
const s = await (await fetch("examples/" + fn)).text();
prog.setValue(s);
};
//const fn = "print.wirth";
const fn = "";
const fn0 = location.hash.substring(1) || fn;
selexamples.value = fn0;
selexamples.oninput();
chkvar.oninput = () => onchange();
</script>
</body></html>