@pyscript/core 0.4.10 → 0.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/{codemirror-HQuHfCf6.js → codemirror-ZDIr9iJj.js} +2 -2
  2. package/dist/{codemirror-HQuHfCf6.js.map → codemirror-ZDIr9iJj.js.map} +1 -1
  3. package/dist/{codemirror_commands-Be_sxRoS.js → codemirror_commands-NWDrre9v.js} +2 -2
  4. package/dist/{codemirror_commands-Be_sxRoS.js.map → codemirror_commands-NWDrre9v.js.map} +1 -1
  5. package/dist/{codemirror_lang-python-6K8_k_-l.js → codemirror_lang-python-dHfsE0jo.js} +2 -2
  6. package/dist/{codemirror_lang-python-6K8_k_-l.js.map → codemirror_lang-python-dHfsE0jo.js.map} +1 -1
  7. package/dist/{codemirror_language-xLdmYCLC.js → codemirror_language-BPcKWOkU.js} +2 -2
  8. package/dist/{codemirror_language-xLdmYCLC.js.map → codemirror_language-BPcKWOkU.js.map} +1 -1
  9. package/dist/codemirror_view-DJFeHd78.js +2 -0
  10. package/dist/codemirror_view-DJFeHd78.js.map +1 -0
  11. package/dist/core-CvTioHxa.js +3 -0
  12. package/dist/core-CvTioHxa.js.map +1 -0
  13. package/dist/core.js +1 -1
  14. package/dist/{deprecations-manager-D6IyV1wc.js → deprecations-manager-Bm0TU0rg.js} +2 -2
  15. package/dist/{deprecations-manager-D6IyV1wc.js.map → deprecations-manager-Bm0TU0rg.js.map} +1 -1
  16. package/dist/{error-MzpFwTpe.js → error-BLGrvvXt.js} +2 -2
  17. package/dist/{error-MzpFwTpe.js.map → error-BLGrvvXt.js.map} +1 -1
  18. package/dist/{index-DV62RffY.js → index-Db1V4drY.js} +2 -2
  19. package/dist/{index-DV62RffY.js.map → index-Db1V4drY.js.map} +1 -1
  20. package/dist/py-editor-CaickG-k.js +2 -0
  21. package/dist/py-editor-CaickG-k.js.map +1 -0
  22. package/dist/py-terminal-D1RN49ok.js +2 -0
  23. package/dist/py-terminal-D1RN49ok.js.map +1 -0
  24. package/package.json +5 -5
  25. package/src/core.js +3 -0
  26. package/src/plugins/py-editor.js +30 -5
  27. package/src/plugins/py-terminal.js +193 -144
  28. package/types/core.d.ts +3 -6
  29. package/types/plugins/py-terminal.d.ts +1 -2
  30. package/dist/codemirror_view-DnYSj9rm.js +0 -2
  31. package/dist/codemirror_view-DnYSj9rm.js.map +0 -1
  32. package/dist/core-DG139wN3.js +0 -3
  33. package/dist/core-DG139wN3.js.map +0 -1
  34. package/dist/py-editor-KuhLAVrs.js +0 -2
  35. package/dist/py-editor-KuhLAVrs.js.map +0 -1
  36. package/dist/py-terminal-BMEpK70u.js +0 -2
  37. package/dist/py-terminal-BMEpK70u.js.map +0 -1
