@whitesev/domutils 1.7.5 → 1.8.1

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,11 +1,12 @@
1
1
  import { CommonUtils } from "./CommonUtils";
2
- import { GlobalData } from "./GlobalData";
3
2
  import { ElementAnimate } from "./ElementAnimate";
3
+ import { GlobalData } from "./GlobalData";
4
4
  import { OriginPrototype } from "./OriginPrototype";
5
5
  import type {
6
6
  DOMUtils_Event,
7
7
  DOMUtils_EventType,
8
8
  DOMUtilsAddEventListenerResult,
9
+ DOMUtilsDoubleClickOption,
9
10
  DOMUtilsElementEventType,
10
11
  DOMUtilsEventListenerOption,
11
12
  DOMUtilsEventListenerOptionsAttribute,
@@ -32,7 +33,7 @@ class ElementEvent extends ElementAnimate {
32
33
  * 绑定事件
33
34
  * @param element 需要绑定的元素|元素数组|window
34
35
  * @param eventType 需要监听的事件
35
- * @param callback 绑定事件触发的回调函数
36
+ * @param handler 绑定事件触发的回调函数
36
37
  * @param option
37
38
  * + capture 表示事件是否在捕获阶段触发。默认为false,即在冒泡阶段触发
38
39
  * + once 表示事件是否只触发一次。默认为false
@@ -49,14 +50,14 @@ class ElementEvent extends ElementAnimate {
49
50
  on<T extends DOMUtils_EventType>(
50
51
  element: DOMUtilsElementEventType,
51
52
  eventType: T | T[],
52
- callback: (this: HTMLElement, event: DOMUtils_Event[T]) => void,
53
+ handler: (this: HTMLElement, event: DOMUtils_Event[T]) => void,
53
54
  option?: DOMUtilsEventListenerOption | boolean
54
55
  ): DOMUtilsAddEventListenerResult;
55
56
  /**
56
57
  * 绑定事件
57
58
  * @param element 需要绑定的元素|元素数组|window
58
59
  * @param eventType 需要监听的事件
59
- * @param callback 绑定事件触发的回调函数
60
+ * @param handler 绑定事件触发的回调函数
60
61
  * @param option
61
62
  * + capture 表示事件是否在捕获阶段触发。默认为false,即在冒泡阶段触发
62
63
  * + once 表示事件是否只触发一次。默认为false
@@ -73,7 +74,7 @@ class ElementEvent extends ElementAnimate {
73
74
  on<T extends Event>(
74
75
  element: DOMUtilsElementEventType,
75
76
  eventType: string | string[],
76
- callback: (this: HTMLElement, event: T) => void,
77
+ handler: (this: HTMLElement, event: T) => void,
77
78
  option?: DOMUtilsEventListenerOption | boolean
78
79
  ): DOMUtilsAddEventListenerResult;
79
80
  /**
@@ -81,7 +82,7 @@ class ElementEvent extends ElementAnimate {
81
82
  * @param element 需要绑定的元素|元素数组|window
82
83
  * @param eventType 需要监听的事件
83
84
  * @param selector 子元素选择器
84
- * @param callback 绑定事件触发的回调函数
85
+ * @param handler 绑定事件触发的回调函数
85
86
  * @param option
86
87
  * + capture 表示事件是否在捕获阶段触发。默认为false,即在冒泡阶段触发
87
88
  * + once 表示事件是否只触发一次。默认为false
@@ -104,7 +105,7 @@ class ElementEvent extends ElementAnimate {
104
105
  element: DOMUtilsElementEventType,
105
106
  eventType: T | T[],
106
107
  selector: string | string[] | undefined | null,
107
- callback: (this: HTMLElement, event: DOMUtils_Event[T], selectorTarget: HTMLElement) => void,
108
+ handler: (this: HTMLElement, event: DOMUtils_Event[T], selectorTarget: HTMLElement) => void,
108
109
  option?: DOMUtilsEventListenerOption | boolean
109
110
  ): DOMUtilsAddEventListenerResult;
110
111
  /**
@@ -112,7 +113,7 @@ class ElementEvent extends ElementAnimate {
112
113
  * @param element 需要绑定的元素|元素数组|window
113
114
  * @param eventType 需要监听的事件
114
115
  * @param selector 子元素选择器
115
- * @param callback 绑定事件触发的回调函数
116
+ * @param handler 绑定事件触发的回调函数
116
117
  * @param option
117
118
  * + capture 表示事件是否在捕获阶段触发。默认为false,即在冒泡阶段触发
118
119
  * + once 表示事件是否只触发一次。默认为false
@@ -135,7 +136,7 @@ class ElementEvent extends ElementAnimate {
135
136
  element: DOMUtilsElementEventType,
136
137
  eventType: string | string[],
137
138
  selector: string | string[] | undefined | null,
138
- callback: (this: HTMLElement, event: T, selectorTarget: HTMLElement) => void,
139
+ handler: (this: HTMLElement, event: T, selectorTarget: HTMLElement) => void,
139
140
  option?: DOMUtilsEventListenerOption | boolean
140
141
  ): DOMUtilsAddEventListenerResult;
141
142
  on<T extends Event>(
@@ -193,7 +194,7 @@ class ElementEvent extends ElementAnimate {
193
194
  if (element == null) {
194
195
  return {
195
196
  off() {},
196
- trigger() {},
197
+ emit() {},
197
198
  };
198
199
  }
199
200
  let $elList: (Element | Document | Window)[] = [];
@@ -332,10 +333,10 @@ class ElementEvent extends ElementAnimate {
332
333
  /**
333
334
  * 主动触发事件
334
335
  * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
335
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
336
+ * @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
336
337
  */
337
- trigger: (details?: object, useDispatchToTriggerEvent?: boolean) => {
338
- that.trigger($elList, eventTypeList, details, useDispatchToTriggerEvent);
338
+ emit: (details?: object, useDispatchToEmit?: boolean) => {
339
+ that.emit($elList, eventTypeList, details, useDispatchToEmit);
339
340
  },
340
341
  };
341
342
  }
@@ -658,18 +659,18 @@ class ElementEvent extends ElementAnimate {
658
659
  * 等待文档加载完成后执行指定的函数
659
660
  * @param callback 需要执行的函数
660
661
  * @example
661
- * DOMUtils.ready(function(){
662
+ * DOMUtils.onReady(function(){
662
663
  * console.log("文档加载完毕")
663
664
  * });
664
665
  * > "文档加载完毕"
665
666
  * @example
666
- * await DOMUtils.ready();
667
+ * await DOMUtils.onReady();
667
668
  * console.log("文档加载完毕");
668
669
  * > "文档加载完毕"
669
670
  */
670
- ready(): Promise<void>;
671
- ready(callback: (...args: any[]) => any): void;
672
- ready(...args: any[]): void | Promise<void> {
671
+ onReady(): Promise<void>;
672
+ onReady(callback: (...args: any[]) => any): void;
673
+ onReady(...args: any[]): void | Promise<void> {
673
674
  const callback: ((...args: any[]) => any) | undefined = args[0];
674
675
  // 异步回调
675
676
  let resolve: ((...args: any[]) => any) | undefined = void 0;
@@ -762,60 +763,60 @@ class ElementEvent extends ElementAnimate {
762
763
  * @param element 需要触发的元素|元素数组|window
763
764
  * @param eventType 需要触发的事件
764
765
  * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
765
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
766
+ * @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
766
767
  * @example
767
768
  * // 触发元素a.xx的click事件
768
- * DOMUtils.trigger(document.querySelector("a.xx"),"click")
769
- * DOMUtils.trigger("a.xx","click")
769
+ * DOMUtils.emit(document.querySelector("a.xx"),"click")
770
+ * DOMUtils.emit("a.xx","click")
770
771
  * // 触发元素a.xx的click、tap、hover事件
771
- * DOMUtils.trigger(document.querySelector("a.xx"),"click tap hover")
772
- * DOMUtils.trigger("a.xx",["click","tap","hover"])
772
+ * DOMUtils.emit(document.querySelector("a.xx"),"click tap hover")
773
+ * DOMUtils.emit("a.xx",["click","tap","hover"])
773
774
  */
774
- trigger(
775
+ emit(
775
776
  element: DOMUtilsTargetElementType | Element | DocumentFragment | any[] | typeof globalThis | Window | Document,
776
777
  eventType: string | string[],
777
778
  details?: object,
778
- useDispatchToTriggerEvent?: boolean
779
+ useDispatchToEmit?: boolean
779
780
  ): void;
780
781
  /**
781
782
  * 主动触发事件
782
783
  * @param element 需要触发的元素|元素数组|window
783
784
  * @param eventType 需要触发的事件
784
785
  * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
785
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
786
+ * @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
786
787
  * @example
787
788
  * // 触发元素a.xx的click事件
788
- * DOMUtils.trigger(document.querySelector("a.xx"),"click")
789
- * DOMUtils.trigger("a.xx","click")
789
+ * DOMUtils.emit(document.querySelector("a.xx"),"click")
790
+ * DOMUtils.emit("a.xx","click")
790
791
  * // 触发元素a.xx的click、tap、hover事件
791
- * DOMUtils.trigger(document.querySelector("a.xx"),"click tap hover")
792
- * DOMUtils.trigger("a.xx",["click","tap","hover"])
792
+ * DOMUtils.emit(document.querySelector("a.xx"),"click tap hover")
793
+ * DOMUtils.emit("a.xx",["click","tap","hover"])
793
794
  */
794
- trigger(
795
+ emit(
795
796
  element: Element | string | NodeList | any[] | Window | Document,
796
797
  eventType: DOMUtils_EventType | DOMUtils_EventType[],
797
798
  details?: object,
798
- useDispatchToTriggerEvent?: boolean
799
+ useDispatchToEmit?: boolean
799
800
  ): void;
800
801
  /**
801
802
  * 主动触发事件
802
803
  * @param element 需要触发的元素|元素数组|window
803
804
  * @param eventType 需要触发的事件
804
805
  * @param details 赋予触发的Event的额外属性,如果是Event类型,那么将自动代替默认new的Event对象
805
- * @param useDispatchToTriggerEvent 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
806
+ * @param useDispatchToEmit 是否使用dispatchEvent来触发事件,默认true,如果为false,则直接调用callback,但是这种会让使用了selectorTarget的没有值
806
807
  * @example
807
808
  * // 触发元素a.xx的click事件
808
- * DOMUtils.trigger(document.querySelector("a.xx"),"click")
809
- * DOMUtils.trigger("a.xx","click")
809
+ * DOMUtils.emit(document.querySelector("a.xx"),"click")
810
+ * DOMUtils.emit("a.xx","click")
810
811
  * // 触发元素a.xx的click、tap、hover事件
811
- * DOMUtils.trigger(document.querySelector("a.xx"),"click tap hover")
812
- * DOMUtils.trigger("a.xx",["click","tap","hover"])
812
+ * DOMUtils.emit(document.querySelector("a.xx"),"click tap hover")
813
+ * DOMUtils.emit("a.xx",["click","tap","hover"])
813
814
  */
814
- trigger(
815
+ emit(
815
816
  element: Element | string | NodeList | any[] | Window | Document,
816
817
  eventType: DOMUtils_EventType | DOMUtils_EventType[] | string | string[],
817
818
  details?: object,
818
- useDispatchToTriggerEvent: boolean = true
819
+ useDispatchToEmit: boolean = true
819
820
  ) {
820
821
  const that = this;
821
822
  if (typeof element === "string") {
@@ -858,7 +859,7 @@ class ElementEvent extends ElementAnimate {
858
859
  });
859
860
  }
860
861
  }
861
- if (useDispatchToTriggerEvent == false && eventTypeItem in elementEvents) {
862
+ if (useDispatchToEmit == false && eventTypeItem in elementEvents) {
862
863
  // 直接调用监听的事件
863
864
  elementEvents[eventTypeItem].forEach((eventsItem: any) => {
864
865
  eventsItem.callback(event);
@@ -871,11 +872,11 @@ class ElementEvent extends ElementAnimate {
871
872
  }
872
873
 
873
874
  /**
874
- * 绑定或触发元素的click事件
875
+ * 监听或触发元素的click事件
875
876
  * @param element 目标元素
876
877
  * @param handler (可选)事件处理函数
877
878
  * @param details (可选)赋予触发的Event的额外属性
878
- * @param useDispatchToTriggerEvent (可选)是否使用dispatchEvent来触发事件,默认true
879
+ * @param useDispatchToEmit (可选)是否使用dispatchEvent来触发事件,默认true
879
880
  * @example
880
881
  * // 触发元素a.xx的click事件
881
882
  * DOMUtils.click(document.querySelector("a.xx"))
@@ -888,7 +889,7 @@ class ElementEvent extends ElementAnimate {
888
889
  element: DOMUtilsTargetElementType | Element | DocumentFragment | typeof globalThis | Window,
889
890
  handler?: (this: HTMLElement, event: DOMUtils_Event["click"]) => void,
890
891
  details?: any,
891
- useDispatchToTriggerEvent?: boolean
892
+ useDispatchToEmit?: boolean
892
893
  ) {
893
894
  const that = this;
894
895
  if (typeof element === "string") {
@@ -900,22 +901,22 @@ class ElementEvent extends ElementAnimate {
900
901
  if (CommonUtils.isNodeList(element)) {
901
902
  // 设置
902
903
  element.forEach(($ele) => {
903
- that.click($ele as HTMLElement, handler, details, useDispatchToTriggerEvent);
904
+ that.click($ele as HTMLElement, handler, details, useDispatchToEmit);
904
905
  });
905
906
  return;
906
907
  }
907
908
  if (handler == null) {
908
- that.trigger(element, "click", details, useDispatchToTriggerEvent);
909
+ that.emit(element, "click", details, useDispatchToEmit);
909
910
  } else {
910
911
  that.on(element, "click", null, handler);
911
912
  }
912
913
  }
913
914
  /**
914
- * 绑定或触发元素的blur事件
915
+ * 监听或触发元素的blur事件
915
916
  * @param element 目标元素
916
917
  * @param handler (可选)事件处理函数
917
918
  * @param details (可选)赋予触发的Event的额外属性
918
- * @param useDispatchToTriggerEvent (可选)是否使用dispatchEvent来触发事件,默认true
919
+ * @param useDispatchToEmit (可选)是否使用dispatchEvent来触发事件,默认true
919
920
  * @example
920
921
  * // 触发元素a.xx的blur事件
921
922
  * DOMUtils.blur(document.querySelector("a.xx"))
@@ -928,7 +929,7 @@ class ElementEvent extends ElementAnimate {
928
929
  element: DOMUtilsTargetElementType | Element | DocumentFragment | typeof globalThis | Window,
929
930
  handler?: (this: HTMLElement, event: DOMUtils_Event["blur"]) => void,
930
931
  details?: object,
931
- useDispatchToTriggerEvent?: boolean
932
+ useDispatchToEmit?: boolean
932
933
  ) {
933
934
  const that = this;
934
935
  if (typeof element === "string") {
@@ -940,22 +941,22 @@ class ElementEvent extends ElementAnimate {
940
941
  if (CommonUtils.isNodeList(element)) {
941
942
  // 设置
942
943
  element.forEach(($ele) => {
943
- that.focus($ele as HTMLElement, handler, details, useDispatchToTriggerEvent);
944
+ that.focus($ele as HTMLElement, handler, details, useDispatchToEmit);
944
945
  });
945
946
  return;
946
947
  }
947
948
  if (handler === null) {
948
- that.trigger(element, "blur", details, useDispatchToTriggerEvent);
949
+ that.emit(element, "blur", details, useDispatchToEmit);
949
950
  } else {
950
951
  that.on(element, "blur", null, handler as (event: Event) => void);
951
952
  }
952
953
  }
953
954
  /**
954
- * 绑定或触发元素的focus事件
955
+ * 监听或触发元素的focus事件
955
956
  * @param element 目标元素
956
957
  * @param handler (可选)事件处理函数
957
958
  * @param details (可选)赋予触发的Event的额外属性
958
- * @param useDispatchToTriggerEvent (可选)是否使用dispatchEvent来触发事件,默认true
959
+ * @param useDispatchToEmit (可选)是否使用dispatchEvent来触发事件,默认true
959
960
  * @example
960
961
  * // 触发元素a.xx的focus事件
961
962
  * DOMUtils.focus(document.querySelector("a.xx"))
@@ -968,7 +969,7 @@ class ElementEvent extends ElementAnimate {
968
969
  element: DOMUtilsTargetElementType | Element | DocumentFragment | typeof globalThis | Window,
969
970
  handler?: (this: HTMLElement, event: DOMUtils_Event["focus"]) => void,
970
971
  details?: object,
971
- useDispatchToTriggerEvent?: boolean
972
+ useDispatchToEmit?: boolean
972
973
  ) {
973
974
  const that = this;
974
975
  if (typeof element === "string") {
@@ -980,12 +981,12 @@ class ElementEvent extends ElementAnimate {
980
981
  if (CommonUtils.isNodeList(element)) {
981
982
  // 设置
982
983
  element.forEach(($ele) => {
983
- that.focus($ele as HTMLElement, handler, details, useDispatchToTriggerEvent);
984
+ that.focus($ele as HTMLElement, handler, details, useDispatchToEmit);
984
985
  });
985
986
  return;
986
987
  }
987
988
  if (handler == null) {
988
- that.trigger(element, "focus", details, useDispatchToTriggerEvent);
989
+ that.emit(element, "focus", details, useDispatchToEmit);
989
990
  } else {
990
991
  that.on(element, "focus", null, handler);
991
992
  }
@@ -997,14 +998,14 @@ class ElementEvent extends ElementAnimate {
997
998
  * @param option 配置
998
999
  * @example
999
1000
  * // 监听a.xx元素的移入或移出
1000
- * DOMUtils.hover(document.querySelector("a.xx"),()=>{
1001
+ * DOMUtils.onHover(document.querySelector("a.xx"),()=>{
1001
1002
  * console.log("移入/移除");
1002
1003
  * })
1003
- * DOMUtils.hover("a.xx",()=>{
1004
+ * DOMUtils.onHover("a.xx",()=>{
1004
1005
  * console.log("移入/移除");
1005
1006
  * })
1006
1007
  */
1007
- hover(
1008
+ onHover(
1008
1009
  element: DOMUtilsTargetElementType | Element | DocumentFragment | Node,
1009
1010
  handler: (this: HTMLElement, event: DOMUtils_Event["hover"]) => void,
1010
1011
  option?: boolean | DOMUtilsEventListenerOption
@@ -1019,7 +1020,7 @@ class ElementEvent extends ElementAnimate {
1019
1020
  if (CommonUtils.isNodeList(element)) {
1020
1021
  // 设置
1021
1022
  element.forEach(($ele) => {
1022
- that.hover($ele as HTMLElement, handler, option);
1023
+ that.onHover($ele as HTMLElement, handler, option);
1023
1024
  });
1024
1025
  return;
1025
1026
  }
@@ -1027,12 +1028,12 @@ class ElementEvent extends ElementAnimate {
1027
1028
  that.on(element, "mouseleave", null, handler, option);
1028
1029
  }
1029
1030
  /**
1030
- * 当动画结束时触发事件
1031
+ * 监听动画结束
1031
1032
  * @param element 监听的元素
1032
1033
  * @param handler 触发的回调函数
1033
1034
  * @param option 配置项,这里默认配置once为true
1034
1035
  */
1035
- animationend(
1036
+ onAnimationend(
1036
1037
  element: HTMLElement | string | Element | DocumentFragment,
1037
1038
  handler: (this: HTMLElement, event: DOMUtils_Event["animationend"]) => void,
1038
1039
  option?: boolean | DOMUtilsEventListenerOption
@@ -1059,12 +1060,12 @@ class ElementEvent extends ElementAnimate {
1059
1060
  }
1060
1061
  }
1061
1062
  /**
1062
- * 当过渡结束时触发事件
1063
+ * 监听过渡结束
1063
1064
  * @param element 监听的元素
1064
1065
  * @param handler 触发的回调函数
1065
1066
  * @param option 配置项,这里默认配置once为true
1066
1067
  */
1067
- transitionend(
1068
+ onTransitionend(
1068
1069
  element: HTMLElement | string | Element | DocumentFragment,
1069
1070
  handler: (this: HTMLElement, event: DOMUtils_Event["transitionend"]) => void,
1070
1071
  option?: boolean | DOMUtilsEventListenerOption
@@ -1105,7 +1106,7 @@ class ElementEvent extends ElementAnimate {
1105
1106
  * console.log("按键松开");
1106
1107
  * })
1107
1108
  */
1108
- keyup(
1109
+ onKeyup(
1109
1110
  element: DOMUtilsTargetElementType | Element | DocumentFragment | Window | Node | typeof globalThis,
1110
1111
  handler: (this: HTMLElement, event: DOMUtils_Event["keyup"]) => void,
1111
1112
  option?: boolean | DOMUtilsEventListenerOption
@@ -1120,7 +1121,7 @@ class ElementEvent extends ElementAnimate {
1120
1121
  if (CommonUtils.isNodeList(element)) {
1121
1122
  // 设置
1122
1123
  element.forEach(($ele) => {
1123
- that.keyup($ele as HTMLElement, handler, option);
1124
+ that.onKeyup($ele as HTMLElement, handler, option);
1124
1125
  });
1125
1126
  return;
1126
1127
  }
@@ -1141,7 +1142,7 @@ class ElementEvent extends ElementAnimate {
1141
1142
  * console.log("按键按下");
1142
1143
  * })
1143
1144
  */
1144
- keydown(
1145
+ onKeydown(
1145
1146
  element: DOMUtilsTargetElementType | Element | DocumentFragment | Window | Node | typeof globalThis,
1146
1147
  handler: (this: HTMLElement, event: DOMUtils_Event["keydown"]) => void,
1147
1148
  option?: boolean | DOMUtilsEventListenerOption
@@ -1156,7 +1157,7 @@ class ElementEvent extends ElementAnimate {
1156
1157
  if (CommonUtils.isNodeList(element)) {
1157
1158
  // 设置
1158
1159
  element.forEach(($ele) => {
1159
- that.keydown($ele as HTMLElement, handler, option);
1160
+ that.onKeydown($ele as HTMLElement, handler, option);
1160
1161
  });
1161
1162
  return;
1162
1163
  }
@@ -1177,7 +1178,7 @@ class ElementEvent extends ElementAnimate {
1177
1178
  * console.log("按键按下");
1178
1179
  * })
1179
1180
  */
1180
- keypress(
1181
+ onKeypress(
1181
1182
  element: DOMUtilsTargetElementType | Element | DocumentFragment | Window | Node | typeof globalThis,
1182
1183
  handler: (this: HTMLElement, event: DOMUtils_Event["keypress"]) => void,
1183
1184
  option?: boolean | DOMUtilsEventListenerOption
@@ -1192,79 +1193,79 @@ class ElementEvent extends ElementAnimate {
1192
1193
  if (CommonUtils.isNodeList(element)) {
1193
1194
  // 设置
1194
1195
  element.forEach(($ele) => {
1195
- that.keypress($ele as HTMLElement, handler, option);
1196
+ that.onKeypress($ele as HTMLElement, handler, option);
1196
1197
  });
1197
1198
  return;
1198
1199
  }
1199
1200
  that.on(element, "keypress", null, handler, option);
1200
1201
  }
1201
1202
  /**
1202
- * 监听某个元素键盘按键事件或window全局按键事件
1203
- * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
1204
- * @param element 需要监听的对象,可以是全局Window或者某个元素
1205
- * @param eventName 事件名,默认keypress
1206
- * @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
1203
+ * 监听某个元素键盘按键事件或window全局按键事件
1204
+ * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
1205
+ * @param element 需要监听的对象,可以是全局Window或者某个元素
1206
+ * @param eventName 事件名,默认keypress,keydown - > keypress - > keyup
1207
+ * @param handler 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
1207
1208
  * @param options 监听事件的配置
1208
- * @example
1209
- Utils.listenKeyboard(window,(keyName,keyValue,otherKey,event)=>{
1210
- if(keyName === "Enter"){
1211
- console.log("回车按键的值是:"+keyValue)
1212
- }
1213
- if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
1214
- console.log("Ctrl和回车键");
1215
- }
1216
- })
1217
- * @example
1218
- 字母和数字键的键码值(keyCode)
1219
- 按键 键码 按键 键码 按键 键码 按键 键码
1220
- A 65 J 74 S 83 1 49
1221
- B 66 K 75 T 84 2 50
1222
- C 67 L 76 U 85 3 51
1223
- D 68 M 77 V 86 4 52
1224
- E 69 N 78 W 87 5 53
1225
- F 70 O 79 X 88 6 54
1226
- G 71 P 80 Y 89 7 55
1227
- H 72 Q 81 Z 90 8 56
1228
- I 73 R 82 0 48 9 57
1229
-
1230
- 数字键盘上的键的键码值(keyCode)
1231
- 功能键键码值(keyCode)
1232
- 按键 键码 按键 键码 按键 键码 按键 键码
1233
- 0 96 8 104 F1 112 F7 118
1234
- 1 97 9 105 F2 113 F8 119
1235
- 2 98 * 106 F3 114 F9 120
1236
- 3 99 + 107 F4 115 F10 121
1237
- 4 100 Enter 108 F5 116 F11 122
1238
- 5 101 - 109 F6 117 F12 123
1239
- 6 102 . 110
1240
- 7 103 / 111
1241
-
1242
- 控制键键码值(keyCode)
1243
- 按键 键码 按键 键码 按键 键码 按键 键码
1244
- BackSpace 8 Esc 27 → 39 -_ 189
1245
- Tab 9 Spacebar 32 ↓ 40 .> 190
1246
- Clear 12 Page Up 33 Insert 45 /? 191
1247
- Enter 13 Page Down 34 Delete 46 `~ 192
1248
- Shift 16 End 35 Num Lock 144 [{ 219
1249
- Control 17 Home 36 ;: 186 \| 220
1250
- Alt 18 ← 37 =+ 187 ]} 221
1251
- Cape Lock 20 ↑ 38 ,< 188 '" 222
1252
-
1253
- 多媒体键码值(keyCode)
1254
- 按键 键码
1255
- 音量加 175
1256
- 音量减 174
1257
- 停止 179
1258
- 静音 173
1259
- 浏览器 172
1260
- 邮件 180
1261
- 搜索 170
1262
- 收藏 171
1263
- **/
1264
- listenKeyboard(
1209
+ * @example
1210
+ Utils.onKeyboard(window,(keyName,keyValue,otherKey,event)=>{
1211
+ if(keyName === "Enter"){
1212
+ console.log("回车按键的值是:"+keyValue)
1213
+ }
1214
+ if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
1215
+ console.log("Ctrl和回车键");
1216
+ }
1217
+ })
1218
+ * @example
1219
+ 字母和数字键的键码值(keyCode)
1220
+ 按键 键码 按键 键码 按键 键码 按键 键码
1221
+ A 65 J 74 S 83 1 49
1222
+ B 66 K 75 T 84 2 50
1223
+ C 67 L 76 U 85 3 51
1224
+ D 68 M 77 V 86 4 52
1225
+ E 69 N 78 W 87 5 53
1226
+ F 70 O 79 X 88 6 54
1227
+ G 71 P 80 Y 89 7 55
1228
+ H 72 Q 81 Z 90 8 56
1229
+ I 73 R 82 0 48 9 57
1230
+
1231
+ 数字键盘上的键的键码值(keyCode)
1232
+ 功能键键码值(keyCode)
1233
+ 按键 键码 按键 键码 按键 键码 按键 键码
1234
+ 0 96 8 104 F1 112 F7 118
1235
+ 1 97 9 105 F2 113 F8 119
1236
+ 2 98 * 106 F3 114 F9 120
1237
+ 3 99 + 107 F4 115 F10 121
1238
+ 4 100 Enter 108 F5 116 F11 122
1239
+ 5 101 - 109 F6 117 F12 123
1240
+ 6 102 . 110
1241
+ 7 103 / 111
1242
+
1243
+ 控制键键码值(keyCode)
1244
+ 按键 键码 按键 键码 按键 键码 按键 键码
1245
+ BackSpace 8 Esc 27 → 39 -_ 189
1246
+ Tab 9 Spacebar 32 ↓ 40 .> 190
1247
+ Clear 12 Page Up 33 Insert 45 /? 191
1248
+ Enter 13 Page Down 34 Delete 46 `~ 192
1249
+ Shift 16 End 35 Num Lock 144 [{ 219
1250
+ Control 17 Home 36 ;: 186 \| 220
1251
+ Alt 18 ← 37 =+ 187 ]} 221
1252
+ Cape Lock 20 ↑ 38 ,< 188 '" 222
1253
+
1254
+ 多媒体键码值(keyCode)
1255
+ 按键 键码
1256
+ 音量加 175
1257
+ 音量减 174
1258
+ 停止 179
1259
+ 静音 173
1260
+ 浏览器 172
1261
+ 邮件 180
1262
+ 搜索 170
1263
+ 收藏 171
1264
+ **/
1265
+ onKeyboard(
1265
1266
  element: DOMUtilsTargetElementType | Element | DocumentFragment | Window | Node | typeof globalThis,
1266
- eventName: "keyup" | "keypress" | "keydown" = "keypress",
1267
- callback: (keyName: string, keyValue: number, otherCodeList: string[], event: KeyboardEvent) => void,
1267
+ eventName: "keydown" | "keypress" | "keyup" = "keypress",
1268
+ handler: (keyName: string, keyValue: number, otherCodeList: string[], event: KeyboardEvent) => void,
1268
1269
  options?: DOMUtilsEventListenerOption | boolean
1269
1270
  ): {
1270
1271
  removeListen(): void;
@@ -1292,8 +1293,8 @@ class ElementEvent extends ElementAnimate {
1292
1293
  if (event.shiftKey) {
1293
1294
  otherCodeList.push("shift");
1294
1295
  }
1295
- if (typeof callback === "function") {
1296
- callback(keyName, keyValue, otherCodeList, event);
1296
+ if (typeof handler === "function") {
1297
+ handler(keyName, keyValue, otherCodeList, event);
1297
1298
  }
1298
1299
  };
1299
1300
  that.on(element, eventName, keyboardEventCallBack, options);
@@ -1303,6 +1304,168 @@ class ElementEvent extends ElementAnimate {
1303
1304
  },
1304
1305
  };
1305
1306
  }
1307
+ /**
1308
+ * 监input、textarea的输入框值改变的事件(当输入法输入时,不会触发该监听)
1309
+ * @param $el 输入框元素
1310
+ * @param handler 回调函数
1311
+ * @param option 配置
1312
+ */
1313
+ onInput(
1314
+ $el: HTMLInputElement | HTMLTextAreaElement,
1315
+ handler: (evt: InputEvent) => void | Promise<void>,
1316
+ option?: DOMUtilsEventListenerOption | boolean
1317
+ ) {
1318
+ /**
1319
+ * 是否正在输入中
1320
+ */
1321
+ let isComposite = false;
1322
+ const __callback = async (event: InputEvent) => {
1323
+ if (isComposite) return;
1324
+ await handler(event);
1325
+ };
1326
+ const __composition_start_callback = () => {
1327
+ isComposite = true;
1328
+ };
1329
+ const __composition_end_callback = () => {
1330
+ isComposite = false;
1331
+ this.emit($el, "input", {
1332
+ isComposite,
1333
+ });
1334
+ };
1335
+ const inputListener = this.on($el, "input", __callback, option);
1336
+ const compositionStartListener = this.on($el, "compositionstart", __composition_start_callback, option);
1337
+ const compositionEndListener = this.on($el, "compositionend", __composition_end_callback, option);
1338
+
1339
+ return {
1340
+ off: () => {
1341
+ inputListener.off();
1342
+ compositionStartListener.off();
1343
+ compositionEndListener.off();
1344
+ },
1345
+ };
1346
+ }
1347
+ /**
1348
+ * 双击监听,适配移动端
1349
+ * @param $el 监听的元素
1350
+ * @param handler 处理的回调函数
1351
+ * @param options 监听器的配置
1352
+ */
1353
+ onDoubleClick(
1354
+ $el: DOMUtilsTargetElementType,
1355
+ handler: (event: MouseEvent | PointerEvent | TouchEvent, option: DOMUtilsDoubleClickOption) => void | Promise<void>,
1356
+ options?: DOMUtilsEventListenerOption | boolean
1357
+ ): {
1358
+ off(): void;
1359
+ };
1360
+ /**
1361
+ * 双击监听,适配移动端
1362
+ * @param $el 监听的元素
1363
+ * @param selector 子元素选择器
1364
+ * @param handler 处理的回调函数
1365
+ * @param options 监听器的配置
1366
+ */
1367
+ onDoubleClick(
1368
+ $el: DOMUtilsTargetElementType,
1369
+ selector: string | string[] | undefined | null,
1370
+ handler: (event: MouseEvent | PointerEvent | TouchEvent, option: DOMUtilsDoubleClickOption) => void | Promise<void>,
1371
+ options?: DOMUtilsEventListenerOption | boolean
1372
+ ): {
1373
+ off(): void;
1374
+ };
1375
+ onDoubleClick(...args: any[]): {
1376
+ off(): void;
1377
+ } {
1378
+ const $el: DOMUtilsTargetElementType = args[0];
1379
+ let selector: string | string[] | undefined | null = void 0;
1380
+ let handler: (
1381
+ event: MouseEvent | PointerEvent | TouchEvent,
1382
+ option: DOMUtilsDoubleClickOption
1383
+ ) => void | Promise<void>;
1384
+ let options: DOMUtilsEventListenerOption | boolean | undefined;
1385
+ if (args.length === 2) {
1386
+ if (typeof args[1] === "function") {
1387
+ handler = args[1];
1388
+ } else {
1389
+ throw new TypeError("handler is not a function");
1390
+ }
1391
+ } else if (args.length === 3) {
1392
+ if (typeof args[1] === "function") {
1393
+ handler = args[1];
1394
+ options = args[2];
1395
+ } else {
1396
+ selector = args[1];
1397
+ handler = args[2];
1398
+ }
1399
+ } else if (args.length === 4) {
1400
+ selector = args[1];
1401
+ handler = args[2];
1402
+ options = args[3];
1403
+ } else {
1404
+ throw new Error("args length error");
1405
+ }
1406
+
1407
+ let $click: Element | null = null;
1408
+ let isDoubleClick = false;
1409
+ let timer: number | undefined = void 0;
1410
+ /** 是否是移动端点击 */
1411
+ let isMobileTouch = false;
1412
+
1413
+ const dblclick_handler = async (evt: MouseEvent | PointerEvent | TouchEvent, option: DOMUtilsDoubleClickOption) => {
1414
+ if (evt.type === "dblclick" && isMobileTouch) {
1415
+ // 禁止在移动端触发dblclick事件
1416
+ return;
1417
+ }
1418
+ await handler(evt, option);
1419
+ };
1420
+
1421
+ const dblClickListener = this.on(
1422
+ $el,
1423
+ "dblclick",
1424
+ (evt) => {
1425
+ dblclick_handler(evt, {
1426
+ isDoubleClick: true,
1427
+ });
1428
+ },
1429
+ options
1430
+ );
1431
+ const touchEndListener = this.on(
1432
+ $el,
1433
+ "touchend",
1434
+ selector,
1435
+ (evt, selectorTarget) => {
1436
+ isMobileTouch = true;
1437
+ CommonUtils.clearTimeout(timer);
1438
+ timer = void 0;
1439
+ if (isDoubleClick && $click === selectorTarget) {
1440
+ isDoubleClick = false;
1441
+ $click = null;
1442
+ /* 判定为双击 */
1443
+ dblclick_handler(evt, {
1444
+ isDoubleClick: true,
1445
+ });
1446
+ } else {
1447
+ timer = CommonUtils.setTimeout(() => {
1448
+ isDoubleClick = false;
1449
+ // 判断为单击
1450
+ dblclick_handler(evt, {
1451
+ isDoubleClick: false,
1452
+ });
1453
+ }, 200);
1454
+ isDoubleClick = true;
1455
+ $click = selectorTarget;
1456
+ }
1457
+ },
1458
+ options
1459
+ );
1460
+
1461
+ return {
1462
+ off() {
1463
+ $click = null;
1464
+ dblClickListener.off();
1465
+ touchEndListener.off();
1466
+ },
1467
+ };
1468
+ }
1306
1469
  /**
1307
1470
  * 阻止事件传递
1308
1471
  * @param event 要阻止传递的事件