@whitesev/domutils 1.9.3 → 1.9.5

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.
@@ -6,6 +6,8 @@ import type {
6
6
  DOMUtils_Event,
7
7
  DOMUtils_EventType,
8
8
  DOMUtilsAddEventListenerResult,
9
+ DOMUtilsDoubleClickHandler,
10
+ DOMUtilsDoubleClickHandlerWithSelector,
9
11
  DOMUtilsDoubleClickOption,
10
12
  DOMUtilsElementEventType,
11
13
  DOMUtilsEventListenerOption,
@@ -1398,7 +1400,7 @@ class ElementEvent extends ElementAnimate {
1398
1400
  * @param options 监听器的配置
1399
1401
  */
1400
1402
  onDoubleClick(
1401
- $el: DOMUtilsTargetElementType,
1403
+ $el: DOMUtilsElementEventType,
1402
1404
  handler: (event: MouseEvent | PointerEvent | TouchEvent, option: DOMUtilsDoubleClickOption) => void | Promise<void>,
1403
1405
  options?: DOMUtilsEventListenerOption | boolean
1404
1406
  ): {
@@ -1411,10 +1413,14 @@ class ElementEvent extends ElementAnimate {
1411
1413
  * @param handler 处理的回调函数
1412
1414
  * @param options 监听器的配置
1413
1415
  */
1414
- onDoubleClick(
1415
- $el: DOMUtilsTargetElementType,
1416
- selector: string | string[] | undefined | null,
1417
- handler: (event: MouseEvent | PointerEvent | TouchEvent, option: DOMUtilsDoubleClickOption) => void | Promise<void>,
1416
+ onDoubleClick<T = HTMLElement>(
1417
+ $el: DOMUtilsElementEventType,
1418
+ selector: string | string[],
1419
+ handler: (
1420
+ event: MouseEvent | PointerEvent | TouchEvent,
1421
+ $selector: T,
1422
+ option: DOMUtilsDoubleClickOption
1423
+ ) => void | Promise<void>,
1418
1424
  options?: DOMUtilsEventListenerOption | boolean
1419
1425
  ): {
1420
1426
  off(): void;
@@ -1422,12 +1428,9 @@ class ElementEvent extends ElementAnimate {
1422
1428
  onDoubleClick(...args: any[]): {
1423
1429
  off(): void;
1424
1430
  } {
1425
- const $el: DOMUtilsTargetElementType = args[0];
1431
+ const $el: DOMUtilsElementEventType = args[0];
1426
1432
  let selector: string | string[] | undefined | null = void 0;
1427
- let handler: (
1428
- event: MouseEvent | PointerEvent | TouchEvent,
1429
- option: DOMUtilsDoubleClickOption
1430
- ) => void | Promise<void>;
1433
+ let handler: DOMUtilsDoubleClickHandler | DOMUtilsDoubleClickHandlerWithSelector;
1431
1434
  let options: DOMUtilsEventListenerOption | boolean | undefined;
1432
1435
  if (args.length === 2) {
1433
1436
  if (typeof args[1] === "function") {
@@ -1451,61 +1454,57 @@ class ElementEvent extends ElementAnimate {
1451
1454
  throw new Error("args length error");
1452
1455
  }
1453
1456
 
1454
- let $click: Element | null = null;
1457
+ let clickMap = new WeakMap<Element, PointerEvent>();
1455
1458
  let isDoubleClick = false;
1456
1459
  let timer: number | undefined = void 0;
1457
- /** 是否是移动端点击 */
1458
- let isMobileTouch = false;
1459
1460
  /** 检测是否是单击的延迟时间 */
1460
1461
  const checkClickTime = 200;
1461
1462
 
1462
- const dblclick_handler = async (evt: MouseEvent | PointerEvent | TouchEvent, option: DOMUtilsDoubleClickOption) => {
1463
- if (evt.type === "dblclick" && isMobileTouch) {
1464
- // 禁止在移动端触发dblclick事件
1465
- return;
1463
+ const dblclick_handler = (
1464
+ evt: MouseEvent | PointerEvent | TouchEvent,
1465
+ option: DOMUtilsDoubleClickOption,
1466
+ $selector?: HTMLElement
1467
+ ) => {
1468
+ if ($selector) {
1469
+ return (<DOMUtilsDoubleClickHandlerWithSelector>handler)(evt, $selector, option);
1470
+ } else {
1471
+ return (<DOMUtilsDoubleClickHandler>handler)(evt, option);
1466
1472
  }
1467
- await handler(evt, option);
1468
1473
  };
1469
1474
 
1470
- const dblClickListener = this.on(
1471
- $el,
1472
- "dblclick",
1473
- (evt) => {
1474
- this.preventEvent(evt);
1475
- dblclick_handler(evt, {
1476
- isDoubleClick: true,
1477
- });
1478
- },
1479
- options
1480
- );
1481
- const touchEndListener = this.on(
1475
+ const pointerUpListener = this.on(
1482
1476
  $el,
1483
1477
  "pointerup",
1484
1478
  selector,
1485
1479
  (evt, $selector) => {
1486
- this.preventEvent(evt);
1487
- if (evt.pointerType === "touch") {
1488
- isMobileTouch = true;
1489
- }
1480
+ // this.preventEvent(evt);
1490
1481
  clearTimeout(timer);
1491
1482
  timer = void 0;
1492
- if (isDoubleClick && $click === $selector) {
1483
+ if (isDoubleClick && clickMap.has($selector as Element)) {
1493
1484
  isDoubleClick = false;
1494
- $click = null;
1485
+ clickMap.delete($selector);
1495
1486
  /* 判定为双击 */
1496
- dblclick_handler(evt, {
1497
- isDoubleClick: true,
1498
- });
1487
+ dblclick_handler(
1488
+ evt,
1489
+ {
1490
+ isDoubleClick: true,
1491
+ },
1492
+ $selector
1493
+ );
1499
1494
  } else {
1500
1495
  timer = setTimeout(() => {
1501
1496
  isDoubleClick = false;
1502
1497
  // 判断为单击
1503
- dblclick_handler(evt, {
1504
- isDoubleClick: false,
1505
- });
1498
+ dblclick_handler(
1499
+ evt,
1500
+ {
1501
+ isDoubleClick: false,
1502
+ },
1503
+ $selector
1504
+ );
1506
1505
  }, checkClickTime);
1507
1506
  isDoubleClick = true;
1508
- $click = $selector;
1507
+ clickMap.set($selector as Element, evt);
1509
1508
  }
1510
1509
  },
1511
1510
  options
@@ -1513,9 +1512,9 @@ class ElementEvent extends ElementAnimate {
1513
1512
 
1514
1513
  return {
1515
1514
  off() {
1516
- $click = null;
1517
- dblClickListener.off();
1518
- touchEndListener.off();
1515
+ pointerUpListener.off();
1516
+ // @ts-expect-error
1517
+ clickMap = null;
1519
1518
  },
1520
1519
  };
1521
1520
  }
@@ -418,3 +418,20 @@ export declare type DOMUtilsDoubleClickOption = {
418
418
  */
419
419
  isDoubleClick: boolean;
420
420
  };
421
+
422
+ /**
423
+ * 双击的处理函数
424
+ */
425
+ export declare type DOMUtilsDoubleClickHandler = (
426
+ event: MouseEvent | PointerEvent | TouchEvent,
427
+ option: DOMUtilsDoubleClickOption
428
+ ) => void | Promise<void>;
429
+
430
+ /**
431
+ * 双击的处理函数(传入了selector)
432
+ */
433
+ export declare type DOMUtilsDoubleClickHandlerWithSelector<T = HTMLElement> = (
434
+ event: MouseEvent | PointerEvent | TouchEvent,
435
+ $selector: T,
436
+ option: DOMUtilsDoubleClickOption
437
+ ) => void | Promise<void>;