@whitesev/domutils 2.0.2 → 2.0.4

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,
@@ -163,7 +164,7 @@ class ElementEvent extends ElementAnimate {
163
164
  * @param option
164
165
  */
165
166
  const getOption = function (args: IArguments, startIndex: number, option: DOMUtilsEventListenerOption) {
166
- const currentParam = args[startIndex];
167
+ const currentParam: boolean | DOMUtilsEventListenerOption = args[startIndex];
167
168
  if (typeof currentParam === "boolean") {
168
169
  option.capture = currentParam;
169
170
  if (typeof args[startIndex + 1] === "boolean") {
@@ -173,16 +174,21 @@ class ElementEvent extends ElementAnimate {
173
174
  option.passive = args[startIndex + 2];
174
175
  }
175
176
  } else if (
177
+ currentParam &&
176
178
  typeof currentParam === "object" &&
177
179
  ("capture" in currentParam ||
178
180
  "once" in currentParam ||
179
181
  "passive" in currentParam ||
180
- "isComposedPath" in currentParam)
182
+ "isComposedPath" in currentParam ||
183
+ "overrideTarget" in currentParam ||
184
+ "isPreventEvent" in currentParam)
181
185
  ) {
182
186
  option.capture = currentParam.capture;
183
187
  option.once = currentParam.once;
184
188
  option.passive = currentParam.passive;
185
189
  option.isComposedPath = currentParam.isComposedPath;
190
+ option.overrideTarget = currentParam.overrideTarget;
191
+ option.isPreventEvent = currentParam.isPreventEvent;
186
192
  }
187
193
  return option;
188
194
  };
@@ -231,6 +237,7 @@ class ElementEvent extends ElementAnimate {
231
237
  once: false,
232
238
  passive: false,
233
239
  isComposedPath: false,
240
+ overrideTarget: true,
234
241
  };
235
242
  if (typeof selector === "function") {
236
243
  // 这是为没有selector的情况
@@ -260,6 +267,9 @@ class ElementEvent extends ElementAnimate {
260
267
  * @param event
261
268
  */
262
269
  const handlerCallBack = function (event: Event) {
270
+ if (listenerOption.isPreventEvent) {
271
+ that.preventEvent(event);
272
+ }
263
273
  let call_this: Element | undefined = void 0;
264
274
  let call_event: Event | undefined = void 0;
265
275
  let call_$selector: HTMLElement | undefined = void 0;
@@ -294,8 +304,25 @@ class ElementEvent extends ElementAnimate {
294
304
  return false;
295
305
  });
296
306
  if (findValue) {
297
- // 这里尝试使用defineProperty修改event的target值
298
- // 不建议使用覆盖target,因为可能会有兼容性问题
307
+ if (listenerOption.overrideTarget) {
308
+ // 这里尝试使用defineProperty修改event的target
309
+ try {
310
+ const originTarget = event.target;
311
+ OriginPrototype.Object.defineProperty(event, "target", {
312
+ value: $target,
313
+ get() {
314
+ return $target;
315
+ },
316
+ });
317
+ OriginPrototype.Object.defineProperty(event, "originTarget", {
318
+ value: $target,
319
+ get() {
320
+ return originTarget;
321
+ },
322
+ });
323
+ // oxlint-disable-next-line no-empty
324
+ } catch {}
325
+ }
299
326
  execCallback = true;
300
327
  call_this = $target;
301
328
  call_event = event;
@@ -491,10 +518,15 @@ class ElementEvent extends ElementAnimate {
491
518
  * @param option
492
519
  */
493
520
  const getOption = function (args1: IArguments, startIndex: number, option: EventListenerOptions) {
494
- const currentParam: EventListenerOptions | boolean = args1[startIndex];
521
+ const currentParam: boolean | DOMUtilsEventListenerOption = args1[startIndex];
495
522
  if (typeof currentParam === "boolean") {
496
523
  option.capture = currentParam;
497
- } else if (typeof currentParam === "object" && currentParam != null && "capture" in currentParam) {
524
+ } else if (
525
+ currentParam &&
526
+ typeof currentParam === "object" &&
527
+ currentParam != null &&
528
+ "capture" in currentParam
529
+ ) {
498
530
  option.capture = currentParam.capture;
499
531
  }
500
532
  return option;
@@ -366,12 +366,26 @@ export declare interface DOMUtilsEventListenerOptionsAttribute {
366
366
  */
367
367
  export declare type DOMUtilsEventListenerOption = AddEventListenerOptions & {
368
368
  /**
369
- * 是否使用 event.composedPath() 来代替 event.target
369
+ * 前提:传入了`selector`参数
370
370
  *
371
- * 一般用于设置了selector参数
371
+ * 是否使用 event.composedPath() 来代替 event.target
372
372
  * @default false
373
373
  */
374
374
  isComposedPath?: boolean;
375
+ /**
376
+ * 前提:传入了`selector`参数
377
+ *
378
+ * 是否覆写`target`,仅传入了子元素的`selectorTarget`时才会生效
379
+ *
380
+ * 原始的`target`将命名为`originTarget`
381
+ * @default true
382
+ */
383
+ overrideTarget?: boolean;
384
+ /**
385
+ * 是否阻止事件传播,包括阻止默认行为
386
+ * @default false
387
+ */
388
+ isPreventEvent?: boolean;
375
389
  };
376
390
  export declare type DOMUtilsElementEventType =
377
391
  | HTMLElement