mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-23 12:12:59 -05:00
Use a recommended method (#363)
This commit is contained in:
@@ -54,12 +54,12 @@ export class PyButton extends BaseEvalElement {
|
||||
let registrationCode = `${this.mount_name} = Element("${mainDiv.id}")`;
|
||||
if (this.code.includes('def on_focus')) {
|
||||
this.code = this.code.replace('def on_focus', `def on_focus_${this.mount_name}`);
|
||||
registrationCode += `\n${this.mount_name}.element.onfocus = on_focus_${this.mount_name}`;
|
||||
registrationCode += `\n${this.mount_name}.element.addEventListener('focus', on_focus_${this.mount_name})`;
|
||||
}
|
||||
|
||||
if (this.code.includes('def on_click')) {
|
||||
this.code = this.code.replace('def on_click', `def on_click_${this.mount_name}`);
|
||||
registrationCode += `\n${this.mount_name}.element.onclick = on_click_${this.mount_name}`;
|
||||
registrationCode += `\n${this.mount_name}.element.addEventListener('click', on_click_${this.mount_name})`;
|
||||
}
|
||||
|
||||
// now that we appended and the element is attached, lets connect with the event handlers
|
||||
|
||||
@@ -181,9 +181,9 @@ export class PyConfig extends BaseEvalElement {
|
||||
const script = document.createElement("script"); // create a script DOM node
|
||||
const runtimeSpec = new PyodideRuntime(runtime.src);
|
||||
script.src = runtime.src; // set its src to the provided URL
|
||||
script.onload = () => {
|
||||
runtimeSpec.initialize();
|
||||
}
|
||||
script.addEventListener('load', () => {
|
||||
void runtimeSpec.initialize();
|
||||
});
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ export class PyInputBox extends BaseEvalElement {
|
||||
let registrationCode = `${this.mount_name} = Element("${mainDiv.id}")`;
|
||||
if (this.code.includes('def on_keypress')) {
|
||||
this.code = this.code.replace('def on_keypress', `def on_keypress_${this.mount_name}`);
|
||||
registrationCode += `\n${this.mount_name}.element.onkeypress = on_keypress_${this.mount_name}`;
|
||||
registrationCode += `\n${this.mount_name}.element.addEventListener('keypress', on_keypress_${this.mount_name})`;
|
||||
}
|
||||
|
||||
// TODO: For now we delay execution to allow pyodide to load but in the future this
|
||||
|
||||
@@ -104,14 +104,9 @@ export class PyRepl extends BaseEvalElement {
|
||||
addClasses(this.btnRun, ['absolute', 'right-1', 'bottom-1', 'opacity-0', 'group-hover:opacity-100']);
|
||||
this.editorNode.appendChild(this.btnRun);
|
||||
|
||||
this.btnRun.onclick = wrap(this);
|
||||
|
||||
function wrap(el: any) {
|
||||
function evaluatePython() {
|
||||
el.evaluate();
|
||||
}
|
||||
return evaluatePython;
|
||||
}
|
||||
this.btnRun.addEventListener('click', () => {
|
||||
void this.evaluate();
|
||||
});
|
||||
|
||||
if (!this.id) {
|
||||
console.log(
|
||||
|
||||
@@ -157,7 +157,6 @@ async function createElementsWithEventListeners(pyodide: any, pysAttribute: stri
|
||||
`;
|
||||
await pyodide.runPythonAsync(source);
|
||||
|
||||
|
||||
// TODO: Should we actually map handlers in JS instead of Python?
|
||||
// el.onclick = (evt: any) => {
|
||||
// console.log("click");
|
||||
|
||||
Reference in New Issue
Block a user