-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.html
372 lines (343 loc) · 13.7 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
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
<!DOCTYPE html>
<html>
<head>
<title>Cherry</title>
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script src="https://unpkg.com/node-inspect-extracted/dist/inspect.js"></script>
<script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js" crossorigin="anonymous"></script>
<script type="importmap">
{
"imports": {
"cherry-cljs": "https://cdn.jsdelivr.net/npm/[email protected]/index.js",
"cherry-cljs/cljs.core.js": "https://cdn.jsdelivr.net/npm/[email protected]/cljs.core.js",
"cherry-cljs/lib/clojure.string.js": "https://cdn.jsdelivr.net/npm/[email protected]/lib/clojure.string.js",
"cherry-cljs/lib/clojure.set.js": "https://cdn.jsdelivr.net/npm/[email protected]/lib/clojure.set.js",
"cherry-cljs/lib/clojure.walk.js": "https://cdn.jsdelivr.net/npm/[email protected]/lib/clojure.walk.js",
"squint-cljs": "https://unpkg.com/[email protected]/index.js",
"squint-cljs/core.js": "https://unpkg.com/[email protected]/core.js",
"squint-cljs/string.js": "https://unpkg.com/[email protected]/string.js",
"squint-cljs/src/squint/string.js": "https://unpkg.com/[email protected]/src/squint/string.js",
"squint-cljs/src/squint/set.js": "https://unpkg.com/[email protected]/src/squint/set.js",
"squint-cljs/src/squint/html.js": "https://unpkg.com/[email protected]/src/squint/html.js",
"@codemirror/language": "https://ga.jspm.io/npm:@codemirror/[email protected]/dist/index.js",
"@codemirror/state": "https://ga.jspm.io/npm:@codemirror/[email protected]/dist/index.js",
"@codemirror/view": "https://ga.jspm.io/npm:@codemirror/[email protected]/dist/index.js",
"@codemirror/commands": "https://ga.jspm.io/npm:@codemirror/[email protected]/dist/index.js",
"@lezer/highlight": "https://ga.jspm.io/npm:@lezer/[email protected]/dist/index.js",
"@lezer/common": "https://ga.jspm.io/npm:@lezer/[email protected]/dist/index.js",
"@nextjournal/lezer-clojure": "https://ga.jspm.io/npm:@nextjournal/[email protected]/dist/index.es.js",
"@nextjournal/clojure-mode": "https://unpkg.com/@nextjournal/[email protected]/dist/nextjournal/clojure_mode.mjs",
"@nextjournal/clojure-mode/extensions/eval-region": "https://unpkg.com/@nextjournal/[email protected]/dist/nextjournal/clojure_mode/extensions/eval_region.mjs",
"react": "https://esm.sh/[email protected]",
"react/jsx-runtime": "https://esm.sh/[email protected]/jsx-runtime",
"react/jsx-dev-runtime": "https://esm.sh/[email protected]/jsx-dev-runtime",
"react-dom": "https://esm.sh/[email protected]",
"@lezer/lr": "https://ga.jspm.io/npm:@lezer/[email protected]/dist/index.js",
"@lezer/markdown": "https://ga.jspm.io/npm:@lezer/[email protected]/dist/index.js",
"style-mod": "https://ga.jspm.io/npm:[email protected]/src/style-mod.js",
"w3c-keyname": "https://ga.jspm.io/npm:[email protected]/index.js"}
}
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/light.css" />
<style>
:root {
--teal-color: #31afd0;
}
body {
max-width: inherit;
}
#main {
display: flex;
flex-direction: column;
max-height: 90dvh;
@media screen and (min-width: 768px) {
overflow: auto;
flex-direction: row;
}
}
#editor {
width: 100%;
max-height: 90dvh;
max-width: calc(100dvw - 20px);
border: 1px solid grey;
border-radius: 10px;
overflow: auto;
@media screen and (min-width: 768px) {
margin-bottom: 10px;
width: 50%;
}
}
#sidebar {
width: 100%;
max-height: 90dvh;
max-width: calc(100dvw - 40px);
border: 1px solid grey;
border-radius: 10px;
padding: 10px;
margin-top: 10px;
overflow: auto;
@media screen and (min-width: 768px) {
margin-top: 0px;
margin-left: 10px;
margin-bottom: 10px;
width: 50%;
}
}
</style>
<script type="module">
import { default_extensions, complete_keymap } from '@nextjournal/clojure-mode';
import { history, historyKeymap } from '@codemirror/commands';
import * as eval_region from '@nextjournal/clojure-mode/extensions/eval-region';
import { EditorView, drawSelection, keymap } from '@codemirror/view';
import { EditorState } from '@codemirror/state';
import { syntaxHighlighting, defaultHighlightStyle, foldGutter } from '@codemirror/language';
let theme = EditorView.theme({
".cm-content": {whitespace: "pre-wrap",
passing: "10px 0",
flex: "1 1 0"},
"&.cm-focused": {outline: "0 !important"},
".cm-line": {"padding": "0 9px",
"line-height": "1.6",
"font-size": "16px",
"font-family": "var(--code-font)"},
".cm-matchingBracket": {"border-bottom": "1px solid var(--teal-color)",
"color": "inherit"},
".cm-gutters": {background: "transparent",
border: "none"},
".cm-gutterElement": {"margin-left": "5px"},
// only show cursor when focused
".cm-cursor": {visibility: "hidden"},
"&.cm-focused .cm-cursor": {visibility: "visible"}
});
let squintExtension = ( opts ) => {
return keymap.of([{key: "Alt-Enter", run: evalCell},
{key: opts.modifier + "-Enter",
run: evalAtCursor,
shift: evalToplevel
}])}
let urlParams = new URLSearchParams(window.location.search);
var repl = urlParams.get('repl') !== 'false';
let compilerState;
let evalCode = async (code) => {
try {
compilerState = compileStringEx(`(do ${code}\n)`, {repl: repl, "elide-exports": repl, context: repl ? 'return' : 'statement'}, compilerState);
let js = compilerState.javascript;
if (dev) {
js = js.replace('cherry-cljs/cljs.core.js', 'http://' + window.location.host + '/cljs.core.js');
}
document.querySelector('#compiledCode').innerText = js;
if (!repl) {
const encodedJs = encodeURIComponent(js);
const dataUri = 'data:text/javascript;charset=utf-8;eval=' + Date.now() + ',' + encodedJs;
let result = await import(dataUri);
} else {
let result = await eval(`(async function() { ${js} })()`);
document.querySelector('#result').innerText = pr_str(result);
if (docChanged) {
let url = new URL(window.location.href);
url.searchParams.delete("src");
window.history.replaceState(null, null, url);
localStorage.setItem("document", editor.state.doc.toString());
docChanged = false;
}
}
}
catch (e) {
document.querySelector('#result').innerText = util.inspect(e);
console.error(e);
}
}
let evalCell = (opts) => {
let js = opts.state.doc.toString();
evalCode(js);
return true;
}
let evalAtCursor = (opts) => {
if (!repl) return;
let js = eval_region.cursor_node_string(opts.state).toString();
evalCode(js);
return true;
}
let docChanged = false;
let evalToplevel = (js) => {
if (!repl) return;
return true;
}
let updateExt = EditorView.updateListener.of((update) => {
if (update.docChanged) docChanged = true;
});
const platform = navigator.platform;
const isMac = platform.startsWith('Mac') || platform.startsWith('iP');
let modifier = localStorage.getItem("editor.modifier") || (isMac ? 'Meta' : 'Ctrl');
if (!isMac || modifier === 'Ctrl') document.getElementById('modifierDesc').innerText = 'Ctrl'
let extensions = [
history(),
theme,
foldGutter(),
syntaxHighlighting(defaultHighlightStyle),
drawSelection(),
keymap.of(complete_keymap),
keymap.of(historyKeymap),
squintExtension({modifier: modifier}),
eval_region.extension({modifier: modifier}),
updateExt,
...default_extensions
];
let doc = `
(comment
(fizz-buzz 1)
(fizz-buzz 3)
(fizz-buzz 5)
(fizz-buzz 15)
(fizz-buzz 17)
(fizz-buzz 42))
(defn fizz-buzz [n]
(condp (fn [a b] (zero? (mod b a))) n
15 "fizzbuzz"
3 "fizz"
5 "buzz"
n))
(require '["https://esm.sh/[email protected]$default" :as confetti])
(do
(js-await (confetti))
(+ 1 2 3))
`.trim();
const aocDoc = `
;; Helper functions:
;; (fetch-input year day) - get AOC input
;; (append str) - append str to DOM
;; (spy x) - log x to console and return x
;; Remember to update the year and day in the fetch-input call.
(def input (->> (js-await (fetch-input 2022 1))
#_spy
str/split-lines
(mapv parse-long)))
(defn part-1
[]
(->> input
(partition-by nil?)
(take-nth 2)
(map #(apply + %))
(apply max)))
(defn part-2
[]
(->> input
(partition-by nil?)
(take-nth 2)
(map #(apply + %))
(sort-by -)
(take 3)
(apply +)))
(time (part-1))
#_(time (part-2))`.trim();
const aocBoilerplateUrl = 'https://gist.githubusercontent.com/borkdude/cf94b492d948f7f418aa81ba54f428ff/raw/a6e9992b079e20e21d753e8c75a7353c5908b225/aoc_ui.cljs';
let boilerplate = urlParams.get('boilerplate');
let boilerplateSrc;
if (boilerplate) {
boilerplateSrc = await fetch(boilerplate).then(p => p.text());
}
let src = urlParams.get('src');
if (src) {
if (/http(s)?:\/\/.*/.test(src)) {
src = await fetch(src).then(p => p.text());
}
else {
src = atob(src)
};
doc = src;
} else {
doc = localStorage.getItem("document") || doc;
}
let state = EditorState.create({doc: doc,
extensions: extensions });
let editorElt = document.querySelector('#editor');
let editor = new EditorView({state: state,
parent: editorElt,
extensions: extensions });
globalThis.editor = editor;
var dev = JSON.parse(urlParams.get('dev')) ?? location.hostname === 'localhost';
var squintUrl;
if (dev) {
console.log('Loading development cherry core.cljs.js')
squintUrl = '/index.js';
} else {
squintUrl = 'cherry-cljs';
}
var squint = await(import(squintUrl));
var compileStringEx = squint.compileStringEx;
let cherry_core_url = (dev ? 'http://' + window.location.host + '/cljs.core.js' : 'cherry-cljs/cljs.core.js');
let cherry_core = await(import(cherry_core_url));
let pr_str = cherry_core.pr_str;
window.compile = () => {
let code = editor.state.doc.toString();
code = '' + boilerplateSrc + '\n\n' + code;
evalCode(code);
}
window.share = () => {
let code = editor.state.doc.toString().trim();
code = btoa(code);
let url = new URL(window.location);
url.searchParams.set('src', code);
window.location = url;
}
window.blankAOC = () => {
const code = btoa(aocDoc);
const url = new URL(window.location);
url.searchParams.set('src', code);
url.searchParams.set('boilerplate', aocBoilerplateUrl);
url.searchParams.set('repl', true);
window.location = url;
}
window.changeREPL = (target) => {
document.getElementById('result').innerText = '';
if (target.checked) {
repl = true;
compile();
} else {
repl = false;
compile();
}
let url = new URL(window.location.href);
url.searchParams.set("repl", repl);
window.history.replaceState(null, null, url);
}
if (repl) {
document.getElementById('replCheckBox').checked = true;
}
compile();
</script>
</head>
<body>
<div id="main">
<div style="position: absolute; right: 0px; top: 0px; margin: 2px">
<a href="https://gitHub.com/squint-cljs/cherry"><img src="https://img.shields.io/github/stars/squint-cljs/cherry.svg?style=social&label=Star"></a></div>
<div id="editor">
</div>
<div id="sidebar">
<button onClick="compile()">
Compile!
</button>
<button onClick="share()" title="When sharing, the window URL will be updated to encode the code in the text editor. Copy it and share it with your friends!">
Share!
</button>
<button style="margin-bottom: 10px;" onClick="blankAOC()">
Create blank AOC Playground!
</button>
<br>
<label>REPL-mode
<input type="checkbox" id="replCheckBox" name="repl" onChange="changeREPL(this)" />
</label>
<br>
In REPL-mode you can evaluate individual expression with <span id="modifierDesc">⌘</span> + Enter. The result will appear below.
To evaluate the whole program at once, use Alt + Enter;
<pre><code style="white-space: pre-wrap;max-width:80%;" id="result"></code>
</pre>
Compiled code:
<pre><code style="white-space: pre-wrap;max-width:80%;" id="compiledCode"></code>
</pre>
</div>
</div>
</body>
</html>