forked from betaveros/noulith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
180 lines (173 loc) · 4.76 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
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
<style>
body {
font-family: sans-serif;
}
textarea {
width: 100%;
font-family: monospace;
}
#output, #error {
background-color: #f8f8f8;
border: 1px solid #ddd;
padding: 0.5em;
font-family: monospace;
white-space: pre-wrap;
}
#result {
font-family: monospace;
white-space: pre-wrap;
}
table {
border-collapse: collapse;
}
td {
border: 1px solid lightgray;
padding: 0.25em;
}
.default {
opacity: 0.5;
}
#error {
color: #c00;
}
#outer-output, #url {
display: none;
}
</style>
</head>
<body>
<h1>noulith</h1>
<label for="code">Code:</label>
<textarea id="code"></textarea>
<label for="input">Input:</label>
<textarea id="input"></textarea>
<button id="run" type="button" role="button" disabled>Loading...</button> <label><input type="checkbox" id="fancy" checked /> Fancy output</label>
<div id="outer-output">
<div id="mid-output">Output: <div id="output"></div></div>
<div id="mid-result">Result: <div id="result"></div></div>
<div id="mid-error">Error: <div id="error"></div></div>
</div>
<button id="gen-url" type="button" role="button">Generate URL</button>
<textarea id="url"></textarea>
<script src='pkg/paradoc.js'></script>
<script type="module">
// https://rustwasm.github.io/docs/wasm-bindgen/examples/without-a-bundler.html (???)
// wasm-pack build --target web
// import init, { encapsulated_eval } from './pkg/paradoc.js';
// wasm-pack build --target no-modules
// const { encapsulated_eval } = wasm_bindgen;
async function prepare(worker) {
// await wasm_bindgen('./pkg/paradoc_bg.wasm');
const code = document.getElementById('code');
const input = document.getElementById('input');
const runner = document.getElementById('run');
const fancy = document.getElementById('fancy');
function run() {
runner.disabled = true;
worker.postMessage([code.value, input.value, fancy.checked]);
}
runner.addEventListener('click', run);
code.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.key == "Enter") {
run();
}
});
runner.disabled = false;
runner.textContent = "Run!";
}
function b64encode(s) {
const carr = [];
new TextEncoder().encode(s).forEach(function(u8) {
carr.push(String.fromCharCode(u8));
});
return btoa(carr.join(""));
}
function b64decode(s) {
const bs = atob(s);
const uarr = new Uint8Array(bs.length);
for (let i = 0; i < bs.length; i++) {
uarr[i] = bs.charCodeAt(i);
}
return new TextDecoder().decode(uarr);
}
function renderInto(obj, node) {
console.log(obj);
console.log(node.tagName);
if (node.tagName === 'TR') {
if (Array.isArray(obj)) {
for (let elt of obj) {
let td = document.createElement("td");
renderInto(elt, td);
node.appendChild(td);
}
} else {
let td = document.createElement("td");
if (obj === false) {
td.textContent = "default";
td.classList.add("default");
} else {
td.textContent = obj;
}
node.appendChild(td);
}
} else {
if (Array.isArray(obj)) {
let table = document.createElement("table");
for (let elt of obj) {
let tr = document.createElement("tr");
renderInto(elt, tr);
table.appendChild(tr);
}
node.appendChild(table);
} else {
if (obj === false) {
node.textContent = "default";
node.classList.add("default");
} else {
node.textContent = obj;
}
}
}
}
document.addEventListener('DOMContentLoaded', function() {
var base64 = location.hash.substr(1);
const code = document.getElementById('code');
const urlElem = document.getElementById('url');
if (base64.length) {
code.value = b64decode(base64);
}
document.getElementById('gen-url').addEventListener('click', function() {
urlElem.value = location.protocol + '//' + location.host + location.pathname + '#' + b64encode(code.value);
urlElem.style.display = 'block';
});
const outerOutput = document.getElementById('outer-output');
const output = document.getElementById('output');
const result = document.getElementById('result');
const error = document.getElementById('error');
const midOutput = document.getElementById('mid-output');
const midResult = document.getElementById('mid-result');
const midError = document.getElementById('mid-error');
const runner = document.getElementById('run');
const myWorker = new Worker('worker.js');
myWorker.onmessage = function(e) {
if (e.data === "ready") {
prepare(myWorker);
} else {
runner.disabled = false;
outerOutput.style.display = 'block';
output.textContent = e.data[0];
midOutput.style.display = e.data[0] ? 'block' : 'none';
error.textContent = e.data[1];
midError.style.display = e.data[1] ? 'block' : 'none';
result.textContent = '';
renderInto(e.data[2], result);
midResult.style.display = (e.data[2] === null || e.data[2] === undefined) ? 'none' : 'block';
}
};
});
</script>
</body>
</html>