vue3-core/packages/runtime-vapor/src/dom/event.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-02-07 01:37:07 +08:00
import {
getCurrentEffect,
getCurrentScope,
onEffectCleanup,
onScopeDispose,
} from '@vue/reactivity'
import { recordMetadata } from '../metadata'
2024-02-07 01:37:07 +08:00
import { withKeys, withModifiers } from '@vue/runtime-dom'
export function addEventListener(
el: HTMLElement,
event: string,
handler: (...args: any) => any,
options?: AddEventListenerOptions,
) {
el.addEventListener(event, handler, options)
return () => el.removeEventListener(event, handler, options)
}
export function on(
el: HTMLElement,
event: string,
handlerGetter: () => undefined | ((...args: any[]) => any),
options?: AddEventListenerOptions,
{ modifiers, keys }: { modifiers?: string[]; keys?: string[] } = {},
) {
const handler = (...args: any[]) => {
let handler = handlerGetter()
if (!handler) return
2024-02-07 01:37:07 +08:00
if (modifiers) {
handler = withModifiers(handler, modifiers)
}
if (keys) {
handler = withKeys(handler, keys)
}
handler && handler(...args)
}
recordMetadata(el, 'events', event, handler)
const cleanup = addEventListener(el, event, handler, options)
2024-02-07 01:37:07 +08:00
const scope = getCurrentScope()
const effect = getCurrentEffect()
if (effect && effect.scope === scope) {
onEffectCleanup(cleanup)
} else if (scope) {
onScopeDispose(cleanup)
}
}