Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and WebReflection committed Mar 27, 2024
1 parent c9f5679 commit 18a8cb6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
20 changes: 13 additions & 7 deletions pyscript.core/src/plugins/py-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let id = 0;
const getID = (type) => `${type}-editor-${id++}`;

const envs = new Map();
const configs = new Set();

const hooks = {
worker: {
Expand Down Expand Up @@ -37,10 +38,10 @@ async function execute({ currentTarget }) {
const { config } = this;
if (config) {
details.configURL = config;
const { parse } = config.endsWith('.toml')
const { parse } = config.endsWith(".toml")
? await import(/* webpackIgnore: true */ "../3rd-party/toml.js")
: JSON
details.config = parse(await fetch(config).then(r => r.text()));
: JSON;
details.config = parse(await fetch(config).then((r) => r.text()));
}

const xworker = XWorker.call(new Hook(null, hooks), srcLink, details);
Expand Down Expand Up @@ -148,17 +149,22 @@ const init = async (script, type, interpreter) => {

const isSetup = script.hasAttribute("setup");
const hasConfig = script.hasAttribute("config");
if (hasConfig && !isSetup)
throw new SyntaxError("only editors with a setup can have a config");

const env = `${interpreter}-${script.getAttribute("env") || getID(type)}`;

if (hasConfig && configs.has(env))
throw new SyntaxError(`duplicated config for env: ${env}`);

configs.add(env);

const source = script.src
? await fetch(script.src).then((b) => b.text())
: script.textContent;
const context = {
interpreter,
env,
config: hasConfig && new URL(script.getAttribute("config"), location.href).href,
config:
hasConfig &&
new URL(script.getAttribute("config"), location.href).href,
get pySrc() {
return isSetup ? source : editor.state.doc.toString();
},
Expand Down
29 changes: 23 additions & 6 deletions pyscript.core/test/py-editor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,35 @@
<script type="module" src="../../dist/core.js"></script>
</head>
<body>
<script type="mpy-editor" src="task1.py" env="task1" setup></script>
<!-- a setup node with a config for an env -->
<script type="mpy-editor" src="task1.py" config="./config.toml" env="task1" setup></script>
<script type="mpy-editor" env="task1">
print(a)
from pyscript.js_modules.html_escaper import escape, unescape
print(unescape(escape("<OK>")))
a = 1
</script>
<script type="mpy-editor" config="./config.toml" env="task2" setup>
from pyscript import window
<!-- a share-nothing micropython editor -->
<script type="mpy-editor" config="./config.toml">
from pyscript.js_modules.html_escaper import escape, unescape
window.console.log(unescape(escape("<OK>")))
print(unescape(escape("<OK>")))
b = 2
try:
print(a)
except:
print("all good")
</script>
<!-- a config once micropython env -->
<script type="mpy-editor" env="task2" config="./config.toml">
from pyscript.js_modules.html_escaper import escape, unescape
print(unescape(escape("<OK>")))
c = 3
try:
print(b)
except:
print("all good")
</script>
<script type="mpy-editor" env="task2">
print(b)
print(c)
</script>
</body>
</html>

0 comments on commit 18a8cb6

Please sign in to comment.