@pyscript/core 0.5.0-rc1 → 0.5.0-rc2

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.
package/dist/core.js CHANGED
@@ -1,2 +1,2 @@
1
- export{g as MPWorker,f as PyWorker,T as TYPES,h as config,e as hooks,i as inputFailure,o as offline_interpreter,b as optional,r as relative_url,s as stdlib,j as whenDefined}from"./core-jHCq6GXZ.js";
1
+ export{g as MPWorker,f as PyWorker,T as TYPES,h as config,e as hooks,i as inputFailure,o as offline_interpreter,b as optional,r as relative_url,s as stdlib,j as whenDefined}from"./core-sVxLb9Ur.js";
2
2
  //# sourceMappingURL=core.js.map
@@ -1,2 +1,2 @@
1
- import{e}from"./core-jHCq6GXZ.js";import{notify as o}from"./error-BZdmXuMQ.js";function r(){const e=document.querySelectorAll("script");for(const o of e)s(o.src)}function s(e){/\/pyscript\.net\/latest/.test(e)&&o("Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead.")}e.main.onReady.add(r),e.main.onWorker.add(r);
2
- //# sourceMappingURL=deprecations-manager-JLWNpJ-L.js.map
1
+ import{e}from"./core-sVxLb9Ur.js";import{notify as o}from"./error-BE-P8gi6.js";function r(){const e=document.querySelectorAll("script");for(const o of e)s(o.src)}function s(e){/\/pyscript\.net\/latest/.test(e)&&o("Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead.")}e.main.onReady.add(r),e.main.onWorker.add(r);
2
+ //# sourceMappingURL=deprecations-manager-C1bCFFAJ.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"deprecations-manager-JLWNpJ-L.js","sources":["../src/plugins/deprecations-manager.js"],"sourcesContent":["// PyScript Derepcations Plugin\nimport { hooks } from \"../core.js\";\nimport { notify } from \"./error.js\";\n\n// react lazily on PyScript bootstrap\nhooks.main.onReady.add(checkDeprecations);\nhooks.main.onWorker.add(checkDeprecations);\n\n/**\n * Check that there are no scripts loading from pyscript.net/latest\n */\nfunction checkDeprecations() {\n const scripts = document.querySelectorAll(\"script\");\n for (const script of scripts) checkLoadingScriptsFromLatest(script.src);\n}\n\n/**\n * Check if src being loaded from pyscript.net/latest and display a notification if true\n * * @param {string} src\n */\nfunction checkLoadingScriptsFromLatest(src) {\n if (/\\/pyscript\\.net\\/latest/.test(src)) {\n notify(\n \"Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead.\",\n );\n }\n}\n"],"names":["checkDeprecations","scripts","document","querySelectorAll","script","checkLoadingScriptsFromLatest","src","test","notify","hooks","main","onReady","add","onWorker"],"mappings":"+EAWA,SAASA,IACL,MAAMC,EAAUC,SAASC,iBAAiB,UAC1C,IAAK,MAAMC,KAAUH,EAASI,EAA8BD,EAAOE,IACvE,CAMA,SAASD,EAA8BC,GAC/B,0BAA0BC,KAAKD,IAC/BE,EACI,6GAGZ,CArBAC,EAAMC,KAAKC,QAAQC,IAAIZ,GACvBS,EAAMC,KAAKG,SAASD,IAAIZ"}
1
+ {"version":3,"file":"deprecations-manager-C1bCFFAJ.js","sources":["../src/plugins/deprecations-manager.js"],"sourcesContent":["// PyScript Derepcations Plugin\nimport { hooks } from \"../core.js\";\nimport { notify } from \"./error.js\";\n\n// react lazily on PyScript bootstrap\nhooks.main.onReady.add(checkDeprecations);\nhooks.main.onWorker.add(checkDeprecations);\n\n/**\n * Check that there are no scripts loading from pyscript.net/latest\n */\nfunction checkDeprecations() {\n const scripts = document.querySelectorAll(\"script\");\n for (const script of scripts) checkLoadingScriptsFromLatest(script.src);\n}\n\n/**\n * Check if src being loaded from pyscript.net/latest and display a notification if true\n * * @param {string} src\n */\nfunction checkLoadingScriptsFromLatest(src) {\n if (/\\/pyscript\\.net\\/latest/.test(src)) {\n notify(\n \"Loading scripts from latest is deprecated and will be removed soon. Please use a specific version instead.\",\n );\n }\n}\n"],"names":["checkDeprecations","scripts","document","querySelectorAll","script","checkLoadingScriptsFromLatest","src","test","notify","hooks","main","onReady","add","onWorker"],"mappings":"+EAWA,SAASA,IACL,MAAMC,EAAUC,SAASC,iBAAiB,UAC1C,IAAK,MAAMC,KAAUH,EAASI,EAA8BD,EAAOE,IACvE,CAMA,SAASD,EAA8BC,GAC/B,0BAA0BC,KAAKD,IAC/BE,EACI,6GAGZ,CArBAC,EAAMC,KAAKC,QAAQC,IAAIZ,GACvBS,EAAMC,KAAKG,SAASD,IAAIZ"}
@@ -1,2 +1,2 @@
1
- import{e}from"./core-jHCq6GXZ.js";function n(e){const n=document.createElement("div");n.className="py-error",n.textContent=e,n.style.cssText="\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n ",document.body.append(n)}e.main.onReady.add((function o(r){e.main.onReady.delete(o);const{stderr:t}=r.io;r.io.stderr=(e,...o)=>(n(e.message||e),t(e,...o)),addEventListener("error",(({message:e})=>{e.startsWith("Uncaught PythonError")&&n(e)}))}));export{n as notify};
2
- //# sourceMappingURL=error-BZdmXuMQ.js.map
1
+ import{e}from"./core-sVxLb9Ur.js";function n(e){const n=document.createElement("div");n.className="py-error",n.textContent=e,n.style.cssText="\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n ",document.body.append(n)}e.main.onReady.add((function o(r){e.main.onReady.delete(o);const{stderr:t}=r.io;r.io.stderr=(e,...o)=>(n(e.message||e),t(e,...o)),addEventListener("error",(({message:e})=>{e.startsWith("Uncaught PythonError")&&n(e)}))}));export{n as notify};
2
+ //# sourceMappingURL=error-BE-P8gi6.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"error-BZdmXuMQ.js","sources":["../src/plugins/error.js"],"sourcesContent":["// PyScript Error Plugin\nimport { hooks } from \"../core.js\";\n\nhooks.main.onReady.add(function override(pyScript) {\n // be sure this override happens only once\n hooks.main.onReady.delete(override);\n\n // trap generic `stderr` to propagate to it regardless\n const { stderr } = pyScript.io;\n\n // override it with our own logic\n pyScript.io.stderr = (error, ...rest) => {\n notify(error.message || error);\n // let other plugins or stderr hook, if any, do the rest\n return stderr(error, ...rest);\n };\n\n // be sure uncaught Python errors are also visible\n addEventListener(\"error\", ({ message }) => {\n if (message.startsWith(\"Uncaught PythonError\")) notify(message);\n });\n});\n\n// Error hook utilities\n\n// Custom function to show notifications\n\n/**\n * Add a banner to the top of the page, notifying the user of an error\n * @param {string} message\n */\nexport function notify(message) {\n const div = document.createElement(\"div\");\n div.className = \"py-error\";\n div.textContent = message;\n div.style.cssText = `\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n `;\n document.body.append(div);\n}\n"],"names":["notify","message","div","document","createElement","className","textContent","style","cssText","body","append","hooks","main","onReady","add","override","pyScript","delete","stderr","io","error","rest","addEventListener","startsWith"],"mappings":"kCA+BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CA3CAS,EAAMC,KAAKC,QAAQC,KAAI,SAASC,EAASC,GAErCL,EAAMC,KAAKC,QAAQI,OAAOF,GAG1B,MAAMG,OAAEA,GAAWF,EAASG,GAG5BH,EAASG,GAAGD,OAAS,CAACE,KAAUC,KAC5BrB,EAAOoB,EAAMnB,SAAWmB,GAEjBF,EAAOE,KAAUC,IAI5BC,iBAAiB,SAAS,EAAGrB,cACrBA,EAAQsB,WAAW,yBAAyBvB,EAAOC,EAAQ,GAEvE"}
1
+ {"version":3,"file":"error-BE-P8gi6.js","sources":["../src/plugins/error.js"],"sourcesContent":["// PyScript Error Plugin\nimport { hooks } from \"../core.js\";\n\nhooks.main.onReady.add(function override(pyScript) {\n // be sure this override happens only once\n hooks.main.onReady.delete(override);\n\n // trap generic `stderr` to propagate to it regardless\n const { stderr } = pyScript.io;\n\n // override it with our own logic\n pyScript.io.stderr = (error, ...rest) => {\n notify(error.message || error);\n // let other plugins or stderr hook, if any, do the rest\n return stderr(error, ...rest);\n };\n\n // be sure uncaught Python errors are also visible\n addEventListener(\"error\", ({ message }) => {\n if (message.startsWith(\"Uncaught PythonError\")) notify(message);\n });\n});\n\n// Error hook utilities\n\n// Custom function to show notifications\n\n/**\n * Add a banner to the top of the page, notifying the user of an error\n * @param {string} message\n */\nexport function notify(message) {\n const div = document.createElement(\"div\");\n div.className = \"py-error\";\n div.textContent = message;\n div.style.cssText = `\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n `;\n document.body.append(div);\n}\n"],"names":["notify","message","div","document","createElement","className","textContent","style","cssText","body","append","hooks","main","onReady","add","override","pyScript","delete","stderr","io","error","rest","addEventListener","startsWith"],"mappings":"kCA+BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CA3CAS,EAAMC,KAAKC,QAAQC,KAAI,SAASC,EAASC,GAErCL,EAAMC,KAAKC,QAAQI,OAAOF,GAG1B,MAAMG,OAAEA,GAAWF,EAASG,GAG5BH,EAASG,GAAGD,OAAS,CAACE,KAAUC,KAC5BrB,EAAOoB,EAAMnB,SAAWmB,GAEjBF,EAAOE,KAAUC,IAI5BC,iBAAiB,SAAS,EAAGrB,cACrBA,EAAQsB,WAAW,yBAAyBvB,EAAOC,EAAQ,GAEvE"}
@@ -1,2 +1,2 @@
1
- import{e,i as r,d as t}from"./core-jHCq6GXZ.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))},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-BY7uk_OU.js"),import("./xterm_addon-fit--gyF3PcZ.js"),import("./xterm_addon-web-links-Cnej-nJ6.js")]),m={disableStdin:!1,cursorBlink:!0,cursorStyle:"block"};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-CA-8GLzg.js.map
1
+ import{e,i as r,d as t}from"./core-sVxLb9Ur.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))},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-BY7uk_OU.js"),import("./xterm_addon-fit--gyF3PcZ.js"),import("./xterm_addon-web-links-Cnej-nJ6.js")]),m={disableStdin:!1,cursorBlink:!0,cursorStyle:"block"};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-awU48RT6.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mpy-CA-8GLzg.js","sources":["../src/plugins/py-terminal/mpy.js"],"sourcesContent":["// PyScript pyodide terminal plugin\nimport { hooks, inputFailure } from \"../../core.js\";\nimport { defineProperties } from \"polyscript/exports\";\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 // 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 };\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","interact","encoder","TextEncoderStream","readable","pipeTo","WritableStream","write","c","replProcessChar","writer","writable","getWriter","pyterminal_stream_write","replInit","mpy","async","element","Terminal","FitAddon","WebLinksAddon","Promise","all","import","terminalOptions","disableStdin","cursorBlink","cursorStyle","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,EAKpDjB,EAAYS,iBAAiB,OAAQ,CACjC,QAAAW,GACI,MAAMC,EAAU,IAAIC,kBACpBD,EAAQE,SAASC,OACb,IAAIC,eAAe,CACf,KAAAC,CAAMX,GACF,IAAK,MAAMY,KAAKZ,EAAQf,EAAY4B,gBAAgBD,EACvD,KAIT,MAAME,EAASR,EAAQS,SAASC,YAChC3B,EAAK4B,wBAA2BjB,GAAWc,EAAOH,MAAMX,GAExDf,EAAYiC,UACf,IAGL3B,GAAkB,EAGtB,IAAe4B,EAAAC,MAAOC,IAElB,OAAOC,SAAEA,IAAYC,SAAEA,IAAYC,cAAEA,UAAyBC,QAAQC,IAAI,CACtEC,OAAiC,uBACjCA,OAAiC,iCACjCA,OAC8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al/mpy.js"],"sourcesContent":["// PyScript pyodide terminal plugin\nimport { hooks, inputFailure } from \"../../core.js\";\nimport { defineProperties } from \"polyscript/exports\";\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 // 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 };\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","interact","encoder","TextEncoderStream","readable","pipeTo","WritableStream","write","c","replProcessChar","writer","writable","getWriter","pyterminal_stream_write","replInit","mpy","async","element","Terminal","FitAddon","WebLinksAddon","Promise","all","import","terminalOptions","disableStdin","cursorBlink","cursorStyle","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,EAKpDjB,EAAYS,iBAAiB,OAAQ,CACjC,QAAAW,GACI,MAAMC,EAAU,IAAIC,kBACpBD,EAAQE,SAASC,OACb,IAAIC,eAAe,CACf,KAAAC,CAAMX,GACF,IAAK,MAAMY,KAAKZ,EAAQf,EAAY4B,gBAAgBD,EACvD,KAIT,MAAME,EAASR,EAAQS,SAASC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,CAAMX,GACF,IAAK,MAAMY,KAAKZ,EACZf,EAAY4B,gBAAgBD,EACnC,KAIToB,EAAS1B,EAAQS,SAASC,YAC1B6B,EAASgC,QAAQ7E,GAAWgC,EAAOrB,MAAMX,KAEzCf,EAAYiC,UACf,GAEjB,IACK"}
@@ -1,2 +1,2 @@
1
- import{e,d as t}from"./core-jHCq6GXZ.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-BY7uk_OU.js"),import("./xterm-readline-DjufFwfn.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};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"}),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)):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-tfDQPNNm.js.map
1
+ import{e,d as t}from"./core-sVxLb9Ur.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-BY7uk_OU.js"),import("./xterm-readline-DjufFwfn.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};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"}),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)):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-DQxM9K2E.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"py-tfDQPNNm.js","sources":["../src/plugins/py-terminal/py.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { hooks } from \"../../core.js\";\nimport { defineProperties } from \"polyscript/exports\";\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 });\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 } 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","read","bind","onReady","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,EAAS,CAC1BsB,MAAO,CACHC,WAAY,UACZC,WAAY,cAEbf,IAEDgB,EAAW,IAAIvB,EAwBrB,OAvBAmB,EAASK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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,EAAS8C,KAAK3E,IAE3C,GACK"}
1
+ {"version":3,"file":"py-DQxM9K2E.js","sources":["../src/plugins/py-terminal/py.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { hooks } from \"../../core.js\";\nimport { defineProperties } from \"polyscript/exports\";\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 });\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 } 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","read","bind","onReady","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,SAItB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AAOC,EAAGC,GAGnC/E,EAAagF,IAAID,KACrB/E,EAAa4E,IAAIG,GAGjBN,EAAMC,KAAKC,SAASM,OAAOJ,GAE3BrC,EAAK,CACD0C,cAAc,EACdC,aAAa,EACbC,YAAa,UAGjBL,EAAQxE,KAAKC,cAAgB,KAAM,EACnCuE,EAAQxE,KAAKI,gBAAkB4B,EAAS8C,KAAKC,KAAK/C,GAClDwC,EAAQxE,KAAKK,iBAAmB2B,EAAStB,MAAMqE,KAAK/C,GAChE,IAIQkC,EAAMI,OAAOU,QAAQX,IAAI1E,IAIzBuE,EAAMC,KAAKa,QAAQX,KAAI,SAASF,GAAKvE,YAAEA,EAAWC,GAAEA,EAAEC,IAAEA,EAAGC,KAAEA,IACzD,GAAa,OAATA,EAAe,OAEnBkF,QAAQC,KAAK,2CACbhB,EAAMC,KAAKa,QAAQN,OAAOP,GAG1BgB,WAAWC,gBAAkBnD,EAAK,CAC9B0C,cAAc,EACdC,aAAa,EACbC,YAAa,cAEjB/E,EAAI,yDACGqF,WAAWC,gBAElBvF,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,EAAS8C,KAAK3E,IAE3C,GACK"}
@@ -1,2 +1,2 @@
1
- import{T as e,d as t,a as r,r as n,o,X as s,H as i,s as a}from"./core-jHCq6GXZ.js";let c=0;const l=e=>`${e}-editor-${c++}`,d=new Map,u=new Map,p={worker:{codeBeforeRun:()=>a,onReady:({runAsync:e,io:t},{sync:r})=>{t.stdout=t.buffered(r.write),t.stderr=t.buffered(r.writeErr),r.revoke(),r.runAsync=e}}};async function m({currentTarget:e}){const{env:t,pySrc:r,outDiv:a}=this,c=!!e;if(c&&(e.disabled=!0,a.innerHTML=""),!d.has(t)){const e=URL.createObjectURL(new Blob([""])),r={type:this.interpreter},{config:a}=this;if(a){if(r.configURL=n(a),a.endsWith(".toml")){const[{parse:e},t]=await Promise.all([import("./toml-CvAfdf9_.js"),fetch(a).then((e=>e.text()))]);r.config=e(t)}else a.endsWith(".json")?r.config=await fetch(a).then((e=>e.json())):(r.configURL=n("./config.txt"),r.config=JSON.parse(a));r.version=o(r.config)}else r.config={};const c=s.call(new i(null,p),e,r),{sync:l}=c,{promise:u,resolve:m}=Promise.withResolvers();d.set(t,u),l.revoke=()=>{URL.revokeObjectURL(e),m(c)}}return d.get(t).then((t=>{t.onerror=({error:e})=>{c&&(a.innerHTML+=`<span style='color:red'>${e.message||e}</span>\n`),console.error(e)};const n=()=>{c&&(e.disabled=!1)},{sync:o}=t;o.write=e=>{c&&(a.innerText+=`${e}\n`)},o.writeErr=e=>{c&&(a.innerHTML+=`<span style='color:red'>${e}</span>\n`)},o.runAsync(r).then(n,n)}))}const f=(e,t)=>{const r=document.createElement("div");r.className=`${t}-editor-input`,r.setAttribute("aria-label","Python Script Area");const n=((e,t)=>{const r=document.createElement("button");return r.className=`absolute ${t}-editor-run-button`,r.innerHTML='<svg style="height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green" viewBox="0 0 384 512" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg"><g transform="translate(192 256)" transform-origin="96 0"><g transform="translate(0,0) scale(1,1)"><path d="M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z" fill="currentColor" transform="translate(-192 -256)"></path></g></g></svg>',r.setAttribute("aria-label","Python Script Run Button"),r.addEventListener("click",(async t=>{r.blur(),await e.handleEvent(t)})),r})(e,t),o=document.createElement("div");return o.addEventListener("keydown",(e=>{e.stopPropagation()})),r.append(n,o),r},g=(e,t)=>{const r=document.createElement("div");r.className=`${t}-editor-box`;const n=f(e,t),o=(e=>{const t=document.createElement("div");return t.className=`${e}-editor-output`,t.id=`${l(e)}-output`,t})(t);return r.append(n,o),[r,o,n.querySelector("button")]},h=async(e,n,o)=>{const[{basicSetup:s,EditorView:i},{Compartment:a},{python:c},{indentUnit:d},{keymap:p},{defaultKeymap:f,indentWithTab:h}]=await Promise.all([import("./codemirror-ZWX__zRS.js"),import("./codemirror_state-D1qTXrff.js"),import("./codemirror_lang-python-CAjFAwEr.js"),import("./codemirror_language-BzugrQDC.js").then((function(e){return e.x})),import("./codemirror_view-DrrFKRyn.js").then((function(e){return e.q})),import("./codemirror_commands-BvofQ4B2.js")]);let v=e.hasAttribute("setup");const y=e.hasAttribute("config"),b=`${o}-${e.getAttribute("env")||l(n)}`;if(y&&u.has(b))throw new SyntaxError(u.get(b)?`duplicated config for env: ${b}`:`unable to add a config to the env: ${b}`);u.set(b,y);let w=e.src?await fetch(e.src).then((e=>e.text())):e.textContent;const E={handleEvent:m,interpreter:o,env:b,config:y&&e.getAttribute("config"),get pySrc(){return v?w:R.state.doc.toString()},get outDiv(){return v?null:S}};let $;t(e,{target:{get:()=>$},handleEvent:{get:()=>E.handleEvent,set:e=>{E.handleEvent=e===m?m:async r=>{const{currentTarget:n}=r;t(r,{code:{value:E.pySrc}}),!1!==await e(r)&&await m.call(E,{currentTarget:n})}}},code:{get:()=>E.pySrc,set:e=>{v||R.update([R.state.update({changes:{from:0,to:R.state.doc.length,insert:e}})])}},process:{value(e){const t=v,r=w;v=!0,w=e;const n=()=>{v=t,w=r};return E.handleEvent({currentTarget:null}).then(n,n)}}});const x=()=>{const t=new Event(`${n}-editor`,{bubbles:!0});e.dispatchEvent(t)};if(v)return await E.handleEvent({currentTarget:null}),void x();const A=e.getAttribute("target");if(A){if($=document.getElementById(A)||document.querySelector(A),!$)throw new Error(`Unknown target ${A}`)}else $=document.createElement(`${n}-editor`),$.style.display="block",e.after($);$.id||($.id=l(n)),$.hasAttribute("exec-id")||$.setAttribute("exec-id",0),$.hasAttribute("root")||$.setAttribute("root",$.id);const[L,S,C]=g(E,n);L.dataset.env=e.hasAttribute("env")?b:o;const T=L.querySelector(`.${n}-editor-input > div`).attachShadow({mode:"open"});T.innerHTML="<style> :host { all: initial; }</style>",$.appendChild(L);const k=r(e.textContent).trim(),j=/^([ \t]+)/m.test(k)?RegExp.$1:" ",M=()=>C.click(),R=new i({extensions:[d.of(j),(new a).of(c()),p.of([...f,{key:"Ctrl-Enter",run:M,preventDefault:!0},{key:"Cmd-Enter",run:M,preventDefault:!0},{key:"Shift-Enter",run:M,preventDefault:!0},h]),s],foldGutter:!0,gutters:["CodeMirror-linenumbers","CodeMirror-foldgutter"],parent:T,doc:k});R.focus(),x()};let v=0,y=Promise.resolve();const b=()=>{v=0,w()},w=()=>{if(!v){v=setTimeout(b,250);for(const[t,r]of e){const e=`script[type="${t}-editor"]`;for(const n of document.querySelectorAll(e))n.type+="-active",y=y.then((()=>h(n,t,r)))}return y}};new MutationObserver(w).observe(document,{childList:!0,subtree:!0});var E=w();export{E as default};
2
- //# sourceMappingURL=py-editor-BFwOcA7b.js.map
1
+ import{T as e,d as t,a as r,r as n,o,X as s,H as i,s as a}from"./core-sVxLb9Ur.js";let c=0;const l=e=>`${e}-editor-${c++}`,d=new Map,u=new Map,p={worker:{codeBeforeRun:()=>a,onReady:({runAsync:e,io:t},{sync:r})=>{t.stdout=t.buffered(r.write),t.stderr=t.buffered(r.writeErr),r.revoke(),r.runAsync=e}}};async function m({currentTarget:e}){const{env:t,pySrc:r,outDiv:a}=this,c=!!e;if(c&&(e.disabled=!0,a.innerHTML=""),!d.has(t)){const e=URL.createObjectURL(new Blob([""])),r={type:this.interpreter},{config:a}=this;if(a){if(r.configURL=n(a),a.endsWith(".toml")){const[{parse:e},t]=await Promise.all([import("./toml-CvAfdf9_.js"),fetch(a).then((e=>e.text()))]);r.config=e(t)}else a.endsWith(".json")?r.config=await fetch(a).then((e=>e.json())):(r.configURL=n("./config.txt"),r.config=JSON.parse(a));r.version=o(r.config)}else r.config={};const c=s.call(new i(null,p),e,r),{sync:l}=c,{promise:u,resolve:m}=Promise.withResolvers();d.set(t,u),l.revoke=()=>{URL.revokeObjectURL(e),m(c)}}return d.get(t).then((t=>{t.onerror=({error:e})=>{c&&(a.innerHTML+=`<span style='color:red'>${e.message||e}</span>\n`),console.error(e)};const n=()=>{c&&(e.disabled=!1)},{sync:o}=t;o.write=e=>{c&&(a.innerText+=`${e}\n`)},o.writeErr=e=>{c&&(a.innerHTML+=`<span style='color:red'>${e}</span>\n`)},o.runAsync(r).then(n,n)}))}const f=(e,t)=>{const r=document.createElement("div");r.className=`${t}-editor-input`,r.setAttribute("aria-label","Python Script Area");const n=((e,t)=>{const r=document.createElement("button");return r.className=`absolute ${t}-editor-run-button`,r.innerHTML='<svg style="height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green" viewBox="0 0 384 512" aria-hidden="true" role="img" xmlns="http://www.w3.org/2000/svg"><g transform="translate(192 256)" transform-origin="96 0"><g transform="translate(0,0) scale(1,1)"><path d="M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z" fill="currentColor" transform="translate(-192 -256)"></path></g></g></svg>',r.setAttribute("aria-label","Python Script Run Button"),r.addEventListener("click",(async t=>{r.blur(),await e.handleEvent(t)})),r})(e,t),o=document.createElement("div");return o.addEventListener("keydown",(e=>{e.stopPropagation()})),r.append(n,o),r},g=(e,t)=>{const r=document.createElement("div");r.className=`${t}-editor-box`;const n=f(e,t),o=(e=>{const t=document.createElement("div");return t.className=`${e}-editor-output`,t.id=`${l(e)}-output`,t})(t);return r.append(n,o),[r,o,n.querySelector("button")]},h=async(e,n,o)=>{const[{basicSetup:s,EditorView:i},{Compartment:a},{python:c},{indentUnit:d},{keymap:p},{defaultKeymap:f,indentWithTab:h}]=await Promise.all([import("./codemirror-ZWX__zRS.js"),import("./codemirror_state-D1qTXrff.js"),import("./codemirror_lang-python-CAjFAwEr.js"),import("./codemirror_language-BzugrQDC.js").then((function(e){return e.x})),import("./codemirror_view-DrrFKRyn.js").then((function(e){return e.q})),import("./codemirror_commands-BvofQ4B2.js")]);let v=e.hasAttribute("setup");const y=e.hasAttribute("config"),b=`${o}-${e.getAttribute("env")||l(n)}`;if(y&&u.has(b))throw new SyntaxError(u.get(b)?`duplicated config for env: ${b}`:`unable to add a config to the env: ${b}`);u.set(b,y);let w=e.src?await fetch(e.src).then((e=>e.text())):e.textContent;const E={handleEvent:m,interpreter:o,env:b,config:y&&e.getAttribute("config"),get pySrc(){return v?w:R.state.doc.toString()},get outDiv(){return v?null:S}};let $;t(e,{target:{get:()=>$},handleEvent:{get:()=>E.handleEvent,set:e=>{E.handleEvent=e===m?m:async r=>{const{currentTarget:n}=r;t(r,{code:{value:E.pySrc}}),!1!==await e(r)&&await m.call(E,{currentTarget:n})}}},code:{get:()=>E.pySrc,set:e=>{v||R.update([R.state.update({changes:{from:0,to:R.state.doc.length,insert:e}})])}},process:{value(e){const t=v,r=w;v=!0,w=e;const n=()=>{v=t,w=r};return E.handleEvent({currentTarget:null}).then(n,n)}}});const x=()=>{const t=new Event(`${n}-editor`,{bubbles:!0});e.dispatchEvent(t)};if(v)return await E.handleEvent({currentTarget:null}),void x();const A=e.getAttribute("target");if(A){if($=document.getElementById(A)||document.querySelector(A),!$)throw new Error(`Unknown target ${A}`)}else $=document.createElement(`${n}-editor`),$.style.display="block",e.after($);$.id||($.id=l(n)),$.hasAttribute("exec-id")||$.setAttribute("exec-id",0),$.hasAttribute("root")||$.setAttribute("root",$.id);const[L,S,C]=g(E,n);L.dataset.env=e.hasAttribute("env")?b:o;const T=L.querySelector(`.${n}-editor-input > div`).attachShadow({mode:"open"});T.innerHTML="<style> :host { all: initial; }</style>",$.appendChild(L);const k=r(e.textContent).trim(),j=/^([ \t]+)/m.test(k)?RegExp.$1:" ",M=()=>C.click(),R=new i({extensions:[d.of(j),(new a).of(c()),p.of([...f,{key:"Ctrl-Enter",run:M,preventDefault:!0},{key:"Cmd-Enter",run:M,preventDefault:!0},{key:"Shift-Enter",run:M,preventDefault:!0},h]),s],foldGutter:!0,gutters:["CodeMirror-linenumbers","CodeMirror-foldgutter"],parent:T,doc:k});R.focus(),x()};let v=0,y=Promise.resolve();const b=()=>{v=0,w()},w=()=>{if(!v){v=setTimeout(b,250);for(const[t,r]of e){const e=`script[type="${t}-editor"]`;for(const n of document.querySelectorAll(e))n.type+="-active",y=y.then((()=>h(n,t,r)))}return y}};new MutationObserver(w).observe(document,{childList:!0,subtree:!0});var E=w();export{E as default};
2
+ //# sourceMappingURL=py-editor-CUd-CQKV.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"py-editor-BFwOcA7b.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\";\n\nconst RUN_BUTTON = `<svg style=\"height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green\" viewBox=\"0 0 384 512\" aria-hidden=\"true\" role=\"img\" xmlns=\"http://www.w3.org/2000/svg\"><g transform=\"translate(192 256)\" transform-origin=\"96 0\"><g transform=\"translate(0,0) scale(1,1)\"><path d=\"M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z\" fill=\"currentColor\" transform=\"translate(-192 -256)\"></path></g></g></svg>`;\n\nlet id = 0;\nconst getID = (type) => `${type}-editor-${id++}`;\n\nconst envs = new Map();\nconst configs = new Map();\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\nasync function execute({ currentTarget }) {\n const { env, pySrc, outDiv } = this;\n const hasRunButton = !!currentTarget;\n\n if (hasRunButton) {\n currentTarget.disabled = true;\n outDiv.innerHTML = \"\";\n }\n\n if (!envs.has(env)) {\n const srcLink = URL.createObjectURL(new Blob([\"\"]));\n const details = { type: this.interpreter };\n const { config } = this;\n if (config) {\n details.configURL = relative_url(config);\n if (config.endsWith(\".toml\")) {\n const [{ parse }, toml] = await Promise.all([\n import(/* webpackIgnore: true */ \"../3rd-party/toml.js\"),\n fetch(config).then((r) => r.text()),\n ]);\n details.config = parse(toml);\n } else if (config.endsWith(\".json\")) {\n details.config = await fetch(config).then((r) => r.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 } else {\n details.config = {};\n }\n\n const xworker = XWorker.call(new Hook(null, hooks), srcLink, details);\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.innerHTML += `<span style='color:red'>${\n error.message || error\n }</span>\\n`;\n }\n console.error(error);\n };\n\n const enable = () => {\n if (hasRunButton) currentTarget.disabled = false;\n };\n const { sync } = xworker;\n sync.write = (str) => {\n if (hasRunButton) outDiv.innerText += `${str}\\n`;\n };\n sync.writeErr = (str) => {\n if (hasRunButton) {\n outDiv.innerHTML += `<span style='color:red'>${str}</span>\\n`;\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 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 import(/* webpackIgnore: true */ \"../3rd-party/codemirror.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_state.js\"),\n import(\n /* webpackIgnore: true */ \"../3rd-party/codemirror_lang-python.js\"\n ),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_language.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_view.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_commands.js\"),\n ]);\n\n let isSetup = script.hasAttribute(\"setup\");\n const hasConfig = script.hasAttribute(\"config\");\n const env = `${interpreter}-${script.getAttribute(\"env\") || getID(type)}`;\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.src\n ? await fetch(script.src).then((b) => b.text())\n : script.textContent;\n const context = {\n // allow the listener to be overridden at distance\n handleEvent: execute,\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 * @returns {Promise<...>} fulfill once code has been evaluated.\n */\n value(code) {\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 notify = () => {\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 notify();\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 editor.focus();\n notify();\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":["id","getID","type","envs","Map","configs","hooks","worker","codeBeforeRun","stdlib","onReady","runAsync","io","sync","stdout","buffered","write","stderr","writeErr","revoke","async","execute","currentTarget","env","pySrc","outDiv","this","hasRunButton","disabled","innerHTML","has","srcLink","URL","createObjectURL","Blob","details","interpreter","config","configURL","relative_url","endsWith","parse","toml","Promise","all","import","fetch","then","r","text","json","JSON","version","offline_interpreter","xworker","XWorker","call","Hook","promise","resolve","withResolvers","set","revokeObjectURL","get","onerror","error","message","console","enable","str","innerText","makeEditorDiv","handler","editorDiv","document","createElement","className","setAttribute","runButton","addEventListener","event","blur","handleEvent","makeRunButton","editorShadowContainer","stopPropagation","append","makeBoxDiv","boxDiv","makeOutDiv","querySelector","init","script","basicSetup","EditorView","Compartment","python","indentUnit","keymap","defaultKeymap","indentWithTab","n","x","q","isSetup","hasAttribute","hasConfig","getAttribute","SyntaxError","source","src","b","textContent","context","editor","state","doc","toString","target","defineProperties","callback","code","value","insert","update","changes","from","to","length","process","wasSetup","wasSource","restore","notify","Event","bubbles","dispatchEvent","selector","getElementById","Error","style","display","after","dataset","parent","attachShadow","mode","appendChild","dedent","trim","indentation","test","RegExp","$1","listener","click","extensions","of","key","run","preventDefault","foldGutter","gutters","focus","timeout","queue","resetTimeout","pyEditor","setTimeout","TYPES","querySelectorAll","MutationObserver","observe","childList","subtree","pyEditor$1"],"mappings":"mFAMA,IAAIA,EAAK,EACT,MAAMC,EAASC,GAAS,GAAGA,YAAeF,MAEpCG,EAAO,IAAIC,IACXC,EAAU,IAAID,IAEdE,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,IAKpCS,eAAeC,GAAQC,cAAEA,IACrB,MAAMC,IAAEA,EAAGC,MAAEA,EAAKC,OAAEA,GAAWC,KACzBC,IAAiBL,EAOvB,GALIK,IACAL,EAAcM,UAAW,EACzBH,EAAOI,UAAY,KAGlB1B,EAAK2B,IAAIP,GAAM,CAChB,MAAMQ,EAAUC,IAAIC,gBAAgB,IAAIC,KAAK,CAAC,MACxCC,EAAU,CAAEjC,KAAMwB,KAAKU,cACvBC,OAAEA,GAAWX,KACnB,GAAIW,EAAQ,CAER,GADAF,EAAQG,UAAYC,EAAaF,GAC7BA,EAAOG,SAAS,SAAU,CAC1B,OAAOC,MAAEA,GAASC,SAAcC,QAAQC,IAAI,CACxCC,OAAiC,sBACjCC,MAAMT,GAAQU,MAAMC,GAAMA,EAAEC,WAEhCd,EAAQE,OAASI,EAAMC,EAC1B,MAAUL,EAAOG,SAAS,SACvBL,EAAQE,aAAeS,MAAMT,GAAQU,MAAMC,GAAMA,EAAEE,UAEnDf,EAAQG,UAAYC,EAAa,gBACjCJ,EAAQE,OAASc,KAAKV,MAAMJ,IAEhCF,EAAQiB,QAAUC,EAAoBlB,EAAQE,OAC1D,MACYF,EAAQE,OAAS,GAGrB,MAAMiB,EAAUC,EAAQC,KAAK,IAAIC,EAAK,KAAMnD,GAAQyB,EAASI,IAEvDtB,K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EAAQ1F,EAAMkC,IAEnD,CACD,OAAOiI,CAZa,CAYR,EAGhB,IAAIM,iBAAiBJ,GAAUK,QAAQlG,SAAU,CAC7CmG,WAAW,EACXC,SAAS,IAIb,IAAAC,EAAeR"}
1
+ {"version":3,"file":"py-editor-CUd-CQKV.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\";\n\nconst RUN_BUTTON = `<svg style=\"height:20px;width:20px;vertical-align:-.125em;transform-origin:center;overflow:visible;color:green\" viewBox=\"0 0 384 512\" aria-hidden=\"true\" role=\"img\" xmlns=\"http://www.w3.org/2000/svg\"><g transform=\"translate(192 256)\" transform-origin=\"96 0\"><g transform=\"translate(0,0) scale(1,1)\"><path d=\"M361 215C375.3 223.8 384 239.3 384 256C384 272.7 375.3 288.2 361 296.1L73.03 472.1C58.21 482 39.66 482.4 24.52 473.9C9.377 465.4 0 449.4 0 432V80C0 62.64 9.377 46.63 24.52 38.13C39.66 29.64 58.21 29.99 73.03 39.04L361 215z\" fill=\"currentColor\" transform=\"translate(-192 -256)\"></path></g></g></svg>`;\n\nlet id = 0;\nconst getID = (type) => `${type}-editor-${id++}`;\n\nconst envs = new Map();\nconst configs = new Map();\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\nasync function execute({ currentTarget }) {\n const { env, pySrc, outDiv } = this;\n const hasRunButton = !!currentTarget;\n\n if (hasRunButton) {\n currentTarget.disabled = true;\n outDiv.innerHTML = \"\";\n }\n\n if (!envs.has(env)) {\n const srcLink = URL.createObjectURL(new Blob([\"\"]));\n const details = { type: this.interpreter };\n const { config } = this;\n if (config) {\n details.configURL = relative_url(config);\n if (config.endsWith(\".toml\")) {\n const [{ parse }, toml] = await Promise.all([\n import(/* webpackIgnore: true */ \"../3rd-party/toml.js\"),\n fetch(config).then((r) => r.text()),\n ]);\n details.config = parse(toml);\n } else if (config.endsWith(\".json\")) {\n details.config = await fetch(config).then((r) => r.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 } else {\n details.config = {};\n }\n\n const xworker = XWorker.call(new Hook(null, hooks), srcLink, details);\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.innerHTML += `<span style='color:red'>${\n error.message || error\n }</span>\\n`;\n }\n console.error(error);\n };\n\n const enable = () => {\n if (hasRunButton) currentTarget.disabled = false;\n };\n const { sync } = xworker;\n sync.write = (str) => {\n if (hasRunButton) outDiv.innerText += `${str}\\n`;\n };\n sync.writeErr = (str) => {\n if (hasRunButton) {\n outDiv.innerHTML += `<span style='color:red'>${str}</span>\\n`;\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 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 import(/* webpackIgnore: true */ \"../3rd-party/codemirror.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_state.js\"),\n import(\n /* webpackIgnore: true */ \"../3rd-party/codemirror_lang-python.js\"\n ),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_language.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_view.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/codemirror_commands.js\"),\n ]);\n\n let isSetup = script.hasAttribute(\"setup\");\n const hasConfig = script.hasAttribute(\"config\");\n const env = `${interpreter}-${script.getAttribute(\"env\") || getID(type)}`;\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.src\n ? await fetch(script.src).then((b) => b.text())\n : script.textContent;\n const context = {\n // allow the listener to be overridden at distance\n handleEvent: execute,\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 * @returns {Promise<...>} fulfill once code has been evaluated.\n */\n value(code) {\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 notify = () => {\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 notify();\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 editor.focus();\n notify();\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":["id","getID","type","envs","Map","configs","hooks","worker","codeBeforeRun","stdlib","onReady","runAsync","io","sync","stdout","buffered","write","stderr","writeErr","revoke","async","execute","currentTarget","env","pySrc","outDiv","this","hasRunButton","disabled","innerHTML","has","srcLink","URL","createObjectURL","Blob","details","interpreter","config","configURL","relative_url","endsWith","parse","toml","Promise","all","import","fetch","then","r","text","json","JSON","version","offline_interpreter","xworker","XWorker","call","Hook","promise","resolve","withResolvers","set","revokeObjectURL","get","onerror","error","message","console","enable","str","innerText","makeEditorDiv","handler","editorDiv","document","createElement","className","setAttribute","runButton","addEventListener","event","blur","handleEvent","makeRunButton","editorShadowContainer","stopPropagation","append","makeBoxDiv","boxDiv","makeOutDiv","querySelector","init","script","basicSetup","EditorView","Compartment","python","indentUnit","keymap","defaultKeymap","indentWithTab","n","x","q","isSetup","hasAttribute","hasConfig","getAttribute","SyntaxError","source","src","b","textContent","context","editor","state","doc","toString","target","defineProperties","callback","code","value","insert","update","changes","from","to","length","process","wasSetup","wasSource","restore","notify","Event","bubbles","dispatchEvent","selector","getElementById","Error","style","display","after","dataset","parent","attachShadow","mode","appendChild","dedent","trim","indentation","test","RegExp","$1","listener","click","extensions","of","key","run","preventDefault","foldGutter","gutters","focus","timeout","queue","resetTimeout","pyEditor","setTimeout","TYPES","querySelectorAll","MutationObserver","observe","childList","subtree","pyEditor$1"],"mappings":"mFAMA,IAAIA,EAAK,EACT,MAAMC,EAASC,GAAS,GAAGA,YAAeF,MAEpCG,EAAO,IAAIC,IACXC,EAAU,IAAID,IAEdE,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,IAKpCS,eAAeC,GAAQC,cAAEA,IACrB,MAAMC,IAAEA,EAAGC,MAAEA,EAAKC,OAAEA,GAAWC,KACzBC,IAAiBL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3BzC,EAAS,IAAIpB,EAAW,CAC1B8D,WAAY,CACR3D,EAAW4D,GAAGP,IACd,IAAIvD,GAAc8D,GAAG7D,KACrBE,EAAO2D,GAAG,IACH1D,EACH,CAAE2D,IAAK,aAAcC,IAAKL,EAAUM,gBAAgB,GACpD,CAAEF,IAAK,YAAaC,IAAKL,EAAUM,gBAAgB,GACnD,CAAEF,IAAK,cAAeC,IAAKL,EAAUM,gBAAgB,GAErD5D,IAEJP,GAEJoE,YAAY,EACZC,QAAS,CAAC,yBAA0B,yBACpClB,SACA5B,QAGJF,EAAOiD,QACP9B,GAAQ,EAIZ,IAAI+B,EAAU,EAGVC,EAAQ1H,QAAQgB,UAGpB,MAAM2G,EAAe,KACjBF,EAAU,EACVG,GAAU,EAIRA,EAAW,KACb,IAAIH,EAAJ,CACAA,EAAUI,WAAWF,EAAc,KACnC,IAAK,MAAOpK,EAAMkC,KAAgBqI,EAAO,CACrC,MAAMhC,EAAW,gBAAgBvI,aACjC,IAAK,MAAM0F,KAAUlB,SAASgG,iBAAiBjC,GAE3C7C,EAAO1F,MAAQ,UAGfmK,EAAQA,EAAMtH,MAAK,IAAM4C,EAAKC,EAAQ1F,EAAMkC,IAEnD,CACD,OAAOiI,CAZa,CAYR,EAGhB,IAAIM,iBAAiBJ,GAAUK,QAAQlG,SAAU,CAC7CmG,WAAW,EACXC,SAAS,IAIb,IAAAC,EAAeR"}
@@ -1,2 +1,2 @@
1
- import{T as t,c as e,r}from"./core-jHCq6GXZ.js";import{notify as s}from"./error-BZdmXuMQ.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-CA-8GLzg.js").then(i):await import("./py-tfDQPNNm.js").then(i)}))}
2
- //# sourceMappingURL=py-terminal-GLATgrHP.js.map
1
+ import{T as t,c as e,r}from"./core-sVxLb9Ur.js";import{notify as s}from"./error-BE-P8gi6.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-awU48RT6.js").then(i):await import("./py-DQxM9K2E.js").then(i)}))}
2
+ //# sourceMappingURL=py-terminal-CgSL5otC.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"py-terminal-GLATgrHP.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,EAEP,GAET"}
1
+ {"version":3,"file":"py-terminal-CgSL5otC.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,EAEP,GAET"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyscript/core",
3
- "version": "0.5.0-rc1",
3
+ "version": "0.5.0-rc2",
4
4
  "type": "module",
5
5
  "description": "PyScript",
6
6
  "module": "./index.js",
@@ -50,6 +50,7 @@ if RUNNING_IN_WORKER:
50
50
  globalThis.console.warn(message)
51
51
  window = NotSupported("pyscript.window", message)
52
52
  document = NotSupported("pyscript.document", message)
53
+ js_import = None
53
54
 
54
55
  sync = polyscript.xworker.sync
55
56
 
@@ -7,7 +7,7 @@ export default {
7
7
  "fetch.py": "import json\n\nimport js\nfrom pyscript.util import as_bytearray\n\n\n### wrap the response to grant Pythonic results\nclass _Response:\n def __init__(self, response):\n self._response = response\n\n # grant access to response.ok and other fields\n def __getattr__(self, attr):\n return getattr(self._response, attr)\n\n # exposed methods with Pythonic results\n async def arrayBuffer(self):\n buffer = await self._response.arrayBuffer()\n # works in Pyodide\n if hasattr(buffer, \"to_py\"):\n return buffer.to_py()\n # shims in MicroPython\n return memoryview(as_bytearray(buffer))\n\n async def blob(self):\n return await self._response.blob()\n\n async def bytearray(self):\n buffer = await self._response.arrayBuffer()\n return as_bytearray(buffer)\n\n async def json(self):\n return json.loads(await self.text())\n\n async def text(self):\n return await self._response.text()\n\n\n### allow direct await to _Response methods\nclass _DirectResponse:\n @staticmethod\n def setup(promise, response):\n promise._response = _Response(response)\n return promise._response\n\n def __init__(self, promise):\n self._promise = promise\n promise._response = None\n promise.arrayBuffer = self.arrayBuffer\n promise.blob = self.blob\n promise.bytearray = self.bytearray\n promise.json = self.json\n promise.text = self.text\n\n async def _response(self):\n if not self._promise._response:\n await self._promise\n return self._promise._response\n\n async def arrayBuffer(self):\n response = await self._response()\n return await response.arrayBuffer()\n\n async def blob(self):\n response = await self._response()\n return await response.blob()\n\n async def bytearray(self):\n response = await self._response()\n return await response.bytearray()\n\n async def json(self):\n response = await self._response()\n return await response.json()\n\n async def text(self):\n response = await self._response()\n return await response.text()\n\n\ndef fetch(url, **kw):\n # workaround Pyodide / MicroPython dict <-> js conversion\n options = js.JSON.parse(json.dumps(kw))\n awaited = lambda response, *args: _DirectResponse.setup(promise, response)\n promise = js.fetch(url, options).then(awaited)\n _DirectResponse(promise)\n return promise\n",
8
8
  "ffi.py": "try:\n import js\n from pyodide.ffi import create_proxy as _cp\n from pyodide.ffi import to_js as _py_tjs\n\n from_entries = js.Object.fromEntries\n\n def _tjs(value, **kw):\n if not hasattr(kw, \"dict_converter\"):\n kw[\"dict_converter\"] = from_entries\n return _py_tjs(value, **kw)\n\nexcept:\n from jsffi import create_proxy as _cp\n from jsffi import to_js as _tjs\n\ncreate_proxy = _cp\nto_js = _tjs\n",
9
9
  "flatted.py": "# https://www.npmjs.com/package/flatted\n\nimport json as _json\n\n\nclass _Known:\n def __init__(self):\n self.key = []\n self.value = []\n\n\nclass _String:\n def __init__(self, value):\n self.value = value\n\n\ndef _array_keys(value):\n keys = []\n i = 0\n for _ in value:\n keys.append(i)\n i += 1\n return keys\n\n\ndef _object_keys(value):\n keys = []\n for key in value:\n keys.append(key)\n return keys\n\n\ndef _is_array(value):\n return isinstance(value, list) or isinstance(value, tuple)\n\n\ndef _is_object(value):\n return isinstance(value, dict)\n\n\ndef _is_string(value):\n return isinstance(value, str)\n\n\ndef _index(known, input, value):\n input.append(value)\n index = str(len(input) - 1)\n known.key.append(value)\n known.value.append(index)\n return index\n\n\ndef _loop(keys, input, known, output):\n for key in keys:\n value = output[key]\n if isinstance(value, _String):\n _ref(key, input[int(value.value)], input, known, output)\n\n return output\n\n\ndef _ref(key, value, input, known, output):\n if _is_array(value) and not value in known:\n known.append(value)\n value = _loop(_array_keys(value), input, known, value)\n elif _is_object(value) and not value in known:\n known.append(value)\n value = _loop(_object_keys(value), input, known, value)\n\n output[key] = value\n\n\ndef _relate(known, input, value):\n if _is_string(value) or _is_array(value) or _is_object(value):\n try:\n return known.value[known.key.index(value)]\n except:\n return _index(known, input, value)\n\n return value\n\n\ndef _transform(known, input, value):\n if _is_array(value):\n output = []\n for val in value:\n output.append(_relate(known, input, val))\n return output\n\n if _is_object(value):\n obj = {}\n for key in value:\n obj[key] = _relate(known, input, value[key])\n return obj\n\n return value\n\n\ndef _wrap(value):\n if _is_string(value):\n return _String(value)\n\n if _is_array(value):\n i = 0\n for val in value:\n value[i] = _wrap(val)\n i += 1\n\n elif _is_object(value):\n for key in value:\n value[key] = _wrap(value[key])\n\n return value\n\n\ndef parse(value, *args, **kwargs):\n json = _json.loads(value, *args, **kwargs)\n wrapped = []\n for value in json:\n wrapped.append(_wrap(value))\n\n input = []\n for value in wrapped:\n if isinstance(value, _String):\n input.append(value.value)\n else:\n input.append(value)\n\n value = input[0]\n\n if _is_array(value):\n return _loop(_array_keys(value), input, [value], value)\n\n if _is_object(value):\n return _loop(_object_keys(value), input, [value], value)\n\n return value\n\n\ndef stringify(value, *args, **kwargs):\n known = _Known()\n input = []\n output = []\n i = int(_index(known, input, value))\n while i < len(input):\n output.append(_transform(known, input, input[i]))\n i += 1\n return _json.dumps(output, *args, **kwargs)\n",
10
- "magic_js.py": "import json\nimport sys\n\nimport js as globalThis\nfrom polyscript import config as _config\nfrom polyscript import js_modules\nfrom pyscript.util import NotSupported\n\nRUNNING_IN_WORKER = not hasattr(globalThis, \"document\")\n\nconfig = json.loads(globalThis.JSON.stringify(_config))\n\n\n# allow `from pyscript.js_modules.xxx import yyy`\nclass JSModule:\n def __init__(self, name):\n self.name = name\n\n def __getattr__(self, field):\n # avoid pyodide looking for non existent fields\n if not field.startswith(\"_\"):\n return getattr(getattr(js_modules, self.name), field)\n\n\n# generate N modules in the system that will proxy the real value\nfor name in globalThis.Reflect.ownKeys(js_modules):\n sys.modules[f\"pyscript.js_modules.{name}\"] = JSModule(name)\nsys.modules[\"pyscript.js_modules\"] = js_modules\n\nif RUNNING_IN_WORKER:\n import polyscript\n\n PyWorker = NotSupported(\n \"pyscript.PyWorker\",\n \"pyscript.PyWorker works only when running in the main thread\",\n )\n\n try:\n import js\n\n window = polyscript.xworker.window\n document = window.document\n js.document = document\n # this is the same as js_import on main and it lands modules on main\n js_import = window.Function(\n \"return (...urls) => Promise.all(urls.map((url) => import(url)))\"\n )()\n except:\n message = \"Unable to use `window` or `document` -> https://docs.pyscript.net/latest/faq/#sharedarraybuffer\"\n globalThis.console.warn(message)\n window = NotSupported(\"pyscript.window\", message)\n document = NotSupported(\"pyscript.document\", message)\n\n sync = polyscript.xworker.sync\n\n # in workers the display does not have a default ID\n # but there is a sync utility from xworker\n def current_target():\n return polyscript.target\n\nelse:\n import _pyscript\n from _pyscript import PyWorker, js_import\n\n window = globalThis\n document = globalThis.document\n sync = NotSupported(\n \"pyscript.sync\", \"pyscript.sync works only when running in a worker\"\n )\n\n # in MAIN the current element target exist, just use it\n def current_target():\n return _pyscript.target\n",
10
+ "magic_js.py": "import json\nimport sys\n\nimport js as globalThis\nfrom polyscript import config as _config\nfrom polyscript import js_modules\nfrom pyscript.util import NotSupported\n\nRUNNING_IN_WORKER = not hasattr(globalThis, \"document\")\n\nconfig = json.loads(globalThis.JSON.stringify(_config))\n\n\n# allow `from pyscript.js_modules.xxx import yyy`\nclass JSModule:\n def __init__(self, name):\n self.name = name\n\n def __getattr__(self, field):\n # avoid pyodide looking for non existent fields\n if not field.startswith(\"_\"):\n return getattr(getattr(js_modules, self.name), field)\n\n\n# generate N modules in the system that will proxy the real value\nfor name in globalThis.Reflect.ownKeys(js_modules):\n sys.modules[f\"pyscript.js_modules.{name}\"] = JSModule(name)\nsys.modules[\"pyscript.js_modules\"] = js_modules\n\nif RUNNING_IN_WORKER:\n import polyscript\n\n PyWorker = NotSupported(\n \"pyscript.PyWorker\",\n \"pyscript.PyWorker works only when running in the main thread\",\n )\n\n try:\n import js\n\n window = polyscript.xworker.window\n document = window.document\n js.document = document\n # this is the same as js_import on main and it lands modules on main\n js_import = window.Function(\n \"return (...urls) => Promise.all(urls.map((url) => import(url)))\"\n )()\n except:\n message = \"Unable to use `window` or `document` -> https://docs.pyscript.net/latest/faq/#sharedarraybuffer\"\n globalThis.console.warn(message)\n window = NotSupported(\"pyscript.window\", message)\n document = NotSupported(\"pyscript.document\", message)\n js_import = None\n\n sync = polyscript.xworker.sync\n\n # in workers the display does not have a default ID\n # but there is a sync utility from xworker\n def current_target():\n return polyscript.target\n\nelse:\n import _pyscript\n from _pyscript import PyWorker, js_import\n\n window = globalThis\n document = globalThis.document\n sync = NotSupported(\n \"pyscript.sync\", \"pyscript.sync works only when running in a worker\"\n )\n\n # in MAIN the current element target exist, just use it\n def current_target():\n return _pyscript.target\n",
11
11
  "storage.py": "from polyscript import storage as _storage\nfrom pyscript.flatted import parse as _parse\nfrom pyscript.flatted import stringify as _stringify\n\n\n# convert a Python value into an IndexedDB compatible entry\ndef _to_idb(value):\n if value is None:\n return _stringify([\"null\", 0])\n if isinstance(value, (bool, float, int, str, list, dict, tuple)):\n return _stringify([\"generic\", value])\n if isinstance(value, bytearray):\n return _stringify([\"bytearray\", [v for v in value]])\n if isinstance(value, memoryview):\n return _stringify([\"memoryview\", [v for v in value]])\n raise TypeError(f\"Unexpected value: {value}\")\n\n\n# convert an IndexedDB compatible entry into a Python value\ndef _from_idb(value):\n (\n kind,\n result,\n ) = _parse(value)\n if kind == \"null\":\n return None\n if kind == \"generic\":\n return result\n if kind == \"bytearray\":\n return bytearray(result)\n if kind == \"memoryview\":\n return memoryview(bytearray(result))\n return value\n\n\nclass Storage(dict):\n def __init__(self, store):\n super().__init__({k: _from_idb(v) for k, v in store.entries()})\n self.__store__ = store\n\n def __delitem__(self, attr):\n self.__store__.delete(attr)\n super().__delitem__(attr)\n\n def __setitem__(self, attr, value):\n self.__store__.set(attr, _to_idb(value))\n super().__setitem__(attr, value)\n\n def clear(self):\n self.__store__.clear()\n super().clear()\n\n async def sync(self):\n await self.__store__.sync()\n\n\nasync def storage(name=\"\", storage_class=Storage):\n if not name:\n raise ValueError(\"The storage name must be defined\")\n return storage_class(await _storage(f\"@pyscript/{name}\"))\n",
12
12
  "util.py": "import js\n\n\ndef as_bytearray(buffer):\n ui8a = js.Uint8Array.new(buffer)\n size = ui8a.length\n ba = bytearray(size)\n for i in range(0, size):\n ba[i] = ui8a[i]\n return ba\n\n\nclass NotSupported:\n \"\"\"\n Small helper that raises exceptions if you try to get/set any attribute on\n it.\n \"\"\"\n\n def __init__(self, name, error):\n object.__setattr__(self, \"name\", name)\n object.__setattr__(self, \"error\", error)\n\n def __repr__(self):\n return f\"<NotSupported {self.name} [{self.error}]>\"\n\n def __getattr__(self, attr):\n raise AttributeError(self.error)\n\n def __setattr__(self, attr, value):\n raise AttributeError(self.error)\n\n def __call__(self, *args):\n raise TypeError(self.error)\n",
13
13
  "web": {