@@ -0,0 +1,2 @@
1
+ import{T as t,d as e,X as r,H as n,s as o}from"./core-CvTioHxa.js";let i=0;const s=t=>`${t}-editor-${i++}`,a=new Map,c=new Map,l={worker:{codeBeforeRun:()=>o,onReady:({runAsync:t,io:e},{sync:r})=>{e.stdout=t=>r.write(t),e.stderr=t=>r.writeErr(t),r.revoke(),r.runAsync=t}}};async function u({currentTarget:t}){const{env:e,pySrc:o,outDiv:i}=this,s=!!t;if(s&&(t.disabled=!0,i.innerHTML=""),!a.has(e)){const t=URL.createObjectURL(new Blob([""])),o={type:this.interpreter},{config:i}=this;if(i){o.configURL=i;const{parse:t}=i.endsWith(".toml")?await import("./toml-CvAfdf9_.js"):JSON;o.config=t(await fetch(i).then((t=>t.text())))}const s=r.call(new n(null,l),t,o),{sync:c}=s,{promise:u,resolve:d}=Promise.withResolvers();a.set(e,u),c.revoke=()=>{URL.revokeObjectURL(t),d(s)}}a.get(e).then((e=>{e.onerror=({error:t})=>{s&&(i.innerHTML+=`<span style='color:red'>${t.message||t}</span>\n`),console.error(t)};const r=()=>{s&&(t.disabled=!1)},{sync:n}=e;n.write=t=>{s&&(i.innerText+=`${t}\n`)},n.writeErr=t=>{s&&(i.innerHTML+=`<span style='color:red'>${t}</span>\n`)},n.runAsync(o).then(r,r)}))}const d=(t,e)=>{const r=document.createElement("div");r.className=`${e}-editor-input`,r.setAttribute("aria-label","Python Script Area");const n=((t,e)=>{const r=document.createElement("button");return r.className=`absolute ${e}-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",t),r})(t,e),o=document.createElement("div");return o.addEventListener("keydown",(t=>{t.stopPropagation()})),r.append(n,o),r},m=(t,e)=>{const r=document.createElement("div");r.className=`${e}-editor-box`;const n=d(t,e),o=(t=>{const e=document.createElement("div");return e.className=`${t}-editor-output`,e.id=`${s(t)}-output`,e})(e);return r.append(n,o),[r,o]},p=async(t,r,n)=>{const[{basicSetup:o,EditorView:i},{Compartment:a},{python:l},{indentUnit:d},{keymap:p},{defaultKeymap:f}]=await Promise.all([import("./codemirror-ZDIr9iJj.js"),import("./codemirror_state-BKbyfKsm.js"),import("./codemirror_lang-python-dHfsE0jo.js"),import("./codemirror_language-BPcKWOkU.js").then((function(t){return t.x})),import("./codemirror_view-DJFeHd78.js").then((function(t){return t.q})),import("./codemirror_commands-NWDrre9v.js")]),h=t.hasAttribute("setup"),g=t.hasAttribute("config"),v=`${n}-${t.getAttribute("env")||s(r)}`;if(g&&c.has(v))throw new SyntaxError(c.get(v)?`duplicated config for env: ${v}`:`unable to add a config to the env: ${v}`);c.set(v,g);const y=t.src?await fetch(t.src).then((t=>t.text())):t.textContent,w={interpreter:n,env:v,config:g&&new URL(t.getAttribute("config"),location.href).href,get pySrc(){return h?y:k.state.doc.toString()},get outDiv(){return h?null:A}};if(h)return void u.call(w,{currentTarget:null});const b=t.getAttribute("target");let $;if(b){if($=document.getElementById(b)||document.querySelector(b),!$)throw new Error(`Unknown target ${b}`)}else $=document.createElement(`${r}-editor`),$.style.display="block",t.after($);$.id||($.id=s(r)),$.hasAttribute("exec-id")||$.setAttribute("exec-id",0),$.hasAttribute("root")||$.setAttribute("root",$.id);const E=u.bind(w),[x,A]=m(E,r);x.dataset.env=t.hasAttribute("env")?v:n;const L=x.querySelector(`.${r}-editor-input > div`).attachShadow({mode:"open"});L.innerHTML="<style> :host { all: initial; }</style>",$.appendChild(x);const C=e(t.textContent).trim(),S=/^(\s+)/m.test(C)?RegExp.$1:" ",k=new i({extensions:[d.of(S),(new a).of(l()),p.of([...f,{key:"Ctrl-Enter",run:E,preventDefault:!0},{key:"Cmd-Enter",run:E,preventDefault:!0},{key:"Shift-Enter",run:E,preventDefault:!0}]),o],parent:L,doc:C});k.focus()};let f=0,h=Promise.resolve();const g=()=>{f=0,v()},v=()=>{if(!f){f=setTimeout(g,250);for(const[e,r]of t){const t=`script[type="${e}-editor"]`;for(const n of document.querySelectorAll(t))n.type+="-active",h=h.then((()=>p(n,e,r)))}return h}};new MutationObserver(v).observe(document,{childList:!0,subtree:!0});var y=v();export{y as default};
2
+ //# sourceMappingURL=py-editor-CaickG-k.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"py-editor-CaickG-k.js","sources":["../src/plugins/py-editor.js"],"sourcesContent":["// PyScript py-editor plugin\nimport { Hook, XWorker, dedent } from \"polyscript/exports\";\nimport { TYPES, 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 = (line) => sync.write(line);\n io.stderr = (line) => sync.writeErr(line);\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 = config;\n const { parse } = config.endsWith(\".toml\")\n ? await import(/* webpackIgnore: true */ \"../3rd-party/toml.js\")\n : JSON;\n details.config = parse(await fetch(config).then((r) => r.text()));\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 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 = (listener, 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\", listener);\n return runButton;\n};\n\nconst makeEditorDiv = (listener, 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(listener, 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 = (listener, type) => {\n const boxDiv = document.createElement(\"div\");\n boxDiv.className = `${type}-editor-box`;\n\n const editorDiv = makeEditorDiv(listener, type);\n const outDiv = makeOutDiv(type);\n boxDiv.append(editorDiv, outDiv);\n\n return [boxDiv, outDiv];\n};\n\nconst init = async (script, type, interpreter) => {\n const [\n { basicSetup, EditorView },\n { Compartment },\n { python },\n { indentUnit },\n { keymap },\n { defaultKeymap },\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 const 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 const source = script.src\n ? await fetch(script.src).then((b) => b.text())\n : script.textContent;\n const context = {\n interpreter,\n env,\n config:\n hasConfig &&\n new URL(script.getAttribute(\"config\"), location.href).href,\n get pySrc() {\n return isSetup ? source : editor.state.doc.toString();\n },\n get outDiv() {\n return isSetup ? null : outDiv;\n },\n };\n\n if (isSetup) {\n execute.call(context, { currentTarget: null });\n return;\n }\n\n const selector = script.getAttribute(\"target\");\n\n let 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(`${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 listener = execute.bind(context);\n const [boxDiv, outDiv] = makeBoxDiv(listener, 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 = /^(\\s+)/m.test(doc) ? RegExp.$1 : \" \";\n\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 ]),\n basicSetup,\n ],\n parent,\n doc,\n });\n\n editor.focus();\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","line","write","stderr","writeErr","revoke","async","execute","currentTarget","env","pySrc","outDiv","this","hasRunButton","disabled","innerHTML","has","srcLink","URL","createObjectURL","Blob","details","interpreter","config","configURL","parse","endsWith","import","JSON","fetch","then","r","text","xworker","XWorker","call","Hook","promise","resolve","Promise","withResolvers","set","revokeObjectURL","get","onerror","error","message","console","enable","str","innerText","makeEditorDiv","listener","editorDiv","document","createElement","className","setAttribute","runButton","addEventListener","makeRunButton","editorShadowContainer","event","stopPropagation","append","makeBoxDiv","boxDiv","makeOutDiv","init","script","basicSetup","EditorView","Compartment","python","indentUnit","keymap","defaultKeymap","all","n","x","q","isSetup","hasAttribute","hasConfig","getAttribute","SyntaxError","source","src","b","textContent","context","location","href","editor","state","doc","toString","selector","target","getElementById","querySelector","Error","style","display","after","bind","dataset","parent","attachShadow","mode","appendChild","dedent","trim","indentation","test","RegExp","$1","extensions","of","key","run","preventDefault","focus","timeout","queue","resetTimeout","pyEditor","setTimeout","TYPES","querySelectorAll","MutationObserver","observe","childList","subtree","pyEditor$1"],"mappings":"mEAMA,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,OAAUC,GAASF,EAAKG,MAAMD,GACjCH,EAAGK,OAAUF,GAASF,EAAKK,SAASH,GACpCF,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,CACRF,EAAQG,UAAYD,EACpB,MAAME,MAAEA,GAAUF,EAAOG,SAAS,eACtBC,OAAiC,sBACvCC,KACNP,EAAQE,OAASE,QAAYI,MAAMN,GAAQO,MAAMC,GAAMA,EAAEC,SAC5D,CAED,MAAMC,EAAUC,EAAQC,KAAK,IAAIC,EAAK,KAAM5C,GAAQyB,EAASI,IAEvDtB,KAAEA,GAASkC,GACXI,QAAEA,EAAOC,QAAEA,GAAYC,QAAQC,gBACrCnD,EAAKoD,IAAIhC,E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C,IAAK,MAAMiF,KAAUf,SAAS2E,iBAAiBhC,GAE3C5B,EAAOjF,MAAQ,UAGfwI,EAAQA,EAAM9F,MAAK,IAAMsC,EAAKC,EAAQjF,EAAMkC,IAEnD,CACD,OAAOsG,CAZa,CAYR,EAGhB,IAAIM,iBAAiBJ,GAAUK,QAAQ7E,SAAU,CAC7C8E,WAAW,EACXC,SAAS,IAIb,IAAAC,EAAeR"}
@@ -0,0 +1,2 @@
1
+ import{T as e,c as t,e as r,a as n}from"./core-CvTioHxa.js";import{notify as i}from"./error-BLGrvvXt.js";const o=[],s=e=>{throw i(e),new Error(e)},a=({attributes:{worker:e}})=>!e,d=new WeakSet;let l=!0;const c=({interpreter:e,io:t,run:r,type:n},{sync:i})=>{if(!i.is_pyterminal())return;r("from polyscript import currentScript as _; __terminal__ = _.terminal; del _"),t.stderr=e=>{i.pyterminal_write(`${e.message||e}\n`)};const o="mpy"===n;if(o){const r=new TextEncoder,n=()=>{if(a.length)for(let t=0,n=r.encode(`${a}\r`);t<n.length;t++){const r=e.replProcessChar(n[t]);if(r)throw new Error(`replProcessChar failed with code ${r}`)}a=">>> ",a=t.stdin(),n()};e.setStderr=Object,e.setStdout=({write:e})=>{t.stdout=t=>t.startsWith(`>>> ${a}`)?0:e(`${t}\n`)},e.setStdin=({stdin:e})=>{t.stdin=e},e.registerJsModule("code",{interact(){e.replInit(),a="",n()}})}const s=new TextDecoder;let a="";const d={isatty:!0,write:e=>(a=o?e:s.decode(e),i.pyterminal_write(a),e.length)};e.setStdout(d),e.setStderr(d),e.setStdin({isatty:!0,stdin:()=>i.pyterminal_read(a)})},m=async e=>{const[{Terminal:t},{Readline:i},{FitAddon:o}]=await Promise.all([import("./xterm-DqawCVsv.js"),import("./xterm-readline-D247p8vq.js"),import("./xterm_addon-fit--gyF3PcZ.js")]),s=new i,a=r=>{let i=e;const a=e.getAttribute("target");if(a){if(i=document.getElementById(a)||document.querySelector(a),!i)throw new Error(`Unknown target ${a}`)}else i=document.createElement("py-terminal"),i.style.display="block",e.after(i);const d=new t({theme:{background:"#191A19",foreground:"#F5F2E7"},...r}),l=new o;return d.loadAddon(l),d.loadAddon(s),d.open(i),l.fit(),d.focus(),n(e,"terminal",{value:d}),d};e.hasAttribute("worker")?(r.main.onWorker.add((function e(t,n){d.has(n)||(d.add(n),r.main.onWorker.delete(e),a({disableStdin:!1,cursorBlink:!0,cursorStyle:"block"}),n.sync.is_pyterminal=()=>!0,n.sync.pyterminal_read=s.read.bind(s),n.sync.pyterminal_write=s.write.bind(s))})),r.worker.onReady.add(c)):r.main.onReady.add((function e({interpreter:t,io:n,run:i,type:o}){console.warn("py-terminal is read only on main thread"),r.main.onReady.delete(e),globalThis.__py_terminal__=a({disableStdin:!0,cursorBlink:!1,cursorStyle:"underline"}),i("from js import __py_terminal__ as __terminal__"),delete globalThis.__py_terminal__,n.stderr=e=>{s.write(`${e.message||e}\n`)};const d="mpy"===o;d&&(t.setStderr=Object,t.setStdin=Object,t.setStdout=({write:e})=>{n.stdout=t=>e(`${t}\n`)});const l=new TextDecoder;let c="";const m={isatty:!0,write:e=>(c=d?e:l.decode(e),s.write(c),e.length)};t.setStdout(m),t.setStderr(m),t.setStdin({isatty:!0,stdin:()=>s.read(c)})}))};for(const r of e.keys()){const e=`script[type="${r}"][terminal],${r}-script[terminal]`;o.push(e),t.set(e,(async e=>{const t=document.querySelectorAll(o.join(","));[].filter.call(t,a).length>1&&s("You can use at most 1 main terminal"),l&&(l=!1,document.head.append(Object.assign(document.createElement("link"),{rel:"stylesheet",href:new URL("./xterm.css",import.meta.url)}))),await m(e)}))}
2
+ //# sourceMappingURL=py-terminal-D1RN49ok.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"py-terminal-D1RN49ok.js","sources":["../src/plugins/py-terminal.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { TYPES, hooks } from \"../core.js\";\nimport { notify } from \"./error.js\";\nimport { customObserver, defineProperty } from \"polyscript/exports\";\n\n// will contain all valid selectors\nconst SELECTORS = [];\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\nconst bootstrapped = new WeakSet();\n\nlet addStyle = true;\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 (!sync.is_pyterminal()) return;\n\n // in workers it's always safe to grab the polyscript currentScript\n // the ugly `_` dance is due MicroPython not able to import via:\n // `from polyscript.currentScript import terminal as __terminal__`\n run(\n \"from polyscript import currentScript as _; __terminal__ = _.terminal; del _\",\n );\n\n // This part is shared among both Pyodide and MicroPython\n io.stderr = (error) => {\n sync.pyterminal_write(`${error.message || error}\\n`);\n };\n\n const isMicroPython = type === \"mpy\";\n\n // MicroPython has no code or code.interact()\n // This part patches it in a way that simulate\n // the code.interact() module in Pyodide.\n if (isMicroPython) {\n const encoder = new TextEncoder();\n const processData = () => {\n if (data.length) {\n for (\n let i = 0, b = encoder.encode(`${data}\\r`);\n i < b.length;\n i++\n ) {\n const code = interpreter.replProcessChar(b[i]);\n if (code) {\n throw new Error(\n `replProcessChar failed with code ${code}`,\n );\n }\n }\n }\n data = \">>> \";\n data = io.stdin();\n processData();\n };\n interpreter.setStderr = Object; // as no-op\n interpreter.setStdout = ({ write }) => {\n io.stdout = (str) => {\n // avoid duplicated outcome due i/o + readline\n const ignore = str.startsWith(`>>> ${data}`);\n return ignore ? 0 : write(`${str}\\n`);\n };\n };\n interpreter.setStdin = ({ stdin }) => {\n io.stdin = stdin;\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 interpreter.replInit();\n data = \"\";\n processData();\n },\n });\n }\n\n // This part is inevitably duplicated as external scope\n // can't be reached by workers out of the box.\n // The detail is that here we use sync though, not readline.\n const decoder = new TextDecoder();\n let data = \"\";\n const generic = {\n isatty: true,\n write(buffer) {\n data = isMicroPython ? buffer : decoder.decode(buffer);\n sync.pyterminal_write(data);\n return buffer.length;\n },\n };\n interpreter.setStdout(generic);\n interpreter.setStderr(generic);\n interpreter.setStdin({\n isatty: true,\n stdin: () => sync.pyterminal_read(data),\n });\n};\n\nconst pyTerminal = async (element) => {\n // lazy load these only when a valid terminal is found\n const [{ Terminal }, { Readline }, { FitAddon }] = await Promise.all([\n import(/* webpackIgnore: true */ \"../3rd-party/xterm.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/xterm-readline.js\"),\n import(/* webpackIgnore: true */ \"../3rd-party/xterm_addon-fit.js\"),\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.open(target);\n fitAddon.fit();\n terminal.focus();\n defineProperty(element, \"terminal\", { value: terminal });\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 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(`${error.message || error}\\n`);\n };\n\n const isMicroPython = type === \"mpy\";\n\n if (isMicroPython) {\n interpreter.setStderr = Object; // as no-op\n interpreter.setStdin = Object; // as no-op\n interpreter.setStdout = ({ write }) => {\n io.stdout = (str) => write(`${str}\\n`);\n };\n }\n\n // This part is inevitably duplicated as external scope\n // can't be reached by workers out of the box.\n // The detail is that here we use readline here, not sync.\n const decoder = new TextDecoder();\n let data = \"\";\n const generic = {\n isatty: true,\n write(buffer) {\n data = isMicroPython ? buffer : 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: true,\n stdin: () => readline.read(data),\n });\n });\n }\n};\n\nfor (const key of TYPES.keys()) {\n const selector = `script[type=\"${key}\"][terminal],${key}-script[terminal]`;\n SELECTORS.push(selector);\n customObserver.set(selector, async (element) => {\n // if (key === \"mpy\") notifyAndThrow(`Unsupported ${key} terminal.`);\n\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: new URL(\"./xterm.css\", import.meta.url),\n }),\n );\n }\n\n await pyTerminal(element);\n });\n}\n"],"names":["SELECTORS","notifyAndThrow","message","notify","Error","onceOnMain","attributes","worker","bootstrapped","WeakSet","addStyle","workerReady","interpreter","io","run","type","sync","is_pyterminal","stderr","error","pyterminal_write","isMicroPython","encoder","TextEncoder","processData","data","length","i","b","encode","code","replProcessChar","stdin","setStderr","Object","setStdout","write","stdout","str","startsWith","setStdin","registerJsModule","interact","replInit","decoder","TextDecoder","generic","isatty","buffer","decode","pyterminal_read","pyTerminal","async","element","Terminal","Readline","FitAddon","Promise","all","import","readline","init","options","target","selector","getAttribute","document","getElementById","querySelector","createElement","style","display","after","terminal","theme","background","foreground","fitAddon","loadAddon","open","fit","focus","defineProperty","value","hasAttribute","hooks","main","onWorker","add","_","xworker","has","delete","disableStdin","cursorBlink","cursorStyle","read","bind","onReady","console","warn","globalThis","__py_terminal__","key","TYPES","keys","push","customObserver","set","terminals","querySelectorAll","join","filter","call","head","append","assign","rel","href","URL","url"],"mappings":"yGAMA,MAAMA,EAAY,GAIZC,EAAkBC,IAEpB,MADAC,EAAOD,GACD,IAAIE,MAAMF,EAAQ,EAGtBG,EAAa,EAAGC,YAAcC,cAAgBA,EAE9CC,EAAe,IAAIC,QAEzB,IAAIC,GAAW,EAKf,MAAMC,EAAc,EAAGC,cAAaC,KAAIC,MAAKC,SAAUC,WACnD,IAAKA,EAAKC,gBAAiB,OAK3BH,EACI,+EAIJD,EAAGK,OAAUC,IACTH,EAAKI,iBAAiB,GAAGD,EAAMjB,SAAWiB,MAAU,EAGxD,MAAME,EAAyB,QAATN,EAKtB,GAAIM,EAAe,CACf,MAAMC,EAAU,IAAIC,YACdC,EAAc,KAChB,GAAIC,EAAKC,OACL,IACI,IAAIC,EAAI,EAAGC,EAAIN,EAAQO,OAAO,GAAGJ,OACjCE,EAAIC,EAAEF,OACNC,IACF,CACE,MAAMG,EAAOlB,EAAYmB,gBAAgBH,EA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AAMX,GACRuB,EAAOtB,SAGtBd,EAAYuB,UAAUW,GACtBlC,EAAYqB,UAAUa,GACtBlC,EAAY4B,SAAS,CACjBO,QAAQ,EACRf,MAAO,IAAM4B,EAASoC,KAAKvE,IAE3C,GACK,EAGL,IAAK,MAAM8E,KAAOC,EAAMC,OAAQ,CAC5B,MAAMzC,EAAW,gBAAgBuC,iBAAmBA,qBACpDvG,EAAU0G,KAAK1C,GACf2C,EAAeC,IAAI5C,GAAUZ,MAAOC,IAIhC,MAAMwD,EAAY3C,SAAS4C,iBAAiB9G,EAAU+G,KAAK,MACvD,GAAGC,OAAOC,KAAKJ,EAAWxG,GAAYqB,OAAS,GAC/CzB,EAAe,uCAGfS,IACAA,GAAW,EACXwD,SAASgD,KAAKC,OACVjF,OAAOkF,OAAOlD,SAASG,cAAc,QAAS,CAC1CgD,IAAK,aACLC,KAAM,IAAIC,IAAI,0BAA2BC,eAK/CrE,EAAWE,EAAQ,GAEjC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyscript/core",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "type": "module",
5
5
  "description": "PyScript",
6
6
  "module": "./index.js",
@@ -42,17 +42,17 @@
42
42
  "dependencies": {
43
43
  "@ungap/with-resolvers": "^0.1.0",
44
44
  "basic-devtools": "^0.1.6",
45
- "polyscript": "^0.11.3",
45
+ "polyscript": "^0.12.1",
46
46
  "sticky-module": "^0.1.1",
47
47
  "to-json-callback": "^0.1.1",
48
48
  "type-checked-collections": "^0.1.7"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@codemirror/commands": "^6.3.3",
52
- "@codemirror/lang-python": "^6.1.4",
52
+ "@codemirror/lang-python": "^6.1.5",
53
53
  "@codemirror/language": "^6.10.1",
54
54
  "@codemirror/state": "^6.4.1",
55
- "@codemirror/view": "^6.26.0",
55
+ "@codemirror/view": "^6.26.1",
56
56
  "@playwright/test": "^1.42.1",
57
57
  "@rollup/plugin-commonjs": "^25.0.7",
58
58
  "@rollup/plugin-node-resolve": "^15.2.3",
@@ -62,7 +62,7 @@
62
62
  "chokidar": "^3.6.0",
63
63
  "codemirror": "^6.0.1",
64
64
  "eslint": "^8.57.0",
65
- "rollup": "^4.13.0",
65
+ "rollup": "^4.13.2",
66
66
  "rollup-plugin-postcss": "^4.0.2",
67
67
  "rollup-plugin-string": "^3.0.0",
68
68
  "static-handler": "^0.4.3",
package/src/core.js CHANGED
@@ -26,6 +26,9 @@ import { ErrorCode } from "./exceptions.js";
26
26
  import { robustFetch as fetch, getText } from "./fetch.js";
27
27
  import { hooks, main, worker, codeFor, createFunction } from "./hooks.js";
28
28
 
29
+ import stdlib from "./stdlib.js";
30
+ export { stdlib };
31
+
29
32
  // generic helper to disambiguate between custom element and script
30
33
  const isScript = ({ tagName }) => tagName === "SCRIPT";
31
34
 
@@ -1,6 +1,6 @@
1
1
  // PyScript py-editor plugin
2
2
  import { Hook, XWorker, dedent } from "polyscript/exports";
3
- import { TYPES } from "../core.js";
3
+ import { TYPES, stdlib } from "../core.js";
4
4
 
5
5
  const 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>`;
6
6
 
@@ -8,9 +8,11 @@ let id = 0;
8
8
  const getID = (type) => `${type}-editor-${id++}`;
9
9
 
10
10
  const envs = new Map();
11
+ const configs = new Map();
11
12
 
12
13
  const hooks = {
13
14
  worker: {
15
+ codeBeforeRun: () => stdlib,
14
16
  // works on both Pyodide and MicroPython
15
17
  onReady: ({ runAsync, io }, { sync }) => {
16
18
  io.stdout = (line) => sync.write(line);
@@ -32,9 +34,17 @@ async function execute({ currentTarget }) {
32
34
 
33
35
  if (!envs.has(env)) {
34
36
  const srcLink = URL.createObjectURL(new Blob([""]));
35
- const xworker = XWorker.call(new Hook(null, hooks), srcLink, {
36
- type: this.interpreter,
37
- });
37
+ const details = { type: this.interpreter };
38
+ const { config } = this;
39
+ if (config) {
40
+ details.configURL = config;
41
+ const { parse } = config.endsWith(".toml")
42
+ ? await import(/* webpackIgnore: true */ "../3rd-party/toml.js")
43
+ : JSON;
44
+ details.config = parse(await fetch(config).then((r) => r.text()));
45
+ }
46
+
47
+ const xworker = XWorker.call(new Hook(null, hooks), srcLink, details);
38
48
 
39
49
  const { sync } = xworker;
40
50
  const { promise, resolve } = Promise.withResolvers();
@@ -138,13 +148,28 @@ const init = async (script, type, interpreter) => {
138
148
  ]);
139
149
 
140
150
  const isSetup = script.hasAttribute("setup");
151
+ const hasConfig = script.hasAttribute("config");
141
152
  const env = `${interpreter}-${script.getAttribute("env") || getID(type)}`;
153
+
154
+ if (hasConfig && configs.has(env)) {
155
+ throw new SyntaxError(
156
+ configs.get(env)
157
+ ? `duplicated config for env: ${env}`
158
+ : `unable to add a config to the env: ${env}`,
159
+ );
160
+ }
161
+
162
+ configs.set(env, hasConfig);
163
+
142
164
  const source = script.src
143
165
  ? await fetch(script.src).then((b) => b.text())
144
166
  : script.textContent;
145
167
  const context = {
146
168
  interpreter,
147
169
  env,
170
+ config:
171
+ hasConfig &&
172
+ new URL(script.getAttribute("config"), location.href).href,
148
173
  get pySrc() {
149
174
  return isSetup ? source : editor.state.doc.toString();
150
175
  },
@@ -225,7 +250,7 @@ const resetTimeout = () => {
225
250
  };
226
251
 
227
252
  // triggered both ASAP on the living DOM and via MutationObserver later
228
- const pyEditor = async () => {
253
+ const pyEditor = () => {
229
254
  if (timeout) return;
230
255
  timeout = setTimeout(resetTimeout, 250);
231
256
  for (const [type, interpreter] of TYPES) {
@@ -1,11 +1,10 @@
1
1
  // PyScript py-terminal plugin
2
2
  import { TYPES, hooks } from "../core.js";
3
3
  import { notify } from "./error.js";
4
- import { defineProperty } from "polyscript/exports";
4
+ import { customObserver, defineProperty } from "polyscript/exports";
5
5
 
6
- const SELECTOR = [...TYPES.keys()]
7
- .map((type) => `script[type="${type}"][terminal],${type}-script[terminal]`)
8
- .join(",");
6
+ // will contain all valid selectors
7
+ const SELECTORS = [];
9
8
 
10
9
  // show the error on main and
11
10
  // stops the module from keep executing
@@ -14,8 +13,6 @@ const notifyAndThrow = (message) => {
14
13
  throw new Error(message);
15
14
  };
16
15
 
17
- const notParsedYet = (script) => !bootstrapped.has(script);
18
-
19
16
  const onceOnMain = ({ attributes: { worker } }) => !worker;
20
17
 
21
18
  const bootstrapped = new WeakSet();
@@ -25,11 +22,68 @@ let addStyle = true;
25
22
  // this callback will be serialized as string and it never needs
26
23
  // to be invoked multiple times. Each xworker here is bootstrapped
27
24
  // only once thanks to the `sync.is_pyterminal()` check.
28
- const workerReady = ({ interpreter, io, run }, { sync }) => {
25
+ const workerReady = ({ interpreter, io, run, type }, { sync }) => {
29
26
  if (!sync.is_pyterminal()) return;
30
27
 
31
28
  // in workers it's always safe to grab the polyscript currentScript
32
- run("from polyscript.currentScript import terminal as __terminal__");
29
+ // the ugly `_` dance is due MicroPython not able to import via:
30
+ // `from polyscript.currentScript import terminal as __terminal__`
31
+ run(
32
+ "from polyscript import currentScript as _; __terminal__ = _.terminal; del _",
33
+ );
34
+
35
+ // This part is shared among both Pyodide and MicroPython
36
+ io.stderr = (error) => {
37
+ sync.pyterminal_write(`${error.message || error}\n`);
38
+ };
39
+
40
+ const isMicroPython = type === "mpy";
41
+
42
+ // MicroPython has no code or code.interact()
43
+ // This part patches it in a way that simulate
44
+ // the code.interact() module in Pyodide.
45
+ if (isMicroPython) {
46
+ const encoder = new TextEncoder();
47
+ const processData = () => {
48
+ if (data.length) {
49
+ for (
50
+ let i = 0, b = encoder.encode(`${data}\r`);
51
+ i < b.length;
52
+ i++
53
+ ) {
54
+ const code = interpreter.replProcessChar(b[i]);
55
+ if (code) {
56
+ throw new Error(
57
+ `replProcessChar failed with code ${code}`,
58
+ );
59
+ }
60
+ }
61
+ }
62
+ data = ">>> ";
63
+ data = io.stdin();
64
+ processData();
65
+ };
66
+ interpreter.setStderr = Object; // as no-op
67
+ interpreter.setStdout = ({ write }) => {
68
+ io.stdout = (str) => {
69
+ // avoid duplicated outcome due i/o + readline
70
+ const ignore = str.startsWith(`>>> ${data}`);
71
+ return ignore ? 0 : write(`${str}\n`);
72
+ };
73
+ };
74
+ interpreter.setStdin = ({ stdin }) => {
75
+ io.stdin = stdin;
76
+ };
77
+ // tiny shim of the code module with only interact
78
+ // to bootstrap a REPL like environment
79
+ interpreter.registerJsModule("code", {
80
+ interact() {
81
+ interpreter.replInit();
82
+ data = "";
83
+ processData();
84
+ },
85
+ });
86
+ }
33
87
 
34
88
  // This part is inevitably duplicated as external scope
35
89
  // can't be reached by workers out of the box.
@@ -39,7 +93,7 @@ const workerReady = ({ interpreter, io, run }, { sync }) => {
39
93
  const generic = {
40
94
  isatty: true,
41
95
  write(buffer) {
42
- data = decoder.decode(buffer);
96
+ data = isMicroPython ? buffer : decoder.decode(buffer);
43
97
  sync.pyterminal_write(data);
44
98
  return buffer.length;
45
99
  },
@@ -50,38 +104,9 @@ const workerReady = ({ interpreter, io, run }, { sync }) => {
50
104
  isatty: true,
51
105
  stdin: () => sync.pyterminal_read(data),
52
106
  });
53
-
54
- io.stderr = (error) => {
55
- sync.pyterminal_write(`${error.message || error}\n`);
56
- };
57
107
  };
58
108
 
59
- const pyTerminal = async () => {
60
- const terminals = document.querySelectorAll(SELECTOR);
61
-
62
- const unknown = [].filter.call(terminals, notParsedYet);
63
-
64
- // no results will look further for runtime nodes
65
- if (!unknown.length) return;
66
- // early flag elements as known to avoid concurrent
67
- // MutationObserver invokes of this async handler
68
- else unknown.forEach(bootstrapped.add, bootstrapped);
69
-
70
- // we currently support only one terminal as in "classic"
71
- if ([].filter.call(terminals, onceOnMain).length > 1)
72
- notifyAndThrow("You can use at most 1 main terminal");
73
-
74
- // import styles lazily
75
- if (addStyle) {
76
- addStyle = false;
77
- document.head.append(
78
- Object.assign(document.createElement("link"), {
79
- rel: "stylesheet",
80
- href: new URL("./xterm.css", import.meta.url),
81
- }),
82
- );
83
- }
84
-
109
+ const pyTerminal = async (element) => {
85
110
  // lazy load these only when a valid terminal is found
86
111
  const [{ Terminal }, { Readline }, { FitAddon }] = await Promise.all([
87
112
  import(/* webpackIgnore: true */ "../3rd-party/xterm.js"),
@@ -89,118 +114,142 @@ const pyTerminal = async () => {
89
114
  import(/* webpackIgnore: true */ "../3rd-party/xterm_addon-fit.js"),
90
115
  ]);
91
116
 
92
- for (const element of unknown) {
93
- // hopefully to be removed in the near future!
94
- if (element.matches('script[type="mpy"],mpy-script'))
95
- notifyAndThrow("Unsupported terminal.");
96
-
97
- const readline = new Readline();
98
-
99
- // common main thread initialization for both worker
100
- // or main case, bootstrapping the terminal on its target
101
- const init = (options) => {
102
- let target = element;
103
- const selector = element.getAttribute("target");
104
- if (selector) {
105
- target =
106
- document.getElementById(selector) ||
107
- document.querySelector(selector);
108
- if (!target) throw new Error(`Unknown target ${selector}`);
109
- } else {
110
- target = document.createElement("py-terminal");
111
- target.style.display = "block";
112
- element.after(target);
113
- }
114
- const terminal = new Terminal({
115
- theme: {
116
- background: "#191A19",
117
- foreground: "#F5F2E7",
118
- },
119
- ...options,
117
+ const readline = new Readline();
118
+
119
+ // common main thread initialization for both worker
120
+ // or main case, bootstrapping the terminal on its target
121
+ const init = (options) => {
122
+ let target = element;
123
+ const selector = element.getAttribute("target");
124
+ if (selector) {
125
+ target =
126
+ document.getElementById(selector) ||
127
+ document.querySelector(selector);
128
+ if (!target) throw new Error(`Unknown target ${selector}`);
129
+ } else {
130
+ target = document.createElement("py-terminal");
131
+ target.style.display = "block";
132
+ element.after(target);
133
+ }
134
+ const terminal = new Terminal({
135
+ theme: {
136
+ background: "#191A19",
137
+ foreground: "#F5F2E7",
138
+ },
139
+ ...options,
140
+ });
141
+ const fitAddon = new FitAddon();
142
+ terminal.loadAddon(fitAddon);
143
+ terminal.loadAddon(readline);
144
+ terminal.open(target);
145
+ fitAddon.fit();
146
+ terminal.focus();
147
+ defineProperty(element, "terminal", { value: terminal });
148
+ return terminal;
149
+ };
150
+
151
+ // branch logic for the worker
152
+ if (element.hasAttribute("worker")) {
153
+ // add a hook on the main thread to setup all sync helpers
154
+ // also bootstrapping the XTerm target on main *BUT* ...
155
+ hooks.main.onWorker.add(function worker(_, xworker) {
156
+ // ... as multiple workers will add multiple callbacks
157
+ // be sure no xworker is ever initialized twice!
158
+ if (bootstrapped.has(xworker)) return;
159
+ bootstrapped.add(xworker);
160
+
161
+ // still cleanup this callback for future scripts/workers
162
+ hooks.main.onWorker.delete(worker);
163
+
164
+ init({
165
+ disableStdin: false,
166
+ cursorBlink: true,
167
+ cursorStyle: "block",
120
168
  });
121
- const fitAddon = new FitAddon();
122
- terminal.loadAddon(fitAddon);
123
- terminal.loadAddon(readline);
124
- terminal.open(target);
125
- fitAddon.fit();
126
- terminal.focus();
127
- defineProperty(element, "terminal", { value: terminal });
128
- return terminal;
129
- };
130
169
 
131
- // branch logic for the worker
132
- if (element.hasAttribute("worker")) {
133
- // add a hook on the main thread to setup all sync helpers
134
- // also bootstrapping the XTerm target on main *BUT* ...
135
- hooks.main.onWorker.add(function worker(_, xworker) {
136
- // ... as multiple workers will add multiple callbacks
137
- // be sure no xworker is ever initialized twice!
138
- if (bootstrapped.has(xworker)) return;
139
- bootstrapped.add(xworker);
140
-
141
- // still cleanup this callback for future scripts/workers
142
- hooks.main.onWorker.delete(worker);
143
-
144
- init({
145
- disableStdin: false,
146
- cursorBlink: true,
147
- cursorStyle: "block",
148
- });
149
-
150
- xworker.sync.is_pyterminal = () => true;
151
- xworker.sync.pyterminal_read = readline.read.bind(readline);
152
- xworker.sync.pyterminal_write = readline.write.bind(readline);
170
+ xworker.sync.is_pyterminal = () => true;
171
+ xworker.sync.pyterminal_read = readline.read.bind(readline);
172
+ xworker.sync.pyterminal_write = readline.write.bind(readline);
173
+ });
174
+
175
+ // setup remote thread JS/Python code for whenever the
176
+ // worker is ready to become a terminal
177
+ hooks.worker.onReady.add(workerReady);
178
+ } else {
179
+ // in the main case, just bootstrap XTerm without
180
+ // allowing any input as that's not possible / awkward
181
+ hooks.main.onReady.add(function main({ interpreter, io, run, type }) {
182
+ console.warn("py-terminal is read only on main thread");
183
+ hooks.main.onReady.delete(main);
184
+
185
+ // on main, it's easy to trash and clean the current terminal
186
+ globalThis.__py_terminal__ = init({
187
+ disableStdin: true,
188
+ cursorBlink: false,
189
+ cursorStyle: "underline",
153
190
  });
191
+ run("from js import __py_terminal__ as __terminal__");
192
+ delete globalThis.__py_terminal__;
154
193
 
155
- // setup remote thread JS/Python code for whenever the
156
- // worker is ready to become a terminal
157
- hooks.worker.onReady.add(workerReady);
158
- } else {
159
- // in the main case, just bootstrap XTerm without
160
- // allowing any input as that's not possible / awkward
161
- hooks.main.onReady.add(function main({ interpreter, io, run }) {
162
- console.warn("py-terminal is read only on main thread");
163
- hooks.main.onReady.delete(main);
164
-
165
- // on main, it's easy to trash and clean the current terminal
166
- globalThis.__py_terminal__ = init({
167
- disableStdin: true,
168
- cursorBlink: false,
169
- cursorStyle: "underline",
170
- });
171
- run("from js import __py_terminal__ as __terminal__");
172
- delete globalThis.__py_terminal__;
173
-
174
- // This part is inevitably duplicated as external scope
175
- // can't be reached by workers out of the box.
176
- // The detail is that here we use readline here, not sync.
177
- const decoder = new TextDecoder();
178
- let data = "";
179
- const generic = {
180
- isatty: true,
181
- write(buffer) {
182
- data = decoder.decode(buffer);
183
- readline.write(data);
184
- return buffer.length;
185
- },
186
- };
187
- interpreter.setStdout(generic);
188
- interpreter.setStderr(generic);
189
- interpreter.setStdin({
190
- isatty: true,
191
- stdin: () => readline.read(data),
192
- });
193
-
194
- io.stderr = (error) => {
195
- readline.write(`${error.message || error}\n`);
194
+ io.stderr = (error) => {
195
+ readline.write(`${error.message || error}\n`);
196
+ };
197
+
198
+ const isMicroPython = type === "mpy";
199
+
200
+ if (isMicroPython) {
201
+ interpreter.setStderr = Object; // as no-op
202
+ interpreter.setStdin = Object; // as no-op
203
+ interpreter.setStdout = ({ write }) => {
204
+ io.stdout = (str) => write(`${str}\n`);
196
205
  };
206
+ }
207
+
208
+ // This part is inevitably duplicated as external scope
209
+ // can't be reached by workers out of the box.
210
+ // The detail is that here we use readline here, not sync.
211
+ const decoder = new TextDecoder();
212
+ let data = "";
213
+ const generic = {
214
+ isatty: true,
215
+ write(buffer) {
216
+ data = isMicroPython ? buffer : decoder.decode(buffer);
217
+ readline.write(data);
218
+ return buffer.length;
219
+ },
220
+ };
221
+ interpreter.setStdout(generic);
222
+ interpreter.setStderr(generic);
223
+ interpreter.setStdin({
224
+ isatty: true,
225
+ stdin: () => readline.read(data),
197
226
  });
198
- }
227
+ });
199
228
  }
200
229
  };
201
230
 
202
- const mo = new MutationObserver(pyTerminal);
203
- mo.observe(document, { childList: true, subtree: true });
231
+ for (const key of TYPES.keys()) {
232
+ const selector = `script[type="${key}"][terminal],${key}-script[terminal]`;
233
+ SELECTORS.push(selector);
234
+ customObserver.set(selector, async (element) => {
235
+ // if (key === "mpy") notifyAndThrow(`Unsupported ${key} terminal.`);
236
+
237
+ // we currently support only one terminal on main as in "classic"
238
+ const terminals = document.querySelectorAll(SELECTORS.join(","));
239
+ if ([].filter.call(terminals, onceOnMain).length > 1)
240
+ notifyAndThrow("You can use at most 1 main terminal");
241
+
242
+ // import styles lazily
243
+ if (addStyle) {
244
+ addStyle = false;
245
+ document.head.append(
246
+ Object.assign(document.createElement("link"), {
247
+ rel: "stylesheet",
248
+ href: new URL("./xterm.css", import.meta.url),
249
+ }),
250
+ );
251
+ }
204
252
 
205
- // try to check the current document ASAP
206
- export default pyTerminal();
253
+ await pyTerminal(element);
254
+ });
255
+ }