-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
100 lines (81 loc) · 2.16 KB
/
script.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
const wait = 100;
const intro = '# intro';
const intro_doc = {
description: 'You can type math.js expressions and see the result.',
examples: [
'2 + 2',
'round(e, 3)',
'log(100000, 10)',
'10cm to inch',
'sin(90 deg)',
'det([-1, 2; 3, 1])',
'1 kg * 1 m / s^2',
'help("round")',
]
}
function showDoc(doc) {
if (!doc) {
help.style.display = 'none';
return;
}
function hideEmpty(elem, value) {
elem.style.display = value ? 'block' : 'none';
}
help_name.textContent = doc.name;
help_description.textContent = doc.description;
help_syntax_code.textContent = doc.syntax?.join("\n");
hljs.highlightElement(help_syntax_code);
hideEmpty(help_syntax, doc.syntax);
help_examples_code.textContent = doc.examples?.join("\n");
hljs.highlightElement(help_examples_code);
hideEmpty(help_examples, doc.examples);
help_seealso_text.textContent = doc.seealso?.join(", ");
hideEmpty(help_seealso, doc.seealso);
help.style.display = 'block';
}
function doMath(input) {
let output = [];
let scope = {};
let doc;
for (const line of input.split('\n')) {
let output_line = '';
if (line) {
if (line.startsWith('#')) {
if (line == '# intro') doc = intro_doc;
output_line = '#';
} else {
try {
const r = math.evaluate(line, scope);
if (r) {
if (r.doc) doc = r.doc;
else output_line = math.format(r, 14);
}
} catch(e) {
output_line = e.toString();
}
}
}
output.push(output_line);
}
results.updateCode(output.join('\n'));
showDoc(doc);
}
function dropHandler(ev) {
ev.preventDefault();
const file = ev.dataTransfer.items[0].getAsFile();
file.text().then(e => editor.updateCode(e));
}
async function start(url) {
let code = intro;
if (url) code = await (await fetch(url)).text();
editor.updateCode(code);
doMath(editor.toString());
}
var timer;
editor.onUpdate(code => {
clearTimeout(timer);
timer = setTimeout(doMath, wait, code);
});
hljs.configure({ ignoreUnescapedHTML: true });
const params = new URLSearchParams(window.location.search);
start(params.get('input'));