Skip to content

Commit 9cfe01b

Browse files
committed
Fix #2360 - Better shared env/setup handling
1 parent b528ba6 commit 9cfe01b

3 files changed

Lines changed: 66 additions & 43 deletions

File tree

core/package-lock.json

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"@webreflection/utils": "^0.1.1",
7171
"add-promise-listener": "^0.1.3",
7272
"basic-devtools": "^0.1.6",
73-
"polyscript": "^0.18.1",
73+
"polyscript": "^0.18.2",
7474
"sticky-module": "^0.1.1",
7575
"to-json-callback": "^0.1.1",
7676
"type-checked-collections": "^0.1.7"
@@ -81,7 +81,7 @@
8181
"@codemirror/language": "^6.11.2",
8282
"@codemirror/state": "^6.5.2",
8383
"@codemirror/view": "^6.38.0",
84-
"@playwright/test": "^1.53.2",
84+
"@playwright/test": "^1.54.0",
8585
"@rollup/plugin-commonjs": "^28.0.6",
8686
"@rollup/plugin-node-resolve": "^16.0.1",
8787
"@rollup/plugin-terser": "^0.4.4",

core/src/plugins/py-editor.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const getRelatedScript = (target, type) => {
3838
return editor?.parentNode?.previousElementSibling;
3939
};
4040

41-
async function execute({ currentTarget }) {
41+
async function execute({ currentTarget, script }) {
4242
const { env, pySrc, outDiv } = this;
4343
const hasRunButton = !!currentTarget;
4444

@@ -91,14 +91,13 @@ async function execute({ currentTarget }) {
9191
// creation and destruction of editors on the fly
9292
if (hasRunButton) {
9393
for (const type of TYPES.keys()) {
94-
const script = getRelatedScript(currentTarget, type);
95-
if (script) {
96-
defineProperties(script, { xworker: { value: xworker } });
97-
break;
98-
}
94+
script = getRelatedScript(currentTarget, type);
95+
if (script) break;
9996
}
10097
}
10198

99+
defineProperties(script, { xworker: { value: xworker } });
100+
102101
const { sync } = xworker;
103102
const { promise, resolve } = withResolvers();
104103
envs.set(env, promise);
@@ -157,6 +156,20 @@ async function execute({ currentTarget }) {
157156
});
158157
}
159158

159+
const replaceScript = (script, type) => {
160+
script.xworker?.terminate();
161+
const clone = script.cloneNode(true);
162+
clone.type = `${type}-editor`;
163+
const editor = editors.get(script);
164+
if (editor) {
165+
const content = editor.state.doc.toString();
166+
clone.textContent = content;
167+
editors.delete(script);
168+
script.nextElementSibling.remove();
169+
}
170+
script.replaceWith(clone);
171+
};
172+
160173
const makeRunButton = (handler, type) => {
161174
const runButton = document.createElement("button");
162175
runButton.className = `absolute ${type}-editor-run-button`;
@@ -169,15 +182,25 @@ const makeRunButton = (handler, type) => {
169182
) {
170183
const script = getRelatedScript(runButton, type);
171184
if (script) {
172-
const editor = editors.get(script);
173-
const content = editor.state.doc.toString();
174-
const clone = script.cloneNode(true);
175-
clone.type = `${type}-editor`;
176-
clone.textContent = content;
177-
script.xworker.terminate();
178-
script.nextElementSibling.remove();
179-
script.replaceWith(clone);
180-
editors.delete(script);
185+
const env = script.getAttribute("env");
186+
// remove the bootstrapped env which could be one or shared
187+
if (env) {
188+
for (const [key, value] of TYPES) {
189+
if (key === type) {
190+
configs.delete(`${value}-${env}`);
191+
envs.delete(`${value}-${env}`);
192+
break;
193+
}
194+
}
195+
}
196+
// lonley script without setup node should be replaced
197+
if (script.xworker) replaceScript(script, type);
198+
// all scripts sharing the same env should be replaced
199+
else {
200+
const sel = `script[type^="${type}-editor"][env="${env}"]`;
201+
for (const script of document.querySelectorAll(sel))
202+
replaceScript(script, type);
203+
}
181204
}
182205
return;
183206
}
@@ -365,7 +388,7 @@ const init = async (script, type, interpreter) => {
365388
};
366389

367390
if (isSetup) {
368-
await context.handleEvent({ currentTarget: null });
391+
await context.handleEvent({ currentTarget: null, script });
369392
notifyEditor();
370393
return;
371394
}

0 commit comments

Comments
 (0)