import { SyntheticEvent } from '../../SyntheticEvent'; import { nativeEvents } from '../internal/NativeEvents'; import { NativeKeyboardEvent } from './NativeKeyboardEvent'; import { NativeMouseEvent } from './NativeMouseEvent'; import { NativeWheelEvent } from './NativeWheelEvent'; interface NativeEvent { map(e: any): SyntheticEvent; readonly type: string; } export interface Mapping { target: any; mapper: (e: any) => void; options: any; type: string; } export class NativeEventMapper extends nativeEvents() { static map(target: any, type: string, callbackfn: (event: SyntheticEvent, type: string, nativeEvent: any) => void, thisArg?: any, options?: any) { const event = NativeEventMapper.event(type); function mapper(nativeEvent: any) { callbackfn.call(thisArg, event.map(nativeEvent), type, nativeEvent); } target.addEventListener(event.type, mapper, options); return { target, mapper, options, type: event.type } as Mapping; } static removeMapping({ target, mapper, options, type }: Mapping) { target.removeEventListener(type, mapper, options); } } ([ ['keydown', NativeKeyboardEvent], ['keyup', NativeKeyboardEvent], ['keypress', NativeKeyboardEvent], ['mouseenter', NativeMouseEvent], ['mouseleave', NativeMouseEvent], ['mousemove', NativeMouseEvent], ['mousedown', NativeMouseEvent], ['mouseup', NativeMouseEvent], ['click', NativeMouseEvent], ['dblclick', NativeMouseEvent], ] as [ string, { map(e: any): SyntheticEvent; new(): any; } ][]).forEach(([type, NativeEvent]) => { NativeEventMapper.register(type, class extends NativeEvent { static readonly type = type; }); }); //! \see https://developer.mozilla.org/en-US/docs/Web/Events/wheel function availableWheelEventType() { return typeof document !== 'object' || 'onwheel' in document.createElement('div') ? 'wheel' : 'onmousewheel' in document ? 'mousewheel' : 'DOMMouseScroll'; } NativeEventMapper.register('wheel', class extends NativeWheelEvent { static readonly type = availableWheelEventType(); });