@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.umd.js CHANGED
@@ -4,7 +4,7 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.DOMUtils = factory());
5
5
  })(this, (function () { 'use strict';
6
6
 
7
- const version = "1.9.4";
7
+ const version = "1.9.6";
8
8
 
9
9
  class WindowApi {
10
10
  /** 默认的配置 */
@@ -1935,7 +1935,8 @@
1935
1935
  that.emit(element, "click", details, useDispatchToEmit);
1936
1936
  }
1937
1937
  else {
1938
- that.on(element, "click", null, handler);
1938
+ const listener = that.on(element, "click", null, handler);
1939
+ return listener;
1939
1940
  }
1940
1941
  }
1941
1942
  /**
@@ -1971,7 +1972,8 @@
1971
1972
  that.emit(element, "blur", details, useDispatchToEmit);
1972
1973
  }
1973
1974
  else {
1974
- that.on(element, "blur", null, handler);
1975
+ const listener = that.on(element, "blur", null, handler);
1976
+ return listener;
1975
1977
  }
1976
1978
  }
1977
1979
  /**
@@ -2007,7 +2009,8 @@
2007
2009
  that.emit(element, "focus", details, useDispatchToEmit);
2008
2010
  }
2009
2011
  else {
2010
- that.on(element, "focus", null, handler);
2012
+ const listener = that.on(element, "focus", null, handler);
2013
+ return listener;
2011
2014
  }
2012
2015
  }
2013
2016
  /**
@@ -2034,13 +2037,30 @@
2034
2037
  }
2035
2038
  if (CommonUtils.isNodeList(element)) {
2036
2039
  // 设置
2040
+ const listenerList = [];
2037
2041
  element.forEach(($ele) => {
2038
- that.onHover($ele, handler, option);
2042
+ const listener = that.onHover($ele, handler, option);
2043
+ listenerList.push(listener);
2039
2044
  });
2040
- return;
2045
+ return {
2046
+ off() {
2047
+ listenerList.forEach((listener) => {
2048
+ if (!listener) {
2049
+ return;
2050
+ }
2051
+ listener.off();
2052
+ });
2053
+ },
2054
+ };
2041
2055
  }
2042
- that.on(element, "mouseenter", null, handler, option);
2043
- that.on(element, "mouseleave", null, handler, option);
2056
+ const mouseenter_listener = that.on(element, "mouseenter", null, handler, option);
2057
+ const mouseleave_listener = that.on(element, "mouseleave", null, handler, option);
2058
+ return {
2059
+ off() {
2060
+ mouseenter_listener.off();
2061
+ mouseleave_listener.off();
2062
+ },
2063
+ };
2044
2064
  }
2045
2065
  /**
2046
2066
  * 监听动画结束
@@ -2123,12 +2143,23 @@
2123
2143
  }
2124
2144
  if (CommonUtils.isNodeList(element)) {
2125
2145
  // 设置
2146
+ const listenerList = [];
2126
2147
  element.forEach(($ele) => {
2127
- that.onKeyup($ele, handler, option);
2148
+ const listener = that.onKeyup($ele, handler, option);
2149
+ listenerList.push(listener);
2128
2150
  });
2129
- return;
2151
+ return {
2152
+ off() {
2153
+ listenerList.forEach((listener) => {
2154
+ if (!listener) {
2155
+ return;
2156
+ }
2157
+ listener.off();
2158
+ });
2159
+ },
2160
+ };
2130
2161
  }
2131
- that.on(element, "keyup", null, handler, option);
2162
+ return that.on(element, "keyup", null, handler, option);
2132
2163
  }
2133
2164
  /**
2134
2165
  * 当按键按下时触发事件
@@ -2155,12 +2186,23 @@
2155
2186
  }
2156
2187
  if (CommonUtils.isNodeList(element)) {
2157
2188
  // 设置
2189
+ const listenerList = [];
2158
2190
  element.forEach(($ele) => {
2159
- that.onKeydown($ele, handler, option);
2191
+ const listener = that.onKeydown($ele, handler, option);
2192
+ listenerList.push(listener);
2160
2193
  });
2161
- return;
2194
+ return {
2195
+ off() {
2196
+ listenerList.forEach((listener) => {
2197
+ if (!listener) {
2198
+ return;
2199
+ }
2200
+ listener.off();
2201
+ });
2202
+ },
2203
+ };
2162
2204
  }
2163
- that.on(element, "keydown", null, handler, option);
2205
+ return that.on(element, "keydown", null, handler, option);
2164
2206
  }
2165
2207
  /**
2166
2208
  * 当按键按下时触发事件
@@ -2187,12 +2229,23 @@
2187
2229
  }
2188
2230
  if (CommonUtils.isNodeList(element)) {
2189
2231
  // 设置
2232
+ const listenerList = [];
2190
2233
  element.forEach(($ele) => {
2191
- that.onKeypress($ele, handler, option);
2234
+ const listener = that.onKeypress($ele, handler, option);
2235
+ listenerList.push(listener);
2192
2236
  });
2193
- return;
2237
+ return {
2238
+ off() {
2239
+ listenerList.forEach((listener) => {
2240
+ if (!listener) {
2241
+ return;
2242
+ }
2243
+ listener.off();
2244
+ });
2245
+ },
2246
+ };
2194
2247
  }
2195
- that.on(element, "keypress", null, handler, option);
2248
+ return that.on(element, "keypress", null, handler, option);
2196
2249
  }
2197
2250
  /**
2198
2251
  * 监听某个元素键盘按键事件或window全局按键事件
@@ -2285,12 +2338,8 @@
2285
2338
  handler(keyName, keyValue, otherCodeList, event);
2286
2339
  }
2287
2340
  };
2288
- that.on(element, eventName, keyboardEventCallBack, options);
2289
- return {
2290
- removeListen: () => {
2291
- that.off(element, eventName, keyboardEventCallBack, options);
2292
- },
2293
- };
2341
+ const listener = that.on(element, eventName, keyboardEventCallBack, options);
2342
+ return listener;
2294
2343
  }
2295
2344
  /**
2296
2345
  * 监input、textarea的输入框值改变的事件(当输入法输入时,不会触发该监听)
@@ -2321,11 +2370,14 @@
2321
2370
  const compositionStartListener = this.on($el, "compositionstart", __composition_start_callback, option);
2322
2371
  const compositionEndListener = this.on($el, "compositionend", __composition_end_callback, option);
2323
2372
  return {
2324
- off: () => {
2373
+ off() {
2325
2374
  inputListener.off();
2326
2375
  compositionStartListener.off();
2327
2376
  compositionEndListener.off();
2328
2377
  },
2378
+ emit(details, useDispatchToEmit) {
2379
+ inputListener.emit(details, useDispatchToEmit);
2380
+ },
2329
2381
  };
2330
2382
  }
2331
2383
  onDoubleClick(...args) {
@@ -2359,41 +2411,26 @@
2359
2411
  else {
2360
2412
  throw new Error("args length error");
2361
2413
  }
2362
- let $click = null;
2414
+ let clickMap = new WeakMap();
2363
2415
  let isDoubleClick = false;
2364
2416
  let timer = void 0;
2365
- /** 是否是移动端点击 */
2366
- let isMobileTouch = false;
2367
2417
  /** 检测是否是单击的延迟时间 */
2368
2418
  const checkClickTime = 200;
2369
- const dblclick_handler = async (evt, option, $selector) => {
2370
- if (evt.type === "dblclick" && isMobileTouch) {
2371
- // 禁止在移动端触发dblclick事件
2372
- return;
2373
- }
2419
+ const dblclick_handler = (evt, option, $selector) => {
2374
2420
  if ($selector) {
2375
- await handler(evt, $selector, option);
2421
+ return handler(evt, $selector, option);
2376
2422
  }
2377
2423
  else {
2378
- await handler(evt, option);
2424
+ return handler(evt, option);
2379
2425
  }
2380
2426
  };
2381
- const dblClickListener = this.on($el, "dblclick", selector, (evt, $selector) => {
2382
- this.preventEvent(evt);
2383
- dblclick_handler(evt, {
2384
- isDoubleClick: true,
2385
- }, $selector);
2386
- }, options);
2387
2427
  const pointerUpListener = this.on($el, "pointerup", selector, (evt, $selector) => {
2388
- this.preventEvent(evt);
2389
- if (evt.pointerType === "touch") {
2390
- isMobileTouch = true;
2391
- }
2428
+ // this.preventEvent(evt);
2392
2429
  clearTimeout(timer);
2393
2430
  timer = void 0;
2394
- if (isDoubleClick && $click === $selector) {
2431
+ if (isDoubleClick && clickMap.has($selector)) {
2395
2432
  isDoubleClick = false;
2396
- $click = null;
2433
+ clickMap.delete($selector);
2397
2434
  /* 判定为双击 */
2398
2435
  dblclick_handler(evt, {
2399
2436
  isDoubleClick: true,
@@ -2408,14 +2445,14 @@
2408
2445
  }, $selector);
2409
2446
  }, checkClickTime);
2410
2447
  isDoubleClick = true;
2411
- $click = $selector;
2448
+ clickMap.set($selector, evt);
2412
2449
  }
2413
2450
  }, options);
2414
2451
  return {
2415
2452
  off() {
2416
- $click = null;
2417
- dblClickListener.off();
2418
2453
  pointerUpListener.off();
2454
+ // @ts-expect-error
2455
+ clickMap = null;
2419
2456
  },
2420
2457
  };
2421
2458
  }
@@ -2423,28 +2460,56 @@
2423
2460
  /**
2424
2461
  * 阻止事件的默认行为发生,并阻止事件传播
2425
2462
  */
2426
- const stopEvent = (event) => {
2427
- /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
2463
+ const stopEvent = (event, onlyStopPropagation) => {
2464
+ if (typeof onlyStopPropagation === "boolean" && onlyStopPropagation) {
2465
+ // 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素
2466
+ event?.stopPropagation();
2467
+ // 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发
2468
+ event?.stopImmediatePropagation();
2469
+ return;
2470
+ }
2471
+ // 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL,或者在输入框内输入文字
2428
2472
  event?.preventDefault();
2429
- /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
2430
- event?.stopPropagation();
2431
- /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
2432
- event?.stopImmediatePropagation();
2433
2473
  return false;
2434
2474
  };
2435
- if (args.length === 1) {
2436
- /* 直接阻止事件 */
2437
- return stopEvent(args[0]);
2475
+ if (args[0] instanceof Event) {
2476
+ // 直接阻止事件
2477
+ const onlyStopPropagation = args[1];
2478
+ return stopEvent(args[0], onlyStopPropagation);
2438
2479
  }
2439
2480
  else {
2440
2481
  const $el = args[0];
2441
2482
  let eventNameList = args[1];
2442
- const capture = args[2];
2443
- /* 添加对应的事件来阻止触发 */
2483
+ let selector = void 0;
2484
+ let capture = false;
2485
+ let onlyStopPropagation = false;
2486
+ // 添加对应的事件来阻止触发
2444
2487
  if (typeof eventNameList === "string") {
2445
2488
  eventNameList = [eventNameList];
2446
2489
  }
2447
- this.on($el, eventNameList, stopEvent, { capture: Boolean(capture) });
2490
+ let option = void 0;
2491
+ if (typeof args[2] === "string" || Array.isArray(args[2])) {
2492
+ // selector
2493
+ selector = args[2];
2494
+ if (typeof args[3] === "object" && args[3] != null) {
2495
+ option = args[3];
2496
+ }
2497
+ }
2498
+ else if (typeof args[2] === "object" && args[2] != null && !Array.isArray(args[2])) {
2499
+ // option
2500
+ option = args[2];
2501
+ }
2502
+ else {
2503
+ throw new TypeError("Invalid argument");
2504
+ }
2505
+ if (option) {
2506
+ capture = Boolean(option.capture);
2507
+ onlyStopPropagation = Boolean(option.onlyStopPropagation);
2508
+ }
2509
+ const listener = this.on($el, eventNameList, selector, (evt) => {
2510
+ return stopEvent(evt, onlyStopPropagation);
2511
+ }, { capture: capture });
2512
+ return listener;
2448
2513
  }
2449
2514
  }
2450
2515
  }