-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
137 lines (116 loc) · 3.54 KB
/
app.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
// Helpers
function qs(selector, element = document) {
return element.querySelector(selector)
}
function qsa(selector, element = document) {
return element.querySelectorAll(selector)
}
function addClass(element, classString) {
element.classList.add(...classString.split(" "))
}
function removeClass(element, classString) {
element.classList.remove(...classString.split(" "))
}
function replaceClass(element, fromClassString, toClassString) {
addClass(element, toClassString)
removeClass(element, fromClassString)
}
// Setup codemirror
const codeMirror = CodeMirror.fromTextArea(
qs("#editor"),
{
mode: 'javascript',
lineNumbers: true,
tabSize: 2,
styleActiveLine: true,
theme: 'solarized dark',
autoCloseBrackets: true,
lint: true,
gutters: ["CodeMirror-lint-markers"],
}
)
const $gallery = qs("#gallery")
const $playground = qs("#playground")
const $editor = qs(".CodeMirror")
const $preview = qs("#preview")
const $controls = qs("#controls")
const $runner = qs("#runner")
const $zenMode = qs("#zenmode")
const $examples = Array.from(qsa(".example-image"))
let currentExample = null
let isZenMode = false
addClass($editor, "code f5 fl w-100 vh-50")
const editorTemplate =`
// Edit me!
// Press ▶ or (⌘ + Enter) to run.
function setup() {
createCanvas(windowWidth, windowHeight);
background("lightgray");
}
function draw() {
}`
const previewTemplate = code => `
<meta charset="utf-8">
<style>body { margin: 0; padding: 0}</style>
<script src="./libraries/p5.min.js"></script>
<script src="./libraries/p5.dom.min.js"></script>
<script src="./libraries/p5.sound.min.js"></script>
<script>
${code}
</script>
`
function compile() {
const source = previewTemplate(codeMirror.getValue())
$preview.setAttribute('srcdoc', source)
codeMirror.focus()
}
function main() {
// Set the editor text to the default template and compile.
codeMirror.setValue(editorTemplate)
compile()
// Run by either clicking the run button ...
$runner.addEventListener("click", compile)
// ... or pressing ⌘ + Enter in the editor.
codeMirror.addKeyMap({
"Cmd-Enter": compile, // I'm a mac!
"Ctrl-Enter": compile, // I'm a pc!
})
// On clicking any of the example images ...
$examples.forEach(function(example) {
example.addEventListener("click", function(event) {
// ... toggle the "ba" (border) class
if (currentExample) {
removeClass(currentExample, "ba b--solarized-base00")
}
currentExample = example
addClass(currentExample, "ba b--solarized-base00")
// ... get the source code of the example and compile it.
const path = `./examples/${example.dataset.src}.js`
fetch(path)
.then(data => data.text())
.then(code => codeMirror.setValue(code))
.then(compile)
})
})
// On clicking the zen mode button ...
$zenMode.addEventListener("click", function() {
if (!isZenMode) {
addClass($gallery, "dn")
replaceClass($playground, "w-50", "w-100")
replaceClass($editor, "f5 w-100 vh-50", "f4 w-50 vh-100")
replaceClass($preview, "w-100 vh-50", "w-50 vh-100")
$controls.style.right = "50%";
$zenMode.innerHTML = "← Gallery"
} else {
removeClass($gallery, "dn")
replaceClass($playground, "w-100", "w-50")
replaceClass($editor, "f4 w-50 vh-100", "f5 w-100 vh-50")
replaceClass($preview, "w-50 vh-100", "w-100 vh-50")
$controls.style.right = "0%";
$zenMode.innerHTML = "Zen mode"
}
isZenMode = !isZenMode
compile()
})
}
main()