|
| 1 | +// cSpell:ignore pyodide pyproject |
| 2 | +var pyodide; |
| 3 | +var console = document.getElementById("console"); |
| 4 | +var errors = document.getElementById("errors"); |
| 5 | +var css_box = document.getElementById("css"); |
| 6 | +var source = document.getElementById("schemascii"); |
| 7 | +var download_button = document.getElementById("download"); |
| 8 | +var ver_switcher = document.getElementById("version"); |
| 9 | + |
| 10 | +var schemascii; |
| 11 | +var monkeysrc; |
| 12 | + |
| 13 | +var ver_map; |
| 14 | + |
| 15 | +async function main() { |
| 16 | + try { |
| 17 | + info("Loading Python... "); |
| 18 | + pyodide = await loadPyodide({ stdout: info, stderr: error }); |
| 19 | + info("done\nInstalling micropip..."); |
| 20 | + await pyodide.loadPackage("micropip", { errorCallback: error, messageCallback: () => { } }); |
| 21 | + info("done\nFetching versions... "); |
| 22 | + monkeysrc = await fetch("scripts/monkeypatch.py").then(r => r.text()); |
| 23 | + var foo = await fetch("https://api.github.com/repos/dragoncoder047/schemascii/contents/dist").then(r => r.json()); |
| 24 | + foo = foo.filter(x => x.name.endsWith(".whl")).map(x => x.path); |
| 25 | + ver_map = Object.fromEntries(foo.map(x => [/\/schemascii-([\d.]+)-/.exec(x)[1], x])) |
| 26 | + var all_versions = Object.keys(ver_map); |
| 27 | + //all_versions.push("DEV"); |
| 28 | + for (var v of all_versions) { |
| 29 | + var o = document.createElement("option"); |
| 30 | + o.textContent = o.value = v; |
| 31 | + ver_switcher.append(o); |
| 32 | + } |
| 33 | + var latest_version = await fetch("pyproject.toml").then(r => r.text()).then(r => /version = "([\d.]+)"/.exec(r)[0]); |
| 34 | + ver_switcher.value = latest_version; |
| 35 | + info(`["${all_versions.join('", "')}"]\nlatest=${latest_version}\n`); |
| 36 | + await switch_version(); |
| 37 | + css_box.addEventListener("input", debounce(sync_css)); |
| 38 | + source.addEventListener("input", debounce(catched(render))); |
| 39 | + download_button.addEventListener("click", download); |
| 40 | + ver_switcher.addEventListener("change", acatched(switch_version)); |
| 41 | + |
| 42 | + source.removeAttribute("disabled"); |
| 43 | + css_box.removeAttribute("disabled"); |
| 44 | + console.textContent = "Ready"; |
| 45 | + } catch (e) { |
| 46 | + error(`\nFATAL ERROR:\n${e.stack}\n`); |
| 47 | + throw e; |
| 48 | + } |
| 49 | +} |
| 50 | +function monkeypatch() { |
| 51 | + pyodide.runPython(monkeysrc); |
| 52 | +} |
| 53 | +function info(line) { |
| 54 | + console.textContent += line; |
| 55 | +} |
| 56 | +function error(text) { |
| 57 | + errors.textContent += text; |
| 58 | +} |
| 59 | +function debounce(fun) { |
| 60 | + var timeout; |
| 61 | + return function () { |
| 62 | + if (timeout) clearTimeout(timeout); |
| 63 | + timeout = setTimeout(fun.bind(this, arguments), 100); |
| 64 | + }; |
| 65 | +} |
| 66 | +function catched(fun) { |
| 67 | + return function () { |
| 68 | + try { |
| 69 | + fun.call(this, arguments); |
| 70 | + } catch (e) { |
| 71 | + error(e.stack); |
| 72 | + } |
| 73 | + }; |
| 74 | +} |
| 75 | +async function acatched(fun) { |
| 76 | + return async function() { |
| 77 | + try { |
| 78 | + await fun.call(this, arguments); |
| 79 | + } catch (e) { |
| 80 | + error(e.stack); |
| 81 | + } |
| 82 | + }; |
| 83 | +} |
| 84 | +function sync_css() { |
| 85 | + style_elem.innerHTML = css_box.value; |
| 86 | +} |
| 87 | +function render() { |
| 88 | + console.textContent = ""; |
| 89 | + errors.textContent = ""; |
| 90 | + output.textContent = schemascii.patched_render(source.value); |
| 91 | +} |
| 92 | + |
| 93 | +async function switch_version() { |
| 94 | + info("Installing Schemascii version " + ver_switcher.value + "... ") |
| 95 | + await pyodide.pyimport("micropip").install(ver_map[ver_switcher.value]); |
| 96 | + monkeypatch(); |
| 97 | + schemascii = pyodide.runPython("import schemascii; schemascii"); |
| 98 | + info("done\n"); |
| 99 | +} |
| 100 | + |
| 101 | +function download() { |
| 102 | + var a = document.createElement("a"); |
| 103 | + a.setAttribute("href", URL.createObjectURL(new Blob([output.innerHTML], {"type": "application/svg+xml"}))); |
| 104 | + a.setAttribute("download", `schemascii_playground_${new Date().toISOString()}_no_css.svg`); |
| 105 | + a.click(); |
| 106 | +} |
| 107 | + |
| 108 | +main(); |
| 109 | + |
| 110 | +// fetch("https://github.com/dragoncoder047/schemascii/zipball/main/").then(r => r.arrayBuffer()).then(b => pyodide.unpackArchive(b, "zip")); |
0 commit comments