|
| 1 | +const captureSuffix = 'Capture' |
| 2 | + |
| 3 | +export type ParsedEventBinding = { |
| 4 | + eventName: string |
| 5 | + capture: boolean |
| 6 | +} |
| 7 | + |
| 8 | +const stripCaptureSuffix = (rawName: string): ParsedEventBinding => { |
| 9 | + if (rawName.endsWith(captureSuffix)) { |
| 10 | + return { eventName: rawName.slice(0, -captureSuffix.length), capture: true } |
| 11 | + } |
| 12 | + |
| 13 | + return { eventName: rawName, capture: false } |
| 14 | +} |
| 15 | + |
| 16 | +export const parseEventPropName = (name: string): ParsedEventBinding | null => { |
| 17 | + if (!name.startsWith('on')) { |
| 18 | + return null |
| 19 | + } |
| 20 | + |
| 21 | + if (name.startsWith('on:')) { |
| 22 | + const raw = name.slice(3) |
| 23 | + if (!raw) { |
| 24 | + return null |
| 25 | + } |
| 26 | + const parsed = stripCaptureSuffix(raw) |
| 27 | + if (!parsed.eventName) { |
| 28 | + return null |
| 29 | + } |
| 30 | + return parsed |
| 31 | + } |
| 32 | + |
| 33 | + const raw = name.slice(2) |
| 34 | + if (!raw) { |
| 35 | + return null |
| 36 | + } |
| 37 | + |
| 38 | + const parsed = stripCaptureSuffix(raw) |
| 39 | + if (!parsed.eventName) { |
| 40 | + return null |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + eventName: parsed.eventName.toLowerCase(), |
| 45 | + capture: parsed.capture, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +const isEventListenerObject = (value: unknown): value is EventListenerObject => { |
| 50 | + if (!value || typeof value !== 'object') { |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + return ( |
| 55 | + 'handleEvent' in (value as Record<string, unknown>) && |
| 56 | + typeof (value as EventListenerObject).handleEvent === 'function' |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +export type EventHandlerDescriptor = { |
| 61 | + handler: EventListenerOrEventListenerObject |
| 62 | + capture?: boolean |
| 63 | + once?: boolean |
| 64 | + passive?: boolean |
| 65 | + signal?: AbortSignal | null |
| 66 | + options?: AddEventListenerOptions |
| 67 | +} |
| 68 | + |
| 69 | +const isEventHandlerDescriptor = (value: unknown): value is EventHandlerDescriptor => { |
| 70 | + if (!value || typeof value !== 'object' || !('handler' in value)) { |
| 71 | + return false |
| 72 | + } |
| 73 | + |
| 74 | + const handler = (value as EventHandlerDescriptor).handler |
| 75 | + if (typeof handler === 'function') { |
| 76 | + return true |
| 77 | + } |
| 78 | + |
| 79 | + return isEventListenerObject(handler) |
| 80 | +} |
| 81 | + |
| 82 | +export type ResolvedEventHandler = { |
| 83 | + listener: EventListenerOrEventListenerObject |
| 84 | + options?: AddEventListenerOptions |
| 85 | +} |
| 86 | + |
| 87 | +export const resolveEventHandlerValue = (value: unknown): ResolvedEventHandler | null => { |
| 88 | + if (typeof value === 'function' || isEventListenerObject(value)) { |
| 89 | + return { listener: value as EventListenerOrEventListenerObject } |
| 90 | + } |
| 91 | + |
| 92 | + if (!isEventHandlerDescriptor(value)) { |
| 93 | + return null |
| 94 | + } |
| 95 | + |
| 96 | + const descriptor = value |
| 97 | + let options = descriptor.options ? { ...descriptor.options } : undefined |
| 98 | + |
| 99 | + const assignOption = <K extends keyof AddEventListenerOptions>( |
| 100 | + key: K, |
| 101 | + optionValue: AddEventListenerOptions[K] | null | undefined, |
| 102 | + ) => { |
| 103 | + if (optionValue === undefined || optionValue === null) { |
| 104 | + return |
| 105 | + } |
| 106 | + if (!options) { |
| 107 | + options = {} |
| 108 | + } |
| 109 | + options[key] = optionValue |
| 110 | + } |
| 111 | + |
| 112 | + assignOption('capture', descriptor.capture) |
| 113 | + assignOption('once', descriptor.once) |
| 114 | + assignOption('passive', descriptor.passive) |
| 115 | + assignOption('signal', descriptor.signal ?? undefined) |
| 116 | + |
| 117 | + return { |
| 118 | + listener: descriptor.handler, |
| 119 | + options, |
| 120 | + } |
| 121 | +} |
0 commit comments