-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
280 lines (242 loc) · 7.22 KB
/
Copy pathindex.html
File metadata and controls
280 lines (242 loc) · 7.22 KB
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
<html>
<head>
<title>AlephZero Playground</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bulma/0.7.5/css/bulma.min.css" />
<script src="//cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<script defer id="ace" src="//cdnjs.cloudflare.com/ajax/libs/ace/1.4.5/ace.js"></script>
<script src="//unpkg.com/splitpanes@2.4.1/dist/splitpanes.umd.js"></script>
<link href="//unpkg.com/splitpanes@2.4.1/dist/splitpanes.css" rel="stylesheet">
</head>
<body>
<div id="app">
<nav class="navbar is-dark">
<div class="navbar-brand">
<a class="navbar-item" href="https://github.com/alephzero">
<img src="https://avatars1.githubusercontent.com/u/48891271" height="28" style="filter: invert(1)">
</a>
</div>
<div class="navbar-menu">
<div class="navbar-start">
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
Language ({{lang_display[selected_lang]}})
</a>
<div class="navbar-dropdown">
<a class="navbar-item" v-for="lang in lang_opts" @click="select_lang(lang)">
<div
:class="{'has-text-primary': lang==selected_lang, 'has-text-weight-bold': lang==selected_lang}">
{{lang_display[lang]}}
</div>
</a>
</div>
</div>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
Example
</a>
<div class="navbar-dropdown">
<div class="navbar-item"><b>{{lang_display["cpp"]}}</b></div>
<a class="navbar-item" v-for="ex in examples_cpp" @click="select_example(ex)">
{{ex.name}}
</a>
<hr class="navbar-divider">
<div class="navbar-item"><b>{{lang_display["py"]}}</b></div>
<a class="navbar-item" v-for="ex in examples_py" @click="select_example(ex)">
{{ex.name}}
</a>
</div>
</div>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
Saved Script
</a>
<div class="navbar-dropdown">
<a class="navbar-item field" v-for="snippet in snippets" @click="load_snippet(snippet)"
style="padding-right: 1rem">
<span style="width: 100%">
{{snippet}}
</span>
<span class="icon">
<i class="fas fa-lg fa-times-circle has-text-danger"
@click.stop="delete_snippet(snippet)"></i>
</span>
</a>
</div>
</div>
</div>
</div>
<div class="navbar-end">
<div class="tag is-dark">Ctrl + ENTER to Run | Ctrl + S to Save</div>
</div>
</nav>
<splitpanes>
<pane style="margin: 1px">
<pre id="code" style="height: 100%; font-size: 1.25em;"></pre>
</pane>
<pane style="margin: 1px; overflow: auto;">
<div class="has-background-dark" style="height: 100%;">
<pre class="has-text-primary has-background-dark" style="font-size: 1em"
v-if="output_stdout">{{output_stdout}}</pre>
<pre class="has-text-danger has-background-dark" style="font-size: 1em"
v-if="output_stderr">{{output_stderr}}</pre>
</div>
</pane>
</splitpanes>
</div>
<script>
const vueobj = new Vue({
el: '#app',
components: window.splitpanes,
data: {
lang_opts: ['cpp', 'py'],
lang_display: { cpp: 'C++', py: 'Python' },
selected_lang: undefined,
examples: [],
editor: undefined,
output_stdout: '',
output_stderr: '',
ws: undefined,
snippets: [],
},
mounted() {
const vm = this
const ace_script = document.querySelector('#ace')
ace_script.onload = vm.setup_code_panel
vm.load_examples()
vm.detect_snippets()
},
computed: {
examples_cpp() {
const vm = this
return vm.examples.filter(ex => vm.example_lang(ex) == 'cpp')
},
examples_py() {
const vm = this
return vm.examples.filter(ex => vm.example_lang(ex) == 'py')
},
},
methods: {
setup_code_panel() {
const vm = this
vm.editor = ace.edit('code')
vm.editor.setTheme('ace/theme/merbivore_soft')
vm.editor.setKeyboardHandler("ace/keyboard/sublime")
vm.editor.focus()
vm.editor.setValue(`print('Hello, World!')\n`, 1)
vm.select_lang('py')
vm.editor.commands.addCommand({
name: 'run_code',
bindKey: { win: 'Ctrl-Enter', mac: 'Command-Enter' },
exec: vm.run_code,
})
vm.editor.commands.addCommand({
name: 'save_snippet',
bindKey: { win: 'Ctrl-S', mac: 'Command-S' },
exec: vm.save_snippet,
})
},
load_examples() {
const vm = this
fetch('/examples.json')
.then(r => r.json())
.then(data => { vm.examples = data })
},
detect_snippets() {
const vm = this
vm.snippets = Object.keys(localStorage)
.filter(name => { return name.startsWith('alephzero/snippet/') })
.map(name => { return name.substr('alephzero/snippet/'.length) })
.sort()
},
save_snippet() {
const vm = this
// Get snippet name.
let snippet_name = prompt('Enter snippet name:')
// Check if the user canceled.
if (snippet_name == null) {
return
}
// Check if the user submitted an empty string.
snippet_name = snippet_name.trim()
if (snippet_name.length == 0) {
alert("Script name can't be empty");
return
}
// TODO(lshamis): Verify there is no conflict with an existing snippet.
localStorage.setItem(`alephzero/snippet/${snippet_name}`, JSON.stringify({
lang: vm.selected_lang,
code: vm.editor.getValue()
}))
vm.detect_snippets()
},
load_snippet(snippet_name) {
const vm = this
if (vm.ws) {
vm.ws.close()
}
vm.output_stdout = ''
vm.output_stderr = ''
const snippet = JSON.parse(localStorage.getItem(`alephzero/snippet/${snippet_name}`))
vm.select_lang(snippet.lang)
vm.editor.setValue(snippet.code, 1)
},
delete_snippet(snippet_name) {
const vm = this
localStorage.removeItem(`alephzero/snippet/${snippet_name}`)
vm.detect_snippets()
},
run_code() {
const vm = this
if (vm.ws) {
vm.ws.close()
}
vm.output_stdout = ''
vm.output_stderr = ''
vm.ws = new WebSocket(`ws://${location.host}/api/run`)
vm.ws.onopen = () => {
vm.ws.send(JSON.stringify({
lang: vm.selected_lang,
code: vm.editor.getValue()
}))
}
vm.ws.onmessage = (evt) => {
msg = JSON.parse(evt.data)
if (msg.stream == 'stdout') {
vm.output_stdout += msg.output
}
if (msg.stream == 'stderr') {
vm.output_stderr += msg.output
}
}
},
example_lang(ex) {
return ex.path.split('.').pop()
},
select_lang(lang) {
const vm = this
vm.selected_lang = lang
if (vm.editor) {
vm.editor.session.setMode({
cpp: 'ace/mode/c_cpp',
py: 'ace/mode/python',
}[lang])
}
},
select_example(ex) {
const vm = this
if (vm.ws) {
vm.ws.close()
}
vm.output_stdout = ''
vm.output_stderr = ''
vm.select_lang(vm.example_lang(ex))
fetch(ex.path)
.then(r => { return r.text() })
.then(code => { vm.editor.setValue(code, 1) })
}
}
})
</script>
</body>
</html>