()
| 29 | |
| 30 | class CodeSandbox { |
| 31 | constructor() { |
| 32 | this.extensions = [ |
| 33 | keymap.of([ |
| 34 | {key: "Ctrl-Enter", run: () => { this.runCode(); return true }}, |
| 35 | {key: "Cmd-Enter", run: () => { this.runCode(); return true }} |
| 36 | ]), |
| 37 | modeGuesser |
| 38 | ] |
| 39 | this.editor = new EditorView({ |
| 40 | state: createState("", "javascript", [ |
| 41 | this.extensions, |
| 42 | contextFacet.of({include: [], mode: null}) |
| 43 | ]), |
| 44 | parent: document.querySelector("#editor") |
| 45 | }) |
| 46 | |
| 47 | this.output = new Sandbox.Output(document.querySelector(".sandbox-output")) |
| 48 | this.sandbox = null |
| 49 | |
| 50 | this.chapters = document.querySelector("#chapters") |
| 51 | chapterData.forEach(chapter => { |
| 52 | this.chapters.appendChild(opt(chapter.number, chapter.number + ". " + chapter.title)) |
| 53 | chapter.exercises.forEach(exercise => { |
| 54 | exercise.chapter = chapter |
| 55 | }) |
| 56 | }) |
| 57 | this.chapters.addEventListener("change", () => { |
| 58 | this.selectChapter(this.chapters.value) |
| 59 | document.location.hash = "#" + this.chapters.value |
| 60 | }) |
| 61 | |
| 62 | this.per = document.querySelector("#per_chapter") |
| 63 | this.per.addEventListener("change", () => { |
| 64 | this.selectContext(this.per.value) |
| 65 | document.location.hash = "#" + (this.per.value == "box" ? this.chapters.value : this.per.value) |
| 66 | }) |
| 67 | this.fileList = document.querySelector("#files") |
| 68 | this.fileInfo = document.querySelector("#fileInfo") |
| 69 | this.runLocally = document.querySelector("#runLocally") |
| 70 | this.localFileList = document.querySelector("#local-files") |
| 71 | |
| 72 | document.querySelector("#run").addEventListener("click", () => this.runCode()) |
| 73 | |
| 74 | document.querySelector("#solution").addEventListener("click", () => { |
| 75 | let context = this.editor.state.facet(contextFacet) |
| 76 | this.setEditorState(context.solution, context) |
| 77 | }) |
| 78 | |
| 79 | this.parseFragment() || this.selectChapter(0, "box") |
| 80 | addEventListener("hashchange", () => this.parseFragment()) |
| 81 | } |
| 82 | |
| 83 | setEditorState(code, context) { |
| 84 | this.editor.setState(createState(code, context.type || guessType(code), [this.extensions, contextFacet.of(context)])) |
nothing calls this directly
no test coverage detected