From da61dcb520a194063e604a5b7d120ddf961e3ae6 Mon Sep 17 00:00:00 2001 From: * <8> Date: Wed, 16 Feb 2022 17:32:31 +0800 Subject: [PATCH] Match-id-77abf3fc5441d3da74ce4a05278ae87af13e4421 --- babel.config.js | 2 +- libs/horizon/src/event/HorizonEventMain.ts | 2 +- .../src/event/customEvents/CustomBaseEvent.ts | 108 +++++------------- .../src/event/customEvents/EventFactory.ts | 3 +- .../BeforeInputEventHandler.ts | 1 - .../simulatedEvtHandler/ChangeEventHandler.ts | 1 - .../CompositionEventHandler.ts | 1 - .../SelectionEventHandler.ts | 1 - 8 files changed, 32 insertions(+), 87 deletions(-) diff --git a/babel.config.js b/babel.config.js index eebb794c..b052c6f3 100644 --- a/babel.config.js +++ b/babel.config.js @@ -22,7 +22,7 @@ module.exports = { ['@babel/plugin-transform-spread', { loose: true, useBuiltIns: true }], '@babel/plugin-transform-parameters', ['@babel/plugin-transform-destructuring', { loose: true, useBuiltIns: true }], - ['@babel/plugin-transform-block-scoping', { throwIfClosureRequired: true }], + ['@babel/plugin-transform-block-scoping', { throwIfClosureRequired: false }], '@babel/plugin-transform-classes', '@babel/plugin-transform-runtime', '@babel/plugin-proposal-nullish-coalescing-operator', diff --git a/libs/horizon/src/event/HorizonEventMain.ts b/libs/horizon/src/event/HorizonEventMain.ts index 9a9e2719..3f319045 100644 --- a/libs/horizon/src/event/HorizonEventMain.ts +++ b/libs/horizon/src/event/HorizonEventMain.ts @@ -47,7 +47,7 @@ function getCommonListeners( nativeEvtName = 'blur'; } - const horizonEvent = createCustomEvent(horizonEvtName, nativeEvtName, nativeEvent, null, target); + const horizonEvent = createCustomEvent(horizonEvtName, nativeEvtName, nativeEvent, target); return getListenersFromTree( vNode, horizonEvtName, diff --git a/libs/horizon/src/event/customEvents/CustomBaseEvent.ts b/libs/horizon/src/event/customEvents/CustomBaseEvent.ts index e9ca60df..299a754d 100644 --- a/libs/horizon/src/event/customEvents/CustomBaseEvent.ts +++ b/libs/horizon/src/event/customEvents/CustomBaseEvent.ts @@ -2,31 +2,6 @@ * 自定义的基本事件类型 */ -import {VNode} from '../../renderer/Types'; - -// 从原生事件中复制属性到自定义事件中 -function extendAttribute(target, source) { - let val; - let attr; - for (attr in source) { - // 这两个方法需要override - if (attr === 'preventDefault' || attr === 'stopPropagation') { - continue; - } - - val = source[attr]; - if (val !== undefined) { - if (typeof val === 'function') { - target[attr] = function() { - return source[attr].apply(source, arguments); - }; - } else { - target[attr] = source[attr]; - } - } - } -} - // 兼容IE的event key const uniqueKeyMap = new Map([ ['Esc', 'Escape'], @@ -38,47 +13,50 @@ const uniqueKeyMap = new Map([ ['Del', 'Delete'], ]); +// 从原生事件中复制属性到自定义事件中 +function extendAttribute(target, source) { + let val; + let attr; + for (attr in source) { + val = source[attr]; + if (val !== undefined) { + if (typeof val === 'function') { + let fun = source[attr]; + target[attr] = function() { + return fun.apply(source, arguments); + }; + } else { + target[attr] = val; + } + } + } +} + export class CustomBaseEvent { - defaultPrevented: boolean; - target: EventTarget; isDefaultPrevented: () => boolean; isPropagationStopped: () => boolean; - currentTarget: EventTarget; - relatedTarget: EventTarget; - - // custom事件自定义属性 - customEventName: string; - targetVNode: VNode; - type: string; - timeStamp: number; - nativeEvent: any; + target: EventTarget | null; // 键盘事件属性 key: string; - charCode: number; - keyCode: number; - which: number; + + // custom事件自定义属性 + customEventName: string; + type: string; + nativeEvent: any; constructor( - customEvtName: string | null, + customEvtName: string, nativeEvtName: string, nativeEvt: { [propName: string]: any }, - vNode: VNode | null, - target: null | EventTarget + target: EventTarget | null ) { // 复制原生属性到自定义事件 extendAttribute(this, nativeEvt); - const defaultPrevented = nativeEvt.defaultPrevented != null ? - nativeEvt.defaultPrevented : - nativeEvt.returnValue === false; - this.defaultPrevented = defaultPrevented; - this.preventDefault = this.preventDefault.bind(this); - this.stopPropagation = this.stopPropagation.bind(this); - this.isDefaultPrevented = () => defaultPrevented; - this.isPropagationStopped = () => false; - this.relatedTarget = nativeEvt.relatedTarget; + this.isDefaultPrevented = () => nativeEvt.defaultPrevented; + this.isPropagationStopped = () => nativeEvt.cancelBubble; this.target = target; // 键盘事件属性 @@ -86,35 +64,7 @@ export class CustomBaseEvent { // custom事件自定义属性 this.customEventName = customEvtName; - this.targetVNode = vNode; this.type = nativeEvtName; this.nativeEvent = nativeEvt; } - - // 阻止默认行为 - preventDefault() { - this.defaultPrevented = true; - if (!this.nativeEvent) { - return; - } - - if (typeof this.nativeEvent.preventDefault === 'function') { - this.nativeEvent.preventDefault(); - } - this.nativeEvent.returnValue = false; - this.isDefaultPrevented = () => true; - } - - // 停止冒泡 - stopPropagation() { - if (!this.nativeEvent) { - return; - } - - if (typeof this.nativeEvent.stopPropagation === 'function') { - this.nativeEvent.stopPropagation(); - } - this.nativeEvent.cancelBubble = true; - this.isPropagationStopped = () => true; - } } diff --git a/libs/horizon/src/event/customEvents/EventFactory.ts b/libs/horizon/src/event/customEvents/EventFactory.ts index 59f606ee..c92a8aaa 100644 --- a/libs/horizon/src/event/customEvents/EventFactory.ts +++ b/libs/horizon/src/event/customEvents/EventFactory.ts @@ -1,12 +1,11 @@ import {CustomBaseEvent} from './CustomBaseEvent'; // 创建普通自定义事件对象实例,和原生事件对应 -export function createCustomEvent(customEventName, nativeEvtName, nativeEvent, vNode, currentTarget) { +export function createCustomEvent(customEventName, nativeEvtName, nativeEvent, currentTarget) { return new CustomBaseEvent( customEventName, nativeEvtName, nativeEvent, - vNode, currentTarget, ); } diff --git a/libs/horizon/src/event/simulatedEvtHandler/BeforeInputEventHandler.ts b/libs/horizon/src/event/simulatedEvtHandler/BeforeInputEventHandler.ts index 2ab0fc07..3d26a9a4 100644 --- a/libs/horizon/src/event/simulatedEvtHandler/BeforeInputEventHandler.ts +++ b/libs/horizon/src/event/simulatedEvtHandler/BeforeInputEventHandler.ts @@ -40,7 +40,6 @@ export function getListeners( 'onBeforeInput', 'beforeinput', nativeEvent, - null, target, ); event.data = chars; diff --git a/libs/horizon/src/event/simulatedEvtHandler/ChangeEventHandler.ts b/libs/horizon/src/event/simulatedEvtHandler/ChangeEventHandler.ts index 9c2040c2..b521f7cd 100644 --- a/libs/horizon/src/event/simulatedEvtHandler/ChangeEventHandler.ts +++ b/libs/horizon/src/event/simulatedEvtHandler/ChangeEventHandler.ts @@ -52,7 +52,6 @@ export function getListeners( 'onChange', 'change', nativeEvt, - null, target, ); return getListenersFromTree(vNode, 'onChange', event, EVENT_TYPE_ALL); diff --git a/libs/horizon/src/event/simulatedEvtHandler/CompositionEventHandler.ts b/libs/horizon/src/event/simulatedEvtHandler/CompositionEventHandler.ts index be3a38f1..a7394f5c 100644 --- a/libs/horizon/src/event/simulatedEvtHandler/CompositionEventHandler.ts +++ b/libs/horizon/src/event/simulatedEvtHandler/CompositionEventHandler.ts @@ -24,7 +24,6 @@ export function getListeners( evtType, evtName, nativeEvt, - null, target, ); return getListenersFromTree(vNode, evtType, event, EVENT_TYPE_ALL); diff --git a/libs/horizon/src/event/simulatedEvtHandler/SelectionEventHandler.ts b/libs/horizon/src/event/simulatedEvtHandler/SelectionEventHandler.ts index c5a807c4..f32cc4b2 100644 --- a/libs/horizon/src/event/simulatedEvtHandler/SelectionEventHandler.ts +++ b/libs/horizon/src/event/simulatedEvtHandler/SelectionEventHandler.ts @@ -58,7 +58,6 @@ function getSelectEvent(nativeEvent, target) { horizonEventName, 'select', nativeEvent, - null, target, ); event.target = currentElement;