@pyscript/core 0.6.35 → 0.6.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/{codemirror-Bpb6Ey5f.js → codemirror-7hplgyVJ.js} +2 -2
  2. package/dist/{codemirror-Bpb6Ey5f.js.map → codemirror-7hplgyVJ.js.map} +1 -1
  3. package/dist/{codemirror_commands-SZdOE5nU.js → codemirror_commands-D39x9C5q.js} +2 -2
  4. package/dist/{codemirror_commands-SZdOE5nU.js.map → codemirror_commands-D39x9C5q.js.map} +1 -1
  5. package/dist/{codemirror_lang-python-CT4R7aU5.js → codemirror_lang-python-Q-yAwxyf.js} +2 -2
  6. package/dist/{codemirror_lang-python-CT4R7aU5.js.map → codemirror_lang-python-Q-yAwxyf.js.map} +1 -1
  7. package/dist/{codemirror_language-0XdDQPdy.js → codemirror_language-CCfjGGWm.js} +2 -2
  8. package/dist/{codemirror_language-0XdDQPdy.js.map → codemirror_language-CCfjGGWm.js.map} +1 -1
  9. package/dist/codemirror_view-Bw06hiJi.js +2 -0
  10. package/dist/codemirror_view-Bw06hiJi.js.map +1 -0
  11. package/dist/core-CY38q33K.js +4 -0
  12. package/dist/core-CY38q33K.js.map +1 -0
  13. package/dist/core.js +1 -1
  14. package/dist/{deprecations-manager-D2oeE--a.js → deprecations-manager-apTdS2vL.js} +2 -2
  15. package/dist/{deprecations-manager-D2oeE--a.js.map → deprecations-manager-apTdS2vL.js.map} +1 -1
  16. package/dist/{donkey-BrIehEwe.js → donkey-CEUHpyBi.js} +2 -2
  17. package/dist/{donkey-BrIehEwe.js.map → donkey-CEUHpyBi.js.map} +1 -1
  18. package/dist/{error-CGGNJnMJ.js → error-BnJRaZX6.js} +2 -2
  19. package/dist/{error-CGGNJnMJ.js.map → error-BnJRaZX6.js.map} +1 -1
  20. package/dist/{index-BI1TrUla.js → index-DNWf5Jlg.js} +2 -2
  21. package/dist/{index-BI1TrUla.js.map → index-DNWf5Jlg.js.map} +1 -1
  22. package/dist/{mpy-49r-ARjR.js → mpy-CcFbuP4l.js} +2 -2
  23. package/dist/{mpy-49r-ARjR.js.map → mpy-CcFbuP4l.js.map} +1 -1
  24. package/dist/{py-editor-Dil8sfoT.js → py-editor-B257doLW.js} +2 -2
  25. package/dist/{py-editor-Dil8sfoT.js.map → py-editor-B257doLW.js.map} +1 -1
  26. package/dist/py-game-COQWxJ4-.js +2 -0
  27. package/dist/py-game-COQWxJ4-.js.map +1 -0
  28. package/dist/{py-DZ9P39Fr.js → py-hOIacH0d.js} +2 -2
  29. package/dist/{py-DZ9P39Fr.js.map → py-hOIacH0d.js.map} +1 -1
  30. package/dist/{py-terminal-D4qhEdOa.js → py-terminal-flqGFKNk.js} +2 -2
  31. package/dist/{py-terminal-D4qhEdOa.js.map → py-terminal-flqGFKNk.js.map} +1 -1
  32. package/dist/toml-DiUM0_qs.js.map +1 -1
  33. package/dist/zip-DPXsOtR5.js.map +1 -1
  34. package/package.json +7 -7
  35. package/src/plugins/py-game.js +35 -4
  36. package/types/core.d.ts +1 -1
  37. package/dist/codemirror_view-ZQwfolI2.js +0 -2
  38. package/dist/codemirror_view-ZQwfolI2.js.map +0 -1
  39. package/dist/core-DlvYU3OZ.js +0 -4
  40. package/dist/core-DlvYU3OZ.js.map +0 -1
  41. package/dist/py-game-wZnH0cJT.js +0 -2
  42. package/dist/py-game-wZnH0cJT.js.map +0 -1
