@whitesev/domutils 1.9.4 → 1.9.6

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.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- const version = "1.9.4";
1
+ const version = "1.9.6";
2
2
 
3
3
  class WindowApi {
4
4
  /** 默认的配置 */
@@ -1929,7 +1929,8 @@ class ElementEvent extends ElementAnimate {
1929
1929
  that.emit(element, "click", details, useDispatchToEmit);
1930
1930
  }
1931
1931
  else {
1932
- that.on(element, "click", null, handler);
1932
+ const listener = that.on(element, "click", null, handler);
1933
+ return listener;
1933
1934
  }
1934
1935
  }
1935
1936
  /**
@@ -1965,7 +1966,8 @@ class ElementEvent extends ElementAnimate {
1965
1966
  that.emit(element, "blur", details, useDispatchToEmit);
1966
1967
  }
1967
1968
  else {
1968
- that.on(element, "blur", null, handler);
1969
+ const listener = that.on(element, "blur", null, handler);
1970
+ return listener;
1969
1971
  }
1970
1972
  }
1971
1973
  /**
@@ -2001,7 +2003,8 @@ class ElementEvent extends ElementAnimate {
2001
2003
  that.emit(element, "focus", details, useDispatchToEmit);
2002
2004
  }
2003
2005
  else {
2004
- that.on(element, "focus", null, handler);
2006
+ const listener = that.on(element, "focus", null, handler);
2007
+ return listener;
2005
2008
  }
2006
2009
  }
2007
2010
  /**
@@ -2028,13 +2031,30 @@ class ElementEvent extends ElementAnimate {
2028
2031
  }
2029
2032
  if (CommonUtils.isNodeList(element)) {
2030
2033
  // 设置
2034
+ const listenerList = [];
2031
2035
  element.forEach(($ele) => {
2032
- that.onHover($ele, handler, option);
2036
+ const listener = that.onHover($ele, handler, option);
2037
+ listenerList.push(listener);
2033
2038
  });
2034
- return;
2039
+ return {
2040
+ off() {
2041
+ listenerList.forEach((listener) => {
2042
+ if (!listener) {
2043
+ return;
2044
+ }
2045
+ listener.off();
2046
+ });
2047
+ },
2048
+ };
2035
2049
  }
2036
- that.on(element, "mouseenter", null, handler, option);
2037
- that.on(element, "mouseleave", null, handler, option);
2050
+ const mouseenter_listener = that.on(element, "mouseenter", null, handler, option);
2051
+ const mouseleave_listener = that.on(element, "mouseleave", null, handler, option);
2052
+ return {
2053
+ off() {
2054
+ mouseenter_listener.off();
2055
+ mouseleave_listener.off();
2056
+ },
2057
+ };
2038
2058
  }
2039
2059
  /**
2040
2060
  * 监听动画结束
@@ -2117,12 +2137,23 @@ class ElementEvent extends ElementAnimate {
2117
2137
  }
2118
2138
  if (CommonUtils.isNodeList(element)) {
2119
2139
  // 设置
2140
+ const listenerList = [];
2120
2141
  element.forEach(($ele) => {
2121
- that.onKeyup($ele, handler, option);
2142
+ const listener = that.onKeyup($ele, handler, option);
2143
+ listenerList.push(listener);
2122
2144
  });
2123
- return;
2145
+ return {
2146
+ off() {
2147
+ listenerList.forEach((listener) => {
2148
+ if (!listener) {
2149
+ return;
2150
+ }
2151
+ listener.off();
2152
+ });
2153
+ },
2154
+ };
2124
2155
  }
2125
- that.on(element, "keyup", null, handler, option);
2156
+ return that.on(element, "keyup", null, handler, option);
2126
2157
  }
2127
2158
  /**
2128
2159
  * 当按键按下时触发事件
@@ -2149,12 +2180,23 @@ class ElementEvent extends ElementAnimate {
2149
2180
  }
2150
2181
  if (CommonUtils.isNodeList(element)) {
2151
2182
  // 设置
2183
+ const listenerList = [];
2152
2184
  element.forEach(($ele) => {
2153
- that.onKeydown($ele, handler, option);
2185
+ const listener = that.onKeydown($ele, handler, option);
2186
+ listenerList.push(listener);
2154
2187
  });
2155
- return;
2188
+ return {
2189
+ off() {
2190
+ listenerList.forEach((listener) => {
2191
+ if (!listener) {
2192
+ return;
2193
+ }
2194
+ listener.off();
2195
+ });
2196
+ },
2197
+ };
2156
2198
  }
2157
- that.on(element, "keydown", null, handler, option);
2199
+ return that.on(element, "keydown", null, handler, option);
2158
2200
  }
2159
2201
  /**
2160
2202
  * 当按键按下时触发事件
@@ -2181,12 +2223,23 @@ class ElementEvent extends ElementAnimate {
2181
2223
  }
2182
2224
  if (CommonUtils.isNodeList(element)) {
2183
2225
  // 设置
2226
+ const listenerList = [];
2184
2227
  element.forEach(($ele) => {
2185
- that.onKeypress($ele, handler, option);
2228
+ const listener = that.onKeypress($ele, handler, option);
2229
+ listenerList.push(listener);
2186
2230
  });
2187
- return;
2231
+ return {
2232
+ off() {
2233
+ listenerList.forEach((listener) => {
2234
+ if (!listener) {
2235
+ return;
2236
+ }
2237
+ listener.off();
2238
+ });
2239
+ },
2240
+ };
2188
2241
  }
2189
- that.on(element, "keypress", null, handler, option);
2242
+ return that.on(element, "keypress", null, handler, option);
2190
2243
  }
2191
2244
  /**
2192
2245
  * 监听某个元素键盘按键事件或window全局按键事件
@@ -2279,12 +2332,8 @@ class ElementEvent extends ElementAnimate {
2279
2332
  handler(keyName, keyValue, otherCodeList, event);
2280
2333
  }
2281
2334
  };
2282
- that.on(element, eventName, keyboardEventCallBack, options);
2283
- return {
2284
- removeListen: () => {
2285
- that.off(element, eventName, keyboardEventCallBack, options);
2286
- },
2287
- };
2335
+ const listener = that.on(element, eventName, keyboardEventCallBack, options);
2336
+ return listener;
2288
2337
  }
2289
2338
  /**
2290
2339
  * 监input、textarea的输入框值改变的事件(当输入法输入时,不会触发该监听)
@@ -2315,11 +2364,14 @@ class ElementEvent extends ElementAnimate {
2315
2364
  const compositionStartListener = this.on($el, "compositionstart", __composition_start_callback, option);
2316
2365
  const compositionEndListener = this.on($el, "compositionend", __composition_end_callback, option);
2317
2366
  return {
2318
- off: () => {
2367
+ off() {
2319
2368
  inputListener.off();
2320
2369
  compositionStartListener.off();
2321
2370
  compositionEndListener.off();
2322
2371
  },
2372
+ emit(details, useDispatchToEmit) {
2373
+ inputListener.emit(details, useDispatchToEmit);
2374
+ },
2323
2375
  };
2324
2376
  }
2325
2377
  onDoubleClick(...args) {
@@ -2353,41 +2405,26 @@ class ElementEvent extends ElementAnimate {
2353
2405
  else {
2354
2406
  throw new Error("args length error");
2355
2407
  }
2356
- let $click = null;
2408
+ let clickMap = new WeakMap();
2357
2409
  let isDoubleClick = false;
2358
2410
  let timer = void 0;
2359
- /** 是否是移动端点击 */
2360
- let isMobileTouch = false;
2361
2411
  /** 检测是否是单击的延迟时间 */
