-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeload.js
More file actions
32 lines (27 loc) · 715 Bytes
/
Copy pathcodeload.js
File metadata and controls
32 lines (27 loc) · 715 Bytes
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
"use strict";
document.getElementById('runCode').addEventListener('click', runCode);
document.getElementById('chooseCode').addEventListener('change', e => {
const select = e.target;
const opt = select.options[select.selectedIndex];
retrieveCode(`lesson_code/${opt.value}/main.js`);
});
const myCodeMirror = CodeMirror(document.body, {
mode: "javascript"
});
function runCode(e){
try{
let newInit = eval(myCodeMirror.getValue());
newInit(document.getElementById('renderPort'));
}
catch(err){
alert('Error running code ' + err);
}
}
function retrieveCode(path){
fetch(path)
.then(r => r.text())
.then(mainjs => {
myCodeMirror.setValue(mainjs);
runCode(mainjs);
});
}