@@ -1,2 +1,2 @@
1
- import{e,j as r,d as t}from"./core-DlvYU3OZ.js";const n=new WeakSet,i=({interpreter:e,io:r,run:t,type:n},{sync:i})=>{if("mpy"!==n||!i.is_pyterminal())return;const{pyterminal_ready:o,pyterminal_read:a,pyterminal_write:s}=i;e.registerJsModule("_pyscript_input",{input:a}),t(["from _pyscript_input import input","from polyscript import currentScript as _","__terminal__ = _.terminal","del _"].join(";"));const l=new Uint8Array([13]);r.stdout=e=>{10===e[0]&&s(l),s(e)},r.stderr=e=>{s(String(e.message||e))},i.pyterminal_stream_write=()=>{},e.registerJsModule("code",{interact(){const r=new TextEncoderStream;r.readable.pipeTo(new WritableStream({write(r){for(const t of r)e.replProcessChar(t)}}));const t=r.writable.getWriter();i.pyterminal_stream_write=e=>t.write(e),e.replInit()}}),o()};var o=async o=>{const[{Terminal:a},{FitAddon:s},{WebLinksAddon:l}]=await Promise.all([import("./xterm-7LwxAMsn.js"),import("./xterm_addon-fit--gyF3PcZ.js"),import("./xterm_addon-web-links-Cnej-nJ6.js")]),m={disableStdin:!1,cursorBlink:!0,cursorStyle:"block",lineHeight:1.2};let d;const p=()=>{let e=o;const r=o.getAttribute("target");if(r){if(e=document.getElementById(r)||document.querySelector(r),!e)throw new Error(`Unknown target ${r}`)}else e=document.createElement("py-terminal"),e.style.display="block",o.after(e);const n=new a({theme:{background:"#191A19",foreground:"#F5F2E7"},...m}),i=new s;return n.loadAddon(i),n.loadAddon(new l),n.open(e),i.fit(),n.focus(),t(o,{terminal:{value:n},process:{value:async e=>{for(const r of e.split(/(?:\r\n|\r|\n)/))await d.write(`${r}\r`)}}}),n};o.hasAttribute("worker")?(e.main.onWorker.add((function r(t,i){if(n.has(i))return;n.add(i),e.main.onWorker.delete(r);const o=p(),{sync:a}=i;let s=null,l="";a.is_pyterminal=()=>!0,a.pyterminal_read=e=>(o.write(e),s=Promise.withResolvers(),s.promise),a.pyterminal_write=e=>{s||o.write(e)},a.pyterminal_ready=()=>{let e=Promise.resolve();d={write:r=>e=e.then((()=>a.pyterminal_stream_write(r)))},o.onData((e=>{s?(""===e?l.length?(l=l.slice(0,-1),e="\b \b"):e="":l+=e,e&&(o.write(e),l.endsWith("\r")&&(o.write("\n"),s.resolve(l.slice(0,-1)),s=null,l=""))):d.write(e)}))}})),e.worker.onReady.add(i)):(e.main.codeBeforeRun.delete(r),e.main.onReady.add((function r({interpreter:t,io:n,run:i,type:o}){if("mpy"!==o)return;e.main.onReady.delete(r);const a=p(),s=new Uint8Array([13]);n.stdout=e=>{10===e[0]&&a.write(s),a.write(e)},globalThis.__py_terminal__=a,i(["from js import prompt as input","from js import __py_terminal__ as __terminal__"].join(";")),delete globalThis.__py_terminal__,t.registerJsModule("code",{interact(){const e=new TextEncoderStream;e.readable.pipeTo(new WritableStream({write(e){for(const r of e)t.replProcessChar(r)}})),d=e.writable.getWriter(),a.onData((e=>d.write(e))),t.replInit()}})})))};export{o as default};
2
- //# sourceMappingURL=mpy-49r-ARjR.js.map
1
+ import{e,i as r,d as t}from"./core-CY38q33K.js";const n=new WeakSet,i=({interpreter:e,io:r,run:t,type:n},{sync:i})=>{if("mpy"!==n||!i.is_pyterminal())return;const{pyterminal_ready:o,pyterminal_read:a,pyterminal_write:s}=i;e.registerJsModule("_pyscript_input",{input:a}),t(["from _pyscript_input import input","from polyscript import currentScript as _","__terminal__ = _.terminal","del _"].join(";"));const l=new Uint8Array([13]);r.stdout=e=>{10===e[0]&&s(l),s(e)},r.stderr=e=>{s(String(e.message||e))},i.pyterminal_stream_write=()=>{},e.registerJsModule("code",{interact(){const r=new TextEncoderStream;r.readable.pipeTo(new WritableStream({write(r){for(const t of r)e.replProcessChar(t)}}));const t=r.writable.getWriter();i.pyterminal_stream_write=e=>t.write(e),e.replInit()}}),o()};var o=async o=>{const[{Terminal:a},{FitAddon:s},{WebLinksAddon:l}]=await Promise.all([import("./xterm-7LwxAMsn.js"),import("./xterm_addon-fit--gyF3PcZ.js"),import("./xterm_addon-web-links-Cnej-nJ6.js")]),m={disableStdin:!1,cursorBlink:!0,cursorStyle:"block",lineHeight:1.2};let d;const p=()=>{let e=o;const r=o.getAttribute("target");if(r){if(e=document.getElementById(r)||document.querySelector(r),!e)throw new Error(`Unknown target ${r}`)}else e=document.createElement("py-terminal"),e.style.display="block",o.after(e);const n=new a({theme:{background:"#191A19",foreground:"#F5F2E7"},...m}),i=new s;return n.loadAddon(i),n.loadAddon(new l),n.open(e),i.fit(),n.focus(),t(o,{terminal:{value:n},process:{value:async e=>{for(const r of e.split(/(?:\r\n|\r|\n)/))await d.write(`${r}\r`)}}}),n};o.hasAttribute("worker")?(e.main.onWorker.add((function r(t,i){if(n.has(i))return;n.add(i),e.main.onWorker.delete(r);const o=p(),{sync:a}=i;let s=null,l="";a.is_pyterminal=()=>!0,a.pyterminal_read=e=>(o.write(e),s=Promise.withResolvers(),s.promise),a.pyterminal_write=e=>{s||o.write(e)},a.pyterminal_ready=()=>{let e=Promise.resolve();d={write:r=>e=e.then((()=>a.pyterminal_stream_write(r)))},o.onData((e=>{s?(""===e?l.length?(l=l.slice(0,-1),e="\b \b"):e="":l+=e,e&&(o.write(e),l.endsWith("\r")&&(o.write("\n"),s.resolve(l.slice(0,-1)),s=null,l=""))):d.write(e)}))}})),e.worker.onReady.add(i)):(e.main.codeBeforeRun.delete(r),e.main.onReady.add((function r({interpreter:t,io:n,run:i,type:o}){if("mpy"!==o)return;e.main.onReady.delete(r);const a=p(),s=new Uint8Array([13]);n.stdout=e=>{10===e[0]&&a.write(s),a.write(e)},globalThis.__py_terminal__=a,i(["from js import prompt as input","from js import __py_terminal__ as __terminal__"].join(";")),delete globalThis.__py_terminal__,t.registerJsModule("code",{interact(){const e=new TextEncoderStream;e.readable.pipeTo(new WritableStream({write(e){for(const r of e)t.replProcessChar(r)}})),d=e.writable.getWriter(),a.onData((e=>d.write(e))),t.replInit()}})})))};export{o as default};
2
+ //# sourceMappingURL=mpy-CcFbuP4l.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mpy-49r-ARjR.js","sources":["../src/plugins/py-terminal/mpy.js"],"sourcesContent":["// PyScript pyodide terminal plugin\nimport { defineProperties } from \"polyscript/exports\";\nimport { hooks, inputFailure } from \"../../core.js\";\n\nconst bootstrapped = new WeakSet();\n\n// this callback will be serialized as string and it never needs\n// to be invoked multiple times. Each xworker here is bootstrapped\n// only once thanks to the `sync.is_pyterminal()` check.\nconst workerReady = ({ interpreter, io, run, type }, { sync }) => {\n if (type !== \"mpy\" || !sync.is_pyterminal()) return;\n\n const { pyterminal_ready, pyterminal_read, pyterminal_write } = sync;\n\n interpreter.registerJsModule(\"_pyscript_input\", {\n input: pyterminal_read,\n });\n\n run(\n [\n \"from _pyscript_input import input\",\n \"from polyscript import currentScript as _\",\n \"__terminal__ = _.terminal\",\n \"del _\",\n ].join(\";\"),\n );\n\n const missingReturn = new Uint8Array([13]);\n io.stdout = (buffer) => {\n if (buffer[0] === 10) pyterminal_write(missingReturn);\n pyterminal_write(buffer);\n };\n io.stderr = (error) => {\n pyterminal_write(String(error.message || error));\n };\n\n sync.pyterminal_stream_write = () => {};\n\n // tiny shim of the code module with only interact\n // to bootstrap a REPL like environment\n interpreter.registerJsModule(\"code\", {\n interact() {\n const encoder = new TextEncoderStream();\n encoder.readable.pipeTo(\n new WritableStream({\n write(buffer) {\n for (const c of buffer) interpreter.replProcessChar(c);\n },\n }),\n );\n\n const writer = encoder.writable.getWriter();\n sync.pyterminal_stream_write = (buffer) => writer.write(buffer);\n\n interpreter.replInit();\n },\n });\n\n pyterminal_ready();\n};\n\nexport default async (element) => {\n // lazy load these only when a valid terminal is found\n const [{ Terminal }, { FitAddon }, { WebLinksAddon }] = await Promise.all([\n import(/* webpackIgnore: true */ \"../../3rd-party/xterm.js\"),\n import(/* webpackIgnore: true */ \"../../3rd-party/xterm_addon-fit.js\"),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-web-links.js\"\n ),\n ]);\n\n const terminalOptions = {\n disableStdin: false,\n cursorBlink: true,\n cursorStyle: \"block\",\n lineHeight: 1.2,\n };\n\n let stream;\n\n // common main thread initialization for both worker\n // or main case, bootstrapping the terminal on its target\n const init = () => {\n let target = element;\n const selector = element.getAttribute(\"target\");\n if (selector) {\n target =\n document.getElementById(selector) ||\n document.querySelector(selector);\n if (!target) throw new Error(`Unknown target ${selector}`);\n } else {\n target = document.createElement(\"py-terminal\");\n target.style.display = \"block\";\n element.after(target);\n }\n const terminal = new Terminal({\n theme: {\n background: \"#191A19\",\n foreground: \"#F5F2E7\",\n },\n ...terminalOptions,\n });\n const fitAddon = new FitAddon();\n terminal.loadAddon(fitAddon);\n terminal.loadAddon(new WebLinksAddon());\n terminal.open(target);\n fitAddon.fit();\n terminal.focus();\n defineProperties(element, {\n terminal: { value: terminal },\n process: {\n value: async (code) => {\n for (const line of code.split(/(?:\\r\\n|\\r|\\n)/)) {\n await stream.write(`${line}\\r`);\n }\n },\n },\n });\n return terminal;\n };\n\n // branch logic for the worker\n if (element.hasAttribute(\"worker\")) {\n // add a hook on the main thread to setup all sync helpers\n // also bootstrapping the XTerm target on main *BUT* ...\n hooks.main.onWorker.add(function worker(_, xworker) {\n // ... as multiple workers will add multiple callbacks\n // be sure no xworker is ever initialized twice!\n if (bootstrapped.has(xworker)) return;\n bootstrapped.add(xworker);\n\n // still cleanup this callback for future scripts/workers\n hooks.main.onWorker.delete(worker);\n\n const terminal = init();\n\n const { sync } = xworker;\n\n // handle the read mode on input\n let promisedChunks = null;\n let readChunks = \"\";\n\n sync.is_pyterminal = () => true;\n\n // put the terminal in a read-only state\n // frees the worker on \\r\n sync.pyterminal_read = (buffer) => {\n terminal.write(buffer);\n promisedChunks = Promise.withResolvers();\n return promisedChunks.promise;\n };\n\n // write if not reading input\n sync.pyterminal_write = (buffer) => {\n if (!promisedChunks) terminal.write(buffer);\n };\n\n // add the onData terminal listener which forwards to the worker\n // everything typed in a queued char-by-char way\n sync.pyterminal_ready = () => {\n let queue = Promise.resolve();\n stream = {\n write: (buffer) =>\n (queue = queue.then(() =>\n sync.pyterminal_stream_write(buffer),\n )),\n };\n terminal.onData((buffer) => {\n if (promisedChunks) {\n // handle backspace on input\n if (buffer === \"\\x7f\") {\n // avoid over-greedy backspace\n if (readChunks.length) {\n readChunks = readChunks.slice(0, -1);\n // override previous char position\n // put an empty space to clear the char\n // move back position again\n buffer = \"\\b \\b\";\n } else buffer = \"\";\n } else readChunks += buffer;\n if (buffer) {\n terminal.write(buffer);\n if (readChunks.endsWith(\"\\r\")) {\n terminal.write(\"\\n\");\n promisedChunks.resolve(readChunks.slice(0, -1));\n promisedChunks = null;\n readChunks = \"\";\n }\n }\n } else {\n stream.write(buffer);\n }\n });\n };\n });\n\n // setup remote thread JS/Python code for whenever the\n // worker is ready to become a terminal\n hooks.worker.onReady.add(workerReady);\n } else {\n // ⚠️ In an ideal world the inputFailure should never be used on main.\n // However, Pyodide still can't compete with MicroPython REPL mode\n // so while it's OK to keep that entry on main as default, we need\n // to remove it ASAP from `mpy` use cases, otherwise MicroPython would\n // also throw whenever an `input(...)` is required / digited.\n hooks.main.codeBeforeRun.delete(inputFailure);\n\n // in the main case, just bootstrap XTerm without\n // allowing any input as that's not possible / awkward\n hooks.main.onReady.add(function main({ interpreter, io, run, type }) {\n if (type !== \"mpy\") return;\n\n hooks.main.onReady.delete(main);\n\n const terminal = init();\n\n const missingReturn = new Uint8Array([13]);\n io.stdout = (buffer) => {\n if (buffer[0] === 10) terminal.write(missingReturn);\n terminal.write(buffer);\n };\n\n // expose the __terminal__ one-off reference\n globalThis.__py_terminal__ = terminal;\n run(\n [\n \"from js import prompt as input\",\n \"from js import __py_terminal__ as __terminal__\",\n ].join(\";\"),\n );\n delete globalThis.__py_terminal__;\n\n // NOTE: this is NOT the same as the one within\n // the onWorkerReady callback!\n interpreter.registerJsModule(\"code\", {\n interact() {\n const encoder = new TextEncoderStream();\n encoder.readable.pipeTo(\n new WritableStream({\n write(buffer) {\n for (const c of buffer)\n interpreter.replProcessChar(c);\n },\n }),\n );\n\n stream = encoder.writable.getWriter();\n terminal.onData((buffer) => stream.write(buffer));\n\n interpreter.replInit();\n },\n });\n });\n }\n};\n"],"names":["bootstrapped","WeakSet","workerReady","interpreter","io","run","type","sync","is_pyterminal","pyterminal_ready","pyterminal_read","pyterminal_write","registerJsModule","input","join","missingReturn","Uint8Array","stdout","buffer","stderr","error","String","message","pyterminal_stream_write","interact","encoder","TextEncoderStream","readable","pipeTo","WritableStream","write","c","replProcessChar","writer","writable","getWriter","replInit","mpy","async","element","Terminal","FitAddon","WebLinksAddon","Promise","all","import","terminalOptions","disableStdin","cursorBlink","cursorStyle","lineHeight","stream","init","target","selector","getAttribute","document","getElementById","querySelector","Error","createElement","style","display","after","terminal","theme","background","foreground","fitAddon","loadAddon","open","fit","focus","defineProperties","value","process","code","line","split","hasAttribute","hooks","main","onWorker","add","worker","_","xworker","has","delete","promisedChunks","readChunks","withResolvers","promise","queue","resolve","then","onData","length","slice","endsWith","onReady","codeBeforeRun","inputFailure","globalThis","__py_terminal__"],"mappings":"gDAIA,MAAMA,EAAe,IAAIC,QAKnBC,EAAc,EAAGC,cAAaC,KAAIC,MAAKC,SAAUC,WACnD,GAAa,QAATD,IAAmBC,EAAKC,gBAAiB,OAE7C,MAAMC,iBAAEA,EAAgBC,gBAAEA,EAAeC,iBAAEA,GAAqBJ,EAEhEJ,EAAYS,iBAAiB,kBAAmB,CAC5CC,MAAOH,IAGXL,EACI,CACI,oCACA,4CACA,4BACA,SACFS,KAAK,MAGX,MAAMC,EAAgB,IAAIC,WAAW,CAAC,KACtCZ,EAAGa,OAAUC,IACS,KAAdA,EAAO,IAAWP,EAAiBI,GACvCJ,EAAiBO,EAAO,EAE5Bd,EAAGe,OAAUC,IACTT,EAAiBU,OAAOD,EAAME,SAAWF,GAAO,EAGpDb,EAAKgB,wBAA0B,OAI/BpB,EAAYS,iBAAiB,OAAQ,CACjC,QAAAY,GACI,MAAMC,EAAU,IAAIC,kBACpBD,EAAQE,SAASC,OACb,IAAIC,eAAe,CACf,KAAAC,CAAMZ,GACF,IAAK,MAAMa,KAAKb,EAAQf,EAAY6B,gBAAgBD,EACvD,KAIT,MAAME,EAASR,EAAQS,SAASC,YAChC5B,EAAKgB,wBAA2BL,GAAWe,EAAOH,MAAMZ,GAExDf,EAAYiC,UACf,IAGL3B,GAAkB,EAGtB,IAAe4B,EAAAC,MAAOC,IAElB,OAAOC,SAAEA,IAAYC,SAAEA,IAAYC,cAAEA,UAAyBC,QAAQC,IAAI,CACtEC,OAAiC,uBACjCA,OAAiC,iCACjCA,OAC8B,yCAI5B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["../src/plugins/py-terminal/mpy.js"],"sourcesContent":["// PyScript pyodide terminal plugin\nimport { defineProperties } from \"polyscript/exports\";\nimport { hooks, inputFailure } from \"../../core.js\";\n\nconst bootstrapped = new WeakSet();\n\n// this callback will be serialized as string and it never needs\n// to be invoked multiple times. Each xworker here is bootstrapped\n// only once thanks to the `sync.is_pyterminal()` check.\nconst workerReady = ({ interpreter, io, run, type }, { sync }) => {\n if (type !== \"mpy\" || !sync.is_pyterminal()) return;\n\n const { pyterminal_ready, pyterminal_read, pyterminal_write } = sync;\n\n interpreter.registerJsModule(\"_pyscript_input\", {\n input: pyterminal_read,\n });\n\n run(\n [\n \"from _pyscript_input import input\",\n \"from polyscript import currentScript as _\",\n \"__terminal__ = _.terminal\",\n \"del _\",\n ].join(\";\"),\n );\n\n const missingReturn = new Uint8Array([13]);\n io.stdout = (buffer) => {\n if (buffer[0] === 10) pyterminal_write(missingReturn);\n pyterminal_write(buffer);\n };\n io.stderr = (error) => {\n pyterminal_write(String(error.message || error));\n };\n\n sync.pyterminal_stream_write = () => {};\n\n // tiny shim of the code module with only interact\n // to bootstrap a REPL like environment\n interpreter.registerJsModule(\"code\", {\n interact() {\n const encoder = new TextEncoderStream();\n encoder.readable.pipeTo(\n new WritableStream({\n write(buffer) {\n for (const c of buffer) interpreter.replProcessChar(c);\n },\n }),\n );\n\n const writer = encoder.writable.getWriter();\n sync.pyterminal_stream_write = (buffer) => writer.write(buffer);\n\n interpreter.replInit();\n },\n });\n\n pyterminal_ready();\n};\n\nexport default async (element) => {\n // lazy load these only when a valid terminal is found\n const [{ Terminal }, { FitAddon }, { WebLinksAddon }] = await Promise.all([\n import(/* webpackIgnore: true */ \"../../3rd-party/xterm.js\"),\n import(/* webpackIgnore: true */ \"../../3rd-party/xterm_addon-fit.js\"),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-web-links.js\"\n ),\n ]);\n\n const terminalOptions = {\n disableStdin: false,\n cursorBlink: true,\n cursorStyle: \"block\",\n lineHeight: 1.2,\n };\n\n let stream;\n\n // common main thread initialization for both worker\n // or main case, bootstrapping the terminal on its target\n const init = () => {\n let target = element;\n const selector = element.getAttribute(\"target\");\n if (selector) {\n target =\n document.getElementById(selector) ||\n document.querySelector(selector);\n if (!target) throw new Error(`Unknown target ${selector}`);\n } else {\n target = document.createElement(\"py-terminal\");\n target.style.display = \"block\";\n element.after(target);\n }\n const terminal = new Terminal({\n theme: {\n background: \"#191A19\",\n foreground: \"#F5F2E7\",\n },\n ...terminalOptions,\n });\n const fitAddon = new FitAddon();\n terminal.loadAddon(fitAddon);\n terminal.loadAddon(new WebLinksAddon());\n terminal.open(target);\n fitAddon.fit();\n terminal.focus();\n defineProperties(element, {\n terminal: { value: terminal },\n process: {\n value: async (code) => {\n for (const line of code.split(/(?:\\r\\n|\\r|\\n)/)) {\n await stream.write(`${line}\\r`);\n }\n },\n },\n });\n return terminal;\n };\n\n // branch logic for the worker\n if (element.hasAttribute(\"worker\")) {\n // add a hook on the main thread to setup all sync helpers\n // also bootstrapping the XTerm target on main *BUT* ...\n hooks.main.onWorker.add(function worker(_, xworker) {\n // ... as multiple workers will add multiple callbacks\n // be sure no xworker is ever initialized twice!\n if (bootstrapped.has(xworker)) return;\n bootstrapped.add(xworker);\n\n // still cleanup this callback for future scripts/workers\n hooks.main.onWorker.delete(worker);\n\n const terminal = init();\n\n const { sync } = xworker;\n\n // handle the read mode on input\n let promisedChunks = null;\n let readChunks = \"\";\n\n sync.is_pyterminal = () => true;\n\n // put the terminal in a read-only state\n // frees the worker on \\r\n sync.pyterminal_read = (buffer) => {\n terminal.write(buffer);\n promisedChunks = Promise.withResolvers();\n return promisedChunks.promise;\n };\n\n // write if not reading input\n sync.pyterminal_write = (buffer) => {\n if (!promisedChunks) terminal.write(buffer);\n };\n\n // add the onData terminal listener which forwards to the worker\n // everything typed in a queued char-by-char way\n sync.pyterminal_ready = () => {\n let queue = Promise.resolve();\n stream = {\n write: (buffer) =>\n (queue = queue.then(() =>\n sync.pyterminal_stream_write(buffer),\n )),\n };\n terminal.onData((buffer) => {\n if (promisedChunks) {\n // handle backspace on input\n if (buffer === \"\\x7f\") {\n // avoid over-greedy backspace\n if (readChunks.length) {\n readChunks = readChunks.slice(0, -1);\n // override previous char position\n // put an empty space to clear the char\n // move back position again\n buffer = \"\\b \\b\";\n } else buffer = \"\";\n } else readChunks += buffer;\n if (buffer) {\n terminal.write(buffer);\n if (readChunks.endsWith(\"\\r\")) {\n terminal.write(\"\\n\");\n promisedChunks.resolve(readChunks.slice(0, -1));\n promisedChunks = null;\n readChunks = \"\";\n }\n }\n } else {\n stream.write(buffer);\n }\n });\n };\n });\n\n // setup remote thread JS/Python code for whenever the\n // worker is ready to become a terminal\n hooks.worker.onReady.add(workerReady);\n } else {\n // ⚠️ In an ideal world the inputFailure should never be used on main.\n // However, Pyodide still can't compete with MicroPython REPL mode\n // so while it's OK to keep that entry on main as default, we need\n // to remove it ASAP from `mpy` use cases, otherwise MicroPython would\n // also throw whenever an `input(...)` is required / digited.\n hooks.main.codeBeforeRun.delete(inputFailure);\n\n // in the main case, just bootstrap XTerm without\n // allowing any input as that's not possible / awkward\n hooks.main.onReady.add(function main({ interpreter, io, run, type }) {\n if (type !== \"mpy\") return;\n\n hooks.main.onReady.delete(main);\n\n const terminal = init();\n\n const missingReturn = new Uint8Array([13]);\n io.stdout = (buffer) => {\n if (buffer[0] === 10) terminal.write(missingReturn);\n terminal.write(buffer);\n };\n\n // expose the __terminal__ one-off reference\n globalThis.__py_terminal__ = terminal;\n run(\n [\n \"from js import prompt as input\",\n \"from js import __py_terminal__ as __terminal__\",\n ].join(\";\"),\n );\n delete globalThis.__py_terminal__;\n\n // NOTE: this is NOT the same as the one within\n // the onWorkerReady callback!\n interpreter.registerJsModule(\"code\", {\n interact() {\n const encoder = new TextEncoderStream();\n encoder.readable.pipeTo(\n new WritableStream({\n write(buffer) {\n for (const c of buffer)\n interpreter.replProcessChar(c);\n },\n }),\n );\n\n stream = encoder.writable.getWriter();\n terminal.onData((buffer) => stream.write(buffer));\n\n interpreter.replInit();\n },\n });\n });\n }\n};\n"],"names":["bootstrapped","WeakSet","workerReady","interpreter","io","run","type","sync","is_pyterminal","pyterminal_ready","pyterminal_read","pyterminal_write","registerJsModule","input","join","missingReturn","Uint8Array","stdout","buffer","stderr","error","String","message","pyterminal_stream_write","interact","encoder","TextEncoderStream","readable","pipeTo","WritableStream","write","c","replProcessChar","writer","writable","getWriter","replInit","mpy","async","element","Terminal","FitAddon","WebLinksAddon","Promise","all","import","terminalOptions","disableStdin","cursorBlink","cursorStyle","lineHeight","stream","init","target","selector","getAttribute","document","getElementById","querySelector","Error","createElement","style","display","after","terminal","theme","background","foreground","fitAddon","loadAddon","open","fit","focus","defineProperties","value","process","code","line","split","hasAttribute","hooks","main","onWorker","add","worker","_","xworker","has","delete","promisedChunks","readChunks","withResolvers","promise","queue","resolve","then","onData","length","slice","endsWith","onReady","codeBeforeRun","inputFailure","globalThis","__py_terminal__"],"mappings":"gDAIA,MAAMA,EAAe,IAAIC,QAKnBC,EAAc,EAAGC,cAAaC,KAAIC,MAAKC,SAAUC,WACnD,GAAa,QAATD,IAAmBC,EAAKC,gBAAiB,OAE7C,MAAMC,iBAAEA,EAAgBC,gBAAEA,EAAeC,iBAAEA,GAAqBJ,EAEhEJ,EAAYS,iBAAiB,kBAAmB,CAC5CC,MAAOH,IAGXL,EACI,CACI,oCACA,4CACA,4BACA,SACFS,KAAK,MAGX,MAAMC,EAAgB,IAAIC,WAAW,CAAC,KACtCZ,EAAGa,OAAUC,IACS,KAAdA,EAAO,IAAWP,EAAiBI,GACvCJ,EAAiBO,EAAO,EAE5Bd,EAAGe,OAAUC,IACTT,EAAiBU,OAAOD,EAAME,SAAWF,GAAO,EAGpDb,EAAKgB,wBAA0B,OAI/BpB,EAAYS,iBAAiB,OAAQ,CACjC,QAAAY,GACI,MAAMC,EAAU,IAAIC,kBACpBD,EAAQE,SAASC,OACb,IAAIC,eAAe,CACf,KAAAC,CAAMZ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AYS,iBAAiB,OAAQ,CACjC,QAAAY,GACI,MAAMC,EAAU,IAAIC,kBACpBD,EAAQE,SAASC,OACb,IAAIC,eAAe,CACf,KAAAC,CAAMZ,GACF,IAAK,MAAMa,KAAKb,EACZf,EAAY6B,gBAAgBD,EACnC,KAIToB,EAAS1B,EAAQS,SAASC,YAC1B6B,EAASgC,QAAQ9E,GAAWiC,EAAOrB,MAAMZ,KAEzCf,EAAYiC,UACf,GAEjB,IACA"}
@@ -1,2 +1,2 @@
1
- import{T as e,c as t,X as n,d as r,a as o,r as s,o as i,H as a,s as c}from"./core-DlvYU3OZ.js";import{notify as l}from"./error-CGGNJnMJ.js";const d='<svg style="height:24px;width:24px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19,12a1,1,0,0,1-.55.89l-10,5A1,1,0,0,1,8,18a1,1,0,0,1-.53-.15A1,1,0,0,1,7,17V7a1,1,0,0,1,1.45-.89l10,5A1,1,0,0,1,19,12Z" fill="#464646"/></svg>';let u=0;const p=e=>`${e}-editor-${u++}`,f=new Map,g=new Map,h=new WeakMap,v={worker:{codeBeforeRun:()=>c,onReady:({runAsync:e,io:t},{sync:n})=>{t.stdout=t.buffered(n.write),t.stderr=t.buffered(n.writeErr),n.revoke(),n.runAsync=e}}},m=(e,t)=>{if("boolean"==typeof t)throw`Invalid source: ${e}`;return t},y=(e,t)=>{const n=e.closest(`.${t}-editor-box`);return n?.parentNode?.previousElementSibling};async function w({currentTarget:t}){const{env:o,pySrc:c,outDiv:u}=this,p=!!t;if(p&&(t.classList.add("running"),t.innerHTML='<svg style="height:24px;width:24px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M7 7h10v10H7z" style="fill:#464646;stroke:#464646;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-dasharray:none;paint-order:normal"/></svg>',u.innerHTML=""),!f.has(o)){const c=URL.createObjectURL(new Blob([""])),d={type:this.interpreter,serviceWorker:this.serviceWorker},{config:u}=this;if(u)try{if(d.configURL=s(u),u.endsWith(".toml")){const[{parse:e},t]=await Promise.all([import("./toml-BLBSZ43A.js"),fetch(u).then((e=>e.ok&&e.text()))]);d.config=e(m(u,t))}else if(u.endsWith(".json")){const e=await fetch(u).then((e=>e.ok&&e.json()));d.config=m(u,e)}else d.configURL=s("./config.txt"),d.config=JSON.parse(u);d.version=i(d.config)}catch(e){return void l(e)}else d.config={};const g=n.call(new a(null,v),c,d);if(p)for(const n of e.keys()){const e=y(t,n);if(e){r(e,{xworker:{value:g}});break}}const{sync:h}=g,{promise:w,resolve:b}=Promise.withResolvers();f.set(o,w),h.revoke=()=>{URL.revokeObjectURL(c),b(g)}}return f.get(o).then((e=>{e.onerror=({error:e})=>{p&&u.insertAdjacentHTML("beforeend",`<span style='color:red'>${e.message||e}</span>\n`),console.error(e)};const n=()=>{p&&(t.classList.remove("running"),t.innerHTML=d)},{sync:r}=e;r.write=e=>{p?u.innerText+=`${e}\n`:console.log(e)},r.writeErr=e=>{p?u.insertAdjacentHTML("beforeend",`<span style='color:red'>${e}</span>\n`):(l(e),console.error(e))},r.runAsync(c).then(n,n)}))}const b=(e,t)=>{const n=document.createElement("div");n.className=`${t}-editor-input`,n.setAttribute("aria-label","Python Script Area");const r=((e,t)=>{const n=document.createElement("button");return n.className=`absolute ${t}-editor-run-button`,n.innerHTML=d,n.setAttribute("aria-label","Python Script Run Button"),n.addEventListener("click",(async r=>{if(n.classList.contains("running")&&confirm("Stop evaluating this code?")){const e=y(n,t);if(e){const n=h.get(e).state.doc.toString(),r=e.cloneNode(!0);r.type=`${t}-editor`,r.textContent=n,e.xworker.terminate(),e.nextElementSibling.remove(),e.replaceWith(r),h.delete(e)}}else n.blur(),await e.handleEvent(r)})),n})(e,t),o=document.createElement("div");return o.addEventListener("keydown",(e=>{e.stopPropagation()})),n.append(r,o),n},k=(e,t)=>{const n=document.createElement("div");n.className=`${t}-editor-box`;const r=b(e,t),o=(e=>{const t=document.createElement("div");return t.className=`${e}-editor-output`,t.id=`${p(e)}-output`,t})(t);return n.append(r,o),[n,o,r.querySelector("button")]},E=async(e,s,i)=>{const[{basicSetup:a,EditorView:c},{Compartment:d},{python:u},{indentUnit:f},{keymap:v},{defaultKeymap:y,indentWithTab:b}]=await Promise.all([t.core,t.state,t.python,t.language,t.view,t.commands]);let E=e.hasAttribute("setup");const x=e.hasAttribute("config"),A=e.getAttribute("service-worker"),$=`${i}-${e.getAttribute("env")||p(s)}`;if(A&&(new n("data:application/javascript,postMessage(0)",{type:"dummy",serviceWorker:A}).onmessage=({target:e})=>e.terminate()),x&&g.has($))throw new SyntaxError(g.get($)?`duplicated config for env: ${$}`:`unable to add a config to the env: ${$}`);g.set($,x);let S=e.textContent;const{src:L}=e;if(L)try{S=m(L,await fetch(L).then((e=>e.ok&&e.text())))}catch(e){return void l(e)}const M={handleEvent:w,serviceWorker:A,interpreter:i,env:$,config:x&&e.getAttribute("config"),get pySrc(){return E?S:D.state.doc.toString()},get outDiv(){return E?null:H}};let T;r(e,{target:{get:()=>T},handleEvent:{get:()=>M.handleEvent,set:e=>{M.handleEvent=e===w?w:async t=>{const{currentTarget:n}=t;r(t,{code:{value:M.pySrc}}),!1!==await e(t)&&await w.call(M,{currentTarget:n})}}},code:{get:()=>M.pySrc,set:e=>{E||D.update([D.state.update({changes:{from:0,to:D.state.doc.length,insert:e}})])}},process:{value(e,t=!1){if(t)return B();const n=E,r=S;E=!0,S=e;const o=()=>{E=n,S=r};return M.handleEvent({currentTarget:null}).then(o,o)}}});const j=()=>{const t=new Event(`${s}-editor`,{bubbles:!0});e.dispatchEvent(t)};if(E)return await M.handleEvent({currentTarget:null}),void j();const R=e.getAttribute("target");if(R){if(T=document.getElementById(R)||document.querySelector(R),!T)throw new Error(`Unknown target ${R}`)}else T=document.createElement(`${s}-editor`),T.style.display="block",e.after(T);T.id||(T.id=p(s)),T.hasAttribute("exec-id")||T.setAttribute("exec-id",0),T.hasAttribute("root")||T.setAttribute("root",T.id);const[C,H,W]=k(M,s);C.dataset.env=e.hasAttribute("env")?$:i;const U=C.querySelector(`.${s}-editor-input > div`).attachShadow({mode:"open"});U.innerHTML="<style> :host { all: initial; }</style>",T.appendChild(C);const N=o(e.textContent).trim(),P=/^([ \t]+)/m.test(N)?RegExp.$1:" ",B=()=>W.click(),D=new c({extensions:[f.of(P),(new d).of(u()),v.of([...y,{key:"Ctrl-Enter",run:B,preventDefault:!0},{key:"Cmd-Enter",run:B,preventDefault:!0},{key:"Shift-Enter",run:B,preventDefault:!0},b]),a],foldGutter:!0,gutters:["CodeMirror-linenumbers","CodeMirror-foldgutter"],parent:U,doc:N});h.set(e,D),D.focus(),j()};let x=0,A=Promise.resolve();const $=()=>{x=0,S()},S=()=>{if(!x){x=setTimeout($,250);for(const[t,n]of e){const e=`script[type="${t}-editor"]`;for(const r of document.querySelectorAll(e))r.type+="-active",A=A.then((()=>E(r,t,n)))}return A}};new MutationObserver(S).observe(document,{childList:!0,subtree:!0});var L=S();export{L as default};
2
- //# sourceMappingURL=py-editor-Dil8sfoT.js.map
1
+ import{T as e,c as t,X as n,d as r,a as o,r as s,o as i,H as a,s as c}from"./core-CY38q33K.js";import{notify as l}from"./error-BnJRaZX6.js";const d='<svg style="height:24px;width:24px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19,12a1,1,0,0,1-.55.89l-10,5A1,1,0,0,1,8,18a1,1,0,0,1-.53-.15A1,1,0,0,1,7,17V7a1,1,0,0,1,1.45-.89l10,5A1,1,0,0,1,19,12Z" fill="#464646"/></svg>';let u=0;const p=e=>`${e}-editor-${u++}`,f=new Map,g=new Map,h=new WeakMap,v={worker:{codeBeforeRun:()=>c,onReady:({runAsync:e,io:t},{sync:n})=>{t.stdout=t.buffered(n.write),t.stderr=t.buffered(n.writeErr),n.revoke(),n.runAsync=e}}},m=(e,t)=>{if("boolean"==typeof t)throw`Invalid source: ${e}`;return t},y=(e,t)=>{const n=e.closest(`.${t}-editor-box`);return n?.parentNode?.previousElementSibling};async function w({currentTarget:t}){const{env:o,pySrc:c,outDiv:u}=this,p=!!t;if(p&&(t.classList.add("running"),t.innerHTML='<svg style="height:24px;width:24px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M7 7h10v10H7z" style="fill:#464646;stroke:#464646;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-dasharray:none;paint-order:normal"/></svg>',u.innerHTML=""),!f.has(o)){const c=URL.createObjectURL(new Blob([""])),d={type:this.interpreter,serviceWorker:this.serviceWorker},{config:u}=this;if(u)try{if(d.configURL=s(u),u.endsWith(".toml")){const[{parse:e},t]=await Promise.all([import("./toml-BLBSZ43A.js"),fetch(u).then((e=>e.ok&&e.text()))]);d.config=e(m(u,t))}else if(u.endsWith(".json")){const e=await fetch(u).then((e=>e.ok&&e.json()));d.config=m(u,e)}else d.configURL=s("./config.txt"),d.config=JSON.parse(u);d.version=i(d.config)}catch(e){return void l(e)}else d.config={};const g=n.call(new a(null,v),c,d);if(p)for(const n of e.keys()){const e=y(t,n);if(e){r(e,{xworker:{value:g}});break}}const{sync:h}=g,{promise:w,resolve:b}=Promise.withResolvers();f.set(o,w),h.revoke=()=>{URL.revokeObjectURL(c),b(g)}}return f.get(o).then((e=>{e.onerror=({error:e})=>{p&&u.insertAdjacentHTML("beforeend",`<span style='color:red'>${e.message||e}</span>\n`),console.error(e)};const n=()=>{p&&(t.classList.remove("running"),t.innerHTML=d)},{sync:r}=e;r.write=e=>{p?u.innerText+=`${e}\n`:console.log(e)},r.writeErr=e=>{p?u.insertAdjacentHTML("beforeend",`<span style='color:red'>${e}</span>\n`):(l(e),console.error(e))},r.runAsync(c).then(n,n)}))}const b=(e,t)=>{const n=document.createElement("div");n.className=`${t}-editor-input`,n.setAttribute("aria-label","Python Script Area");const r=((e,t)=>{const n=document.createElement("button");return n.className=`absolute ${t}-editor-run-button`,n.innerHTML=d,n.setAttribute("aria-label","Python Script Run Button"),n.addEventListener("click",(async r=>{if(n.classList.contains("running")&&confirm("Stop evaluating this code?")){const e=y(n,t);if(e){const n=h.get(e).state.doc.toString(),r=e.cloneNode(!0);r.type=`${t}-editor`,r.textContent=n,e.xworker.terminate(),e.nextElementSibling.remove(),e.replaceWith(r),h.delete(e)}}else n.blur(),await e.handleEvent(r)})),n})(e,t),o=document.createElement("div");return o.addEventListener("keydown",(e=>{e.stopPropagation()})),n.append(r,o),n},k=(e,t)=>{const n=document.createElement("div");n.className=`${t}-editor-box`;const r=b(e,t),o=(e=>{const t=document.createElement("div");return t.className=`${e}-editor-output`,t.id=`${p(e)}-output`,t})(t);return n.append(r,o),[n,o,r.querySelector("button")]},E=async(e,s,i)=>{const[{basicSetup:a,EditorView:c},{Compartment:d},{python:u},{indentUnit:f},{keymap:v},{defaultKeymap:y,indentWithTab:b}]=await Promise.all([t.core,t.state,t.python,t.language,t.view,t.commands]);let E=e.hasAttribute("setup");const x=e.hasAttribute("config"),A=e.getAttribute("service-worker"),$=`${i}-${e.getAttribute("env")||p(s)}`;if(A&&(new n("data:application/javascript,postMessage(0)",{type:"dummy",serviceWorker:A}).onmessage=({target:e})=>e.terminate()),x&&g.has($))throw new SyntaxError(g.get($)?`duplicated config for env: ${$}`:`unable to add a config to the env: ${$}`);g.set($,x);let S=e.textContent;const{src:L}=e;if(L)try{S=m(L,await fetch(L).then((e=>e.ok&&e.text())))}catch(e){return void l(e)}const M={handleEvent:w,serviceWorker:A,interpreter:i,env:$,config:x&&e.getAttribute("config"),get pySrc(){return E?S:D.state.doc.toString()},get outDiv(){return E?null:H}};let T;r(e,{target:{get:()=>T},handleEvent:{get:()=>M.handleEvent,set:e=>{M.handleEvent=e===w?w:async t=>{const{currentTarget:n}=t;r(t,{code:{value:M.pySrc}}),!1!==await e(t)&&await w.call(M,{currentTarget:n})}}},code:{get:()=>M.pySrc,set:e=>{E||D.update([D.state.update({changes:{from:0,to:D.state.doc.length,insert:e}})])}},process:{value(e,t=!1){if(t)return B();const n=E,r=S;E=!0,S=e;const o=()=>{E=n,S=r};return M.handleEvent({currentTarget:null}).then(o,o)}}});const j=()=>{const t=new Event(`${s}-editor`,{bubbles:!0});e.dispatchEvent(t)};if(E)return await M.handleEvent({currentTarget:null}),void j();const R=e.getAttribute("target");if(R){if(T=document.getElementById(R)||document.querySelector(R),!T)throw new Error(`Unknown target ${R}`)}else T=document.createElement(`${s}-editor`),T.style.display="block",e.after(T);T.id||(T.id=p(s)),T.hasAttribute("exec-id")||T.setAttribute("exec-id",0),T.hasAttribute("root")||T.setAttribute("root",T.id);const[C,H,W]=k(M,s);C.dataset.env=e.hasAttribute("env")?$:i;const U=C.querySelector(`.${s}-editor-input > div`).attachShadow({mode:"open"});U.innerHTML="<style> :host { all: initial; }</style>",T.appendChild(C);const N=o(e.textContent).trim(),P=/^([ \t]+)/m.test(N)?RegExp.$1:" ",B=()=>W.click(),D=new c({extensions:[f.of(P),(new d).of(u()),v.of([...y,{key:"Ctrl-Enter",run:B,preventDefault:!0},{key:"Cmd-Enter",run:B,preventDefault:!0},{key:"Shift-Enter",run:B,preventDefault:!0},b]),a],foldGutter:!0,gutters:["CodeMirror-linenumbers","CodeMirror-foldgutter"],parent:U,doc:N});h.set(e,D),D.focus(),j()};let x=0,A=Promise.resolve();const $=()=>{x=0,S()},S=()=>{if(!x){x=setTimeout($,250);for(const[t,n]of e){const e=`script[type="${t}-editor"]`;for(const r of document.querySelectorAll(e))r.type+="-active",A=A.then((()=>E(r,t,n)))}return A}};new MutationObserver(S).observe(document,{childList:!0,subtree:!0});var L=S();export{L as default};
2
+ //# sourceMappingURL=py-editor-B257doLW.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"py-editor-Dil8sfoT.js","sources":["../src/plugins/py-editor.js"],"sourcesContent":["// PyScript py-editor plugin\nimport { Hook, XWorker, dedent, defineProperties } from \"polyscript/exports\";\nimport { TYPES, offline_interpreter, relative_url, stdlib } from \"../core.js\";\nimport { notify } from \"./error.js\";\nimport codemirror from \"./codemirror.js\";\n\nconst RUN_BUTTON = `<svg style=\"height:24px;width:24px\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M19,12a1,1,0,0,1-.55.89l-10,5A1,1,0,0,1,8,18a1,1,0,0,1-.53-.15A1,1,0,0,1,7,17V7a1,1,0,0,1,1.45-.89l10,5A1,1,0,0,1,19,12Z\" fill=\"#464646\"/></svg>`;\nconst STOP_BUTTON = `<svg style=\"height:24px;width:24px\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M7 7h10v10H7z\" style=\"fill:#464646;stroke:#464646;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-dasharray:none;paint-order:normal\"/></svg>`;\n\nlet id = 0;\nconst getID = (type) => `${type}-editor-${id++}`;\n\nconst envs = new Map();\nconst configs = new Map();\nconst editors = new WeakMap();\n\nconst hooks = {\n worker: {\n codeBeforeRun: () => stdlib,\n // works on both Pyodide and MicroPython\n onReady: ({ runAsync, io }, { sync }) => {\n io.stdout = io.buffered(sync.write);\n io.stderr = io.buffered(sync.writeErr);\n sync.revoke();\n sync.runAsync = runAsync;\n },\n },\n};\n\nconst validate = (config, result) => {\n if (typeof result === \"boolean\") throw `Invalid source: ${config}`;\n return result;\n};\n\nconst getRelatedScript = (target, type) => {\n const editor = target.closest(`.${type}-editor-box`);\n return editor?.parentNode?.previousElementSibling;\n};\n\nasync function execute({ currentTarget }) {\n const { env, pySrc, outDiv } = this;\n const hasRunButton = !!currentTarget;\n\n if (hasRunButton) {\n currentTarget.classList.add(\"running\");\n currentTarget.innerHTML = STOP_BUTTON;\n outDiv.innerHTML = \"\";\n }\n\n if (!envs.has(env)) {\n const srcLink = URL.createObjectURL(new Blob([\"\"]));\n const details = {\n type: this.interpreter,\n serviceWorker: this.serviceWorker,\n };\n const { config } = this;\n if (config) {\n // verify that config can be parsed and used\n try {\n details.configURL = relative_url(config);\n if (config.endsWith(\".toml\")) {\n const [{ parse }, toml] = await Promise.all([\n import(\n /* webpackIgnore: true */ \"../3rd-party/toml.js\"\n ),\n fetch(config).then((r) => r.ok && r.text()),\n ]);\n details.config = parse(validate(config, toml));\n } else if (config.endsWith(\".json\")) {\n const json = await fetch(config).then(\n (r) => r.ok && r.json(),\n );\n details.config = validate(config, json);\n } else {\n details.configURL = relative_url(\"./config.txt\");\n details.config = JSON.parse(config);\n }\n details.version = offline_interpreter(details.config);\n } catch (error) {\n notify(error);\n return;\n }\n } else {\n details.config = {};\n }\n\n const xworker = XWorker.call(new Hook(null, hooks), srcLink, details);\n\n // expose xworker like in terminal or other workers to allow\n // creation and destruction of editors on the fly\n if (hasRunButton) {\n for (const type of TYPES.keys()) {\n const script = getRelatedScript(currentTarget, type);\n if (script) {\n defineProperties(script, { xworker: { value: xworker } });\n break;\n }\n }\n }\n\n const { sync } = xworker;\n const { promise, resolve } = Promise.withResolvers();\n envs.set(env, promise);\n sync.revoke = () => {\n URL.revokeObjectURL(srcLink);\n resolve(xworker);\n };\n }\n\n // wait for the env then set the target div\n // before executing the current code\n return envs.get(env).then((xworker) => {\n xworker.onerror = ({ error }) => {\n if (hasRunButton) {\n outDiv.insertAdjacentHTML(\n \"beforeend\",\n `<span style='color:red'>${\n error.message || error\n }</span>\\n`,\n );\n }\n console.error(error);\n };\n\n const enable = () => {\n if (hasRunButton) {\n currentTarget.classList.remove(\"running\");\n currentTarget.innerHTML = RUN_BUTTON;\n }\n };\n const { sync } = xworker;\n sync.write = (str) => {\n if (hasRunButton) outDiv.innerText += `${str}\\n`;\n else console.log(str);\n };\n sync.writeErr = (str) => {\n if (hasRunButton) {\n outDiv.insertAdjacentHTML(\n \"beforeend\",\n `<span style='color:red'>${str}</span>\\n`,\n );\n } else {\n notify(str);\n console.error(str);\n }\n };\n sync.runAsync(pySrc).then(enable, enable);\n });\n}\n\nconst makeRunButton = (handler, type) => {\n const runButton = document.createElement(\"button\");\n runButton.className = `absolute ${type}-editor-run-button`;\n runButton.innerHTML = RUN_BUTTON;\n runButton.setAttribute(\"aria-label\", \"Python Script Run Button\");\n runButton.addEventListener(\"click\", async (event) => {\n if (\n runButton.classList.contains(\"running\") &&\n confirm(\"Stop evaluating this code?\")\n ) {\n const script = getRelatedScript(runButton, type);\n if (script) {\n const editor = editors.get(script);\n const content = editor.state.doc.toString();\n const clone = script.cloneNode(true);\n clone.type = `${type}-editor`;\n clone.textContent = content;\n script.xworker.terminate();\n script.nextElementSibling.remove();\n script.replaceWith(clone);\n editors.delete(script);\n }\n return;\n }\n runButton.blur();\n await handler.handleEvent(event);\n });\n return runButton;\n};\n\nconst makeEditorDiv = (handler, type) => {\n const editorDiv = document.createElement(\"div\");\n editorDiv.className = `${type}-editor-input`;\n editorDiv.setAttribute(\"aria-label\", \"Python Script Area\");\n\n const runButton = makeRunButton(handler, type);\n const editorShadowContainer = document.createElement(\"div\");\n\n // avoid outer elements intercepting key events (reveal as example)\n editorShadowContainer.addEventListener(\"keydown\", (event) => {\n event.stopPropagation();\n });\n\n editorDiv.append(runButton, editorShadowContainer);\n\n return editorDiv;\n};\n\nconst makeOutDiv = (type) => {\n const outDiv = document.createElement(\"div\");\n outDiv.className = `${type}-editor-output`;\n outDiv.id = `${getID(type)}-output`;\n return outDiv;\n};\n\nconst makeBoxDiv = (handler, type) => {\n const boxDiv = document.createElement(\"div\");\n boxDiv.className = `${type}-editor-box`;\n\n const editorDiv = makeEditorDiv(handler, type);\n const outDiv = makeOutDiv(type);\n boxDiv.append(editorDiv, outDiv);\n\n return [boxDiv, outDiv, editorDiv.querySelector(\"button\")];\n};\n\nconst init = async (script, type, interpreter) => {\n const [\n { basicSetup, EditorView },\n { Compartment },\n { python },\n { indentUnit },\n { keymap },\n { defaultKeymap, indentWithTab },\n ] = await Promise.all([\n codemirror.core,\n codemirror.state,\n codemirror.python,\n codemirror.language,\n codemirror.view,\n codemirror.commands,\n ]);\n\n let isSetup = script.hasAttribute(\"setup\");\n const hasConfig = script.hasAttribute(\"config\");\n const serviceWorker = script.getAttribute(\"service-worker\");\n const env = `${interpreter}-${script.getAttribute(\"env\") || getID(type)}`;\n\n // helps preventing too lazy ServiceWorker initialization on button run\n if (serviceWorker) {\n new XWorker(\"data:application/javascript,postMessage(0)\", {\n type: \"dummy\",\n serviceWorker,\n }).onmessage = ({ target }) => target.terminate();\n }\n\n if (hasConfig && configs.has(env)) {\n throw new SyntaxError(\n configs.get(env)\n ? `duplicated config for env: ${env}`\n : `unable to add a config to the env: ${env}`,\n );\n }\n\n configs.set(env, hasConfig);\n\n let source = script.textContent;\n\n // verify the src points to a valid file that can be parsed\n const { src } = script;\n if (src) {\n try {\n source = validate(\n src,\n await fetch(src).then((b) => b.ok && b.text()),\n );\n } catch (error) {\n notify(error);\n return;\n }\n }\n\n const context = {\n // allow the listener to be overridden at distance\n handleEvent: execute,\n serviceWorker,\n interpreter,\n env,\n config: hasConfig && script.getAttribute(\"config\"),\n get pySrc() {\n return isSetup ? source : editor.state.doc.toString();\n },\n get outDiv() {\n return isSetup ? null : outDiv;\n },\n };\n\n let target;\n defineProperties(script, {\n target: { get: () => target },\n handleEvent: {\n get: () => context.handleEvent,\n set: (callback) => {\n // do not bother with logic if it was set back as its original handler\n if (callback === execute) context.handleEvent = execute;\n // in every other case be sure that if the listener override returned\n // `false` nothing happens, otherwise keep doing what it always did\n else {\n context.handleEvent = async (event) => {\n // trap the currentTarget ASAP (if any)\n // otherwise it gets lost asynchronously\n const { currentTarget } = event;\n // augment a code snapshot before invoking the override\n defineProperties(event, {\n code: { value: context.pySrc },\n });\n // avoid executing the default handler if the override returned `false`\n if ((await callback(event)) !== false)\n await execute.call(context, { currentTarget });\n };\n }\n },\n },\n code: {\n get: () => context.pySrc,\n set: (insert) => {\n if (isSetup) return;\n editor.update([\n editor.state.update({\n changes: {\n from: 0,\n to: editor.state.doc.length,\n insert,\n },\n }),\n ]);\n },\n },\n process: {\n /**\n * Simulate a setup node overriding the source to evaluate.\n * @param {string} code the Python code to evaluate.\n * @param {boolean} asRunButtonAction invoke the `Run` button handler.\n * @returns {Promise<...>} fulfill once code has been evaluated.\n */\n value(code, asRunButtonAction = false) {\n if (asRunButtonAction) return listener();\n const wasSetup = isSetup;\n const wasSource = source;\n isSetup = true;\n source = code;\n const restore = () => {\n isSetup = wasSetup;\n source = wasSource;\n };\n return context\n .handleEvent({ currentTarget: null })\n .then(restore, restore);\n },\n },\n });\n\n const notifyEditor = () => {\n const event = new Event(`${type}-editor`, { bubbles: true });\n script.dispatchEvent(event);\n };\n\n if (isSetup) {\n await context.handleEvent({ currentTarget: null });\n notifyEditor();\n return;\n }\n\n const selector = script.getAttribute(\"target\");\n\n if (selector) {\n target =\n document.getElementById(selector) ||\n document.querySelector(selector);\n if (!target) throw new Error(`Unknown target ${selector}`);\n } else {\n target = document.createElement(`${type}-editor`);\n target.style.display = \"block\";\n script.after(target);\n }\n\n if (!target.id) target.id = getID(type);\n if (!target.hasAttribute(\"exec-id\")) target.setAttribute(\"exec-id\", 0);\n if (!target.hasAttribute(\"root\")) target.setAttribute(\"root\", target.id);\n\n // @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js\n const [boxDiv, outDiv, runButton] = makeBoxDiv(context, type);\n boxDiv.dataset.env = script.hasAttribute(\"env\") ? env : interpreter;\n\n const inputChild = boxDiv.querySelector(`.${type}-editor-input > div`);\n const parent = inputChild.attachShadow({ mode: \"open\" });\n // avoid inheriting styles from the outer component\n parent.innerHTML = `<style> :host { all: initial; }</style>`;\n\n target.appendChild(boxDiv);\n\n const doc = dedent(script.textContent).trim();\n\n // preserve user indentation, if any\n const indentation = /^([ \\t]+)/m.test(doc) ? RegExp.$1 : \" \";\n\n const listener = () => runButton.click();\n const editor = new EditorView({\n extensions: [\n indentUnit.of(indentation),\n new Compartment().of(python()),\n keymap.of([\n ...defaultKeymap,\n { key: \"Ctrl-Enter\", run: listener, preventDefault: true },\n { key: \"Cmd-Enter\", run: listener, preventDefault: true },\n { key: \"Shift-Enter\", run: listener, preventDefault: true },\n // @see https://codemirror.net/examples/tab/\n indentWithTab,\n ]),\n basicSetup,\n ],\n foldGutter: true,\n gutters: [\"CodeMirror-linenumbers\", \"CodeMirror-foldgutter\"],\n parent,\n doc,\n });\n\n editors.set(script, editor);\n editor.focus();\n notifyEditor();\n};\n\n// avoid too greedy MutationObserver operations at distance\nlet timeout = 0;\n\n// avoid delayed initialization\nlet queue = Promise.resolve();\n\n// reset interval value then check for new scripts\nconst resetTimeout = () => {\n timeout = 0;\n pyEditor();\n};\n\n// triggered both ASAP on the living DOM and via MutationObserver later\nconst pyEditor = () => {\n if (timeout) return;\n timeout = setTimeout(resetTimeout, 250);\n for (const [type, interpreter] of TYPES) {\n const selector = `script[type=\"${type}-editor\"]`;\n for (const script of document.querySelectorAll(selector)) {\n // avoid any further bootstrap by changing the type as active\n script.type += \"-active\";\n // don't await in here or multiple calls might happen\n // while the first script is being initialized\n queue = queue.then(() => init(script, type, interpreter));\n }\n }\n return queue;\n};\n\nnew MutationObserver(pyEditor).observe(document, {\n childList: true,\n subtree: true,\n});\n\n// try to check the current document ASAP\nexport default pyEditor();\n"],"names":["RUN_BUTTON","id","getID","type","envs","Map","configs","editors","WeakMap","hooks","worker","codeBeforeRun","stdlib","onReady","runAsync","io","sync","stdout","buffered","write","stderr","writeErr","revoke","validate","config","result","getRelatedScript","target","editor","closest","parentNode","previousElementSibling","async","execute","currentTarget","env","pySrc","outDiv","this","hasRunButton","classList","add","innerHTML","has","srcLink","URL","createObjectURL","Blob","details","interpreter","serviceWorker","configURL","relative_url","endsWith","parse","toml","Promise","all","import","fetch","then","r","ok","text","json","JSON","version","offline_interpreter","error","notify","xworker","XWorker","call","Hook","TYPES","keys","script","defineProperties","value","promise","resolve","withResolvers","set","revokeObjectURL","get","onerror","insertAdjacentHTML","message","console","enable","remove","str","innerText","log","makeEditorDiv","handler","editorDiv","document","createElement","className","setAttribute","runButton","addEventListener","event","contains","confirm","content","state","doc","toString","clone","cloneNode","textContent","terminate","nextElementSibling","replaceWith","delete","blur","handleEvent","makeRunButton","editorShadowContainer","stopPropagation","append","makeBoxDiv","boxDiv","makeOutDiv","querySelector","init","basicSetup","EditorView","Compartment","python","indentUnit","keymap","defaultKeymap","indentWithTab","codemirror","core","language","view","commands","isSetup","hasAttribute","hasConfig","getAttribute","onmessage","SyntaxError","source","src","b","context","callback","code","insert","update","changes","from","to","length","process","asRunButtonAction","listener","wasSetup","wasSource","restore","notifyEditor","Event","bubbles","dispatchEvent","selector","getElementById","Error","style","display","after","dataset","parent","attachShadow","mode","appendChild","dedent","trim","indentation","test","RegExp","$1","click","extensions","of","key","run","preventDefault","foldGutter","gutters","focus","timeout","queue","resetTimeout","pyEditor","setTimeout","querySelectorAll","MutationObserver","observe","childList","subtree","pyEditor$1"],"mappings":"4IAMA,MAAMA,EAAa,uPAGnB,IAAIC,EAAK,EACT,MAAMC,EAASC,GAAS,GAAGA,YAAeF,MAEpCG,EAAO,IAAIC,IACXC,EAAU,IAAID,IACdE,EAAU,IAAIC,QAEdC,EAAQ,CACVC,OAAQ,CACJC,cAAe,IAAMC,EAErBC,QAAS,EAAGC,WAAUC,OAAQC,WAC1BD,EAAGE,OAASF,EAAGG,SAASF,EAAKG,OAC7BJ,EAAGK,OAASL,EAAGG,SAASF,EAAKK,UAC7BL,EAAKM,SACLN,EAAKF,SAAWA,CAAQ,IAK9BS,EAAW,CAACC,EAAQC,KACtB,GAAsB,kBAAXA,EAAsB,KAAM,mBAAmBD,IAC1D,OAAOC,CAAM,EAGXC,EAAmB,CAACC,EAAQxB,KAC9B,MAAMyB,EAASD,EAAOE,QAAQ,IAAI1B,gBAClC,OAAOyB,GAAQE,YAAYC,sBAAsB,EAGrDC,eAAeC,GAAQC,cAAEA,IACrB,MAAMC,IAAEA,EAAGC,MAAEA,EAAKC,OAAEA,GAAWC,KACzBC,IAAiBL,EAQvB,GANIK,IACAL,EAAcM,UAAUC,IAAI,WAC5BP,EAAcQ,UAtCF,mQAuCZL,EAAOK,UAAY,KAGlBtC,EAAKuC,IAAIR,GAAM,CAChB,MAAMS,EAAUC,IAA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7B,EAAU8B,gBAAgB,GACnD,CAAEF,IAAK,cAAeC,IAAK7B,EAAU8B,gBAAgB,GAErDxD,IAEJP,GAEJgE,YAAY,EACZC,QAAS,CAAC,yBAA0B,yBACpCjB,SACApE,QAGJrG,EAAQ2E,IAAIN,EAAQhD,GACpBA,EAAOsK,QACP7B,GAAc,EAIlB,IAAI8B,EAAU,EAGVC,EAAQ5I,QAAQwB,UAGpB,MAAMqH,EAAe,KACjBF,EAAU,EACVG,GAAU,EAIRA,EAAW,KACb,IAAIH,EAAJ,CACAA,EAAUI,WAAWF,EAAc,KACnC,IAAK,MAAOlM,EAAM8C,KAAgByB,EAAO,CACrC,MAAM+F,EAAW,gBAAgBtK,aACjC,IAAK,MAAMyE,KAAUqB,SAASuG,iBAAiB/B,GAE3C7F,EAAOzE,MAAQ,UAGfiM,EAAQA,EAAMxI,MAAK,IAAMmE,EAAKnD,EAAQzE,EAAM8C,IAExD,CACI,OAAOmJ,CAZM,CAYD,EAGhB,IAAIK,iBAAiBH,GAAUI,QAAQzG,SAAU,CAC7C0G,WAAW,EACXC,SAAS,IAIb,IAAAC,EAAeP"}
1
+ {"version":3,"file":"py-editor-B257doLW.js","sources":["../src/plugins/py-editor.js"],"sourcesContent":["// PyScript py-editor plugin\nimport { Hook, XWorker, dedent, defineProperties } from \"polyscript/exports\";\nimport { TYPES, offline_interpreter, relative_url, stdlib } from \"../core.js\";\nimport { notify } from \"./error.js\";\nimport codemirror from \"./codemirror.js\";\n\nconst RUN_BUTTON = `<svg style=\"height:24px;width:24px\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M19,12a1,1,0,0,1-.55.89l-10,5A1,1,0,0,1,8,18a1,1,0,0,1-.53-.15A1,1,0,0,1,7,17V7a1,1,0,0,1,1.45-.89l10,5A1,1,0,0,1,19,12Z\" fill=\"#464646\"/></svg>`;\nconst STOP_BUTTON = `<svg style=\"height:24px;width:24px\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><path d=\"M7 7h10v10H7z\" style=\"fill:#464646;stroke:#464646;stroke-width:1;stroke-linecap:butt;stroke-linejoin:round;stroke-dasharray:none;paint-order:normal\"/></svg>`;\n\nlet id = 0;\nconst getID = (type) => `${type}-editor-${id++}`;\n\nconst envs = new Map();\nconst configs = new Map();\nconst editors = new WeakMap();\n\nconst hooks = {\n worker: {\n codeBeforeRun: () => stdlib,\n // works on both Pyodide and MicroPython\n onReady: ({ runAsync, io }, { sync }) => {\n io.stdout = io.buffered(sync.write);\n io.stderr = io.buffered(sync.writeErr);\n sync.revoke();\n sync.runAsync = runAsync;\n },\n },\n};\n\nconst validate = (config, result) => {\n if (typeof result === \"boolean\") throw `Invalid source: ${config}`;\n return result;\n};\n\nconst getRelatedScript = (target, type) => {\n const editor = target.closest(`.${type}-editor-box`);\n return editor?.parentNode?.previousElementSibling;\n};\n\nasync function execute({ currentTarget }) {\n const { env, pySrc, outDiv } = this;\n const hasRunButton = !!currentTarget;\n\n if (hasRunButton) {\n currentTarget.classList.add(\"running\");\n currentTarget.innerHTML = STOP_BUTTON;\n outDiv.innerHTML = \"\";\n }\n\n if (!envs.has(env)) {\n const srcLink = URL.createObjectURL(new Blob([\"\"]));\n const details = {\n type: this.interpreter,\n serviceWorker: this.serviceWorker,\n };\n const { config } = this;\n if (config) {\n // verify that config can be parsed and used\n try {\n details.configURL = relative_url(config);\n if (config.endsWith(\".toml\")) {\n const [{ parse }, toml] = await Promise.all([\n import(\n /* webpackIgnore: true */ \"../3rd-party/toml.js\"\n ),\n fetch(config).then((r) => r.ok && r.text()),\n ]);\n details.config = parse(validate(config, toml));\n } else if (config.endsWith(\".json\")) {\n const json = await fetch(config).then(\n (r) => r.ok && r.json(),\n );\n details.config = validate(config, json);\n } else {\n details.configURL = relative_url(\"./config.txt\");\n details.config = JSON.parse(config);\n }\n details.version = offline_interpreter(details.config);\n } catch (error) {\n notify(error);\n return;\n }\n } else {\n details.config = {};\n }\n\n const xworker = XWorker.call(new Hook(null, hooks), srcLink, details);\n\n // expose xworker like in terminal or other workers to allow\n // creation and destruction of editors on the fly\n if (hasRunButton) {\n for (const type of TYPES.keys()) {\n const script = getRelatedScript(currentTarget, type);\n if (script) {\n defineProperties(script, { xworker: { value: xworker } });\n break;\n }\n }\n }\n\n const { sync } = xworker;\n const { promise, resolve } = Promise.withResolvers();\n envs.set(env, promise);\n sync.revoke = () => {\n URL.revokeObjectURL(srcLink);\n resolve(xworker);\n };\n }\n\n // wait for the env then set the target div\n // before executing the current code\n return envs.get(env).then((xworker) => {\n xworker.onerror = ({ error }) => {\n if (hasRunButton) {\n outDiv.insertAdjacentHTML(\n \"beforeend\",\n `<span style='color:red'>${\n error.message || error\n }</span>\\n`,\n );\n }\n console.error(error);\n };\n\n const enable = () => {\n if (hasRunButton) {\n currentTarget.classList.remove(\"running\");\n currentTarget.innerHTML = RUN_BUTTON;\n }\n };\n const { sync } = xworker;\n sync.write = (str) => {\n if (hasRunButton) outDiv.innerText += `${str}\\n`;\n else console.log(str);\n };\n sync.writeErr = (str) => {\n if (hasRunButton) {\n outDiv.insertAdjacentHTML(\n \"beforeend\",\n `<span style='color:red'>${str}</span>\\n`,\n );\n } else {\n notify(str);\n console.error(str);\n }\n };\n sync.runAsync(pySrc).then(enable, enable);\n });\n}\n\nconst makeRunButton = (handler, type) => {\n const runButton = document.createElement(\"button\");\n runButton.className = `absolute ${type}-editor-run-button`;\n runButton.innerHTML = RUN_BUTTON;\n runButton.setAttribute(\"aria-label\", \"Python Script Run Button\");\n runButton.addEventListener(\"click\", async (event) => {\n if (\n runButton.classList.contains(\"running\") &&\n confirm(\"Stop evaluating this code?\")\n ) {\n const script = getRelatedScript(runButton, type);\n if (script) {\n const editor = editors.get(script);\n const content = editor.state.doc.toString();\n const clone = script.cloneNode(true);\n clone.type = `${type}-editor`;\n clone.textContent = content;\n script.xworker.terminate();\n script.nextElementSibling.remove();\n script.replaceWith(clone);\n editors.delete(script);\n }\n return;\n }\n runButton.blur();\n await handler.handleEvent(event);\n });\n return runButton;\n};\n\nconst makeEditorDiv = (handler, type) => {\n const editorDiv = document.createElement(\"div\");\n editorDiv.className = `${type}-editor-input`;\n editorDiv.setAttribute(\"aria-label\", \"Python Script Area\");\n\n const runButton = makeRunButton(handler, type);\n const editorShadowContainer = document.createElement(\"div\");\n\n // avoid outer elements intercepting key events (reveal as example)\n editorShadowContainer.addEventListener(\"keydown\", (event) => {\n event.stopPropagation();\n });\n\n editorDiv.append(runButton, editorShadowContainer);\n\n return editorDiv;\n};\n\nconst makeOutDiv = (type) => {\n const outDiv = document.createElement(\"div\");\n outDiv.className = `${type}-editor-output`;\n outDiv.id = `${getID(type)}-output`;\n return outDiv;\n};\n\nconst makeBoxDiv = (handler, type) => {\n const boxDiv = document.createElement(\"div\");\n boxDiv.className = `${type}-editor-box`;\n\n const editorDiv = makeEditorDiv(handler, type);\n const outDiv = makeOutDiv(type);\n boxDiv.append(editorDiv, outDiv);\n\n return [boxDiv, outDiv, editorDiv.querySelector(\"button\")];\n};\n\nconst init = async (script, type, interpreter) => {\n const [\n { basicSetup, EditorView },\n { Compartment },\n { python },\n { indentUnit },\n { keymap },\n { defaultKeymap, indentWithTab },\n ] = await Promise.all([\n codemirror.core,\n codemirror.state,\n codemirror.python,\n codemirror.language,\n codemirror.view,\n codemirror.commands,\n ]);\n\n let isSetup = script.hasAttribute(\"setup\");\n const hasConfig = script.hasAttribute(\"config\");\n const serviceWorker = script.getAttribute(\"service-worker\");\n const env = `${interpreter}-${script.getAttribute(\"env\") || getID(type)}`;\n\n // helps preventing too lazy ServiceWorker initialization on button run\n if (serviceWorker) {\n new XWorker(\"data:application/javascript,postMessage(0)\", {\n type: \"dummy\",\n serviceWorker,\n }).onmessage = ({ target }) => target.terminate();\n }\n\n if (hasConfig && configs.has(env)) {\n throw new SyntaxError(\n configs.get(env)\n ? `duplicated config for env: ${env}`\n : `unable to add a config to the env: ${env}`,\n );\n }\n\n configs.set(env, hasConfig);\n\n let source = script.textContent;\n\n // verify the src points to a valid file that can be parsed\n const { src } = script;\n if (src) {\n try {\n source = validate(\n src,\n await fetch(src).then((b) => b.ok && b.text()),\n );\n } catch (error) {\n notify(error);\n return;\n }\n }\n\n const context = {\n // allow the listener to be overridden at distance\n handleEvent: execute,\n serviceWorker,\n interpreter,\n env,\n config: hasConfig && script.getAttribute(\"config\"),\n get pySrc() {\n return isSetup ? source : editor.state.doc.toString();\n },\n get outDiv() {\n return isSetup ? null : outDiv;\n },\n };\n\n let target;\n defineProperties(script, {\n target: { get: () => target },\n handleEvent: {\n get: () => context.handleEvent,\n set: (callback) => {\n // do not bother with logic if it was set back as its original handler\n if (callback === execute) context.handleEvent = execute;\n // in every other case be sure that if the listener override returned\n // `false` nothing happens, otherwise keep doing what it always did\n else {\n context.handleEvent = async (event) => {\n // trap the currentTarget ASAP (if any)\n // otherwise it gets lost asynchronously\n const { currentTarget } = event;\n // augment a code snapshot before invoking the override\n defineProperties(event, {\n code: { value: context.pySrc },\n });\n // avoid executing the default handler if the override returned `false`\n if ((await callback(event)) !== false)\n await execute.call(context, { currentTarget });\n };\n }\n },\n },\n code: {\n get: () => context.pySrc,\n set: (insert) => {\n if (isSetup) return;\n editor.update([\n editor.state.update({\n changes: {\n from: 0,\n to: editor.state.doc.length,\n insert,\n },\n }),\n ]);\n },\n },\n process: {\n /**\n * Simulate a setup node overriding the source to evaluate.\n * @param {string} code the Python code to evaluate.\n * @param {boolean} asRunButtonAction invoke the `Run` button handler.\n * @returns {Promise<...>} fulfill once code has been evaluated.\n */\n value(code, asRunButtonAction = false) {\n if (asRunButtonAction) return listener();\n const wasSetup = isSetup;\n const wasSource = source;\n isSetup = true;\n source = code;\n const restore = () => {\n isSetup = wasSetup;\n source = wasSource;\n };\n return context\n .handleEvent({ currentTarget: null })\n .then(restore, restore);\n },\n },\n });\n\n const notifyEditor = () => {\n const event = new Event(`${type}-editor`, { bubbles: true });\n script.dispatchEvent(event);\n };\n\n if (isSetup) {\n await context.handleEvent({ currentTarget: null });\n notifyEditor();\n return;\n }\n\n const selector = script.getAttribute(\"target\");\n\n if (selector) {\n target =\n document.getElementById(selector) ||\n document.querySelector(selector);\n if (!target) throw new Error(`Unknown target ${selector}`);\n } else {\n target = document.createElement(`${type}-editor`);\n target.style.display = \"block\";\n script.after(target);\n }\n\n if (!target.id) target.id = getID(type);\n if (!target.hasAttribute(\"exec-id\")) target.setAttribute(\"exec-id\", 0);\n if (!target.hasAttribute(\"root\")) target.setAttribute(\"root\", target.id);\n\n // @see https://github.com/JeffersGlass/mkdocs-pyscript/blob/main/mkdocs_pyscript/js/makeblocks.js\n const [boxDiv, outDiv, runButton] = makeBoxDiv(context, type);\n boxDiv.dataset.env = script.hasAttribute(\"env\") ? env : interpreter;\n\n const inputChild = boxDiv.querySelector(`.${type}-editor-input > div`);\n const parent = inputChild.attachShadow({ mode: \"open\" });\n // avoid inheriting styles from the outer component\n parent.innerHTML = `<style> :host { all: initial; }</style>`;\n\n target.appendChild(boxDiv);\n\n const doc = dedent(script.textContent).trim();\n\n // preserve user indentation, if any\n const indentation = /^([ \\t]+)/m.test(doc) ? RegExp.$1 : \" \";\n\n const listener = () => runButton.click();\n const editor = new EditorView({\n extensions: [\n indentUnit.of(indentation),\n new Compartment().of(python()),\n keymap.of([\n ...defaultKeymap,\n { key: \"Ctrl-Enter\", run: listener, preventDefault: true },\n { key: \"Cmd-Enter\", run: listener, preventDefault: true },\n { key: \"Shift-Enter\", run: listener, preventDefault: true },\n // @see https://codemirror.net/examples/tab/\n indentWithTab,\n ]),\n basicSetup,\n ],\n foldGutter: true,\n gutters: [\"CodeMirror-linenumbers\", \"CodeMirror-foldgutter\"],\n parent,\n doc,\n });\n\n editors.set(script, editor);\n editor.focus();\n notifyEditor();\n};\n\n// avoid too greedy MutationObserver operations at distance\nlet timeout = 0;\n\n// avoid delayed initialization\nlet queue = Promise.resolve();\n\n// reset interval value then check for new scripts\nconst resetTimeout = () => {\n timeout = 0;\n pyEditor();\n};\n\n// triggered both ASAP on the living DOM and via MutationObserver later\nconst pyEditor = () => {\n if (timeout) return;\n timeout = setTimeout(resetTimeout, 250);\n for (const [type, interpreter] of TYPES) {\n const selector = `script[type=\"${type}-editor\"]`;\n for (const script of document.querySelectorAll(selector)) {\n // avoid any further bootstrap by changing the type as active\n script.type += \"-active\";\n // don't await in here or multiple calls might happen\n // while the first script is being initialized\n queue = queue.then(() => init(script, type, interpreter));\n }\n }\n return queue;\n};\n\nnew MutationObserver(pyEditor).observe(document, {\n childList: true,\n subtree: true,\n});\n\n// try to check the current document ASAP\nexport default pyEditor();\n"],"names":["RUN_BUTTON","id","getID","type","envs","Map","configs","editors","WeakMap","hooks","worker","codeBeforeRun","stdlib","onReady","runAsync","io","sync","stdout","buffered","write","stderr","writeErr","revoke","validate","config","result","getRelatedScript","target","editor","closest","parentNode","previousElementSibling","async","execute","currentTarget","env","pySrc","outDiv","this","hasRunButton","classList","add","innerHTML","has","srcLink","URL","createObjectURL","Blob","details","interpreter","serviceWorker","configURL","relative_url","endsWith","parse","toml","Promise","all","import","fetch","then","r","ok","text","json","JSON","version","offline_interpreter","error","notify","xworker","XWorker","call","Hook","TYPES","keys","script","defineProperties","value","promise","resolve","withResolvers","set","revokeObjectURL","get","onerror","insertAdjacentHTML","message","console","enable","remove","str","innerText","log","makeEditorDiv","handler","editorDiv","document","createElement","className","setAttribute","runButton","addEventListener","event","contains","confirm","content","state","doc","toString","clone","cloneNode","textContent","terminate","nextElementSibling","replaceWith","delete","blur","handleEvent","makeRunButton","editorShadowContainer","stopPropagation","append","makeBoxDiv","boxDiv","makeOutDiv","querySelector","init","basicSetup","EditorView","Compartment","python","indentUnit","keymap","defaultKeymap","indentWithTab","codemirror","core","language","view","commands","isSetup","hasAttribute","hasConfig","getAttribute","onmessage","SyntaxError","source","src","b","context","callback","code","insert","update","changes","from","to","length","process","asRunButtonAction","listener","wasSetup","wasSource","restore","notifyEditor","Event","bubbles","dispatchEvent","selector","getElementById","Error","style","display","after","dataset","parent","attachShadow","mode","appendChild","dedent","trim","indentation","test","RegExp","$1","click","extensions","of","key","run","preventDefault","foldGutter","gutters","focus","timeout","queue","resetTimeout","pyEditor","setTimeout","querySelectorAll","MutationObserver","observe","childList","subtree","pyEditor$1"],"mappings":"4IAMA,MAAMA,EAAa,uPAGnB,IAAIC,EAAK,EACT,MAAMC,EAASC,GAAS,GAAGA,YAAeF,MAEpCG,EAAO,IAAIC,IACXC,EAAU,IAAID,IACdE,EAAU,IAAIC,QAEdC,EAAQ,CACVC,OAAQ,CACJC,cAAe,IAAMC,EAErBC,QAAS,EAAGC,WAAUC,OAAQC,WAC1BD,EAAGE,OAASF,EAAGG,SAASF,EAAKG,OAC7BJ,EAAGK,OAASL,EAAGG,SAASF,EAAKK,UAC7BL,EAAKM,SACLN,EAAKF,SAAWA,CAAQ,IAK9BS,EAAW,CAACC,EAAQC,KACtB,GAAsB,kBAAXA,EAAsB,KAAM,mBAAmBD,IAC1D,OAAOC,CAAM,EAGXC,EAAmB,CAACC,EAAQxB,KAC9B,MAAMyB,EAASD,EAAOE,QAAQ,IAAI1B,gBAClC,OAAOyB,GAAQE,YAAYC,sBAAsB,EAGrDC,eAAeC,GAAQC,cAAEA,IACrB,MAAMC,IAAEA,EAAGC,MAAEA,EAAKC,OAAEA,GAAWC,KACzBC,IAAiBL,EAQvB,GANIK,IACAL,EAAcM,UAAUC,IAAI,WAC5BP,EAAcQ,UAtCF,mQAuCZL,EAAOK,UAAY,KAGlBtC,EAAKuC,IAAIR,GAAM,CAChB,MAAMS,EAAUC,IAAIC,gBAAgB,IAAIC,KAAK,CAAC,MACxCC,EAAU,CACZ7C,KAAMmC,KAAKW,YACXC,cAAeZ,KAAKY,gBAElB1B,OAAEA,GAAWc,KACnB,GAAId,EAEA,IAEI,GADAwB,EAAQG,UAAYC,EAAa5B,GAC7BA,EAAO6B,SAAS,SAAU,CAC1B,OAAOC,MAAEA,GAASC,SAAcC,QAAQC,IAAI,CACxCC,OAC8B,sBAE9BC,MAAMnC,GAAQo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AAU,EACVG,GAAU,EAIRA,EAAW,KACb,IAAIH,EAAJ,CACAA,EAAUI,WAAWF,EAAc,KACnC,IAAK,MAAOlM,EAAM8C,KAAgByB,EAAO,CACrC,MAAM+F,EAAW,gBAAgBtK,aACjC,IAAK,MAAMyE,KAAUqB,SAASuG,iBAAiB/B,GAE3C7F,EAAOzE,MAAQ,UAGfiM,EAAQA,EAAMxI,MAAK,IAAMmE,EAAKnD,EAAQzE,EAAM8C,IAExD,CACI,OAAOmJ,CAZM,CAYD,EAGhB,IAAIK,iBAAiBH,GAAUI,QAAQzG,SAAU,CAC7C0G,WAAW,EACXC,SAAS,IAIb,IAAAC,EAAeP"}
@@ -0,0 +1,2 @@
1
+ import{l as e,h as t,j as a,k as r,s as n,a as s,m as o,n as i}from"./core-CY38q33K.js";const c=i("py-game");let p=!0;const g={main:{onReady:async(a,i)=>{p&&(p=!1,console.warn("⚠️ EXPERIMENTAL `py-game` FEATURE"));let g={};if(i.hasAttribute("config")){const n=i.getAttribute("config"),{json:s,toml:o,text:p,url:m}=await r(n);if(s)g=JSON.parse(p);else if(o){const{parse:e}=await import("./toml-BLBSZ43A.js");g=e(p)}if(g.packages){const e=a.interpreter.pyimport("micropip");await e.install(g.packages,{keep_going:!0}),e.destroy()}await((a,...r)=>e(t.get(a),...r))("py-game",c,a.interpreter,g,m||location.href)}a.interpreter.registerJsModule("_pyscript",{PyWorker(){throw new Error("Unable to use PyWorker in py-game scripts")},js_import:(...e)=>Promise.all(e.map((e=>import(e)))),get target(){return i.id}}),await a.interpreter.runPythonAsync(n);let m=s(i.textContent);i.src&&(m=await fetch(i.src).then(o));const l=i.getAttribute("target")||"canvas",y=document.getElementById(l);a.interpreter.canvas.setCanvas2D(y);const f=new CustomEvent("py-game",{bubbles:!0,cancelable:!0,detail:{canvas:y,code:m,config:g,wrap:a}});i.dispatchEvent(f),f.defaultPrevented||await a.interpreter.runPythonAsync(m)}}};a("py-game",{config:{packages:["pygame-ce"]},configURL:new URL("./config.txt",location.href).href,interpreter:"pyodide",env:"py-game",hooks:g});
2
+ //# sourceMappingURL=py-game-COQWxJ4-.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"py-game-COQWxJ4-.js","sources":["../../../polyscript/esm/exports.js","../src/plugins/py-game.js"],"sourcesContent":["// this file simply exports enough stuff to allow\n// 3rd party libraries, including PyScript, to work\nimport { buffered } from './interpreter/_io.js';\nimport { createProgress } from './interpreter/_utils.js';\nimport { loadProgress as lP } from './interpreter/_python.js';\nimport { registry } from './interpreters.js';\n\nconst loadProgress = (type, ...rest) => lP(registry.get(type), ...rest);\n\nexport { buffered, createProgress, loadProgress };\nexport * from './index.js';\nexport * from './script-handler.js';\nexport * from './utils.js';\n","import { dedent, define, createProgress, loadProgress } from \"polyscript/exports\";\n\nimport { stdlib } from \"../core.js\";\nimport { configDetails } from \"../config.js\";\nimport { getText } from \"../fetch.js\";\n\nconst progress = createProgress('py-game');\n\nlet toBeWarned = true;\n\nconst hooks = {\n main: {\n onReady: async (wrap, script) => {\n if (toBeWarned) {\n toBeWarned = false;\n console.warn(\"⚠️ EXPERIMENTAL `py-game` FEATURE\");\n }\n\n let config = {};\n if (script.hasAttribute(\"config\")) {\n const value = script.getAttribute(\"config\");\n const { json, toml, text, url } = await configDetails(value);\n if (json) config = JSON.parse(text);\n else if (toml) {\n const { parse } = await import(\n /* webpackIgnore: true */ \"../3rd-party/toml.js\"\n );\n config = parse(text);\n }\n if (config.packages) {\n const micropip = wrap.interpreter.pyimport(\"micropip\");\n await micropip.install(config.packages, {\n keep_going: true,\n });\n micropip.destroy();\n }\n await loadProgress(\n 'py-game',\n progress,\n wrap.interpreter,\n config,\n url || location.href,\n );\n }\n\n wrap.interpreter.registerJsModule(\"_pyscript\", {\n PyWorker() {\n throw new Error(\n \"Unable to use PyWorker in py-game scripts\",\n );\n },\n js_import: (...urls) =>\n Promise.all(urls.map((url) => import(url))),\n get target() {\n return script.id;\n },\n });\n\n await wrap.interpreter.runPythonAsync(stdlib);\n\n let code = dedent(script.textContent);\n if (script.src) code = await fetch(script.src).then(getText);\n\n const target = script.getAttribute(\"target\") || \"canvas\";\n const canvas = document.getElementById(target);\n wrap.interpreter.canvas.setCanvas2D(canvas);\n\n // allow 3rd party to hook themselves right before\n // the code gets executed\n const event = new CustomEvent(\"py-game\", {\n bubbles: true,\n cancelable: true,\n detail: {\n canvas,\n code,\n config,\n wrap,\n },\n });\n script.dispatchEvent(event);\n // run only if the default was not prevented\n if (!event.defaultPrevented)\n await wrap.interpreter.runPythonAsync(code);\n },\n },\n};\n\ndefine(\"py-game\", {\n config: { packages: [\"pygame-ce\"] },\n configURL: new URL(\"./config.txt\", location.href).href,\n interpreter: \"pyodide\",\n env: \"py-game\",\n hooks,\n});\n"],"names":["progress","createProgress","toBeWarned","hooks","main","onReady","async","wrap","script","console","warn","config","hasAttribute","value","getAttribute","json","toml","text","url","configDetails","JSON","parse","import","packages","micropip","interpreter","pyimport","install","keep_going","destroy","type","rest","lP","registry","get","loadProgress","location","href","registerJsModule","PyWorker","Error","js_import","urls","Promise","all","map","target","id","runPythonAsync","stdlib","code","dedent","textContent","src","fetch","then","getText","canvas","document","getElementById","setCanvas2D","event","CustomEvent","bubbles","cancelable","detail","dispatchEvent","defaultPrevented","define","configURL","URL","env"],"mappings":"wFAOA,MCDMA,EAAWC,EAAe,WAEhC,IAAIC,GAAa,EAEjB,MAAMC,EAAQ,CACVC,KAAM,CACFC,QAASC,MAAOC,EAAMC,KACdN,IACAA,GAAa,EACbO,QAAQC,KAAK,sCAGjB,IAAIC,EAAS,CAAE,EACf,GAAIH,EAAOI,aAAa,UAAW,CAC/B,MAAMC,EAAQL,EAAOM,aAAa,WAC5BC,KAAEA,EAAIC,KAAEA,EAAIC,KAAEA,EAAIC,IAAEA,SAAcC,EAAcN,GACtD,GAAIE,EAAMJ,EAASS,KAAKC,MAAMJ,QACzB,GAAID,EAAM,CACX,MAAMK,MAAEA,SAAgBC,OACM,sBAE9BX,EAASU,EAAMJ,EACnC,CACgB,GAAIN,EAAOY,SAAU,CACjB,MAAMC,EAAWjB,EAAKkB,YAAYC,SAAS,kBACrCF,EAASG,QAAQhB,EAAOY,SAAU,CACpCK,YAAY,IAEhBJ,EAASK,SAC7B,MD5BqB,EAACC,KAASC,IAASC,EAAGC,EAASC,IAAIJ,MAAUC,GC6B5CI,CACF,UACAnC,EACAO,EAAKkB,YACLd,EACAO,GAAOkB,SAASC,KAEpC,CAEY9B,EAAKkB,YAAYa,iBAAiB,YAAa,CAC3C,QAAAC,GACI,MAAM,IAAIC,MACN,4CAEP,EACDC,UAAW,IAAIC,IACXC,QAAQC,IAAIF,EAAKG,KAAK3B,GAAQI,OAAOJ,MACzC,UAAI4B,GACA,OAAOtC,EAAOuC,EACjB,UAGCxC,EAAKkB,YAAYuB,eAAeC,GAEtC,IAAIC,EAAOC,EAAO3C,EAAO4C,aACrB5C,EAAO6C,MAAKH,QAAaI,MAAM9C,EAAO6C,KAAKE,KAAKC,IAEpD,MAAMV,EAAStC,EAAOM,aAAa,WAAa,SAC1C2C,EAASC,SAASC,eAAeb,GACvCvC,EAAKkB,YAAYgC,OAAOG,YAAYH,GAIpC,MAAMI,EAAQ,IAAIC,YAAY,UAAW,CACrCC,SAAS,EACTC,YAAY,EACZC,OAAQ,CACJR,SACAP,OACAvC,SACAJ,UAGRC,EAAO0D,cAAcL,GAEhBA,EAAMM,wBACD5D,EAAKkB,YAAYuB,eAAeE,EAAK,IAK3DkB,EAAO,UAAW,CACdzD,OAAQ,CAAEY,SAAU,CAAC,cACrB8C,UAAW,IAAIC,IAAI,eAAgBlC,SAASC,MAAMA,KAClDZ,YAAa,UACb8C,IAAK,UACLpE"}
@@ -1,2 +1,2 @@
1
- import{e,d as t}from"./core-DlvYU3OZ.js";const r=new WeakSet,n=({interpreter:e,io:t,run:r,type:n},{sync:i})=>{if("py"!==n||!i.is_pyterminal())return;r(["from polyscript import currentScript as _","__terminal__ = _.terminal","del _"].join(";"));let o="";const{pyterminal_read:a,pyterminal_write:d}=i,s=new TextDecoder,l={isatty:!1,write:e=>(o=s.decode(e),d(o),e.length)};t.stderr=e=>{d(String(e.message||e))},e.setStdout(l),e.setStderr(l),e.setStdin({isatty:!1,stdin:()=>a(o)})};var i=async i=>{const[{Terminal:o},{Readline:a},{FitAddon:d},{WebLinksAddon:s}]=await Promise.all([import("./xterm-7LwxAMsn.js"),import("./xterm-readline-e8QPhSrm.js"),import("./xterm_addon-fit--gyF3PcZ.js"),import("./xterm_addon-web-links-Cnej-nJ6.js")]),l=new a,m=e=>{let r=i;const n=i.getAttribute("target");if(n){if(r=document.getElementById(n)||document.querySelector(n),!r)throw new Error(`Unknown target ${n}`)}else r=document.createElement("py-terminal"),r.style.display="block",i.after(r);const a=new o({theme:{background:"#191A19",foreground:"#F5F2E7"},...e}),m=new d;return a.loadAddon(m),a.loadAddon(l),a.loadAddon(new s),a.open(r),m.fit(),a.focus(),t(i,{terminal:{value:a},process:{value:async e=>{for(const t of e.split(/(?:\r\n|\r|\n)/)){a.paste(`${t}`),a.write("\r\n");do{await new Promise((e=>setTimeout(e,0)))}while(!l.activeRead?.resolve);l.activeRead.resolve(t)}}}}),a};if(i.hasAttribute("worker")){e.main.onWorker.add((function t(n,i){r.has(i)||(r.add(i),e.main.onWorker.delete(t),m({disableStdin:!1,cursorBlink:!0,cursorStyle:"block",lineHeight:1.2}),i.sync.is_pyterminal=()=>!0,i.sync.pyterminal_read=l.read.bind(l),i.sync.pyterminal_write=l.write.bind(l))})),e.worker.onReady.add(n);const t=["import builtins as _b","from pyscript import sync as _s","_b.input = _s.pyterminal_read","del _b","del _s"].join("\n");e.worker.codeBeforeRun.add(t),e.worker.codeBeforeRunAsync.add(t)}else e.main.onReady.add((function t({interpreter:r,io:n,run:i,type:o}){if("py"!==o)return;console.warn("py-terminal is read only on main thread"),e.main.onReady.delete(t),globalThis.__py_terminal__=m({disableStdin:!0,cursorBlink:!1,cursorStyle:"underline"}),i("from js import __py_terminal__ as __terminal__"),delete globalThis.__py_terminal__,n.stderr=e=>{l.write(String(e.message||e))};let a="";const d=new TextDecoder,s={isatty:!1,write:e=>(a=d.decode(e),l.write(a),e.length)};r.setStdout(s),r.setStderr(s),r.setStdin({isatty:!1,stdin:()=>l.read(a)})}))};export{i as default};
2
- //# sourceMappingURL=py-DZ9P39Fr.js.map
1
+ import{e,d as t}from"./core-CY38q33K.js";const r=new WeakSet,n=({interpreter:e,io:t,run:r,type:n},{sync:i})=>{if("py"!==n||!i.is_pyterminal())return;r(["from polyscript import currentScript as _","__terminal__ = _.terminal","del _"].join(";"));let o="";const{pyterminal_read:a,pyterminal_write:d}=i,s=new TextDecoder,l={isatty:!1,write:e=>(o=s.decode(e),d(o),e.length)};t.stderr=e=>{d(String(e.message||e))},e.setStdout(l),e.setStderr(l),e.setStdin({isatty:!1,stdin:()=>a(o)})};var i=async i=>{const[{Terminal:o},{Readline:a},{FitAddon:d},{WebLinksAddon:s}]=await Promise.all([import("./xterm-7LwxAMsn.js"),import("./xterm-readline-e8QPhSrm.js"),import("./xterm_addon-fit--gyF3PcZ.js"),import("./xterm_addon-web-links-Cnej-nJ6.js")]),l=new a,m=e=>{let r=i;const n=i.getAttribute("target");if(n){if(r=document.getElementById(n)||document.querySelector(n),!r)throw new Error(`Unknown target ${n}`)}else r=document.createElement("py-terminal"),r.style.display="block",i.after(r);const a=new o({theme:{background:"#191A19",foreground:"#F5F2E7"},...e}),m=new d;return a.loadAddon(m),a.loadAddon(l),a.loadAddon(new s),a.open(r),m.fit(),a.focus(),t(i,{terminal:{value:a},process:{value:async e=>{for(const t of e.split(/(?:\r\n|\r|\n)/)){a.paste(`${t}`),a.write("\r\n");do{await new Promise((e=>setTimeout(e,0)))}while(!l.activeRead?.resolve);l.activeRead.resolve(t)}}}}),a};if(i.hasAttribute("worker")){e.main.onWorker.add((function t(n,i){r.has(i)||(r.add(i),e.main.onWorker.delete(t),m({disableStdin:!1,cursorBlink:!0,cursorStyle:"block",lineHeight:1.2}),i.sync.is_pyterminal=()=>!0,i.sync.pyterminal_read=l.read.bind(l),i.sync.pyterminal_write=l.write.bind(l))})),e.worker.onReady.add(n);const t=["import builtins as _b","from pyscript import sync as _s","_b.input = _s.pyterminal_read","del _b","del _s"].join("\n");e.worker.codeBeforeRun.add(t),e.worker.codeBeforeRunAsync.add(t)}else e.main.onReady.add((function t({interpreter:r,io:n,run:i,type:o}){if("py"!==o)return;console.warn("py-terminal is read only on main thread"),e.main.onReady.delete(t),globalThis.__py_terminal__=m({disableStdin:!0,cursorBlink:!1,cursorStyle:"underline"}),i("from js import __py_terminal__ as __terminal__"),delete globalThis.__py_terminal__,n.stderr=e=>{l.write(String(e.message||e))};let a="";const d=new TextDecoder,s={isatty:!1,write:e=>(a=d.decode(e),l.write(a),e.length)};r.setStdout(s),r.setStderr(s),r.setStdin({isatty:!1,stdin:()=>l.read(a)})}))};export{i as default};
2
+ //# sourceMappingURL=py-hOIacH0d.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"py-DZ9P39Fr.js","sources":["../src/plugins/py-terminal/py.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { defineProperties } from \"polyscript/exports\";\nimport { hooks } from \"../../core.js\";\n\nconst bootstrapped = new WeakSet();\n\n// this callback will be serialized as string and it never needs\n// to be invoked multiple times. Each xworker here is bootstrapped\n// only once thanks to the `sync.is_pyterminal()` check.\nconst workerReady = ({ interpreter, io, run, type }, { sync }) => {\n if (type !== \"py\" || !sync.is_pyterminal()) return;\n\n run(\n [\n \"from polyscript import currentScript as _\",\n \"__terminal__ = _.terminal\",\n \"del _\",\n ].join(\";\"),\n );\n\n let data = \"\";\n const { pyterminal_read, pyterminal_write } = sync;\n const decoder = new TextDecoder();\n const generic = {\n isatty: false,\n write(buffer) {\n data = decoder.decode(buffer);\n pyterminal_write(data);\n return buffer.length;\n },\n };\n\n io.stderr = (error) => {\n pyterminal_write(String(error.message || error));\n };\n\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: false,\n stdin: () => pyterminal_read(data),\n });\n};\n\nexport default async (element) => {\n // lazy load these only when a valid terminal is found\n const [{ Terminal }, { Readline }, { FitAddon }, { WebLinksAddon }] =\n await Promise.all([\n import(/* webpackIgnore: true */ \"../../3rd-party/xterm.js\"),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm-readline.js\"\n ),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-fit.js\"\n ),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-web-links.js\"\n ),\n ]);\n\n const readline = new Readline();\n\n // common main thread initialization for both worker\n // or main case, bootstrapping the terminal on its target\n const init = (options) => {\n let target = element;\n const selector = element.getAttribute(\"target\");\n if (selector) {\n target =\n document.getElementById(selector) ||\n document.querySelector(selector);\n if (!target) throw new Error(`Unknown target ${selector}`);\n } else {\n target = document.createElement(\"py-terminal\");\n target.style.display = \"block\";\n element.after(target);\n }\n const terminal = new Terminal({\n theme: {\n background: \"#191A19\",\n foreground: \"#F5F2E7\",\n },\n ...options,\n });\n const fitAddon = new FitAddon();\n terminal.loadAddon(fitAddon);\n terminal.loadAddon(readline);\n terminal.loadAddon(new WebLinksAddon());\n terminal.open(target);\n fitAddon.fit();\n terminal.focus();\n defineProperties(element, {\n terminal: { value: terminal },\n process: {\n value: async (code) => {\n for (const line of code.split(/(?:\\r\\n|\\r|\\n)/)) {\n terminal.paste(`${line}`);\n terminal.write(\"\\r\\n\");\n do {\n await new Promise((resolve) =>\n setTimeout(resolve, 0),\n );\n } while (!readline.activeRead?.resolve);\n readline.activeRead.resolve(line);\n }\n },\n },\n });\n return terminal;\n };\n\n // branch logic for the worker\n if (element.hasAttribute(\"worker\")) {\n // add a hook on the main thread to setup all sync helpers\n // also bootstrapping the XTerm target on main *BUT* ...\n hooks.main.onWorker.add(function worker(_, xworker) {\n // ... as multiple workers will add multiple callbacks\n // be sure no xworker is ever initialized twice!\n if (bootstrapped.has(xworker)) return;\n bootstrapped.add(xworker);\n\n // still cleanup this callback for future scripts/workers\n hooks.main.onWorker.delete(worker);\n\n init({\n disableStdin: false,\n cursorBlink: true,\n cursorStyle: \"block\",\n lineHeight: 1.2,\n });\n\n xworker.sync.is_pyterminal = () => true;\n xworker.sync.pyterminal_read = readline.read.bind(readline);\n xworker.sync.pyterminal_write = readline.write.bind(readline);\n });\n\n // setup remote thread JS/Python code for whenever the\n // worker is ready to become a terminal\n hooks.worker.onReady.add(workerReady);\n\n // @see https://github.com/pyscript/pyscript/issues/2246\n const patchInput = [\n \"import builtins as _b\",\n \"from pyscript import sync as _s\",\n \"_b.input = _s.pyterminal_read\",\n \"del _b\",\n \"del _s\",\n ].join(\"\\n\");\n\n hooks.worker.codeBeforeRun.add(patchInput);\n hooks.worker.codeBeforeRunAsync.add(patchInput);\n } else {\n // in the main case, just bootstrap XTerm without\n // allowing any input as that's not possible / awkward\n hooks.main.onReady.add(function main({ interpreter, io, run, type }) {\n if (type !== \"py\") return;\n\n console.warn(\"py-terminal is read only on main thread\");\n hooks.main.onReady.delete(main);\n\n // on main, it's easy to trash and clean the current terminal\n globalThis.__py_terminal__ = init({\n disableStdin: true,\n cursorBlink: false,\n cursorStyle: \"underline\",\n });\n run(\"from js import __py_terminal__ as __terminal__\");\n delete globalThis.__py_terminal__;\n\n io.stderr = (error) => {\n readline.write(String(error.message || error));\n };\n\n let data = \"\";\n const decoder = new TextDecoder();\n const generic = {\n isatty: false,\n write(buffer) {\n data = decoder.decode(buffer);\n readline.write(data);\n return buffer.length;\n },\n };\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: false,\n stdin: () => readline.read(data),\n });\n });\n }\n};\n"],"names":["bootstrapped","WeakSet","workerReady","interpreter","io","run","type","sync","is_pyterminal","join","data","pyterminal_read","pyterminal_write","decoder","TextDecoder","generic","isatty","write","buffer","decode","length","stderr","error","String","message","setStdout","setStderr","setStdin","stdin","py","async","element","Terminal","Readline","FitAddon","WebLinksAddon","Promise","all","import","readline","init","options","target","selector","getAttribute","document","getElementById","querySelector","Error","createElement","style","display","after","terminal","theme","background","foreground","fitAddon","loadAddon","open","fit","focus","defineProperties","value","process","code","line","split","paste","resolve","setTimeout","activeRead","hasAttribute","hooks","main","onWorker","add","worker","_","xworker","has","delete","disableStdin","cursorBlink","cursorStyle","lineHeight","read","bind","onReady","patchInput","codeBeforeRun","codeBeforeRunAsync","console","warn","globalThis","__py_terminal__"],"mappings":"yCAIA,MAAMA,EAAe,IAAIC,QAKnBC,EAAc,EAAGC,cAAaC,KAAIC,MAAKC,SAAUC,WACnD,GAAa,OAATD,IAAkBC,EAAKC,gBAAiB,OAE5CH,EACI,CACI,4CACA,4BACA,SACFI,KAAK,MAGX,IAAIC,EAAO,GACX,MAAMC,gBAAEA,EAAeC,iBAAEA,GAAqBL,EACxCM,EAAU,IAAIC,YACdC,EAAU,CACZC,QAAQ,EACRC,MAAMC,IACFR,EAAOG,EAAQM,OAAOD,GACtBN,EAAiBF,GACVQ,EAAOE,SAItBhB,EAAGiB,OAAUC,IACTV,EAAiBW,OAAOD,EAAME,SAAWF,GAAO,EAGpDnB,EAAYsB,UAAUV,GACtBZ,EAAYuB,UAAUX,GACtBZ,EAAYwB,SAAS,CACjBX,QAAQ,EACRY,MAAO,IAAMjB,EAAgBD,IAC/B,EAGN,IAAemB,EAAAC,MAAOC,IAElB,OAAOC,SAAEA,IAAYC,SAAEA,IAAYC,SAAEA,IAAYC,cAAEA,UACzCC,QAAQC,IAAI,CACdC,OAAiC,uBACjCA,OAC8B,gCAE9BA,OAC8B,iCAE9BA,OAC8B,yCAIhCC,EAAW,IAAIN,EAIfO,EAAQC,IACV,IAAIC,EAASX,EACb,MAAMY,EAAWZ,EAAQa,aAAa,UACtC,GAAID,GAIA,GAHAD,EACIG,SAASC,eAAeH,IACxBE,SAASE,cAAcJ,IACtBD,EAAQ,MAAM,IAAIM,MAAM,kBAAkBL,UAE/CD,EAASG,SAASI,cAAc,eAChCP,EAAOQ,MAAMC,QAAU,QACvBpB,EAAQqB,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zD,GAAa,OAATA,EAAe,OAEnBsF,QAAQC,KAAK,2CACbpB,EAAMC,KAAKc,QAAQP,OAAOP,GAG1BoB,WAAWC,gBAAkBvD,EAAK,CAC9B0C,cAAc,EACdC,aAAa,EACbC,YAAa,cAEjB/E,EAAI,yDACGyF,WAAWC,gBAElB3F,EAAGiB,OAAUC,IACTiB,EAAStB,MAAMM,OAAOD,EAAME,SAAWF,GAAO,EAGlD,IAAIZ,EAAO,GACX,MAAMG,EAAU,IAAIC,YACdC,EAAU,CACZC,QAAQ,EACRC,MAAMC,IACFR,EAAOG,EAAQM,OAAOD,GACtBqB,EAAStB,MAAMP,GACRQ,EAAOE,SAGtBjB,EAAYsB,UAAUV,GACtBZ,EAAYuB,UAAUX,GACtBZ,EAAYwB,SAAS,CACjBX,QAAQ,EACRY,MAAO,IAAMW,EAAS+C,KAAK5E,IAE3C,GACA"}
1
+ {"version":3,"file":"py-hOIacH0d.js","sources":["../src/plugins/py-terminal/py.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { defineProperties } from \"polyscript/exports\";\nimport { hooks } from \"../../core.js\";\n\nconst bootstrapped = new WeakSet();\n\n// this callback will be serialized as string and it never needs\n// to be invoked multiple times. Each xworker here is bootstrapped\n// only once thanks to the `sync.is_pyterminal()` check.\nconst workerReady = ({ interpreter, io, run, type }, { sync }) => {\n if (type !== \"py\" || !sync.is_pyterminal()) return;\n\n run(\n [\n \"from polyscript import currentScript as _\",\n \"__terminal__ = _.terminal\",\n \"del _\",\n ].join(\";\"),\n );\n\n let data = \"\";\n const { pyterminal_read, pyterminal_write } = sync;\n const decoder = new TextDecoder();\n const generic = {\n isatty: false,\n write(buffer) {\n data = decoder.decode(buffer);\n pyterminal_write(data);\n return buffer.length;\n },\n };\n\n io.stderr = (error) => {\n pyterminal_write(String(error.message || error));\n };\n\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: false,\n stdin: () => pyterminal_read(data),\n });\n};\n\nexport default async (element) => {\n // lazy load these only when a valid terminal is found\n const [{ Terminal }, { Readline }, { FitAddon }, { WebLinksAddon }] =\n await Promise.all([\n import(/* webpackIgnore: true */ \"../../3rd-party/xterm.js\"),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm-readline.js\"\n ),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-fit.js\"\n ),\n import(\n /* webpackIgnore: true */ \"../../3rd-party/xterm_addon-web-links.js\"\n ),\n ]);\n\n const readline = new Readline();\n\n // common main thread initialization for both worker\n // or main case, bootstrapping the terminal on its target\n const init = (options) => {\n let target = element;\n const selector = element.getAttribute(\"target\");\n if (selector) {\n target =\n document.getElementById(selector) ||\n document.querySelector(selector);\n if (!target) throw new Error(`Unknown target ${selector}`);\n } else {\n target = document.createElement(\"py-terminal\");\n target.style.display = \"block\";\n element.after(target);\n }\n const terminal = new Terminal({\n theme: {\n background: \"#191A19\",\n foreground: \"#F5F2E7\",\n },\n ...options,\n });\n const fitAddon = new FitAddon();\n terminal.loadAddon(fitAddon);\n terminal.loadAddon(readline);\n terminal.loadAddon(new WebLinksAddon());\n terminal.open(target);\n fitAddon.fit();\n terminal.focus();\n defineProperties(element, {\n terminal: { value: terminal },\n process: {\n value: async (code) => {\n for (const line of code.split(/(?:\\r\\n|\\r|\\n)/)) {\n terminal.paste(`${line}`);\n terminal.write(\"\\r\\n\");\n do {\n await new Promise((resolve) =>\n setTimeout(resolve, 0),\n );\n } while (!readline.activeRead?.resolve);\n readline.activeRead.resolve(line);\n }\n },\n },\n });\n return terminal;\n };\n\n // branch logic for the worker\n if (element.hasAttribute(\"worker\")) {\n // add a hook on the main thread to setup all sync helpers\n // also bootstrapping the XTerm target on main *BUT* ...\n hooks.main.onWorker.add(function worker(_, xworker) {\n // ... as multiple workers will add multiple callbacks\n // be sure no xworker is ever initialized twice!\n if (bootstrapped.has(xworker)) return;\n bootstrapped.add(xworker);\n\n // still cleanup this callback for future scripts/workers\n hooks.main.onWorker.delete(worker);\n\n init({\n disableStdin: false,\n cursorBlink: true,\n cursorStyle: \"block\",\n lineHeight: 1.2,\n });\n\n xworker.sync.is_pyterminal = () => true;\n xworker.sync.pyterminal_read = readline.read.bind(readline);\n xworker.sync.pyterminal_write = readline.write.bind(readline);\n });\n\n // setup remote thread JS/Python code for whenever the\n // worker is ready to become a terminal\n hooks.worker.onReady.add(workerReady);\n\n // @see https://github.com/pyscript/pyscript/issues/2246\n const patchInput = [\n \"import builtins as _b\",\n \"from pyscript import sync as _s\",\n \"_b.input = _s.pyterminal_read\",\n \"del _b\",\n \"del _s\",\n ].join(\"\\n\");\n\n hooks.worker.codeBeforeRun.add(patchInput);\n hooks.worker.codeBeforeRunAsync.add(patchInput);\n } else {\n // in the main case, just bootstrap XTerm without\n // allowing any input as that's not possible / awkward\n hooks.main.onReady.add(function main({ interpreter, io, run, type }) {\n if (type !== \"py\") return;\n\n console.warn(\"py-terminal is read only on main thread\");\n hooks.main.onReady.delete(main);\n\n // on main, it's easy to trash and clean the current terminal\n globalThis.__py_terminal__ = init({\n disableStdin: true,\n cursorBlink: false,\n cursorStyle: \"underline\",\n });\n run(\"from js import __py_terminal__ as __terminal__\");\n delete globalThis.__py_terminal__;\n\n io.stderr = (error) => {\n readline.write(String(error.message || error));\n };\n\n let data = \"\";\n const decoder = new TextDecoder();\n const generic = {\n isatty: false,\n write(buffer) {\n data = decoder.decode(buffer);\n readline.write(data);\n return buffer.length;\n },\n };\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: false,\n stdin: () => readline.read(data),\n });\n });\n }\n};\n"],"names":["bootstrapped","WeakSet","workerReady","interpreter","io","run","type","sync","is_pyterminal","join","data","pyterminal_read","pyterminal_write","decoder","TextDecoder","generic","isatty","write","buffer","decode","length","stderr","error","String","message","setStdout","setStderr","setStdin","stdin","py","async","element","Terminal","Readline","FitAddon","WebLinksAddon","Promise","all","import","readline","init","options","target","selector","getAttribute","document","getElementById","querySelector","Error","createElement","style","display","after","terminal","theme","background","foreground","fitAddon","loadAddon","open","fit","focus","defineProperties","value","process","code","line","split","paste","resolve","setTimeout","activeRead","hasAttribute","hooks","main","onWorker","add","worker","_","xworker","has","delete","disableStdin","cursorBlink","cursorStyle","lineHeight","read","bind","onReady","patchInput","codeBeforeRun","codeBeforeRunAsync","console","warn","globalThis","__py_terminal__"],"mappings":"yCAIA,MAAMA,EAAe,IAAIC,QAKnBC,EAAc,EAAGC,cAAaC,KAAIC,MAAKC,SAAUC,WACnD,GAAa,OAATD,IAAkBC,EAAKC,gBAAiB,OAE5CH,EACI,CACI,4CACA,4BACA,SACFI,KAAK,MAGX,IAAIC,EAAO,GACX,MAAMC,gBAAEA,EAAeC,iBAAEA,GAAqBL,EACxCM,EAAU,IAAIC,YACdC,EAAU,CACZC,QAAQ,EACRC,MAAMC,IACFR,EAAOG,EAAQM,OAAOD,GACtBN,EAAiBF,GACVQ,EAAOE,SAItBhB,EAAGiB,OAAUC,IACTV,EAAiBW,OAAOD,EAAME,SAAWF,GAAO,EAGpDnB,EAAYsB,UAAUV,GACtBZ,EAAYuB,UAAUX,GACtBZ,EAAYwB,SAAS,CACjBX,QAAQ,EACRY,MAAO,IAAMjB,EAAgBD,IAC/B,EAGN,IAAemB,EAAAC,MAAOC,IAElB,OAAOC,SAAEA,IAAYC,SAAEA,IAAYC,SAAEA,IAAYC,cAAEA,UACzCC,QAAQC,IAAI,CACdC,OAAiC,uBACjCA,OAC8B,gCAE9BA,OAC8B,iCAE9BA,OAC8B,yCAIhCC,EAAW,IAAIN,EAIfO,EAAQC,IACV,IAAIC,EAASX,EACb,MAAMY,EAAWZ,EAAQa,aAAa,UACtC,GAAID,GAIA,GAHAD,EACIG,SAASC,eAAeH,IACxBE,SAASE,cAAcJ,IACtBD,EAAQ,MAAM,IAAIM,MAAM,kBAAkBL,UAE/CD,EAASG,SAASI,cAAc,eAChCP,EAAOQ,MAAMC,QAAU,QACvBpB,EAAQqB,MAAMV,GAElB,MAAMW,EAAW,IAAIrB,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QC,KAAK,2CACbpB,EAAMC,KAAKc,QAAQP,OAAOP,GAG1BoB,WAAWC,gBAAkBvD,EAAK,CAC9B0C,cAAc,EACdC,aAAa,EACbC,YAAa,cAEjB/E,EAAI,yDACGyF,WAAWC,gBAElB3F,EAAGiB,OAAUC,IACTiB,EAAStB,MAAMM,OAAOD,EAAME,SAAWF,GAAO,EAGlD,IAAIZ,EAAO,GACX,MAAMG,EAAU,IAAIC,YACdC,EAAU,CACZC,QAAQ,EACRC,MAAMC,IACFR,EAAOG,EAAQM,OAAOD,GACtBqB,EAAStB,MAAMP,GACRQ,EAAOE,SAGtBjB,EAAYsB,UAAUV,GACtBZ,EAAYuB,UAAUX,GACtBZ,EAAYwB,SAAS,CACjBX,QAAQ,EACRY,MAAO,IAAMW,EAAS+C,KAAK5E,IAE3C,GACA"}
@@ -1,2 +1,2 @@
1
- import{T as t,h as e,r}from"./core-DlvYU3OZ.js";import{notify as s}from"./error-CGGNJnMJ.js";const o=[],a=new WeakSet,n=t=>{throw s(t),new Error(t)},m=({attributes:{worker:t}})=>!t;let c=!0;for(const s of t.keys()){const t=`script[type="${s}"][terminal],${s}-script[terminal]`;o.push(t),e.set(t,(async t=>{const e=document.querySelectorAll(o.join(","));if([].filter.call(e,m).length>1&&n("You can use at most 1 main terminal"),c&&(c=!1,document.head.append(Object.assign(document.createElement("link"),{rel:"stylesheet",href:r("./xterm.css",import.meta.url)}))),a.has(t))return;a.add(t);const i=e=>e.default(t);"mpy"===s?await import("./mpy-49r-ARjR.js").then(i):await import("./py-DZ9P39Fr.js").then(i)}))}
2
- //# sourceMappingURL=py-terminal-D4qhEdOa.js.map
1
+ import{T as t,b as e,r}from"./core-CY38q33K.js";import{notify as s}from"./error-BnJRaZX6.js";const o=[],a=new WeakSet,n=t=>{throw s(t),new Error(t)},m=({attributes:{worker:t}})=>!t;let c=!0;for(const s of t.keys()){const t=`script[type="${s}"][terminal],${s}-script[terminal]`;o.push(t),e.set(t,(async t=>{const e=document.querySelectorAll(o.join(","));if([].filter.call(e,m).length>1&&n("You can use at most 1 main terminal"),c&&(c=!1,document.head.append(Object.assign(document.createElement("link"),{rel:"stylesheet",href:r("./xterm.css",import.meta.url)}))),a.has(t))return;a.add(t);const i=e=>e.default(t);"mpy"===s?await import("./mpy-CcFbuP4l.js").then(i):await import("./py-hOIacH0d.js").then(i)}))}
2
+ //# sourceMappingURL=py-terminal-flqGFKNk.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"py-terminal-D4qhEdOa.js","sources":["../src/plugins/py-terminal.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { TYPES, relative_url } from \"../core.js\";\nimport { notify } from \"./error.js\";\nimport { customObserver } from \"polyscript/exports\";\n\n// will contain all valid selectors\nconst SELECTORS = [];\n\n// avoid processing same elements twice\nconst processed = new WeakSet();\n\n// show the error on main and\n// stops the module from keep executing\nconst notifyAndThrow = (message) => {\n notify(message);\n throw new Error(message);\n};\n\nconst onceOnMain = ({ attributes: { worker } }) => !worker;\n\nlet addStyle = true;\n\nfor (const type of TYPES.keys()) {\n const selector = `script[type=\"${type}\"][terminal],${type}-script[terminal]`;\n SELECTORS.push(selector);\n customObserver.set(selector, async (element) => {\n // we currently support only one terminal on main as in \"classic\"\n const terminals = document.querySelectorAll(SELECTORS.join(\",\"));\n if ([].filter.call(terminals, onceOnMain).length > 1)\n notifyAndThrow(\"You can use at most 1 main terminal\");\n\n // import styles lazily\n if (addStyle) {\n addStyle = false;\n document.head.append(\n Object.assign(document.createElement(\"link\"), {\n rel: \"stylesheet\",\n href: relative_url(\"./xterm.css\", import.meta.url),\n }),\n );\n }\n\n if (processed.has(element)) return;\n processed.add(element);\n\n const bootstrap = (module) => module.default(element);\n\n // we can't be smart with template literals for the dynamic import\n // or bundlers are incapable of producing multiple files around\n if (type === \"mpy\") {\n await import(/* webpackIgnore: true */ \"./py-terminal/mpy.js\").then(\n bootstrap,\n );\n } else {\n await import(/* webpackIgnore: true */ \"./py-terminal/py.js\").then(\n bootstrap,\n );\n }\n });\n}\n"],"names":["SELECTORS","processed","WeakSet","notifyAndThrow","message","notify","Error","onceOnMain","attributes","worker","addStyle","type","TYPES","keys","selector","push","customObserver","set","async","element","terminals","document","querySelectorAll","join","filter","call","length","head","append","Object","assign","createElement","rel","href","relative_url","url","has","add","bootstrap","module","default","import","then"],"mappings":"6FAMA,MAAMA,EAAY,GAGZC,EAAY,IAAIC,QAIhBC,EAAkBC,IAEpB,MADAC,EAAOD,GACD,IAAIE,MAAMF,EAAQ,EAGtBG,EAAa,EAAGC,YAAcC,cAAgBA,EAEpD,IAAIC,GAAW,EAEf,IAAK,MAAMC,KAAQC,EAAMC,OAAQ,CAC7B,MAAMC,EAAW,gBAAgBH,iBAAoBA,qBACrDX,EAAUe,KAAKD,GACfE,EAAeC,IAAIH,GAAUI,MAAOC,IAEhC,MAAMC,EAAYC,SAASC,iBAAiBtB,EAAUuB,KAAK,MAe3D,GAdI,GAAGC,OAAOC,KAAKL,EAAWb,GAAYmB,OAAS,GAC/CvB,EAAe,uCAGfO,IACAA,GAAW,EACXW,SAASM,KAAKC,OACVC,OAAOC,OAAOT,SAASU,cAAc,QAAS,CAC1CC,IAAK,aACLC,KAAMC,EAAa,0BAA2BC,SAKtDlC,EAAUmC,IAAIjB,GAAU,OAC5BlB,EAAUoC,IAAIlB,GAEd,MAAMmB,EAAaC,GAAWA,EAAOC,QAAQrB,GAIhC,QAATR,QACM8B,OAAiC,qBAAwBC,KAC3DJ,SAGEG,OAAiC,oBAAuBC,KAC1DJ,EAEhB,GAEA"}
1
+ {"version":3,"file":"py-terminal-flqGFKNk.js","sources":["../src/plugins/py-terminal.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { TYPES, relative_url } from \"../core.js\";\nimport { notify } from \"./error.js\";\nimport { customObserver } from \"polyscript/exports\";\n\n// will contain all valid selectors\nconst SELECTORS = [];\n\n// avoid processing same elements twice\nconst processed = new WeakSet();\n\n// show the error on main and\n// stops the module from keep executing\nconst notifyAndThrow = (message) => {\n notify(message);\n throw new Error(message);\n};\n\nconst onceOnMain = ({ attributes: { worker } }) => !worker;\n\nlet addStyle = true;\n\nfor (const type of TYPES.keys()) {\n const selector = `script[type=\"${type}\"][terminal],${type}-script[terminal]`;\n SELECTORS.push(selector);\n customObserver.set(selector, async (element) => {\n // we currently support only one terminal on main as in \"classic\"\n const terminals = document.querySelectorAll(SELECTORS.join(\",\"));\n if ([].filter.call(terminals, onceOnMain).length > 1)\n notifyAndThrow(\"You can use at most 1 main terminal\");\n\n // import styles lazily\n if (addStyle) {\n addStyle = false;\n document.head.append(\n Object.assign(document.createElement(\"link\"), {\n rel: \"stylesheet\",\n href: relative_url(\"./xterm.css\", import.meta.url),\n }),\n );\n }\n\n if (processed.has(element)) return;\n processed.add(element);\n\n const bootstrap = (module) => module.default(element);\n\n // we can't be smart with template literals for the dynamic import\n // or bundlers are incapable of producing multiple files around\n if (type === \"mpy\") {\n await import(/* webpackIgnore: true */ \"./py-terminal/mpy.js\").then(\n bootstrap,\n );\n } else {\n await import(/* webpackIgnore: true */ \"./py-terminal/py.js\").then(\n bootstrap,\n );\n }\n });\n}\n"],"names":["SELECTORS","processed","WeakSet","notifyAndThrow","message","notify","Error","onceOnMain","attributes","worker","addStyle","type","TYPES","keys","selector","push","customObserver","set","async","element","terminals","document","querySelectorAll","join","filter","call","length","head","append","Object","assign","createElement","rel","href","relative_url","url","has","add","bootstrap","module","default","import","then"],"mappings":"6FAMA,MAAMA,EAAY,GAGZC,EAAY,IAAIC,QAIhBC,EAAkBC,IAEpB,MADAC,EAAOD,GACD,IAAIE,MAAMF,EAAQ,EAGtBG,EAAa,EAAGC,YAAcC,cAAgBA,EAEpD,IAAIC,GAAW,EAEf,IAAK,MAAMC,KAAQC,EAAMC,OAAQ,CAC7B,MAAMC,EAAW,gBAAgBH,iBAAoBA,qBACrDX,EAAUe,KAAKD,GACfE,EAAeC,IAAIH,GAAUI,MAAOC,IAEhC,MAAMC,EAAYC,SAASC,iBAAiBtB,EAAUuB,KAAK,MAe3D,GAdI,GAAGC,OAAOC,KAAKL,EAAWb,GAAYmB,OAAS,GAC/CvB,EAAe,uCAGfO,IACAA,GAAW,EACXW,SAASM,KAAKC,OACVC,OAAOC,OAAOT,SAASU,cAAc,QAAS,CAC1CC,IAAK,aACLC,KAAMC,EAAa,0BAA2BC,SAKtDlC,EAAUmC,IAAIjB,GAAU,OAC5BlB,EAAUoC,IAAIlB,GAEd,MAAMmB,EAAaC,GAAWA,EAAOC,QAAQrB,GAIhC,QAATR,QACM8B,OAAiC,qBAAwBC,KAC3DJ,SAGEG,OAAiC,oBAAuBC,KAC1DJ,EAEhB,GAEA"}
@@ -1 +1 @@
1
- {"version":3,"file":"toml-DiUM0_qs.js","sources":["../node_modules/polyscript/esm/3rd-party/toml.js"],"sourcesContent":["/* c8 ignore start */\n/*! (c) Andrea Giammarchi - ISC */\n\nconst {isArray} = Array;\nconst {parse: jsonParse} = JSON;\n\n/** @typedef {{s: string[], d: Date[]}} Foreign foreign strings and dates */\n\n/**\n * Transform quoted keys into regular keys.\n * @param {string} str the key to eventually normalize\n * @param {Foreign} foreign foreign strings and dates\n * @returns \n */\nconst getKey = (str, {s}) => str.replace(/\"s(\\d+)\"/g, (_, $1) => s[$1]);\n\n/**\n * Given a `'string'` or a `\"string\"` returns a JSON compatible string.\n * @param {string} str a TOML entry to parse\n * @param {Foreign} foreign foreign strings and dates\n * @returns {string}\n */\nconst getValue = (str, foreign) => jsonParse(\n str.replace(/(\\S+?)\\s*=/g, '\"$1\":'),\n (_, value) => typeof value === 'string' ?\n foreign[value[0]][value.slice(1)] :\n value\n);\n\n/**\n * Crawl the `json` object via the given array of keys and handle array entries.\n * @param {string[]} keys a path with all keys to reach the entry\n * @param {Foreign} foreign foreign strings and dates\n * @param {object} entry the root entry of the TOML\n * @param {boolean} asArray handle array entries\n * @returns {object} the current entry to handle\n */\nconst getPath = (keys, foreign, entry, asArray) => {\n for (let i = 0, {length} = keys, last = length - 1; i < length; i++) {\n const key = getKey(keys[i], foreign);\n entry = entry[key] || (entry[key] = (asArray && (i === last) ? [] : {}));\n if (isArray(entry)) {\n if ((i === last) || !entry.length)\n entry.push({});\n entry = entry.at(-1);\n }\n }\n return entry;\n};\n\n/**\n * Given a TOML text, removes stirngs and dates for easier parsing +\n * remove multi-line arrays to not need evaluation.\n * @param {string} toml the TOML text to map\n * @param {string[]} strings mapped strings\n * @param {Date[]} dates mapped Dates\n * @returns {[string, Foreign]}\n */\nconst mapForeign = (toml, strings, dates) => [\n toml\n // map strings in the TOML\n .replace(\n /([\"'])(?:(?=(\\\\?))\\2.)*?\\1/g,\n value => `\"s${strings.push(value.slice(1, -1)) - 1}\"`\n )\n // map dates in the TOML\n .replace(\n /\\d{2,}([:-]\\d{2}){2}([ T:-][\\dZ:-]+)?/g,\n value => `\"d${dates.push(new Date(value)) - 1}\"`\n )\n // avoid multi-line array entries\n .replace(/,\\s*[\\r\\n]+/g, ', ')\n .replace(/\\[\\s*[\\r\\n]+/g, '[')\n .replace(/[\\r\\n]+\\s*]/g, ']'),\n {s: strings, d: dates}\n];\n\n/**\n * Given a simple subset of a TOML file, returns its JS equivalent.\n * @param {string} toml the TOML text to parse\n * @returns {object} the TOML equivalent as JSON serializable\n */\nconst parse = toml => {\n const [text, foreign] = mapForeign(toml, [], []);\n const json = {};\n let entry = json;\n for (let line of text.split(/[\\r\\n]+/)) {\n if ((line = line.trim()) && !line.startsWith('#')) {\n if (/^(\\[+)(.*?)\\]+/.test(line))\n entry = getPath(RegExp.$2.trim().split('.'), foreign, json, RegExp.$1 !== '[');\n else if (/^(\\S+?)\\s*=([^#]+)/.test(line)) {\n const {$1: key, $2: value} = RegExp;\n entry[getKey(key, foreign)] = getValue(value.trim(), foreign);\n }\n }\n }\n return json;\n};\n\nexport { parse };\n\n/* c8 ignore stop */\n"],"names":["isArray","Array","parse","jsonParse","JSON","getKey","str","s","replace","_","$1","getValue","foreign","value","slice","getPath","keys","entry","asArray","i","length","last","key","push","at","toml","text","strings","dates","Date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AAGJ,OAAOD,MAAM,KAAMtB,EAASoB,EAAoB,MAAdM,OAAO5B,SAChE,GAAI,qBAAqB2B,KAAKJ,GAAO,CACxC,MAAOvB,GAAIY,EAAKiB,GAAI1B,GAASyB,OAC7BrB,EAAMZ,EAAOiB,EAAKV,IAAYD,EAASE,EAAMsB,OAAQvB,EAC7D,CAGE,OAAOoB,CAAI","x_google_ignoreList":[0]}
1
+ {"version":3,"file":"toml-DiUM0_qs.js","sources":["../../../polyscript/esm/3rd-party/toml.js"],"sourcesContent":["/* c8 ignore start */\n/*! (c) Andrea Giammarchi - ISC */\n\nconst {isArray} = Array;\nconst {parse: jsonParse} = JSON;\n\n/** @typedef {{s: string[], d: Date[]}} Foreign foreign strings and dates */\n\n/**\n * Transform quoted keys into regular keys.\n * @param {string} str the key to eventually normalize\n * @param {Foreign} foreign foreign strings and dates\n * @returns \n */\nconst getKey = (str, {s}) => str.replace(/\"s(\\d+)\"/g, (_, $1) => s[$1]);\n\n/**\n * Given a `'string'` or a `\"string\"` returns a JSON compatible string.\n * @param {string} str a TOML entry to parse\n * @param {Foreign} foreign foreign strings and dates\n * @returns {string}\n */\nconst getValue = (str, foreign) => jsonParse(\n str.replace(/(\\S+?)\\s*=/g, '\"$1\":'),\n (_, value) => typeof value === 'string' ?\n foreign[value[0]][value.slice(1)] :\n value\n);\n\n/**\n * Crawl the `json` object via the given array of keys and handle array entries.\n * @param {string[]} keys a path with all keys to reach the entry\n * @param {Foreign} foreign foreign strings and dates\n * @param {object} entry the root entry of the TOML\n * @param {boolean} asArray handle array entries\n * @returns {object} the current entry to handle\n */\nconst getPath = (keys, foreign, entry, asArray) => {\n for (let i = 0, {length} = keys, last = length - 1; i < length; i++) {\n const key = getKey(keys[i], foreign);\n entry = entry[key] || (entry[key] = (asArray && (i === last) ? [] : {}));\n if (isArray(entry)) {\n if ((i === last) || !entry.length)\n entry.push({});\n entry = entry.at(-1);\n }\n }\n return entry;\n};\n\n/**\n * Given a TOML text, removes stirngs and dates for easier parsing +\n * remove multi-line arrays to not need evaluation.\n * @param {string} toml the TOML text to map\n * @param {string[]} strings mapped strings\n * @param {Date[]} dates mapped Dates\n * @returns {[string, Foreign]}\n */\nconst mapForeign = (toml, strings, dates) => [\n toml\n // map strings in the TOML\n .replace(\n /([\"'])(?:(?=(\\\\?))\\2.)*?\\1/g,\n value => `\"s${strings.push(value.slice(1, -1)) - 1}\"`\n )\n // map dates in the TOML\n .replace(\n /\\d{2,}([:-]\\d{2}){2}([ T:-][\\dZ:-]+)?/g,\n value => `\"d${dates.push(new Date(value)) - 1}\"`\n )\n // avoid multi-line array entries\n .replace(/,\\s*[\\r\\n]+/g, ', ')\n .replace(/\\[\\s*[\\r\\n]+/g, '[')\n .replace(/[\\r\\n]+\\s*]/g, ']'),\n {s: strings, d: dates}\n];\n\n/**\n * Given a simple subset of a TOML file, returns its JS equivalent.\n * @param {string} toml the TOML text to parse\n * @returns {object} the TOML equivalent as JSON serializable\n */\nconst parse = toml => {\n const [text, foreign] = mapForeign(toml, [], []);\n const json = {};\n let entry = json;\n for (let line of text.split(/[\\r\\n]+/)) {\n if ((line = line.trim()) && !line.startsWith('#')) {\n if (/^(\\[+)(.*?)\\]+/.test(line))\n entry = getPath(RegExp.$2.trim().split('.'), foreign, json, RegExp.$1 !== '[');\n else if (/^(\\S+?)\\s*=([^#]+)/.test(line)) {\n const {$1: key, $2: value} = RegExp;\n entry[getKey(key, foreign)] = getValue(value.trim(), foreign);\n }\n }\n }\n return json;\n};\n\nexport { parse };\n\n/* c8 ignore stop */\n"],"names":["isArray","Array","parse","jsonParse","JSON","getKey","str","s","replace","_","$1","getValue","foreign","value","slice","getPath","keys","entry","asArray","i","length","last","key","push","at","toml","text","strings","dates","Date","d","mapForeign","json","line","split","trim","startsWith","test","RegExp","$2"],"mappings":";AAGA,MAAMA,QAACA,GAAWC,OACXC,MAAOC,GAAaC,KAUrBC,EAAS,CAACC,GAAMC,OAAOD,EAAIE,QAAQ,aAAa,CAACC,EAAGC,I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