@@ -6,7 +6,7 @@ import { keymap, ViewUpdate } from "@codemirror/view";
6
6
import { defaultKeymap } from "@codemirror/commands" ;
7
7
import { oneDarkTheme } from "@codemirror/theme-one-dark" ;
8
8
9
- import { pyodideLoaded , loadedEnvironments , componentDetailsNavOpen , currentComponentDetails , mode , addToScriptsQueue , addInitializer } from '../stores' ;
9
+ import { pyodideLoaded , loadedEnvironments , componentDetailsNavOpen , currentComponentDetails , mode , addToScriptsQueue , addInitializer , addPostInitializer } from '../stores' ;
10
10
import { addClasses } from '../utils' ;
11
11
12
12
// Premise used to connect to the first available pyodide interpreter
@@ -40,6 +40,10 @@ function createCmdHandler(el){
40
40
return toggleCheckbox
41
41
}
42
42
43
+ function htmlDecode ( input ) {
44
+ var doc = new DOMParser ( ) . parseFromString ( input , "text/html" ) ;
45
+ return doc . documentElement . textContent ;
46
+ }
43
47
44
48
class Script {
45
49
source : string ;
@@ -214,7 +218,7 @@ export class PyScript extends HTMLElement {
214
218
// debugger
215
219
try {
216
220
// @ts -ignore
217
- let source = this . editor . state . doc . toString ( ) ;
221
+ let source = htmlDecode ( this . editor . state . doc . toString ( ) ) ;
218
222
let output ;
219
223
if ( source . includes ( "asyncio" ) ) {
220
224
output = await pyodide . runPythonAsync ( source ) ;
@@ -232,6 +236,7 @@ export class PyScript extends HTMLElement {
232
236
}
233
237
} catch ( err ) {
234
238
this . addToOutput ( err ) ;
239
+ console . log ( err ) ;
235
240
}
236
241
}
237
242
@@ -241,9 +246,8 @@ export class PyScript extends HTMLElement {
241
246
}
242
247
}
243
248
249
+ /** Initialize all elements with py-onClick handlers attributes */
244
250
async function initHandlers ( ) {
245
- if ( handlersCollected == true ) return ;
246
-
247
251
console . log ( 'Collecting nodes...' ) ;
248
252
let pyodide = await pyodideReadyPromise ;
249
253
let matches : NodeListOf < HTMLElement > = document . querySelectorAll ( '[pys-onClick]' ) ;
@@ -254,6 +258,7 @@ async function initHandlers() {
254
258
source = `Element("${ el . id } ").element.onclick = ${ handlerCode } ` ;
255
259
output = await pyodide . runPythonAsync ( source ) ;
256
260
261
+ // TODO: Should we actually map handlers in JS instaed of Python?
257
262
// el.onclick = (evt: any) => {
258
263
// console.log("click");
259
264
// new Promise((resolve, reject) => {
@@ -276,11 +281,22 @@ async function initHandlers() {
276
281
output = await pyodide . runPythonAsync ( source ) ;
277
282
}
278
283
}
279
- addInitializer ( initHandlers )
280
284
281
- // if( document.readyState === 'loading' ) {
282
- // document.addEventListener( 'DOMContentLoaded', initHandlers );
283
- // }
284
- // else if( document.readyState === 'interactive' || document.readyState === 'complete' ) {
285
- // initHandlers();
286
- // }
285
+ /** Mount all elements with attribute py-mount into the Python namespace */
286
+ async function mountElements ( ) {
287
+ console . log ( 'Collecting nodes to be mounted into python namespace...' ) ;
288
+ let pyodide = await pyodideReadyPromise ;
289
+ let matches : NodeListOf < HTMLElement > = document . querySelectorAll ( '[py-mount]' ) ;
290
+ let output ;
291
+ let source = "" ;
292
+ for ( var el of matches ) {
293
+ let mountName = el . getAttribute ( 'py-mount' ) ;
294
+ if ( ! mountName ) {
295
+ mountName = el . id . replace ( "-" , "_" ) ;
296
+ }
297
+ source += `\n${ mountName } = Element("${ el . id } ")` ;
298
+ }
299
+ await pyodide . runPythonAsync ( source ) ;
300
+ }
301
+ addPostInitializer ( initHandlers ) ;
302
+ addInitializer ( mountElements ) ;
0 commit comments