2362
2412
  const checkClickTime = 200;
2363
- const dblclick_handler = async (evt, option, $selector) => {
2364
- if (evt.type === "dblclick" && isMobileTouch) {
2365
- // 禁止在移动端触发dblclick事件
2366
- return;
2367
- }
2413
+ const dblclick_handler = (evt, option, $selector) => {
2368
2414
  if ($selector) {
2369
- await handler(evt, $selector, option);
2415
+ return handler(evt, $selector, option);
2370
2416
  }
2371
2417
  else {
2372
- await handler(evt, option);
2418
+ return handler(evt, option);
2373
2419
  }
2374
2420
  };
2375
- const dblClickListener = this.on($el, "dblclick", selector, (evt, $selector) => {
2376
- this.preventEvent(evt);
2377
- dblclick_handler(evt, {
2378
- isDoubleClick: true,
2379
- }, $selector);
2380
- }, options);
2381
2421
  const pointerUpListener = this.on($el, "pointerup", selector, (evt, $selector) => {
2382
- this.preventEvent(evt);
2383
- if (evt.pointerType === "touch") {
2384
- isMobileTouch = true;
2385
- }
2422
+ // this.preventEvent(evt);
2386
2423
  clearTimeout(timer);
2387
2424
  timer = void 0;
2388
- if (isDoubleClick && $click === $selector) {
2425
+ if (isDoubleClick && clickMap.has($selector)) {
2389
2426
  isDoubleClick = false;
2390
- $click = null;
2427
+ clickMap.delete($selector);
2391
2428
  /* 判定为双击 */
2392
2429
  dblclick_handler(evt, {
2393
2430
  isDoubleClick: true,
@@ -2402,14 +2439,14 @@ class ElementEvent extends ElementAnimate {
2402
2439
  }, $selector);
2403
2440
  }, checkClickTime);
2404
2441
  isDoubleClick = true;
2405
- $click = $selector;
2442
+ clickMap.set($selector, evt);
2406
2443
  }
2407
2444
  }, options);
2408
2445
  return {
2409
2446
  off() {
2410
- $click = null;
2411
- dblClickListener.off();
2412
2447
  pointerUpListener.off();
2448
+ // @ts-expect-error
2449
+ clickMap = null;
2413
2450
  },
2414
2451
  };
2415
2452
  }
