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-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',

View File

@ -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,

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
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;
}
}

View File

@ -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,
);
}

View File

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

View File

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

View File

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

View File

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