2021-11-09 03:53:39 +01:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2022-11-28 00:33:04 +01:00
|
|
|
export type EventTargetToMap<A extends EventTarget> = A extends HTMLElement ? HTMLElementEventMap
|
|
|
|
: A extends Document ? DocumentEventMap
|
|
|
|
: A extends Window ? WindowEventMap
|
|
|
|
: A extends FileReader ? FileReaderEventMap
|
|
|
|
: A extends Element ? ElementEventMap
|
|
|
|
: A extends Animation ? AnimationEventMap
|
|
|
|
: A extends EventSource ? EventSourceEventMap
|
|
|
|
: A extends AbortSignal ? AbortSignalEventMap
|
|
|
|
: A extends AbstractWorker ? AbstractWorkerEventMap
|
2021-11-18 10:18:39 +01:00
|
|
|
: never;
|
|
|
|
|
|
|
|
export function on<T extends EventTarget, K extends keyof EventTargetToMap<T>>(
|
2021-11-09 03:53:39 +01:00
|
|
|
target: T,
|
2021-11-18 10:18:39 +01:00
|
|
|
eventType: Exclude<K, symbol | number>,
|
|
|
|
handler: (this: T, event: EventTargetToMap<T>[K]) => void,
|
|
|
|
options?: AddEventListenerOptions,
|
2021-11-09 03:53:39 +01:00
|
|
|
): () => void {
|
2021-11-18 10:18:39 +01:00
|
|
|
target.addEventListener(eventType, handler as EventListener, options);
|
2022-11-28 00:33:04 +01:00
|
|
|
return () => target.removeEventListener(eventType, handler as EventListener, options);
|
2021-11-09 03:53:39 +01:00
|
|
|
}
|
2021-11-23 01:27:32 +01:00
|
|
|
|
|
|
|
export function preventDefault(event: Event): void {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|