|
1 |
| -var editor = ace.edit("editor"); |
2 |
| -editor.setTheme("ace/theme/monokai"); |
3 |
| -editor.session.setMode("ace/mode/python"); |
| 1 | +class CodeEditor extends HTMLDivElement { |
| 2 | + constructor() { |
| 3 | + super(); |
| 4 | + |
| 5 | + let id = this.getAttribute('id'); |
| 6 | + |
| 7 | + this.setAttribute("class", "editor-container p-3 pt-0") |
| 8 | + |
| 9 | + this.innerHTML = ` |
| 10 | + <button id="run-button-${id}" class="btn btn-primary my-3">Run</button> |
| 11 | + <button id="terminate-button-${id}" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Clicking this will restart the Python interpreter, so be careful!" class="btn btn-danger my-3 ms-2">Terminate Pyodide</button> |
| 12 | + <div class="editor" id="ace-editor-${id}">${this.innerHTML}</div> |
| 13 | + <div class="mt-3 input-output-container"> |
| 14 | + <div data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-original-title="Input" class="d-block position-relative input-container"> |
| 15 | + <textarea id="input-${id}" class="input"></textarea> |
| 16 | + </div> |
| 17 | + <div id="output-${id}" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-original-title="Output" class="output-container"> |
| 18 | + |
| 19 | + </div> |
| 20 | + </div> |
| 21 | + ` |
| 22 | + |
| 23 | + const editor = ace.edit(`ace-editor-${id}`); |
| 24 | + editor.setTheme("ace/theme/monokai"); |
| 25 | + editor.session.setMode("ace/mode/python"); |
| 26 | + |
| 27 | + |
| 28 | + document.getElementById(`run-button-${id}`).onclick = async () => { |
| 29 | + await runPython(editor.getValue(), `input-${id}`, `output-${id}`); |
| 30 | + }; |
| 31 | + |
| 32 | + document.getElementById(`terminate-button-${id}`).onclick = async () => { |
| 33 | + await terminatePyodide(); |
| 34 | + }; |
| 35 | + } |
| 36 | +} |
4 | 37 |
|
5 | 38 |
|
6 | 39 |
|
7 |
| -function outf(text) { |
8 |
| - const output_element = document.getElementById("output"); |
9 |
| - output_element.innerText = output_element.innerText + text; |
10 |
| -} |
| 40 | +customElements.define('code-editor', CodeEditor, { extends: "div" }); |
11 | 41 |
|
| 42 | +const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')) |
| 43 | +const tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) { |
| 44 | + return new bootstrap.Tooltip(tooltipTriggerEl) |
| 45 | +}) |
12 | 46 |
|
13 |
| -var current_inp = 0; |
14 | 47 |
|
15 |
| -function get_input(prompt) { |
16 |
| - return new Promise((resolve, reject) => { |
17 |
| - let input = document.getElementById('input-tmp').value.trim().split(/\r?\n/); |
18 |
| - if (input[0] == '') { |
19 |
| - input = []; |
20 |
| - } |
21 |
| - if (current_inp >= input.length) { |
22 |
| - throw "Place your input(s) in the \"input\" box!"; |
| 48 | + |
| 49 | +import { asyncRun, asyncTerminate } from "/assets/js/py-worker.js"; |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +async function runPython(script, input_id, output_id) { |
| 56 | + let stdin = document.getElementById(input_id).value; |
| 57 | + try { |
| 58 | + const { results, error } = await asyncRun(script, stdin); |
| 59 | + if (results !== undefined) { |
| 60 | + |
| 61 | + document.getElementById(output_id).innerText = results; |
| 62 | + } else if (error) { |
| 63 | + document.getElementById(output_id).innerText = error; |
23 | 64 | }
|
24 |
| - resolve(input[current_inp]); |
25 |
| - current_inp++; |
26 |
| - }) |
| 65 | + } catch (e) { |
| 66 | + document.getElementById(output_id).innerText = e.message |
| 67 | + } |
27 | 68 | }
|
28 | 69 |
|
29 |
| -function runit() { |
30 |
| - var prog = editor.getValue(); |
31 |
| - current_inp = 0; |
32 |
| - var output_element = document.getElementById("output"); |
33 |
| - output_element.innerText = ''; |
34 |
| - Sk.pre = "output"; |
35 |
| - Sk.configure({ output: outf, inputfun: get_input, inputfunTakesPrompt: true, execLimit: 1000 }); |
36 |
| - var myPromise = Sk.misceval.asyncToPromise(function () { |
37 |
| - return Sk.importMainWithBody("<stdin>", false, prog, true); |
38 |
| - }); |
39 |
| - myPromise.then(function (mod) { |
40 |
| - |
41 |
| - }, |
42 |
| - function (err) { |
43 |
| - output_element.innerText = err.toString(); |
44 |
| - }); |
| 70 | +async function terminatePyodide() { |
| 71 | + asyncTerminate(); |
45 | 72 | }
|
46 | 73 |
|
47 | 74 |
|
| 75 | + |
| 76 | +// Uncomment the below lines to use Skulpt |
| 77 | + |
| 78 | +// function outf(text) { |
| 79 | +// const output_element = document.getElementById("output"); |
| 80 | +// output_element.innerText = output_element.innerText + text; |
| 81 | +// } |
| 82 | + |
| 83 | + |
| 84 | +// var current_inp = 0; |
| 85 | + |
| 86 | +// function get_input(prompt) { |
| 87 | +// return new Promise((resolve, reject) => { |
| 88 | +// let input = document.getElementById('input-tmp').value.trim().split(/\r?\n/); |
| 89 | +// if (input[0] == '') { |
| 90 | +// input = []; |
| 91 | +// } |
| 92 | +// if (current_inp >= input.length) { |
| 93 | +// throw "Place your input(s) in the \"input\" box!"; |
| 94 | +// } |
| 95 | +// resolve(input[current_inp]); |
| 96 | +// current_inp++; |
| 97 | +// }) |
| 98 | +// } |
| 99 | + |
| 100 | +// function runit() { |
| 101 | +// var prog = editor.getValue(); |
| 102 | +// current_inp = 0; |
| 103 | +// var output_element = document.getElementById("output"); |
| 104 | +// output_element.innerText = ''; |
| 105 | +// Sk.pre = "output"; |
| 106 | +// Sk.configure({ |
| 107 | +// output: outf, |
| 108 | +// inputfun: get_input, |
| 109 | +// inputfunTakesPrompt: true, |
| 110 | +// execLimit: 1000, |
| 111 | +// __future__: Sk.python3 |
| 112 | +// }); |
| 113 | +// var myPromise = Sk.misceval.asyncToPromise(function () { |
| 114 | +// return Sk.importMainWithBody("<stdin>", false, prog, true); |
| 115 | +// }); |
| 116 | +// myPromise.then(function (mod) { |
| 117 | + |
| 118 | +// }, |
| 119 | +// function (err) { |
| 120 | +// output_element.innerText = err.toString(); |
| 121 | +// }); |
| 122 | +// } |
| 123 | + |
| 124 | + |
48 | 125 | // Uncomment below lines to use Pyodide instead of Skulpt!
|
49 | 126 |
|
50 | 127 | // async function setup_pyodide() {
|
|
0 commit comments