Format the TypeScript files (#877)

This commit is contained in:
woxtu
2022-10-28 17:48:27 +09:00
committed by GitHub
parent 1c53d91c6b
commit 9543019336
13 changed files with 235 additions and 286 deletions

View File

@@ -6,9 +6,7 @@ import { pyExec } from '../pyexec';
const logger = getLogger('py-script');
export function make_PyScript(runtime: Runtime) {
class PyScript extends HTMLElement {
async connectedCallback() {
ensureUniqueId(this);
const pySrc = await this.getPySrc();
@@ -24,8 +22,7 @@ export function make_PyScript(runtime: Runtime) {
const url = this.getAttribute('src');
const response = await fetch(url);
return await response.text();
}
else {
} else {
return htmlDecode(this.innerHTML);
}
}
@@ -36,98 +33,98 @@ export function make_PyScript(runtime: Runtime) {
/** Defines all possible py-on* and their corresponding event types */
const pyAttributeToEvent: Map<string, string> = new Map<string, string>([
// Leaving pys-onClick and pys-onKeyDown for backward compatibility
["pys-onClick", "click"],
["pys-onKeyDown", "keydown"],
["py-onClick", "click"],
["py-onKeyDown", "keydown"],
// Window Events
["py-afterprint", "afterprint"],
["py-beforeprint", "beforeprint"],
["py-beforeunload", "beforeunload"],
["py-error", "error"],
["py-hashchange", "hashchange"],
["py-load", "load"],
["py-message", "message"],
["py-offline", "offline"],
["py-online", "online"],
["py-pagehide", "pagehide"],
["py-pageshow", "pageshow"],
["py-popstate", "popstate"],
["py-resize", "resize"],
["py-storage", "storage"],
["py-unload", "unload"],
// Leaving pys-onClick and pys-onKeyDown for backward compatibility
['pys-onClick', 'click'],
['pys-onKeyDown', 'keydown'],
['py-onClick', 'click'],
['py-onKeyDown', 'keydown'],
// Window Events
['py-afterprint', 'afterprint'],
['py-beforeprint', 'beforeprint'],
['py-beforeunload', 'beforeunload'],
['py-error', 'error'],
['py-hashchange', 'hashchange'],
['py-load', 'load'],
['py-message', 'message'],
['py-offline', 'offline'],
['py-online', 'online'],
['py-pagehide', 'pagehide'],
['py-pageshow', 'pageshow'],
['py-popstate', 'popstate'],
['py-resize', 'resize'],
['py-storage', 'storage'],
['py-unload', 'unload'],
// Form Events
["py-blur", "blur"],
["py-change", "change"],
["py-contextmenu", "contextmenu"],
["py-focus", "focus"],
["py-input", "input"],
["py-invalid", "invalid"],
["py-reset", "reset"],
["py-search", "search"],
["py-select", "select"],
["py-submit", "submit"],
// Form Events
['py-blur', 'blur'],
['py-change', 'change'],
['py-contextmenu', 'contextmenu'],
['py-focus', 'focus'],
['py-input', 'input'],
['py-invalid', 'invalid'],
['py-reset', 'reset'],
['py-search', 'search'],
['py-select', 'select'],
['py-submit', 'submit'],
// Keyboard Events
["py-keydown", "keydown"],
["py-keypress", "keypress"],
["py-keyup", "keyup"],
// Keyboard Events
['py-keydown', 'keydown'],
['py-keypress', 'keypress'],
['py-keyup', 'keyup'],
// Mouse Events
["py-click", "click"],
["py-dblclick", "dblclick"],
["py-mousedown", "mousedown"],
["py-mousemove", "mousemove"],
["py-mouseout", "mouseout"],
["py-mouseover", "mouseover"],
["py-mouseup", "mouseup"],
["py-mousewheel", "mousewheel"],
["py-wheel", "wheel"],
// Mouse Events
['py-click', 'click'],
['py-dblclick', 'dblclick'],
['py-mousedown', 'mousedown'],
['py-mousemove', 'mousemove'],
['py-mouseout', 'mouseout'],
['py-mouseover', 'mouseover'],
['py-mouseup', 'mouseup'],
['py-mousewheel', 'mousewheel'],
['py-wheel', 'wheel'],
// Drag Events
["py-drag", "drag"],
["py-dragend", "dragend"],
["py-dragenter", "dragenter"],
["py-dragleave", "dragleave"],
["py-dragover", "dragover"],
["py-dragstart", "dragstart"],
["py-drop", "drop"],
["py-scroll", "scroll"],
// Drag Events
['py-drag', 'drag'],
['py-dragend', 'dragend'],
['py-dragenter', 'dragenter'],
['py-dragleave', 'dragleave'],
['py-dragover', 'dragover'],
['py-dragstart', 'dragstart'],
['py-drop', 'drop'],
['py-scroll', 'scroll'],
// Clipboard Events
["py-copy", "copy"],
["py-cut", "cut"],
["py-paste", "paste"],
// Clipboard Events
['py-copy', 'copy'],
['py-cut', 'cut'],
['py-paste', 'paste'],
// Media Events
["py-abort", "abort"],
["py-canplay", "canplay"],
["py-canplaythrough", "canplaythrough"],
["py-cuechange", "cuechange"],
["py-durationchange", "durationchange"],
["py-emptied", "emptied"],
["py-ended", "ended"],
["py-loadeddata", "loadeddata"],
["py-loadedmetadata", "loadedmetadata"],
["py-loadstart", "loadstart"],
["py-pause", "pause"],
["py-play", "play"],
["py-playing", "playing"],
["py-progress", "progress"],
["py-ratechange", "ratechange"],
["py-seeked", "seeked"],
["py-seeking", "seeking"],
["py-stalled", "stalled"],
["py-suspend", "suspend"],
["py-timeupdate", "timeupdate"],
["py-volumechange", "volumechange"],
["py-waiting", "waiting"],
// Media Events
['py-abort', 'abort'],
['py-canplay', 'canplay'],
['py-canplaythrough', 'canplaythrough'],
['py-cuechange', 'cuechange'],
['py-durationchange', 'durationchange'],
['py-emptied', 'emptied'],
['py-ended', 'ended'],
['py-loadeddata', 'loadeddata'],
['py-loadedmetadata', 'loadedmetadata'],
['py-loadstart', 'loadstart'],
['py-pause', 'pause'],
['py-play', 'play'],
['py-playing', 'playing'],
['py-progress', 'progress'],
['py-ratechange', 'ratechange'],
['py-seeked', 'seeked'],
['py-seeking', 'seeking'],
['py-stalled', 'stalled'],
['py-suspend', 'suspend'],
['py-timeupdate', 'timeupdate'],
['py-volumechange', 'volumechange'],
['py-waiting', 'waiting'],
// Misc Events
["py-toggle", "toggle"],
]);
// Misc Events
['py-toggle', 'toggle'],
]);
/** Initialize all elements with py-* handlers attributes */
export async function initHandlers(runtime: Runtime) {
@@ -142,22 +139,27 @@ async function createElementsWithEventListeners(runtime: Runtime, pyAttribute: s
const matches: NodeListOf<HTMLElement> = document.querySelectorAll(`[${pyAttribute}]`);
for (const el of matches) {
if (el.id.length === 0) {
throw new TypeError(`<${el.tagName.toLowerCase()}> must have an id attribute, when using the ${pyAttribute} attribute`)
throw new TypeError(
`<${el.tagName.toLowerCase()}> must have an id attribute, when using the ${pyAttribute} attribute`,
);
}
const handlerCode = el.getAttribute(pyAttribute);
const event = pyAttributeToEvent.get(pyAttribute);
if (pyAttribute === 'pys-onClick' || pyAttribute === 'pys-onKeyDown'){
console.warn("Use of pys-onClick and pys-onKeyDown attributes is deprecated in favor of py-onClick() and py-onKeyDown(). pys-on* attributes will be deprecated in a future version of PyScript.")
if (pyAttribute === 'pys-onClick' || pyAttribute === 'pys-onKeyDown') {
console.warn(
'Use of pys-onClick and pys-onKeyDown attributes is deprecated in favor of py-onClick() and py-onKeyDown(). pys-on* attributes will be deprecated in a future version of PyScript.',
);
const source = `
from pyodide.ffi import create_proxy
Element("${el.id}").element.addEventListener("${event}", create_proxy(${handlerCode}))
`;
await runtime.run(source);
}
else{
} else {
el.addEventListener(event, () => {
(async() => {await runtime.run(handlerCode)})();
(async () => {
await runtime.run(handlerCode);
})();
});
}
// TODO: Should we actually map handlers in JS instead of Python?
@@ -174,7 +176,6 @@ async function createElementsWithEventListeners(runtime: Runtime, pyAttribute: s
// // pyodide.runPython(handlerCode);
// }
}
}
/** Mount all elements with attribute py-mount into the Python namespace */