From d1a459760ad0d23a019cead8a59614342319f7f2 Mon Sep 17 00:00:00 2001 From: * <8> Date: Wed, 9 Feb 2022 11:10:01 +0800 Subject: [PATCH] Match-id-c95952cfdb908b64baf7fced8799a28544af9cc0 --- libs/horizon/src/event/EventBinding.ts | 3 +- libs/horizon/src/event/HorizonEventMain.ts | 7 ++-- .../src/event/customEvents/CustomBaseEvent.ts | 2 ++ .../event/customEvents/CustomMouseEvent.ts | 26 --------------- .../src/event/customEvents/EventFactory.ts | 32 ++----------------- .../BeforeInputEventHandler.ts | 4 +-- .../simulatedEvtHandler/ChangeEventHandler.ts | 4 +-- .../CompositionEventHandler.ts | 4 +-- .../SelectionEventHandler.ts | 4 +-- libs/horizon/src/event/utils.ts | 10 ------ 10 files changed, 17 insertions(+), 79 deletions(-) delete mode 100644 libs/horizon/src/event/customEvents/CustomMouseEvent.ts diff --git a/libs/horizon/src/event/EventBinding.ts b/libs/horizon/src/event/EventBinding.ts index 1a630fff..c0de3daf 100644 --- a/libs/horizon/src/event/EventBinding.ts +++ b/libs/horizon/src/event/EventBinding.ts @@ -10,7 +10,6 @@ import { } from '../dom/DOMInternalKeys'; import {CustomBaseEvent} from './customEvents/CustomBaseEvent'; import {runDiscreteUpdates} from '../renderer/TreeBuilder'; -import {getEventTarget} from './utils'; import {isMounted} from '../renderer/vnode/VNodeUtils'; import {SuspenseComponent} from '../renderer/vnode/VNodeTags'; import {handleEventMain} from './HorizonEventMain'; @@ -33,7 +32,7 @@ function triggerDelegatedEvent( // 执行之前的调度事件 runDiscreteUpdates(); - const nativeEventTarget = getEventTarget(nativeEvent); + const nativeEventTarget = nativeEvent.target || nativeEvent.srcElement; let targetVNode = getNearestVNode(nativeEventTarget); if (targetVNode !== null) { diff --git a/libs/horizon/src/event/HorizonEventMain.ts b/libs/horizon/src/event/HorizonEventMain.ts index 0245ac27..e8913a18 100644 --- a/libs/horizon/src/event/HorizonEventMain.ts +++ b/libs/horizon/src/event/HorizonEventMain.ts @@ -14,9 +14,8 @@ import { getListeners as getSelectionListeners } from './simulatedEvtHandler/Sel import { getCustomEventNameWithOn, uniqueCharCode, - getEventTarget } from './utils'; -import { createCommonCustomEvent } from './customEvents/EventFactory'; +import { createCustomEvent } from './customEvents/EventFactory'; import { getListenersFromTree } from './ListenerGetter'; import { shouldUpdateValue, updateControlledValue } from './ControlledValueUpdater'; import { asyncUpdates, runDiscreteUpdates } from '../renderer/Renderer'; @@ -54,7 +53,7 @@ function getCommonListeners( nativeEvtName = 'blur'; } - const horizonEvent = createCommonCustomEvent(horizonEvtName, nativeEvtName, nativeEvent, null, target); + const horizonEvent = createCustomEvent(horizonEvtName, nativeEvtName, nativeEvent, null, target); return getListenersFromTree( vNode, horizonEvtName, @@ -142,7 +141,7 @@ function triggerHorizonEvents( nativeEvent: AnyNativeEvent, vNode: VNode | null, ): void { - const nativeEventTarget = getEventTarget(nativeEvent); + const nativeEventTarget = nativeEvent.target || nativeEvent.srcElement; // 获取委托事件队列 const listenerList = getProcessListenersFacade(nativeEvtName, vNode, nativeEvent, nativeEventTarget, isCapture); diff --git a/libs/horizon/src/event/customEvents/CustomBaseEvent.ts b/libs/horizon/src/event/customEvents/CustomBaseEvent.ts index 30776800..8deedd35 100644 --- a/libs/horizon/src/event/customEvents/CustomBaseEvent.ts +++ b/libs/horizon/src/event/customEvents/CustomBaseEvent.ts @@ -64,6 +64,7 @@ export class CustomBaseEvent { isDefaultPrevented: () => boolean; isPropagationStopped: () => boolean; currentTarget: EventTarget; + relatedTarget: EventTarget; constructor( customEvtName: string | null, @@ -92,6 +93,7 @@ export class CustomBaseEvent { this.stopPropagation = this.stopPropagation.bind(this); this.isDefaultPrevented = () => defaultPrevented; this.isPropagationStopped = () => false; + this.relatedTarget = nativeEvt.relatedTarget; } // 兼容性方法 diff --git a/libs/horizon/src/event/customEvents/CustomMouseEvent.ts b/libs/horizon/src/event/customEvents/CustomMouseEvent.ts deleted file mode 100644 index 8ed6f746..00000000 --- a/libs/horizon/src/event/customEvents/CustomMouseEvent.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type {VNode} from '../../renderer/Types'; -import {CustomBaseEvent} from './CustomBaseEvent'; - -export class CustomMouseEvent extends CustomBaseEvent { - relatedTarget: EventTarget; - - constructor( - customEvtName: string | null, - nativeEvtName: string, - nativeEvt: { [propName: string]: any }, - vNode: VNode, - target: null | EventTarget - ) { - super(customEvtName, nativeEvtName, nativeEvt, vNode, target); - - let relatedTarget = nativeEvt.relatedTarget; - if (relatedTarget === undefined) { - if (nativeEvt.fromElement === nativeEvt.srcElement) { - relatedTarget = nativeEvt.toElement; - } else { - relatedTarget = nativeEvt.fromElement; - } - } - this.relatedTarget = relatedTarget; - } -} diff --git a/libs/horizon/src/event/customEvents/EventFactory.ts b/libs/horizon/src/event/customEvents/EventFactory.ts index 74c36a2f..349d7979 100644 --- a/libs/horizon/src/event/customEvents/EventFactory.ts +++ b/libs/horizon/src/event/customEvents/EventFactory.ts @@ -1,30 +1,15 @@ import {CustomKeyboardEvent} from './CustomKeyboardEvent'; -import {CustomMouseEvent} from './CustomMouseEvent'; import {CustomBaseEvent} from './CustomBaseEvent'; -const CommonEventToCustom = { +const keyboardEvents = { keypress: CustomKeyboardEvent, keydown: CustomKeyboardEvent, keyup: CustomKeyboardEvent, - click: CustomMouseEvent, - dblclick: CustomMouseEvent, - mousedown: CustomMouseEvent, - mousemove: CustomMouseEvent, - mouseup: CustomMouseEvent, - mouseout: CustomMouseEvent, - mouseover: CustomMouseEvent, - contextmenu: CustomMouseEvent, - pointercancel: CustomMouseEvent, - pointerdown: CustomMouseEvent, - pointermove: CustomMouseEvent, - pointerout: CustomMouseEvent, - pointerover: CustomMouseEvent, - pointerup: CustomMouseEvent, } // 创建普通自定义事件对象实例,和原生事件对应 -export function createCommonCustomEvent(customEventName, nativeEvtName, nativeEvent, vNode, currentTarget) { - const EventConstructor = CommonEventToCustom[nativeEvtName] || CustomBaseEvent; +export function createCustomEvent(customEventName, nativeEvtName, nativeEvent, vNode, currentTarget) { + const EventConstructor = keyboardEvents[nativeEvtName] || CustomBaseEvent; return new EventConstructor( customEventName, nativeEvtName, @@ -33,14 +18,3 @@ export function createCommonCustomEvent(customEventName, nativeEvtName, nativeEv currentTarget, ); } - -// 创建模拟事件实例对象,需要handler特殊处理 -export function createHandlerCustomEvent(customEventName, nativeEvtName, nativeEvent, vNode, currentTarget) { - return new CustomMouseEvent( - customEventName, - nativeEvtName, - nativeEvent, - vNode, - currentTarget, - ); -} diff --git a/libs/horizon/src/event/simulatedEvtHandler/BeforeInputEventHandler.ts b/libs/horizon/src/event/simulatedEvtHandler/BeforeInputEventHandler.ts index cd96a2e9..2ab0fc07 100644 --- a/libs/horizon/src/event/simulatedEvtHandler/BeforeInputEventHandler.ts +++ b/libs/horizon/src/event/simulatedEvtHandler/BeforeInputEventHandler.ts @@ -1,7 +1,7 @@ import type {VNode} from '../../renderer/Types'; import type {AnyNativeEvent} from '../Types'; import {getListenersFromTree} from '../ListenerGetter'; -import {createHandlerCustomEvent} from '../customEvents/EventFactory'; +import {createCustomEvent} from '../customEvents/EventFactory'; import {CHAR_CODE_SPACE, EVENT_TYPE_ALL} from '../const'; import {CustomBaseEvent} from '../customEvents/CustomBaseEvent'; import {ListenerUnitList} from '../Types'; @@ -36,7 +36,7 @@ export function getListeners( return []; } - const event: CustomBaseEvent = createHandlerCustomEvent( + const event: CustomBaseEvent = createCustomEvent( 'onBeforeInput', 'beforeinput', nativeEvent, diff --git a/libs/horizon/src/event/simulatedEvtHandler/ChangeEventHandler.ts b/libs/horizon/src/event/simulatedEvtHandler/ChangeEventHandler.ts index 7a32a5c8..9c2040c2 100644 --- a/libs/horizon/src/event/simulatedEvtHandler/ChangeEventHandler.ts +++ b/libs/horizon/src/event/simulatedEvtHandler/ChangeEventHandler.ts @@ -1,4 +1,4 @@ -import {createHandlerCustomEvent} from '../customEvents/EventFactory'; +import {createCustomEvent} from '../customEvents/EventFactory'; import {getDom} from '../../dom/DOMInternalKeys'; import {isInputValueChanged} from '../../dom/valueHandler/ValueChangeHandler'; import {addValueUpdateList} from '../ControlledValueUpdater'; @@ -48,7 +48,7 @@ export function getListeners( // 判断是否需要触发change事件 if (shouldTriggerChangeEvent(targetDom, nativeEvtName)) { addValueUpdateList(target); - const event = createHandlerCustomEvent( + const event = createCustomEvent( 'onChange', 'change', nativeEvt, diff --git a/libs/horizon/src/event/simulatedEvtHandler/CompositionEventHandler.ts b/libs/horizon/src/event/simulatedEvtHandler/CompositionEventHandler.ts index c02dc78e..be3a38f1 100644 --- a/libs/horizon/src/event/simulatedEvtHandler/CompositionEventHandler.ts +++ b/libs/horizon/src/event/simulatedEvtHandler/CompositionEventHandler.ts @@ -1,7 +1,7 @@ import type {VNode} from '../../renderer/Types'; import type {AnyNativeEvent} from '../Types'; import {getListenersFromTree} from '../ListenerGetter'; -import {createHandlerCustomEvent} from '../customEvents/EventFactory'; +import {createCustomEvent} from '../customEvents/EventFactory'; import {EVENT_TYPE_ALL} from '../const'; import {ListenerUnitList} from '../Types'; @@ -20,7 +20,7 @@ export function getListeners( ): ListenerUnitList { const evtType = compositionEventObj[evtName]; - const event = createHandlerCustomEvent( + const event = createCustomEvent( evtType, evtName, nativeEvt, diff --git a/libs/horizon/src/event/simulatedEvtHandler/SelectionEventHandler.ts b/libs/horizon/src/event/simulatedEvtHandler/SelectionEventHandler.ts index ac7cbe43..c5a807c4 100644 --- a/libs/horizon/src/event/simulatedEvtHandler/SelectionEventHandler.ts +++ b/libs/horizon/src/event/simulatedEvtHandler/SelectionEventHandler.ts @@ -1,4 +1,4 @@ -import {createHandlerCustomEvent} from '../customEvents/EventFactory'; +import {createCustomEvent} from '../customEvents/EventFactory'; import {shallowCompare} from '../../renderer/utils/compare'; import {getFocusedDom} from '../../dom/utils/Common'; import {getDom} from '../../dom/DOMInternalKeys'; @@ -54,7 +54,7 @@ function getSelectEvent(nativeEvent, target) { if (!shallowCompare(lastSelection, currentSelection)) { lastSelection = currentSelection; - const event = createHandlerCustomEvent( + const event = createCustomEvent( horizonEventName, 'select', nativeEvent, diff --git a/libs/horizon/src/event/utils.ts b/libs/horizon/src/event/utils.ts index 182616a8..12a6735e 100644 --- a/libs/horizon/src/event/utils.ts +++ b/libs/horizon/src/event/utils.ts @@ -1,4 +1,3 @@ -import {isText} from '../dom/utils/Common'; import { CHAR_CODE_ENTER, CHAR_CODE_SPACE } from './const'; export function uniqueCharCode(nativeEvent): number { @@ -22,15 +21,6 @@ export function uniqueCharCode(nativeEvent): number { return 0; } -// 获取事件的target对象 -export function getEventTarget(nativeEvent) { - const target = nativeEvent.target || nativeEvent.srcElement || window; - if (isText(target)) { - return target.parentNode; - } - return target; -} - // 支持的输入框类型 const supportedInputTypes = ['color', 'date', 'datetime', 'datetime-local', 'email', 'month', 'number', 'password', 'range', 'search', 'tel', 'text', 'time', 'url', 'week'];