-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptorium.js
454 lines (387 loc) · 12.6 KB
/
scriptorium.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
// Copyright (C) 2019-2021 by Shigeru Chiba.
'use strict'
function* range(from, to=null) {
if (to === null) {
to = from
from = 0
}
while (from < to)
yield from++
}
const Scriptorium = new class {
constructor() {
// HTML elements' ID
this.filename_id = 'filename'
this.zoom_id = 'zoom-inout'
this.chooser_id = 'filechooser'
this.downloader_id = 'downloader'
this.cells_id = 'cells'
this.editor_id = 'editor'
this.output_id = 'output'
this.canvas_id = 'canvas'
this.bottom_id = 'bottom'
this.run_btn1_id = 'run-btn1'
this.run_btn2_id = 'run-btn2'
this.run_and_new_id = 'run-and-new'
this.editorArea = null
this.consoleText = ''
this.storageKeyName = location.pathname + '/program'
this.backupFileName = location.pathname + '/backup'
this.isPC = !('ontouchend' in document)
this.isSafari = new Error().line
this.isFirefox = new Error().lineNumber
this.running_src = ''
this.audioContext = null
this.ErrorLine = class {
constructor(line, e) {
this.line = line ? line : '?'
this.error = e
}
}
}
onload() {
this.initializeAudio()
const keymap = { 'Tab': 'autocomplete' }
if (this.isPC)
keymap['Shift-Enter'] = function(cm){ Scriptorium.run() }
const editor = document.getElementById(this.editor_id)
this.editorArea = CodeMirror(editor, {
mode: 'javascript',
value: this.restoreAutoSaved(),
lineNumbers: true,
keyMap: 'emacs',
matchBrackets: true,
extraKeys: keymap,
gutters: ["CodeMirror-lint-markers"],
lint: { asi: true,
esversion: 10 },
})
this.editorArea.on('change', this.makeAutoSaver())
const zoomInOut = document.getElementById(this.zoom_id)
zoomInOut.innerHTML = Scriptorium.Msg.zoomOut
const filechooser = document.getElementById(this.chooser_id)
filechooser.onchange = (evt) => { this.readTextFile(evt.target.files[0]) }
const downloader = document.getElementById(this.downloader_id)
downloader.onmousedown = () => { this.makeDownloadLink() }
this.resizeCanvas()
this.editorArea.setSize('100%', '400px')
this.editorArea.focus()
}
initializeAudio() {
this.audioContext = function(win) {
const AudioContext = win.AudioContext || win.webkitAudioContext
return AudioContext ? new AudioContext() : null
}(window)
}
resizeCanvas() {
const editor = document.getElementById(this.editor_id)
const editorWidthWithMargin = editor.clientWidth + 30
let w = document.body.clientWidth - editorWidthWithMargin
let h = document.body.parentNode.clientHeight - 100
if (h < w)
w = h
if (w < 360 || (w < 400 && w < screen.availWidth - editorWidthWithMargin))
w = 690
const c = document.getElementById(this.canvas_id)
const ctx = c.getContext('2d')
const img = ctx.getImageData(0, 0, w, w)
c.width = w
c.height = w
ctx.putImageData(img, 0, 0)
}
// get the editor's content in a format writable into a file.
// this may return '' but does not return null.
getSourceFromEditor() {
return this.getProgramFromEditor()
}
// get a JavaScript program from the editor.
// this returns a non-null string object.
getProgramFromEditor() {
return this.editorArea.getDoc().getValue()
}
// source may be null or ''.
writeSourceToEditor(source) {
if (source === null)
source = ''
this.writeProgramToEditor(source)
}
// program must be a non-null string object.
writeProgramToEditor(program) {
this.editorArea.getDoc().setValue(program)
}
// this may return '' when no backup is found.
restoreAutoSaved() {
const text = localStorage.getItem(this.backupFileName)
if (text === null)
return ''
else
return String(text)
}
makeAutoSaver() {
const timeout = 3000 /* msec. */
const autosaver = () => {
const text = this.getSourceFromEditor()
if (text === '' || text === null)
localStorage.removeItem(this.backupFileName)
else
localStorage.setItem(this.backupFileName, text)
}
const debounce = (f, timeout) => {
let timer
return () => {
if (timer)
clearTimeout(timer)
timer = setTimeout(() => f(), timeout)
}
}
return debounce(autosaver, timeout)
}
/*
Control flow:
run() -> evalSrc() -> postRun() -> T.runTurtle()
-> runSrc() ~> postRun() -> T.runTurtle()
~> onerror -> postRun() -> T.runTurtle()
-> T.runTurtle() -> T.endTurtle()
~> timer -> run() ~> timer
-> End.run() -> endRunning() -> T.endTurtle()
-> StartProcessing.run() -> T.stopTurtle()
~> stopRunning() -> T.stopTurtle()
-> T.stopTurtle() -> endRunning() -> T.endTurtle()
~> : callback
-> : function call
T. : TurtleCmd
*/
run() {
this.turtleCmd.beeper.enableBeep() // for Safari
const src = this.getProgramFromEditor()
if (src === '')
return
if (this.isSafari)
this.evalSrc(src)
else
this.runSrc(src)
}
evalSrc(src) {
let success = true
let result = null
Scriptorium.running_src = src
const geval = eval
try {
// MDN web docs:
// If you use the eval function indirectly,
// by invoking it via a reference other than eval,
// it works in the global scope rather than the local scope.
// If it works in the local scope, the declared functions there
// are not globally visible.
result = geval(src)
}
catch (e) {
success = false
result = this.toErrorLine(e)
Scriptorium.turtleCmd.pushAlert(Scriptorium.Msg.alert(result.line, e))
}
this.postRun(src, success, result)
}
toErrorLine(e) {
// e.line is available only on Safari
// e.lineNumber is available only on Firefox
let line = e.line ? e.line : e.lineNumber
// Safari does not report a correct line number when the error is a syntax error.
if (e instanceof SyntaxError)
line = false
return new this.ErrorLine(line, e)
}
runSrc(src) {
window.onerror = (msg, src, line, col, err) => {
const result = err === null ? msg : err
if (typeof err === 'string')
line = '?' // thrown by Scriptorium.TurtleCmd.assertNumber()
Scriptorium.turtleCmd.pushAlert(Scriptorium.Msg.alert(line, result))
Scriptorium.postRun(Scriptorium.running_src, false, new this.ErrorLine(line, result))
return false // call the default error handler
}
Scriptorium.running_src = src
const s = document.createElement('script')
if (this.isFirefox)
s.innerHTML = 'try{ ' + src + '\n ;\n}catch(e){throw e}\nScriptorium.postRun(Scriptorium.running_src, true, undefined);\n'
else
s.innerHTML = '{ ' + src + '\n ;\n}\nScriptorium.postRun(Scriptorium.running_src, true, undefined);\n'
document.body.appendChild(s)
}
postRun(src, success, result) {
Scriptorium.turtleCmd.pushEnd(src, success, result)
this.consoleText = ''
const btn1 = document.getElementById(this.run_btn1_id)
const btn2 = document.getElementById(this.run_btn2_id)
btn1.innerHTML = btn2.innerHTML = Scriptorium.Msg.stop
btn1.onclick = btn2.onclick = (ev) => { Scriptorium.stopRunning() }
const canvas = document.getElementById(this.canvas_id)
const ctx = canvas.getContext('2d')
Scriptorium.turtleCmd.runTurtle(canvas, ctx)
window.onerror = null
if (this.isPC)
this.editorArea.focus()
}
// callback from turtle.js
print(values) {
let value = ''
if (values instanceof Array)
for (const v of values)
value += v + ' '
else
value = values
const value2 = this.escapeHTML(value) + '<br/>'
this.consoleText += value2
const out = document.getElementById(this.output_id)
out.innerText += value + '\n'
}
// callback from turtle.js
endRunning(src, success, result) {
const run_and_new = document.getElementById(this.run_and_new_id).checked
const cells = document.getElementById(this.cells_id)
if (run_and_new)
cells.innerHTML += '<pre class="codebox">' + this.escapeHTML(src) + '</pre>'
cells.innerHTML += '<p>'
cells.innerHTML += this.consoleText
if (result !== undefined)
cells.innerHTML += this.escapeHTML(this.getResultingMessage(result))
cells.innerHTML += '</p>'
cells.onclick = (ev) => { ev.target.focus() }
if (success && run_and_new) {
if (this.getProgramFromEditor() == src) {
const editor = this.editorArea
CodeMirror.emacs.kill(editor, { line: 0, ch: 0 }, {line: editor.lineCount(), ch: 0 }, true)
}
}
this.changeStopButton()
const out = document.getElementById(this.output_id)
out.innerText = ''
if (this.isPC) {
this.editorArea.focus()
document.getElementById(this.bottom_id).scrollIntoView(false)
}
}
getResultingMessage(result) {
if (result instanceof this.ErrorLine)
return `${result.error} (line: ${result.line})`
else
return result
}
// event handler for the "Stop" button
stopRunning() {
Scriptorium.turtleCmd.stopTurtle(true, undefined)
this.changeStopButton()
if (this.isPC)
this.editorArea.focus()
}
changeStopButton() {
const btn1 = document.getElementById(this.run_btn1_id)
const btn2 = document.getElementById(this.run_btn2_id)
btn1.innerHTML = btn2.innerHTML = Scriptorium.Msg.run
btn1.onclick = btn2.onclick = (ev) => { Scriptorium.run() }
}
buttonClicked(key) {
this.turtleCmd.processingCmd.callKeyPressed(key)
}
reset() {
const canvas = document.getElementById(this.canvas_id)
const ctx = canvas.getContext('2d')
ctx.clearRect(0, 0, canvas.width, canvas.height)
const out = document.getElementById(this.output_id)
out.innerText = ''
Scriptorium.turtleCmd.reset()
if (this.isPC)
this.editorArea.focus()
}
save() {
const program = this.getSourceFromEditor()
if (program !== null
&& confirm(Scriptorium.Msg.save)) {
localStorage.setItem(this.storageKeyName, JSON.stringify(program))
this.editorArea.focus()
}
}
load() {
const program = localStorage.getItem(this.storageKeyName)
if (program != null
&& confirm(Scriptorium.Msg.load)) {
this.writeSourceToEditor(JSON.parse(program))
this.editorArea.focus()
}
}
yank() {
this.writeProgramToEditor(Scriptorium.running_src)
if (this.isPC)
this.editorArea.focus()
}
clearSource() {
this.writeSourceToEditor(null)
if (this.isPC)
this.editorArea.focus()
}
toggleMenu() {
document.getElementById('nav').classList.toggle('in')
document.getElementById('hmenu').classList.toggle('in')
}
closeMenu() {
document.getElementById('nav').classList.remove('in')
document.getElementById('hmenu').classList.remove('in')
}
changeFontSize() {
const menuItem = document.getElementById(Scriptorium.zoom_id)
if (document.body.className == 'small-font') {
document.body.classList.remove('small-font')
menuItem.innerHTML = Scriptorium.Msg.zoomOut
}
else {
document.body.classList.add('small-font')
menuItem.innerHTML = Scriptorium.Msg.zoomIn
}
this.toggleMenu()
}
readTextFile(f) {
const reader = new FileReader()
reader.onload = (event) => {
this.writeSourceToEditor(event.target.result)
this.editorArea.focus()
}
reader.readAsText(f)
this.toggleMenu()
}
makeDownloadLink() {
const filename = document.getElementById(Scriptorium.filename_id).value
let mimeType = 'text/plain'
if (filename.endsWith('.js'))
mimeType = 'text/javascript'
else if (filename.endsWith('.html') || filename.endsWith('.htm'))
mimeType = 'text/html'
else if (filename.endsWith('.xml'))
mimeType = 'application/xml'
const program = this.getSourceFromEditor()
const blob = new Blob([program], { 'type' : mimeType })
const downloader = document.getElementById(this.downloader_id)
window.URL.revokeObjectURL(downloader.href)
downloader.href = window.URL.createObjectURL(blob)
downloader.download = filename
this.toggleMenu()
}
escapeHTML(str) {
let out = ''
for (const c of str + '') {
if(c === '<')
out += '<'
else if(c === '>')
out += '>'
else if(c === "'")
out += '''
else if(c === '"')
out += '"'
else if(c === '&')
out += '&'
else
out += c
}
return out
}
}
window.onload = function () { Scriptorium.onload() }