@whitesev/domutils 2.0.1 → 2.0.3

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.
@@ -1,6 +1,7 @@
1
1
  import { CommonUtils } from "./CommonUtils";
2
2
  import { ElementAnimate } from "./ElementAnimate";
3
3
  import { GlobalData } from "./GlobalData";
4
+ import { OriginPrototype } from "./OriginPrototype";
4
5
  import type {
5
6
  DOMUtils_Event,
6
7
  DOMUtils_EventType,
@@ -177,12 +178,14 @@ class ElementEvent extends ElementAnimate {
177
178
  ("capture" in currentParam ||
178
179
  "once" in currentParam ||
179
180
  "passive" in currentParam ||
180
- "isComposedPath" in currentParam)
181
+ "isComposedPath" in currentParam ||
182
+ "overrideTarget" in currentParam)
181
183
  ) {
182
184
  option.capture = currentParam.capture;
183
185
  option.once = currentParam.once;
184
186
  option.passive = currentParam.passive;
185
187
  option.isComposedPath = currentParam.isComposedPath;
188
+ option.overrideTarget = currentParam.overrideTarget;
186
189
  }
187
190
  return option;
188
191
  };
@@ -231,6 +234,7 @@ class ElementEvent extends ElementAnimate {
231
234
  once: false,
232
235
  passive: false,
233
236
  isComposedPath: false,
237
+ overrideTarget: true,
234
238
  };
235
239
  if (typeof selector === "function") {
236
240
  // 这是为没有selector的情况
@@ -294,8 +298,25 @@ class ElementEvent extends ElementAnimate {
294
298
  return false;
295
299
  });
296
300
  if (findValue) {
297
- // 这里尝试使用defineProperty修改event的target值
298
- // 不建议使用覆盖target,因为可能会有兼容性问题
301
+ if (listenerOption.overrideTarget) {
302
+ // 这里尝试使用defineProperty修改event的target
303
+ try {
304
+ const originTarget = event.target;
305
+ OriginPrototype.Object.defineProperty(event, "target", {
306
+ value: $target,
307
+ get() {
308
+ return $target;
309
+ },
310
+ });
311
+ OriginPrototype.Object.defineProperty(event, "originTarget", {
312
+ value: $target,
313
+ get() {
314
+ return originTarget;
315
+ },
316
+ });
317
+ // oxlint-disable-next-line no-empty
318
+ } catch {}
319
+ }
299
320
  execCallback = true;
300
321
  call_this = $target;
301
322
  call_event = event;
@@ -1456,6 +1477,12 @@ class ElementEvent extends ElementAnimate {
1456
1477
  options?: DOMUtilsDoubleEventEventListenerOption | boolean
1457
1478
  ): {
1458
1479
  off(): void;
1480
+ /**
1481
+ * 主动触发事件
1482
+ * @param event 事件
1483
+ * @param option 配置,如果不传入配置,则默认`isDouble`固定为false
1484
+ */
1485
+ emit(event: Event, option?: DOMUtilsDoubleEventOption): void;
1459
1486
  };
1460
1487
  /**
1461
1488
  * 监听事件单/双次触发
@@ -1475,9 +1502,21 @@ class ElementEvent extends ElementAnimate {
1475
1502
  options?: DOMUtilsDoubleEventEventListenerOption | boolean
1476
1503
  ): {
1477
1504
  off(): void;
1505
+ /**
1506
+ * 主动触发事件
1507
+ * @param event 事件
1508
+ * @param option 配置,如果不传入配置,则默认`isDouble`固定为false
1509
+ */
1510
+ emit(event: Event, option?: DOMUtilsDoubleEventOption): void;
1478
1511
  };
1479
1512
  onOneOrDouble(...args: any[]): {
1480
1513
  off(): void;
1514
+ /**
1515
+ * 主动触发事件
1516
+ * @param event 事件
1517
+ * @param option 配置,如果不传入配置,则默认`isDouble`固定为false
1518
+ */
1519
+ emit(event: Event, option?: DOMUtilsDoubleEventOption): void;
1481
1520
  } {
1482
1521
  const $el: DOMUtilsElementEventType = args[0];
1483
1522
  let selector: string | string[] | undefined | null = void 0;
@@ -1571,6 +1610,14 @@ class ElementEvent extends ElementAnimate {
1571
1610
  // @ts-expect-error
1572
1611
  eventNodeMap = null;
1573
1612
  },
1613
+ emit(event, option) {
1614
+ callback(
1615
+ event,
1616
+ option || {
1617
+ isDouble: false,
1618
+ }
1619
+ );
1620
+ },
1574
1621
  };
1575
1622
  }
1576
1623
  /**
@@ -372,6 +372,13 @@ export declare type DOMUtilsEventListenerOption = AddEventListenerOptions & {
372
372
  * @default false
373
373
  */
374
374
  isComposedPath?: boolean;
375
+ /**
376
+ * 是否覆写`target`,仅传入了子元素的`selectorTarget`时才会生效
377
+ *
378
+ * 原始的`target`将命名为`originTarget`
379
+ * @default true
380
+ */
381
+ overrideTarget?: boolean;
375
382
  };
376
383
  export declare type DOMUtilsElementEventType =
377
384
  | HTMLElement