@whitesev/domutils 1.9.12 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.amd.js +44 -52
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +44 -52
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +44 -52
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +44 -52
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +44 -52
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +44 -52
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/ElementEvent.d.ts +5 -5
- package/dist/types/src/types/DOMUtilsEvent.d.ts +27 -11
- package/package.json +4 -4
- package/src/ElementEvent.ts +61 -66
- package/src/types/DOMUtilsEvent.d.ts +27 -11
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@whitesev/domutils",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.1",
|
|
5
5
|
"description": "适合在浏览器中操作DOM的常用工具类",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"typescript",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"@rollup/plugin-terser": "^1.0.0",
|
|
40
40
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
41
41
|
"browserslist": "^4.28.1",
|
|
42
|
-
"caniuse-lite": "^1.0.
|
|
42
|
+
"caniuse-lite": "^1.0.30001782",
|
|
43
43
|
"eslint": "^10.1.0",
|
|
44
44
|
"eslint-config-prettier": "^10.1.8",
|
|
45
45
|
"eslint-plugin-compat": "^7.0.1",
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
"globals": "^17.4.0",
|
|
48
48
|
"oxfmt": "^0.42.0",
|
|
49
49
|
"oxlint": "1.57.0",
|
|
50
|
-
"rollup": "^4.60.
|
|
50
|
+
"rollup": "^4.60.1",
|
|
51
51
|
"rollup-plugin-clear": "^2.0.7",
|
|
52
52
|
"tslib": "^2.8.1",
|
|
53
53
|
"typescript": "^6.0.2",
|
|
54
|
-
"typescript-eslint": "^8.
|
|
54
|
+
"typescript-eslint": "^8.58.0"
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"lint": "oxlint .",
|
package/src/ElementEvent.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { CommonUtils } from "./CommonUtils";
|
|
2
2
|
import { ElementAnimate } from "./ElementAnimate";
|
|
3
3
|
import { GlobalData } from "./GlobalData";
|
|
4
|
-
import { OriginPrototype } from "./OriginPrototype";
|
|
5
4
|
import type {
|
|
6
5
|
DOMUtils_Event,
|
|
7
6
|
DOMUtils_EventType,
|
|
8
7
|
DOMUtilsAddEventListenerResult,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
DOMUtilsDoubleEventEventListenerOption,
|
|
9
|
+
DOMUtilsDoubleEventHandler,
|
|
10
|
+
DOMUtilsDoubleEventHandlerWithSelector,
|
|
11
|
+
DOMUtilsDoubleEventOption,
|
|
12
12
|
DOMUtilsElementEventType,
|
|
13
13
|
DOMUtilsEventListenerOption,
|
|
14
14
|
DOMUtilsEventListenerOptionsAttribute,
|
|
@@ -242,6 +242,9 @@ class ElementEvent extends ElementAnimate {
|
|
|
242
242
|
listenerOption = getOption(args, 4, listenerOption);
|
|
243
243
|
}
|
|
244
244
|
$elList.forEach(($elItem) => {
|
|
245
|
+
// window和document共用一个对象
|
|
246
|
+
// 这样就能处理子元素选择器无法匹配的问题
|
|
247
|
+
const targetIsWindow = CommonUtils.isWin($elItem);
|
|
245
248
|
// 遍历事件名设置元素事件
|
|
246
249
|
eventTypeList.forEach((eventName) => {
|
|
247
250
|
/**
|
|
@@ -275,12 +278,7 @@ class ElementEvent extends ElementAnimate {
|
|
|
275
278
|
} else {
|
|
276
279
|
$target = event.target as HTMLElement;
|
|
277
280
|
}
|
|
278
|
-
|
|
279
|
-
if (CommonUtils.isWin($parent)) {
|
|
280
|
-
// window和document共用一个对象
|
|
281
|
-
// 这样就能处理子元素选择器无法匹配的问题
|
|
282
|
-
$parent = that.windowApi.document.documentElement;
|
|
283
|
-
}
|
|
281
|
+
const $parent = targetIsWindow ? that.windowApi.document.documentElement : $elItem;
|
|
284
282
|
const findValue = selectorList.find((selectors) => {
|
|
285
283
|
// 判断目标元素是否匹配选择器
|
|
286
284
|
if (that.matches($target, selectors)) {
|
|
@@ -297,20 +295,7 @@ class ElementEvent extends ElementAnimate {
|
|
|
297
295
|
});
|
|
298
296
|
if (findValue) {
|
|
299
297
|
// 这里尝试使用defineProperty修改event的target值
|
|
300
|
-
|
|
301
|
-
try {
|
|
302
|
-
OriginPrototype.Object.defineProperty(event, "target", {
|
|
303
|
-
get() {
|
|
304
|
-
return $target;
|
|
305
|
-
},
|
|
306
|
-
});
|
|
307
|
-
OriginPrototype.Object.defineProperty(event, "originTarget", {
|
|
308
|
-
get() {
|
|
309
|
-
return originTarget;
|
|
310
|
-
},
|
|
311
|
-
});
|
|
312
|
-
// oxlint-disable-next-line no-empty
|
|
313
|
-
} catch {}
|
|
298
|
+
// 不建议使用覆盖target,因为可能会有兼容性问题
|
|
314
299
|
execCallback = true;
|
|
315
300
|
call_this = $target;
|
|
316
301
|
call_event = event;
|
|
@@ -618,6 +603,9 @@ class ElementEvent extends ElementAnimate {
|
|
|
618
603
|
if (handlers.length === 0) {
|
|
619
604
|
// 如果没有任意的handler,那么删除该属性
|
|
620
605
|
CommonUtils.delete(elementEvents, eventType);
|
|
606
|
+
if (Object.keys(elementEvents).length === 0) {
|
|
607
|
+
CommonUtils.delete($elItem, GlobalData.domEventSymbol);
|
|
608
|
+
}
|
|
621
609
|
}
|
|
622
610
|
});
|
|
623
611
|
Reflect.set($elItem, GlobalData.domEventSymbol, elementEvents);
|
|
@@ -663,13 +651,13 @@ class ElementEvent extends ElementAnimate {
|
|
|
663
651
|
}
|
|
664
652
|
$elList.forEach(($elItem) => {
|
|
665
653
|
const symbolList = [...new Set([...Object.getOwnPropertySymbols($elItem), GlobalData.domEventSymbol])];
|
|
666
|
-
symbolList.forEach((
|
|
667
|
-
if (!
|
|
654
|
+
symbolList.forEach((__symbol__) => {
|
|
655
|
+
if (!__symbol__.toString().startsWith("Symbol(events_")) {
|
|
668
656
|
return;
|
|
669
657
|
}
|
|
670
658
|
const elementEvents: {
|
|
671
659
|
[key: string]: DOMUtilsEventListenerOptionsAttribute[];
|
|
672
|
-
} = Reflect.get($elItem,
|
|
660
|
+
} = Reflect.get($elItem, __symbol__) || {};
|
|
673
661
|
const iterEventNameList = eventTypeList.length ? eventTypeList : Object.keys(elementEvents);
|
|
674
662
|
iterEventNameList.forEach((eventName) => {
|
|
675
663
|
const handlers: DOMUtilsEventListenerOptionsAttribute[] = elementEvents[eventName];
|
|
@@ -681,8 +669,11 @@ class ElementEvent extends ElementAnimate {
|
|
|
681
669
|
capture: handler.option.capture,
|
|
682
670
|
});
|
|
683
671
|
}
|
|
684
|
-
const events = Reflect.get($elItem,
|
|
672
|
+
const events = Reflect.get($elItem, __symbol__);
|
|
685
673
|
CommonUtils.delete(events, eventName);
|
|
674
|
+
if (Object.keys(events).length === 0) {
|
|
675
|
+
CommonUtils.delete($elItem, __symbol__);
|
|
676
|
+
}
|
|
686
677
|
});
|
|
687
678
|
});
|
|
688
679
|
});
|
|
@@ -1454,44 +1445,44 @@ class ElementEvent extends ElementAnimate {
|
|
|
1454
1445
|
};
|
|
1455
1446
|
}
|
|
1456
1447
|
/**
|
|
1457
|
-
*
|
|
1448
|
+
* 监听事件单/双次触发
|
|
1458
1449
|
* @param $el 监听的元素
|
|
1459
1450
|
* @param handler 处理的回调函数
|
|
1460
1451
|
* @param options 监听器的配置
|
|
1461
1452
|
*/
|
|
1462
|
-
|
|
1453
|
+
onOneOrDouble(
|
|
1463
1454
|
$el: DOMUtilsElementEventType,
|
|
1464
|
-
handler: (event:
|
|
1465
|
-
options?:
|
|
1455
|
+
handler: (event: Event, option: DOMUtilsDoubleEventOption) => void | Promise<void>,
|
|
1456
|
+
options?: DOMUtilsDoubleEventEventListenerOption | boolean
|
|
1466
1457
|
): {
|
|
1467
1458
|
off(): void;
|
|
1468
1459
|
};
|
|
1469
1460
|
/**
|
|
1470
|
-
*
|
|
1461
|
+
* 监听事件单/双次触发
|
|
1471
1462
|
* @param $el 监听的元素
|
|
1472
1463
|
* @param selector 子元素选择器
|
|
1473
1464
|
* @param handler 处理的回调函数
|
|
1474
1465
|
* @param options 监听器的配置
|
|
1475
1466
|
*/
|
|
1476
|
-
|
|
1467
|
+
onOneOrDouble<T = HTMLElement>(
|
|
1477
1468
|
$el: DOMUtilsElementEventType,
|
|
1478
1469
|
selector: string | string[],
|
|
1479
|
-
handler: (
|
|
1480
|
-
event:
|
|
1470
|
+
handler: <E extends Event = Event>(
|
|
1471
|
+
event: E,
|
|
1481
1472
|
$selector: T,
|
|
1482
|
-
option:
|
|
1473
|
+
option: DOMUtilsDoubleEventOption
|
|
1483
1474
|
) => void | Promise<void>,
|
|
1484
|
-
options?:
|
|
1475
|
+
options?: DOMUtilsDoubleEventEventListenerOption | boolean
|
|
1485
1476
|
): {
|
|
1486
1477
|
off(): void;
|
|
1487
1478
|
};
|
|
1488
|
-
|
|
1479
|
+
onOneOrDouble(...args: any[]): {
|
|
1489
1480
|
off(): void;
|
|
1490
1481
|
} {
|
|
1491
1482
|
const $el: DOMUtilsElementEventType = args[0];
|
|
1492
1483
|
let selector: string | string[] | undefined | null = void 0;
|
|
1493
|
-
let handler:
|
|
1494
|
-
let options:
|
|
1484
|
+
let handler: DOMUtilsDoubleEventHandler | DOMUtilsDoubleEventHandlerWithSelector;
|
|
1485
|
+
let options: DOMUtilsDoubleEventEventListenerOption | boolean | undefined;
|
|
1495
1486
|
if (args.length === 2) {
|
|
1496
1487
|
if (typeof args[1] === "function") {
|
|
1497
1488
|
handler = args[1];
|
|
@@ -1514,57 +1505,61 @@ class ElementEvent extends ElementAnimate {
|
|
|
1514
1505
|
throw new Error("args length error");
|
|
1515
1506
|
}
|
|
1516
1507
|
|
|
1517
|
-
let
|
|
1518
|
-
let
|
|
1508
|
+
let eventNodeMap = new WeakMap<Element, Event>();
|
|
1509
|
+
let isDouble = false;
|
|
1519
1510
|
let timer: number | undefined = void 0;
|
|
1511
|
+
let eventType: DOMUtils_EventType | DOMUtils_EventType[] = "pointerup";
|
|
1520
1512
|
/** 检测是否是单击的延迟时间 */
|
|
1521
|
-
|
|
1513
|
+
let checkClickTime = 200;
|
|
1514
|
+
if (typeof options === "object" && options != null) {
|
|
1515
|
+
if (typeof options.eventType === "string" || Array.isArray(options.eventType)) {
|
|
1516
|
+
eventType = options.eventType;
|
|
1517
|
+
}
|
|
1518
|
+
if (typeof options.checkClickTime === "number") {
|
|
1519
|
+
checkClickTime = options.checkClickTime;
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
1522
|
|
|
1523
|
-
const
|
|
1524
|
-
evt: MouseEvent | PointerEvent | TouchEvent,
|
|
1525
|
-
option: DOMUtilsDoubleClickOption,
|
|
1526
|
-
$selector?: HTMLElement
|
|
1527
|
-
) => {
|
|
1523
|
+
const callback = (evt: Event, option: DOMUtilsDoubleEventOption, $selector?: HTMLElement) => {
|
|
1528
1524
|
if ($selector) {
|
|
1529
|
-
return (<
|
|
1525
|
+
return (<DOMUtilsDoubleEventHandlerWithSelector>handler)(evt, $selector, option);
|
|
1530
1526
|
} else {
|
|
1531
|
-
return (<
|
|
1527
|
+
return (<DOMUtilsDoubleEventHandler>handler)(evt, option);
|
|
1532
1528
|
}
|
|
1533
1529
|
};
|
|
1534
1530
|
|
|
1535
1531
|
const pointerUpListener = this.on(
|
|
1536
1532
|
$el,
|
|
1537
|
-
|
|
1533
|
+
eventType,
|
|
1538
1534
|
selector,
|
|
1539
1535
|
(evt, $selector) => {
|
|
1540
|
-
// this.preventEvent(evt);
|
|
1541
1536
|
clearTimeout(timer);
|
|
1542
1537
|
timer = void 0;
|
|
1543
|
-
if (
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1538
|
+
if (isDouble && eventNodeMap.has($selector as Element)) {
|
|
1539
|
+
isDouble = false;
|
|
1540
|
+
eventNodeMap.delete($selector);
|
|
1541
|
+
// 判定为双次
|
|
1542
|
+
callback(
|
|
1548
1543
|
evt,
|
|
1549
1544
|
{
|
|
1550
|
-
|
|
1545
|
+
isDouble: true,
|
|
1551
1546
|
},
|
|
1552
1547
|
$selector
|
|
1553
1548
|
);
|
|
1554
1549
|
} else {
|
|
1555
1550
|
timer = setTimeout(() => {
|
|
1556
|
-
|
|
1557
|
-
//
|
|
1558
|
-
|
|
1551
|
+
isDouble = false;
|
|
1552
|
+
// 判断为单次
|
|
1553
|
+
callback(
|
|
1559
1554
|
evt,
|
|
1560
1555
|
{
|
|
1561
|
-
|
|
1556
|
+
isDouble: false,
|
|
1562
1557
|
},
|
|
1563
1558
|
$selector
|
|
1564
1559
|
);
|
|
1565
1560
|
}, checkClickTime);
|
|
1566
|
-
|
|
1567
|
-
|
|
1561
|
+
isDouble = true;
|
|
1562
|
+
eventNodeMap.set($selector as Element, evt);
|
|
1568
1563
|
}
|
|
1569
1564
|
},
|
|
1570
1565
|
options
|
|
@@ -1574,7 +1569,7 @@ class ElementEvent extends ElementAnimate {
|
|
|
1574
1569
|
off() {
|
|
1575
1570
|
pointerUpListener.off();
|
|
1576
1571
|
// @ts-expect-error
|
|
1577
|
-
|
|
1572
|
+
eventNodeMap = null;
|
|
1578
1573
|
},
|
|
1579
1574
|
};
|
|
1580
1575
|
}
|
|
@@ -415,29 +415,45 @@ export declare interface DOMUtilsAddEventListenerResult {
|
|
|
415
415
|
/**
|
|
416
416
|
* 传递的双击配置信息
|
|
417
417
|
*/
|
|
418
|
-
export declare type
|
|
418
|
+
export declare type DOMUtilsDoubleEventOption = {
|
|
419
419
|
/**
|
|
420
|
-
*
|
|
420
|
+
* 是否是双次
|
|
421
421
|
*
|
|
422
|
-
* + `true
|
|
423
|
-
* + `false
|
|
422
|
+
* + `true`:双次
|
|
423
|
+
* + `false`:单次
|
|
424
424
|
*/
|
|
425
|
-
|
|
425
|
+
isDouble: boolean;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* 传入的配置项
|
|
430
|
+
*/
|
|
431
|
+
export declare type DOMUtilsDoubleEventEventListenerOption = DOMUtilsEventListenerOption & {
|
|
432
|
+
/**
|
|
433
|
+
* 检测单次的延迟时间(ms)
|
|
434
|
+
* @default 200
|
|
435
|
+
*/
|
|
436
|
+
checkClickTime?: number;
|
|
437
|
+
/**
|
|
438
|
+
* 监听的事件
|
|
439
|
+
* @default "pointerup"
|
|
440
|
+
*/
|
|
441
|
+
eventType?: DOMUtils_EventType;
|
|
426
442
|
};
|
|
427
443
|
|
|
428
444
|
/**
|
|
429
445
|
* 双击的处理函数
|
|
430
446
|
*/
|
|
431
|
-
export declare type
|
|
432
|
-
event:
|
|
433
|
-
option:
|
|
447
|
+
export declare type DOMUtilsDoubleEventHandler = (
|
|
448
|
+
event: Event,
|
|
449
|
+
option: DOMUtilsDoubleEventOption
|
|
434
450
|
) => void | Promise<void>;
|
|
435
451
|
|
|
436
452
|
/**
|
|
437
453
|
* 双击的处理函数(传入了selector)
|
|
438
454
|
*/
|
|
439
|
-
export declare type
|
|
440
|
-
event:
|
|
455
|
+
export declare type DOMUtilsDoubleEventHandlerWithSelector<T = HTMLElement> = (
|
|
456
|
+
event: Event,
|
|
441
457
|
$selector: T,
|
|
442
|
-
option:
|
|
458
|
+
option: DOMUtilsDoubleEventOption
|
|
443
459
|
) => void | Promise<void>;
|