-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmerc-main.js
338 lines (316 loc) · 8.9 KB
/
merc-main.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
// Why must I run this? What does it do?
Asciidoctor$$module$build$asciidoctor_browser();
let cursor;
let runCount = 0;
function addcellmenu(s, f) {
const span = document.createElement("span");
span.classList.add("cellbutton");
span.innerHTML = s;
cellmenubuttons.appendChild(span);
span.addEventListener("click", ev => { f(); ev.stopPropagation(); });
}
addcellmenu("?", ev => {
popuptypemenu.style.display = "block";
document.addEventListener("click", ev => {
popuptypemenu.style.display = "none";
ev.preventDefault();
}, {once:true});
});
const cellmenutype = cellmenubuttons.lastChild;
cellmenutype.style["text-align"] = "left";
cellmenutype.style.width = "6em";
const tyCode = "Code";
const tyRaw = "Raw"
const tyQuack = "AsciiDoc";
function setType(x, t) {
if (t == x.getAttribute("data-type")) return;
x.setAttribute("data-type", t);
if (t == tyRaw || t == tyQuack) {
x.getElementsByClassName("inlabel")[0].innerHTML = "";
const out = x.getElementsByClassName("output")[0];
if (out) out.remove();
} else if (t == tyCode) {
x.getElementsByClassName("inlabel")[0].innerHTML = `[<span class="runcounter"> </span>]:`;
}
}
function addType(t) {
const div = document.createElement("div");
div.classList.add("typemenuitem");
div.innerHTML = t;
div.addEventListener("click", ev => setType(cursor, t));
popuptypemenu.appendChild(div);
}
addType(tyCode);
addType(tyRaw);
addType(tyQuack);
addcellmenu("▶", ev => {
runOnly();
});
addcellmenu("⧉", ev => {
cellmenuclipboard.appendChild(cellmenu);
const n = newCell();
n.innerHTML = cursor.innerHTML;
cursor.after(n);
select(n);
document.activeElement.blur();
});
addcellmenu("⬆", ev => {
const prev = cursor.previousSibling;
if (prev) prev.before(cursor);
});
addcellmenu("⬇", ev => {
const next = cursor.nextSibling;
if (next) next.after(cursor);
});
addcellmenu("👆", ev => insertCell());
addcellmenu("👇", ev => appendCell());
// 🗑 is long.
addcellmenu("✖", ev => {
cellmenuclipboard.appendChild(cellmenu);
const next = cursor.nextSibling;
if (next) {
cursor.remove();
select(next);
} else {
const prev = cursor.previousSibling;
cursor.remove();
if (prev) {
select(prev);
} else {
select(newCell());
convo.appendChild(cursor);
}
}
});
function insertCell() {
const n = newCell();
cursor.before(n);
select(n);
}
function appendCell() {
const n = newCell();
cursor.after(n);
select(n);
}
function select(cell) {
if (cursor) cursor.classList.remove("selectedcell");
cursor = cell;
cursor.classList.add("selectedcell");
cellmenutype.innerHTML = cursor.getAttribute("data-type");
const tri = document.createElement("div");
tri.innerHTML = "▾";
tri.style.float = "right";
cellmenutype.appendChild(tri);
cursor.prepend(cellmenu);
}
function newCell() {
const div = document.createElement("div");
div.classList.add("cell");
div.setAttribute("data-type", tyCode);
div.innerHTML =
`<div style="display:flex;">
<span class="inlabel">[<span class="runcounter"> </span>]:</span>
<pre class="incode" spellcheck=false contenteditable></pre>
</div>`
div.addEventListener('click', ev => {select(div);});
const incode = div.getElementsByClassName("incode")[0];
incode.addEventListener('copy', function(e){
e.clipboardData.setData('text/plain', window.getSelection().toString());
e.preventDefault();
});
return div;
}
let repl;
function runOnly() {
if (!cursor) return;
const ty = cursor.getAttribute("data-type");
if (ty == tyRaw) return;
const incode = cursor.getElementsByClassName("incode")[0];
const s = incode.innerText;
incode.textContent = s;
const out = cursor.getElementsByClassName("output");
if (out.length != 0) out[0].remove();
if (s == "") return;
const div = document.createElement("div");
repl.outdiv = div;
div.classList.add("output");
cursor.appendChild(div);
if (ty == tyQuack) {
div.innerHTML = `<span class="outlabel"></span>`;
const adoc = document.createElement("div");
adoc.classList.add("adoc");
adoc.innerHTML = Opal.Asciidoctor.$convert(s, Opal.hash2(['attributes'], {'attributes': ['showtitle']}));
MathJax.typeset([adoc]);
div.appendChild(adoc);
incode.style.display = "none";
return;
}
runCount++;
cursor.getElementsByClassName("runcounter")[0].innerText = runCount;
const r = repl.run("chat", ["Main"], s + "\n");
document.activeElement.blur();
if (r.buf[0] == "error") {
div.classList.add("errmsg");
div.textContent = r.out;
return;
}
if (r.out != "") {
div.insertAdjacentHTML('afterbegin', `<div style="display:flex;"><span class="outlabel">[` + runCount + `]:</span><pre class="outtext"></pre></div>`);
div.getElementsByClassName("outtext")[0].innerText = r.out;
}
}
function runThenSelect() {
runOnly();
const next = cursor.nextSibling;
if (next) select(next); else appendCell();
}
function saveConvo() {
const p = cellmenu.parentElement;
cellmenuclipboard.appendChild(cellmenu);
const s = convo.innerHTML;
p.prepend(cellmenu);
return s;
}
function loadConvo(s) {
cellmenuclipboard.appendChild(cellmenu);
convo.innerHTML = s.trim();
const cells = convo.getElementsByClassName("cell");
for (const c of cells) {
c.addEventListener('click', function(ev){select(c);});
if (c.classList.contains("selectedcell")) cursor = c;
}
MathJax.typeset();
}
function addTopButton(s, f) {
const b = document.createElement("span");
b.classList.add("topbutton");
b.innerHTML = s;
b.addEventListener('click', f);
topmenu.appendChild(b);
return b;
}
function mercInitKeys() {
document.addEventListener('keydown', ev => {
if (!cursor) return;
switch(ev.keyCode) {
case 13:
if (ev.ctrlKey) {
runOnly();
ev.preventDefault();
} else if (ev.shiftKey) {
runThenSelect();
ev.preventDefault();
} else if (ev.altKey) {
runOnly();
appendCell();
ev.preventDefault();
} else {
if (document.activeElement === document.body) {
const incode = cursor.getElementsByClassName("incode")[0];
if (cursor.getAttribute("data-type") == tyQuack) {
cursor.getElementsByClassName("output")[0].remove();
incode.style.display = "inline-block";
}
incode.focus();
ev.preventDefault();
}
}
break;
case 27:
if (document.activeElement !== document.body) {
document.activeElement.blur();
ev.preventDefault();
}
break;
case 38:
if (document.activeElement === document.body) {
const prev = cursor.previousSibling;
if (prev) {
if (ev.ctrlKey && ev.shiftKey) {
prev.before(cursor);
} else {
select(prev);
}
cursor.scrollIntoView({behavior:"smooth",block:"nearest"});
}
ev.preventDefault();
}
break;
case 40:
if (document.activeElement === document.body) {
const next = cursor.nextSibling;
if (next) {
if (ev.ctrlKey && ev.shiftKey) {
next.after(cursor);
} else {
select(next);
}
cursor.scrollIntoView({behavior:"smooth",block:"nearest"});
}
ev.preventDefault();
}
break;
case 65:
if (document.activeElement === document.body) {
insertCell();
ev.preventDefault();
}
break;
case 66:
if (document.activeElement === document.body) {
appendCell();
ev.preventDefault();
}
break;
}
});
}
async function mercInitLite() {
repl = await mkRepl();
mercInitKeys();
}
async function mercInit(loadFun, saveFun) {
repl = await mkRepl();
addTopButton("💾", saveFun);
addTopButton("📂", loadFun);
addTopButton("↺", async function(ev){
await repl.reset();
runCount = 0;
});
addTopButton("⏩", ev => {
const bak = cursor;
const cells = convo.getElementsByClassName("cell");
for (const c of cells) {
cursor = c;
runOnly();
}
cursor = bak;
});
addTopButton("⭱", ev => {
importdialog.showModal();
ev.stopPropagation();
document.addEventListener("click", ev => {
if (ev.target == importdialog) {
importdialog.close();
}
});
});
importOKButton.addEventListener('click', async function(ev) {
const [file] = importfile.files;
if (file) loadConvo(await file.text());
importdialog.close();
});
addTopButton("⭳", ev => {
const file = new Blob([saveConvo()], {type: "application/octet-stream"});
const a = document.createElement("a"), url = URL.createObjectURL(file);
a.href = url;
a.download = "export.merc";
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
});
mercInitKeys();
}