forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
329 lines (291 loc) · 9.84 KB
/
index.js
File metadata and controls
329 lines (291 loc) · 9.84 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
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
import './style.css';
// Code Mirror
// https://github.com/codemirror/codemirror
import CodeMirror from 'codemirror';
import 'codemirror/mode/python/python';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/css/css';
import 'codemirror/mode/markdown/markdown';
import 'codemirror/mode/stex/stex';
import 'codemirror/addon/comment/comment';
import 'codemirror/lib/codemirror.css';
import 'codemirror/theme/ayu-mirage.css';
import { selectBuffer, openBuffer, newBuf } from './editor';
import { genericFetch } from './tools';
// parsing: copied from the iodide project
// https://github.com/iodide-project/iodide/blob/master/src/editor/iomd-tools/iomd-parser.js
import { iomdParser } from './parse';
// processing: execute/render editor's content
import {
runPython,
runJS,
addCSS,
checkCssStatus,
renderMarkdown,
renderMath,
handlePythonError,
} from './process';
let rp;
// A dependency graph that contains any wasm must be imported asynchronously.
import('rustpython')
.then((rustpython) => {
rp = rustpython;
// so people can play around with it
window.rp = rustpython;
onReady();
})
.catch((e) => {
console.error('Error importing `rustpython`:', e);
document.getElementById('error').textContent = e;
});
const error = document.getElementById('error');
const notebook = document.getElementById('rp-notebook');
// Code Editors
// There is a primary and secondary code editor
// By default only the primary is visible.
// On click of split view, secondary editor is visible
// Each editor can display multiple documents and doc types.
// the created ones are main/python/js/css
// user has the option to add their own documents.
// all new documents are python docs
// adapted/inspired from https://codemirror.net/demo/buffers.html
const primaryEditor = CodeMirror(document.getElementById('primary-editor'), {
theme: 'ayu-mirage',
lineNumbers: true,
lineWrapping: true,
});
const secondaryEditor = CodeMirror(
document.getElementById('secondary-editor'),
{
lineNumbers: true,
lineWrapping: true,
},
);
const buffers = {};
// list of buffers (displayed on UI as inline list item next to run)
const buffersList = document.getElementById('buffers-list');
// dropdown of buffers (visible on click of split view)
const buffersDropDown = document.getElementById('buffers-selection');
// By default open 3 buffers, main, tab1 and css
// TODO: add a JS option
// Params for OpenBuffer (buffers object, name of buffer to create, default content, type, link in UI 1, link in UI 2)
openBuffer(
buffers,
'main',
'# python code or code blocks that start with %%py, %%md %%math.',
'notebook',
buffersDropDown,
buffersList,
);
openBuffer(
buffers,
'python',
'# Python code',
'python',
buffersDropDown,
buffersList,
);
openBuffer(
buffers,
'js',
'// Javascript code goes here',
'javascript',
buffersDropDown,
buffersList,
);
openBuffer(
buffers,
'css',
'/* CSS goes here */',
'css',
buffersDropDown,
buffersList,
);
// select main buffer by default and set the main tab to active
selectBuffer(primaryEditor, buffers, 'main');
selectBuffer(secondaryEditor, buffers, 'main');
document
.querySelector('ul#buffers-list li:first-child')
.classList.add('active');
function onReady() {
/* By default the notebook has the keyword "loading"
once python and doc is ready:
create an empty div and set the id to 'rp_loaded'
so that the test knows that we're ready */
const readyElement = document.createElement('div');
readyElement.id = 'rp_loaded';
document.head.appendChild(readyElement);
// set the notebook to empty
notebook.innerHTML = '';
}
document.getElementById('run-btn').addEventListener('click', executeNotebook);
let pyvm = null;
// on click of run
// 1. add css stylesheet
// 2. get and run content of all tabs (including dynamically added ones)
// 3. run main tab.
async function executeNotebook() {
// Clean the console and errors
notebook.innerHTML = '';
error.textContent = '';
// get the content of the css editor
// and add the css to the head
// use dataset.status for a flag to know when to update
let cssCode = buffers['css'].getValue();
let cssStatus = checkCssStatus();
switch (cssStatus) {
case 'none':
addCSS(cssCode);
break;
case 'modified':
// remove the old style then add the new one
document.getElementsByTagName('style')[0].remove();
addCSS(cssCode);
break;
default:
// do nothing
}
if (pyvm) {
pyvm.destroy();
pyvm = null;
}
pyvm = rp.vmStore.init('notebook_vm');
// add some helpers for js/python code
window.injectPython = (ns) => {
for (const [k, v] of Object.entries(ns)) {
pyvm.addToScope(k, v);
}
};
window.pushNotebook = (elem) => {
notebook.appendChild(elem);
};
window.handlePyError = (err) => {
handlePythonError(error, err);
};
pyvm.setStdout((text) => {
const para = document.createElement('p');
para.appendChild(document.createTextNode(text));
notebook.appendChild(para);
});
for (const el of ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p']) {
pyvm.addToScope(el, (text) => {
const elem = document.createElement(el);
elem.appendChild(document.createTextNode(text));
notebook.appendChild(elem);
});
}
pyvm.addToScope('notebook_html', (html) => {
notebook.innerHTML += html;
});
let jsCode = buffers['js'].getValue();
await runJS(jsCode);
// get all the buffers, except css, js and main
// css is auto executed at the start
// main is parsed then executed at the end
// main can have md, math and python function calls
let { css, main, js, ...pythonBuffers } = buffers;
for (const [name] of Object.entries(pythonBuffers)) {
let pythonCode = buffers[name].getValue();
runPython(pyvm, pythonCode, error);
}
// now parse from the main editor
// gets code from main editor
let mainCode = buffers['main'].getValue();
/*
Split code into chunks.
Uses %%keyword or %% keyword as separator
Returned object has:
- chunkContent, chunkType, chunkId,
- evalFlags, startLine, endLine
*/
let parsedCode = iomdParser(mainCode);
for (const chunk of parsedCode) {
// For each type of chunk, do something
// so far have py for python, md for markdown and math for math ;p
let content = chunk.chunkContent;
switch (chunk.chunkType) {
// by default assume this is python code
// so users don't have to type py manually
case '':
case 'py':
runPython(pyvm, content, error);
break;
// TODO: fix how js is injected and ran
case 'js':
await runJS(content);
break;
case 'md':
notebook.innerHTML += renderMarkdown(content);
break;
case 'math':
notebook.innerHTML += renderMath(content);
break;
default:
// do nothing when we see an unknown chunk for now
}
}
}
function updatePopup(type, message) {
document.getElementById('popup').dataset.type = type;
document.getElementById('popup-header').textContent = message;
}
// import button
// show a url input + fetch button
// takes a url where there is raw code
document
.getElementById('popup-import')
.addEventListener('click', async function () {
let url = document.getElementById('popup-url').value;
let type = document.getElementById('popup').dataset.type;
let code = await genericFetch(url, type);
primaryEditor.setValue(code);
});
document.getElementById('import-code').addEventListener('click', function () {
updatePopup('python', 'URL (raw text format)');
});
// click on an item in the list
CodeMirror.on(buffersList, 'click', function (e) {
selectBuffer(primaryEditor, buffers, e.target.dataset.language);
});
// select an item in the dropdown
CodeMirror.on(buffersDropDown, 'change', function () {
selectBuffer(
secondaryEditor,
buffers,
buffersDropDown.options[buffersDropDown.selectedIndex].value,
);
});
// when css code editor changes
// update data attribute flag to modified
CodeMirror.on(buffers['css'], 'change', function () {
let style = document.getElementsByTagName('style')[0];
if (style) {
style.dataset.status = 'modified';
}
});
document
.getElementById('buffers-list')
.addEventListener('click', function (event) {
let elem = document.querySelector('.active');
if (elem) {
elem.classList.remove('active');
}
event.target.classList.add('active');
});
// new tab, new buffer
document.getElementById('new-tab').addEventListener('click', function () {
newBuf(buffers, buffersDropDown, buffersList, primaryEditor);
});
// TODO: those three addEventListener can be re-written into one thing probably
document.getElementById('split-view').addEventListener('click', function () {
document.getElementById('primary-editor').classList.remove('d-none');
document.getElementById('secondary-editor').classList.remove('d-none');
});
document.getElementById('reader-view').addEventListener('click', function () {
document.getElementById('primary-editor').classList.add('d-none');
document.getElementById('secondary-editor').classList.add('d-none');
});
document.getElementById('default-view').addEventListener('click', function () {
document.getElementById('primary-editor').classList.remove('d-none');
document.getElementById('secondary-editor').classList.add('d-none');
});