Match-id-cfe32b59886c5eb883f942a6a05af42037988bea

This commit is contained in:
* 2022-02-16 17:36:02 +08:00 committed by *
commit 39674377c3
8 changed files with 32 additions and 87 deletions

View File

@ -22,7 +22,7 @@ module.exports = {
['@babel/plugin-transform-spread', { loose: true, useBuiltIns: true }], ['@babel/plugin-transform-spread', { loose: true, useBuiltIns: true }],
'@babel/plugin-transform-parameters', '@babel/plugin-transform-parameters',
['@babel/plugin-transform-destructuring', { loose: true, useBuiltIns: true }], ['@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-classes',
'@babel/plugin-transform-runtime', '@babel/plugin-transform-runtime',
'@babel/plugin-proposal-nullish-coalescing-operator', '@babel/plugin-proposal-nullish-coalescing-operator',

View File

@ -47,7 +47,7 @@ function getCommonListeners(
nativeEvtName = 'blur'; nativeEvtName = 'blur';
} }
const horizonEvent = createCustomEvent(horizonEvtName, nativeEvtName, nativeEvent, null, target); const horizonEvent = createCustomEvent(horizonEvtName, nativeEvtName, nativeEvent, target);
return getListenersFromTree( return getListenersFromTree(
vNode, vNode,
horizonEvtName, horizonEvtName,

View File

@ -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 // 兼容IE的event key
const uniqueKeyMap = new Map([ const uniqueKeyMap = new Map([
['Esc', 'Escape'], ['Esc', 'Escape'],
@ -38,47 +13,50 @@ const uniqueKeyMap = new Map([
['Del', 'Delete'], ['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 { export class CustomBaseEvent {
defaultPrevented: boolean;
target: EventTarget;
isDefaultPrevented: () => boolean; isDefaultPrevented: () => boolean;
isPropagationStopped: () => boolean; isPropagationStopped: () => boolean;
currentTarget: EventTarget; target: EventTarget | null;
relatedTarget: EventTarget;
// custom事件自定义属性
customEventName: string;
targetVNode: VNode;
type: string;
timeStamp: number;
nativeEvent: any;
// 键盘事件属性 // 键盘事件属性
key: string; key: string;
charCode: number;
keyCode: number; // custom事件自定义属性
which: number; customEventName: string;
type: string;
nativeEvent: any;
constructor( constructor(
customEvtName: string | null, customEvtName: string,
nativeEvtName: string, nativeEvtName: string,
nativeEvt: { [propName: string]: any }, nativeEvt: { [propName: string]: any },
vNode: VNode | null, target: EventTarget | null
target: null | EventTarget
) { ) {
// 复制原生属性到自定义事件 // 复制原生属性到自定义事件
extendAttribute(this, nativeEvt); extendAttribute(this, nativeEvt);
const defaultPrevented = nativeEvt.defaultPrevented != null ? this.isDefaultPrevented = () => nativeEvt.defaultPrevented;
nativeEvt.defaultPrevented : this.isPropagationStopped = () => nativeEvt.cancelBubble;
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.target = target; this.target = target;
// 键盘事件属性 // 键盘事件属性
@ -86,35 +64,7 @@ export class CustomBaseEvent {
// custom事件自定义属性 // custom事件自定义属性
this.customEventName = customEvtName; this.customEventName = customEvtName;
this.targetVNode = vNode;
this.type = nativeEvtName; this.type = nativeEvtName;
this.nativeEvent = nativeEvt; 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;
}
} }

View File

@ -1,12 +1,11 @@
import {CustomBaseEvent} from './CustomBaseEvent'; import {CustomBaseEvent} from './CustomBaseEvent';
// 创建普通自定义事件对象实例,和原生事件对应 // 创建普通自定义事件对象实例,和原生事件对应
export function createCustomEvent(customEventName, nativeEvtName, nativeEvent, vNode, currentTarget) { export function createCustomEvent(customEventName, nativeEvtName, nativeEvent, currentTarget) {
return new CustomBaseEvent( return new CustomBaseEvent(
customEventName, customEventName,
nativeEvtName, nativeEvtName,
nativeEvent, nativeEvent,
vNode,
currentTarget, currentTarget,
); );
} }

View File

@ -40,7 +40,6 @@ export function getListeners(
'onBeforeInput', 'onBeforeInput',
'beforeinput', 'beforeinput',
nativeEvent, nativeEvent,
null,
target, target,
); );
event.data = chars; event.data = chars;

View File

@ -52,7 +52,6 @@ export function getListeners(
'onChange', 'onChange',
'change', 'change',
nativeEvt, nativeEvt,
null,
target, target,
); );
return getListenersFromTree(vNode, 'onChange', event, EVENT_TYPE_ALL); return getListenersFromTree(vNode, 'onChange', event, EVENT_TYPE_ALL);

View File

@ -24,7 +24,6 @@ export function getListeners(
evtType, evtType,
evtName, evtName,
nativeEvt, nativeEvt,
null,
target, target,
); );
return getListenersFromTree(vNode, evtType, event, EVENT_TYPE_ALL); return getListenersFromTree(vNode, evtType, event, EVENT_TYPE_ALL);

View File

@ -58,7 +58,6 @@ function getSelectEvent(nativeEvent, target) {
horizonEventName, horizonEventName,
'select', 'select',
nativeEvent, nativeEvent,
null,
target, target,
); );
event.target = currentElement; event.target = currentElement;