@whitesev/domutils 1.2.0 → 1.3.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,7 +1,7 @@
1
1
  import { DOMUtilsCommonUtils } from "./DOMUtilsCommonUtils";
2
- import { DOMUtilsCore } from "./DOMUtilsCore";
3
2
  import { DOMUtilsData } from "./DOMUtilsData";
4
3
  import { OriginPrototype } from "./DOMUtilsOriginPrototype";
4
+ import { UtilsWindowApiOption, WindowApi } from "./WindowApi";
5
5
 
6
6
  export type DOMUtilsEventObject<T extends Node> = Event & {
7
7
  target: T;
@@ -242,7 +242,11 @@ export declare type DOMUtilsElementEventType =
242
242
  | ChildNode
243
243
  | Node;
244
244
 
245
- class DOMUtilsEvent {
245
+ export class DOMUtilsEvent {
246
+ windowApi: UtilsWindowApiOption;
247
+ constructor(windowApiOption?: UtilsWindowApiOption) {
248
+ this.windowApi = new WindowApi(windowApiOption);
249
+ }
246
250
  /**
247
251
  * 绑定事件
248
252
  * @param element 需要绑定的元素|元素数组|window
@@ -404,7 +408,7 @@ class DOMUtilsEvent {
404
408
  let DOMUtilsContext = this;
405
409
  let args = arguments;
406
410
  if (typeof element === "string") {
407
- element = DOMUtilsCore.document.querySelectorAll(element);
411
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
408
412
  }
409
413
  if (element == null) {
410
414
  return;
@@ -459,7 +463,7 @@ class DOMUtilsEvent {
459
463
  if (_selector_) {
460
464
  /* 存在自定义子元素选择器 */
461
465
  let totalParent = DOMUtilsCommonUtils.isWin(elementItem)
462
- ? DOMUtilsCore.document.documentElement
466
+ ? DOMUtilsContext.windowApi.document.documentElement
463
467
  : elementItem;
464
468
  if (target.matches(_selector_)) {
465
469
  /* 当前目标可以被selector所匹配到 */
@@ -658,10 +662,10 @@ class DOMUtilsEvent {
658
662
  }
659
663
  return option;
660
664
  }
661
-
665
+ let DOMUtilsContext = this;
662
666
  let args = arguments;
663
667
  if (typeof element === "string") {
664
- element = DOMUtilsCore.document.querySelectorAll(element);
668
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
665
669
  }
666
670
  if (element == null) {
667
671
  return;
@@ -768,8 +772,9 @@ class DOMUtilsEvent {
768
772
  element: DOMUtilsElementEventType,
769
773
  eventType?: DOMUtils_EventType | DOMUtils_EventType[] | string
770
774
  ) {
775
+ let DOMUtilsContext = this;
771
776
  if (typeof element === "string") {
772
- element = DOMUtilsCore.document.querySelectorAll(element);
777
+ element = DOMUtilsContext.windowApi.document.querySelectorAll(element);
773
778
  }
774
779
  if (element == null) {
775
780
  return;
@@ -827,15 +832,17 @@ class DOMUtilsEvent {
827
832
  if (typeof callback !== "function") {
828
833
  return;
829
834
  }
835
+ let DOMUtilsContext = this;
830
836
  /**
831
837
  * 检测文档是否加载完毕
832
838
  */
833
839
  function checkDOMReadyState() {
834
840
  try {
835
841
  if (
836
- DOMUtilsCore.document.readyState === "complete" ||
837
- (DOMUtilsCore.document.readyState !== "loading" &&
838
- !(DOMUtilsCore.document.documentElement as any).doScroll)
842
+ DOMUtilsContext.windowApi.document.readyState === "complete" ||
843
+ (DOMUtilsContext.windowApi.document.readyState !== "loading" &&
844
+ !(DOMUtilsContext.windowApi.document.documentElement as any)
845
+ .doScroll)
839
846
  ) {
840
847
  return true;
841
848
  } else {
@@ -855,12 +862,12 @@ class DOMUtilsEvent {
855
862
 
856
863
  let targetList = [
857
864
  {
858
- target: DOMUtilsCore.document,
865
+ target: DOMUtilsContext.windowApi.document,
859
866
  eventType: "DOMContentLoaded",
860
867
  callback: completed,
861
868
  },
862
869
  {
863
- target: DOMUtilsCore.window,
870
+ target: DOMUtilsContext.windowApi.window,
864
871
  eventType: "load",
865
872
  callback: completed,
866
873
  },
@@ -951,8 +958,11 @@ class DOMUtilsEvent {
951
958
  details?: object,
952
959
  useDispatchToTriggerEvent: boolean = true
953
960
  ) {
961
+ let DOMUtilsContext = this;
954
962
  if (typeof element === "string") {
955
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
963
+ element = DOMUtilsContext.windowApi.document.querySelector(
964
+ element
965
+ ) as HTMLElement;
956
966
  }
957
967
  if (element == null) {
958
968
  return;
@@ -1019,7 +1029,9 @@ class DOMUtilsEvent {
1019
1029
  ) {
1020
1030
  let DOMUtilsContext = this;
1021
1031
  if (typeof element === "string") {
1022
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
1032
+ element = DOMUtilsContext.windowApi.document.querySelector(
1033
+ element
1034
+ ) as HTMLElement;
1023
1035
  }
1024
1036
  if (element == null) {
1025
1037
  return;
@@ -1057,7 +1069,9 @@ class DOMUtilsEvent {
1057
1069
  ) {
1058
1070
  let DOMUtilsContext = this;
1059
1071
  if (typeof element === "string") {
1060
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
1072
+ element = DOMUtilsContext.windowApi.document.querySelector(
1073
+ element
1074
+ ) as HTMLElement;
1061
1075
  }
1062
1076
  if (element == null) {
1063
1077
  return;
@@ -1100,7 +1114,9 @@ class DOMUtilsEvent {
1100
1114
  ) {
1101
1115
  let DOMUtilsContext = this;
1102
1116
  if (typeof element === "string") {
1103
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
1117
+ element = DOMUtilsContext.windowApi.document.querySelector(
1118
+ element
1119
+ ) as HTMLElement;
1104
1120
  }
1105
1121
  if (element == null) {
1106
1122
  return;
@@ -1137,7 +1153,9 @@ class DOMUtilsEvent {
1137
1153
  ) {
1138
1154
  let DOMUtilsContext = this;
1139
1155
  if (typeof element === "string") {
1140
- element = DOMUtilsCore.document.querySelector(element) as HTMLElement;
1156
+ element = DOMUtilsContext.windowApi.document.querySelector(
1157
+ element
1158
+ ) as HTMLElement;
1141
1159
  }
1142
1160
  if (element == null) {
1143
1161
  return;
@@ -1170,7 +1188,9 @@ class DOMUtilsEvent {
1170
1188
  return;
1171
1189
  }
1172
1190
  if (typeof target === "string") {
1173
- target = DOMUtilsCore.document.querySelector(target) as HTMLElement;
1191
+ target = DOMUtilsContext.windowApi.document.querySelector(
1192
+ target
1193
+ ) as HTMLElement;
1174
1194
  }
1175
1195
  DOMUtilsContext.on(target, "keyup", null, handler, option);
1176
1196
  }
@@ -1199,7 +1219,9 @@ class DOMUtilsEvent {
1199
1219
  return;
1200
1220
  }
1201
1221
  if (typeof target === "string") {
1202
- target = DOMUtilsCore.document.querySelector(target) as HTMLElement;
1222
+ target = DOMUtilsContext.windowApi.document.querySelector(
1223
+ target
1224
+ ) as HTMLElement;
1203
1225
  }
1204
1226
  DOMUtilsContext.on(target, "keydown", null, handler, option);
1205
1227
  }
@@ -1228,10 +1250,114 @@ class DOMUtilsEvent {
1228
1250
  return;
1229
1251
  }
1230
1252
  if (typeof target === "string") {
1231
- target = DOMUtilsCore.document.querySelector(target) as HTMLElement;
1253
+ target = DOMUtilsContext.windowApi.document.querySelector(
1254
+ target
1255
+ ) as HTMLElement;
1232
1256
  }
1233
1257
  DOMUtilsContext.on(target, "keypress", null, handler, option);
1234
1258
  }
1235
- }
1236
1259
 
1237
- export { DOMUtilsEvent };
1260
+ /**
1261
+ * 监听某个元素键盘按键事件或window全局按键事件
1262
+ * 按下有值的键时触发,按下Ctrl\Alt\Shift\Meta是无值键。按下先触发keydown事件,再触发keypress事件。
1263
+ * @param target 需要监听的对象,可以是全局Window或者某个元素
1264
+ * @param eventName 事件名,默认keypress
1265
+ * @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
1266
+ * @param options 监听事件的配置
1267
+ * @example
1268
+ Utils.listenKeyboard(window,(keyName,keyValue,otherKey,event)=>{
1269
+ if(keyName === "Enter"){
1270
+ console.log("回车按键的值是:"+keyValue)
1271
+ }
1272
+ if(otherKey.indexOf("ctrl") && keyName === "Enter" ){
1273
+ console.log("Ctrl和回车键");
1274
+ }
1275
+ })
1276
+ * @example
1277
+ 字母和数字键的键码值(keyCode)
1278
+ 按键 键码 按键 键码 按键 键码 按键 键码
1279
+ A 65 J 74 S 83 1 49
1280
+ B 66 K 75 T 84 2 50
1281
+ C 67 L 76 U 85 3 51
1282
+ D 68 M 77 V 86 4 52
1283
+ E 69 N 78 W 87 5 53
1284
+ F 70 O 79 X 88 6 54
1285
+ G 71 P 80 Y 89 7 55
1286
+ H 72 Q 81 Z 90 8 56
1287
+ I 73 R 82 0 48 9 57
1288
+
1289
+ 数字键盘上的键的键码值(keyCode)
1290
+ 功能键键码值(keyCode)
1291
+ 按键 键码 按键 键码 按键 键码 按键 键码
1292
+ 0 96 8 104 F1 112 F7 118
1293
+ 1 97 9 105 F2 113 F8 119
1294
+ 2 98 * 106 F3 114 F9 120
1295
+ 3 99 + 107 F4 115 F10 121
1296
+ 4 100 Enter 108 F5 116 F11 122
1297
+ 5 101 - 109 F6 117 F12 123
1298
+ 6 102 . 110
1299
+ 7 103 / 111
1300
+
1301
+ 控制键键码值(keyCode)
1302
+ 按键 键码 按键 键码 按键 键码 按键 键码
1303
+ BackSpace 8 Esc 27 → 39 -_ 189
1304
+ Tab 9 Spacebar 32 ↓ 40 .> 190
1305
+ Clear 12 Page Up 33 Insert 45 /? 191
1306
+ Enter 13 Page Down 34 Delete 46 `~ 192
1307
+ Shift 16 End 35 Num Lock 144 [{ 219
1308
+ Control 17 Home 36 ;: 186 \| 220
1309
+ Alt 18 ← 37 =+ 187 ]} 221
1310
+ Cape Lock 20 ↑ 38 ,< 188 '" 222
1311
+
1312
+ 多媒体键码值(keyCode)
1313
+ 按键 键码
1314
+ 音量加 175
1315
+ 音量减 174
1316
+ 停止 179
1317
+ 静音 173
1318
+ 浏览器 172
1319
+ 邮件 180
1320
+ 搜索 170
1321
+ 收藏 171
1322
+ **/
1323
+ listenKeyboard(
1324
+ target: Window | Node | HTMLElement | typeof globalThis,
1325
+ eventName: "keyup" | "keypress" | "keydown" = "keypress",
1326
+ callback: (
1327
+ keyName: string,
1328
+ keyValue: string,
1329
+ otherCodeList: string[],
1330
+ event: KeyboardEvent
1331
+ ) => void,
1332
+ options?: AddEventListenerOptions | boolean
1333
+ ): {
1334
+ removeListen(): void;
1335
+ } {
1336
+ let keyboardEventCallBack = function (event: KeyboardEvent) {
1337
+ let keyName = event.key || event.code;
1338
+ let keyValue = event.charCode || event.keyCode || event.which;
1339
+ let otherCodeList = [];
1340
+ if (event.ctrlKey) {
1341
+ otherCodeList.push("ctrl");
1342
+ }
1343
+ if (event.altKey) {
1344
+ otherCodeList.push("alt");
1345
+ }
1346
+ if (event.metaKey) {
1347
+ otherCodeList.push("meta");
1348
+ }
1349
+ if (event.shiftKey) {
1350
+ otherCodeList.push("shift");
1351
+ }
1352
+ if (typeof callback === "function") {
1353
+ callback(keyName, keyValue.toString(), otherCodeList, event);
1354
+ }
1355
+ };
1356
+ this.on(target, eventName, keyboardEventCallBack as any, options);
1357
+ return {
1358
+ removeListen: () => {
1359
+ this.off(target, eventName, keyboardEventCallBack, options);
1360
+ },
1361
+ };
1362
+ }
1363
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 配置类型
3
+ */
4
+ export type UtilsWindowApiOption = {
5
+ document: Document;
6
+ window: Window & typeof globalThis;
7
+ globalThis: typeof globalThis | Window;
8
+ self: Window & typeof globalThis;
9
+ top: Window;
10
+ };
11
+
12
+ export class WindowApi {
13
+ /** 默认的配置 */
14
+ private defaultApi: UtilsWindowApiOption = {
15
+ document: document,
16
+ window: window,
17
+ globalThis: globalThis,
18
+ self: self,
19
+ top: top!,
20
+ };
21
+ /** 使用的配置 */
22
+ private api: UtilsWindowApiOption;
23
+ constructor(option?: UtilsWindowApiOption) {
24
+ if (!option) {
25
+ option = Object.assign({}, this.defaultApi);
26
+ }
27
+ this.api = Object.assign({}, option);
28
+ }
29
+ get document() {
30
+ return this.api.document;
31
+ }
32
+ get window() {
33
+ return this.api.window;
34
+ }
35
+ get globalThis() {
36
+ return this.api.globalThis;
37
+ }
38
+ get self() {
39
+ return this.api.self;
40
+ }
41
+ get top() {
42
+ return this.api.top;
43
+ }
44
+ }
@@ -1,15 +0,0 @@
1
- export interface DOMUtilsCoreOption {
2
- document: Document;
3
- window: Window | typeof globalThis;
4
- globalThis: Window | typeof globalThis;
5
- self: Window | typeof globalThis;
6
- top: Window | typeof globalThis;
7
- }
8
- declare const DOMUtilsCore: {
9
- init(option?: DOMUtilsCoreOption): void;
10
- readonly document: Document;
11
- readonly window: typeof globalThis | Window;
12
- readonly globalThis: typeof globalThis | Window;
13
- readonly self: typeof globalThis | Window;
14
- };
15
- export { DOMUtilsCore };
@@ -1,45 +0,0 @@
1
- export interface DOMUtilsCoreOption {
2
- document: Document;
3
- window: Window | typeof globalThis;
4
- globalThis: Window | typeof globalThis;
5
- self: Window | typeof globalThis;
6
- top: Window | typeof globalThis;
7
- }
8
-
9
- const DOMUtilsCoreDefaultEnv: DOMUtilsCoreOption = {
10
- document: document,
11
- window: window,
12
- globalThis: globalThis,
13
- self: self,
14
- top: top!,
15
- };
16
- const DOMUtilsCoreEnv: DOMUtilsCoreOption = {
17
- document: document,
18
- window: window,
19
- globalThis: globalThis,
20
- self: self,
21
- top: top!,
22
- };
23
-
24
- const DOMUtilsCore = {
25
- init(option?: DOMUtilsCoreOption) {
26
- if (!option) {
27
- option = Object.assign({}, DOMUtilsCoreDefaultEnv);
28
- }
29
- Object.assign(DOMUtilsCoreEnv, option);
30
- },
31
- get document() {
32
- return DOMUtilsCoreEnv.document;
33
- },
34
- get window() {
35
- return DOMUtilsCoreEnv.window;
36
- },
37
- get globalThis() {
38
- return DOMUtilsCoreEnv.globalThis;
39
- },
40
- get self() {
41
- return DOMUtilsCoreEnv.self;
42
- },
43
- };
44
-
45
- export { DOMUtilsCore };