8000 Use a recommended method (#363) · Siddharth-cmd/pyscript@39774a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 39774a8

Browse files
authored
Use a recommended method (pyscript#363)
1 parent c040158 commit 39774a8

File tree

5 files changed

+9
-15
lines changed

5 files changed

+9
-15
lines changed

pyscriptjs/src/components/pybutton.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ export class PyButton extends BaseEvalElement {
5454
let registrationCode = `${this.mount_name} = Element("${mainDiv.id}")`;
5555
if (this.code.includes('def on_focus')) {
5656
this.code = this.code.replace('def on_focus', `def on_focus_${this.mount_name}`);
57-
registrationCode += `\n${this.mount_name}.element.onfocus = on_focus_${this.mount_name}`;
57+
registrationCode += `\n${this.mount_name}.element.addEventListener('focus', on_focus_${this.mount_name})`;
5858
}
5959

6060
if (this.code.includes('def on_click')) {
6161
this.code = this.code.replace('def on_click', `def on_click_${this.mount_name}`);
62-
registrationCode += `\n${this.mount_name}.element.onclick = on_click_${this.mount_name}`;
62+
registrationCode += `\n${this.mount_name}.element.addEventListener('click', on_click_${this.mount_name})`;
6363
}
6464

6565
// now that we appended and the element is attached, lets connect with the event handlers

pyscriptjs/src/components/pyconfig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ export class PyConfig extends BaseEvalElement {
181181
const script = document.createElement("script"); // create a script DOM node
182182
const runtimeSpec = new PyodideRuntime(runtime.src);
183183
script.src = runtime.src; // set its src to the provided URL
184-
script.onload = () => {
185-
runtimeSpec.initialize();
186-
}
184+
script.addEventListener('load', () => {
185+
void runtimeSpec.initialize();
186+
});
187187
document.head.appendChild(script);
188188
}
189189
}

pyscriptjs/src/components/pyinputbox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class PyInputBox extends BaseEvalElement {
3737
let registrationCode = `${this.mount_name} = Element("${mainDiv.id}")`;
3838
if (this.code.includes('def on_keypress')) {
3939
this.code = this.code.replace('def on_keypress', `def on_keypress_${this.mount_name}`);
40-
registrationCode += `\n${this.mount_name}.element.onkeypress = on_keypress_${this.mount_name}`;
40+
registrationCode += `\n${this.mount_name}.element.addEventListener('keypress', on_keypress_${this.mount_name})`;
4141
}
4242

4343
// TODO: For now we delay execution to allow pyodide to load but in the future this

pyscriptjs/src/components/pyrepl.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,9 @@ export class PyRepl extends BaseEvalElement {
104104
addClasses(this.btnRun, ['absolute', 'right-1', 'bottom-1', 'opacity-0', 'group-hover:opacity-100']);
105105
this.editorNode.appendChild(this.btnRun);
106106

107-
this.btnRun.onclick = wrap(this);
108-
109-
function wrap(el: any) {
110-
function evaluatePython() {
111-
el.evaluate();
112-
}
113-
return evaluatePython;
114-
}
107+
this.btnRun.addEventListener('click', () => {
108+
void this.evaluate();
109+
});
115110

116111
if (!this.id) {
117112
console.log(

pyscriptjs/src/components/pyscript.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ async function createElementsWithEventListeners(pyodide: any, pysAttribute: stri
157157
`;
158158
await pyodide.runPythonAsync(source);
159159

160-
161160
// TODO: Should we actually map handlers in JS instead of Python?
162161
// el.onclick = (evt: any) => {
163162
// console.log("click");

0 commit comments

Comments
 (0)
0