@@ -2417,28 +2454,56 @@ class ElementEvent extends ElementAnimate {
2417
2454
  /**
2418
2455
  * 阻止事件的默认行为发生,并阻止事件传播
2419
2456
  */
2420
- const stopEvent = (event) => {
2421
- /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
2457
+ const stopEvent = (event, onlyStopPropagation) => {
2458
+ if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
2459
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2460
+ event?.stopPropagation();
2461
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2462
+ event?.stopImmediatePropagation();
2463
+ return;
2464
+ }
2465
+ // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字
2422
2466
  event?.preventDefault();
2423
- /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
2424
- event?.stopPropagation();
2425
- /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
2426
- event?.stopImmediatePropagation();
2427
2467
  return false;
2428
2468
  };
2429
- if (args.length === 1) {
2430
- /* 直接阻止事件 */
2431
- return stopEvent(args[0]);
2469
+ if (args[0] instanceof Event) {
2470
+ // 直接阻止事件
2471
+ const onlyStopPropagation = args[1];
2472
+ return stopEvent(args[0], onlyStopPropagation);
2432
2473
  }
2433
2474
  else {
2434
2475
  const $el = args[0];
2435
2476
  let eventNameList = args[1];
2436
- const capture = args[2];
2437
- /* 添加对应的事件来阻止触发 */
2477
+ let selector = void 0;
2478
+ let capture = false;
2479
+ let onlyStopPropagation = false;
2480
+ // 添加对应的事件来阻止触发
2438
2481
  if (typeof eventNameList === "string") {
2439
2482
  eventNameList = [eventNameList];
2440
2483
  }
2441
- this.on($el, eventNameList, stopEvent, { capture: Boolean(capture) });
2484
+ let option = void 0;
2485
+ if (typeof args[2] === "string" || Array.isArray(args[2])) {
2486
+ // selector
2487
+ selector = args[2];
2488
+ if (typeof args[3] === "object" && args[3] != null) {
2489
+ option = args[3];
2490
+ }
2491
+ }
2492
+ else if (typeof args[2] === "object" && args[2] != null && !Array.isArray(args[2])) {
2493
+ // option
2494
+ option = args[2];
2495
+ }
2496
+ else {
2497
+ throw new TypeError("Invalid argument");
2498
+ }
2499
+ if (option) {
2500
+ capture = Boolean(option.capture);
2501
+ onlyStopPropagation = Boolean(option.onlyStopPropagation);
2502
+ }
2503
+ const listener = this.on($el, eventNameList, selector, (evt) => {
2504
+ return stopEvent(evt, onlyStopPropagation);
2505
+ }, { capture: capture });
2506
+ return listener;
2442
2507
  }
2443
2508
  }
2444
2509
  }