-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
71 lines (49 loc) · 1.63 KB
/
index.coffee
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
import { CodeJar } from "https://medv.io/codejar/codejar.js";
import { compile } from "https://esm.sh/@danielx/civet";
import Prism from "https://cdn.skypack.dev/prismjs";
defaultCode = """sum := (a:number, b:number) => a + b
square := (a:number) => a * a
fun := ->
console.log "It's all fun and games till it breaks"
song := ["do", "re", "mi", "fa", "so"]
singers := {Jagger: "Rock", Elvis: "Roll"}
bitlist := [
1, 0, 1
0, 0, 1
1, 1, 0
];
// hey jsx dudes... here
Component := () => <>{bitlist.map (x) => <p>x</p> }</>"""
editor = document.getElementById "editor"
preview = document.getElementById "preview"
errorBox = document.getElementById "error-box"
shareButton = document.getElementById "share-btn"
initialSourceCode = defaultCode
if window.location.hash
withoutHash = window.location.hash.trim().replace(/^#/, "");
initialSourceCode = atob(withoutHash);
highlight = (c) ->
out = Prism.highlight(c.innerText,Prism.languages.javascript,"javascript");
c.innerHTML = out
previewJar = CodeJar(preview, highlight,{
tab: " ".repeat(2),
});
editorJar = CodeJar(editor, ->, {
tab: " ".repeat(2),
});
onUpdate = (c) ->
try
browserCompile = compile(c);
previewJar.updateCode(browserCompile);
errorBox.innerText = "";
catch err
errorBox.innerText = err.message;
editorJar.onUpdate onUpdate
editorJar.updateCode(initialSourceCode);
previewJar.updateCode compile initialSourceCode
onShare = ->
code = editorJar.toString();
base64 = btoa(code);
window.location.hash = base64;
copyToClipboard(window.location.href);
shareButton.addEventListener("click",onShare)