@whitesev/utils 2.11.5 → 2.11.7

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.
@@ -325,6 +325,13 @@ declare class Utils {
325
325
  * > 456
326
326
  */
327
327
  getMaxValue(val: UtilsOwnObject<number>, handler: (key: any, value: any) => number): number;
328
+ /**
329
+ * 获取最大值
330
+ * @example
331
+ * Utils.getMaxValue([{1:123},{2:345},{3:456}],(value,index,value)=>{return parseInt(index)})
332
+ * > 2
333
+ */
334
+ getMaxValue<T>(val: T[], handler: (index: number, value: T, array: T[]) => number): number;
328
335
  /**
329
336
  * 获取页面中最大的z-index的元素信息
330
337
  * @param deviation 获取最大的z-index值的偏移,默认是1
@@ -337,10 +344,63 @@ declare class Utils {
337
344
  * zIndex: 1001
338
345
  * }
339
346
  **/
340
- getMaxZIndexNodeInfo(deviation?: number, target?: Element | ShadowRoot | Document, ignoreCallBack?: ($ele: Element | HTMLElement | ShadowRoot) => boolean | void): {
347
+ getMaxZIndexNodeInfo(deviation?: number, target?: Element | ShadowRoot | Document | null, ignoreCallBack?: ($ele: Element | HTMLElement | ShadowRoot) => boolean | void): {
341
348
  node: Element;
342
349
  zIndex: number;
343
350
  };
351
+ /**
352
+ * 获取页面的坐标中最大的z-index的元素信息
353
+ *
354
+ * 其中坐标为
355
+ *
356
+ * + 左上角(宽: 1/8,高: 1/8)
357
+ * + 右上角(宽: 7/8,高: 1/8)
358
+ * + 左下角(宽: 1/8,高: 7/8)
359
+ * + 右下角(宽: 7/8,高: 7/8)
360
+ * + 中间(宽: 1/2,高: 1/2)
361
+ * @param $el 仅检测目标元素最大的z-index(自动往上层找)
362
+ * @param deviation 将对所有获取到的z-index处理偏移量(增加或减少),默认为10
363
+ */
364
+ getMaxZIndexNodeInfoFromPoint($el?: IFunction<HTMLElement | HTMLElement[]>, deviation?: number): {
365
+ /** 处理了偏移量后的z-index值 */
366
+ zIndex: number;
367
+ /** 原始z-index值 */
368
+ originZIndex: number;
369
+ /** 拥有最大z-index的元素 */
370
+ node: HTMLElement | null;
371
+ /** 目标坐标元素 */
372
+ positionNode: HTMLElement;
373
+ /** x坐标 */
374
+ positionX: number;
375
+ /** y坐标 */
376
+ positionY: number;
377
+ }[];
378
+ /**
379
+ * 获取页面的坐标中最大的z-index的元素信息
380
+ *
381
+ * 其中坐标为
382
+ *
383
+ * + 左上角(宽: 1/8,高: 1/8)
384
+ * + 右上角(宽: 7/8,高: 1/8)
385
+ * + 左下角(宽: 1/8,高: 7/8)
386
+ * + 右下角(宽: 7/8,高: 7/8)
387
+ * + 中间(宽: 1/2,高: 1/2)
388
+ * @param deviation 将对所有获取到的z-index处理偏移量(增加或减少)
389
+ */
390
+ getMaxZIndexNodeInfoFromPoint(deviation: IFunction<number>): {
391
+ /** 处理了偏移量后的z-index值 */
392
+ zIndex: number;
393
+ /** 原始z-index值 */
394
+ originZIndex: number;
395
+ /** 拥有最大z-index的元素 */
396
+ node: HTMLElement | null;
397
+ /** 目标坐标元素 */
398
+ positionNode: HTMLElement;
399
+ /** x坐标 */
400
+ positionX: number;
401
+ /** y坐标 */
402
+ positionY: number;
403
+ }[];
344
404
  /**
345
405
  * 获取页面中最大的z-index
346
406
  * @param deviation 获取最大的z-index值的偏移,默认是1
@@ -5,3 +5,7 @@ declare var unsafeWindow: Window;
5
5
  declare var mozIndexedDB: IDBFactory | null;
6
6
  declare var webkitIndexedDB: IDBFactory | null;
7
7
  declare var msIndexedDB: IDBFactory | null;
8
+
9
+ declare type IPromise<T> = T | Promise<T>;
10
+
11
+ declare type IFunction<T> = T | (() => T);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.11.5",
4
+ "version": "2.11.7",
5
5
  "description": "一个常用的工具库",
6
6
  "keywords": [
7
7
  "ScriptCat",
package/src/Utils.ts CHANGED
@@ -735,26 +735,33 @@ class Utils {
735
735
  /**
736
736
  * 获取最大值
737
737
  * @example
738
- * Utils.getMaxValue([{1:123},{2:345},{3:456}],(index,value)=>{return parseInt(index)})
738
+ * Utils.getMaxValue([{1:123},{2:345},{3:456}],(value,index,value)=>{return parseInt(index)})
739
739
  * > 2
740
740
  */
741
+ getMaxValue<T>(val: T[], handler: (index: number, value: T, array: T[]) => number): number;
741
742
  getMaxValue(...args: any[]): number {
742
743
  const result = [...args];
743
- let newResult: number[] = [];
744
+ const newResult: number[] = [];
744
745
  if (result.length === 0) {
745
746
  return void 0 as any as number;
746
747
  }
747
748
  if (result.length > 1) {
748
749
  if (result.length === 2 && typeof result[0] === "object" && typeof result[1] === "function") {
749
750
  const data = result[0];
750
- const handleDataFunc = result[1];
751
+ const handleDataFunc: (keyName: string, value: any) => number = result[1];
751
752
  Object.keys(data).forEach((keyName) => {
752
- newResult = [...newResult, handleDataFunc(keyName, data[keyName])];
753
+ newResult.push(handleDataFunc(keyName, data[keyName]));
754
+ });
755
+ } else if (result.length === 2 && Array.isArray(result[0]) && typeof result[1] === "function") {
756
+ const data = result[0];
757
+ const handleDataFunc: (value: any, index: number, data: any[]) => number = result[1];
758
+ data.forEach((value, index, __data__) => {
759
+ newResult.push(handleDataFunc(value, index, __data__));
753
760
  });
754
761
  } else {
755
762
  result.forEach((item) => {
756
763
  if (!isNaN(parseFloat(item))) {
757
- newResult = [...newResult, parseFloat(item)];
764
+ newResult.push(parseFloat(item));
758
765
  }
759
766
  });
760
767
  }
@@ -762,7 +769,7 @@ class Utils {
762
769
  } else {
763
770
  result[0].forEach((item: any) => {
764
771
  if (!isNaN(parseFloat(item))) {
765
- newResult = [...newResult, parseFloat(item)];
772
+ newResult.push(parseFloat(item));
766
773
  }
767
774
  });
768
775
  return Math.max(...newResult);
@@ -782,7 +789,7 @@ class Utils {
782
789
  **/
783
790
  getMaxZIndexNodeInfo(
784
791
  deviation?: number,
785
- target?: Element | ShadowRoot | Document,
792
+ target?: Element | ShadowRoot | Document | null,
786
793
  ignoreCallBack?: ($ele: Element | HTMLElement | ShadowRoot) => boolean | void
787
794
  ): {
788
795
  node: Element;
@@ -808,45 +815,60 @@ class Utils {
808
815
  let maxZIndexNode: Element | null = null;
809
816
  /**
810
817
  * 元素是否可见
818
+ * @param $el
811
819
  * @param $css
812
820
  */
813
- function isVisibleNode($css: CSSStyleDeclaration): boolean {
814
- return $css.position !== "static" && $css.display !== "none";
821
+ function isVisibleNode($el: Element, $css: CSSStyleDeclaration): boolean {
822
+ let flag = true;
823
+ if (typeof $el.checkVisibility === "function") {
824
+ flag = $el.checkVisibility();
825
+ } else {
826
+ flag =
827
+ $css.position !== "static" && $css.display !== "none" && $css.visibility !== "hidden" && $css.opacity !== "0";
828
+ }
829
+ if (flag) {
830
+ // css样式上可见
831
+ // 再判断宽高
832
+ const rect = $el.getBoundingClientRect();
833
+ // 确保该元素的中心点在屏幕内
834
+ flag = rect.width > 0 && rect.height > 0 && rect.x > 0 && rect.y > 0;
835
+ }
836
+ return flag;
815
837
  }
816
838
  /**
817
839
  * 查询元素的z-index
818
840
  * 并比较值是否是已获取的最大值
819
- * @param $ele
841
+ * @param $el
820
842
  */
821
- function queryMaxZIndex($ele: Element) {
843
+ function queryMaxZIndex($el: Element) {
822
844
  if (typeof ignoreCallBack === "function") {
823
- const ignoreResult = ignoreCallBack($ele);
845
+ const ignoreResult = ignoreCallBack($el);
824
846
  if (typeof ignoreResult === "boolean" && !ignoreResult) {
825
847
  return;
826
848
  }
827
849
  }
828
850
  /** 元素的样式 */
829
- const nodeStyle = that.windowApi.window.getComputedStyle($ele);
851
+ const nodeStyle = that.windowApi.window.getComputedStyle($el);
830
852
  /* 不对position为static和display为none的元素进行获取它们的z-index */
831
- if (isVisibleNode(nodeStyle)) {
853
+ if (isVisibleNode($el, nodeStyle)) {
832
854
  const nodeZIndex = parseInt(nodeStyle.zIndex);
833
855
  if (!isNaN(nodeZIndex)) {
834
856
  if (nodeZIndex > zIndex) {
835
857
  // 赋值到全局
836
858
  zIndex = nodeZIndex;
837
- maxZIndexNode = $ele;
859
+ maxZIndexNode = $el;
838
860
  }
839
861
  }
840
862
  // 判断shadowRoot
841
- if ($ele.shadowRoot != null && $ele instanceof ShadowRoot) {
842
- $ele.shadowRoot.querySelectorAll("*").forEach(($shadowEle) => {
863
+ if ($el.shadowRoot != null && $el instanceof ShadowRoot) {
864
+ $el.shadowRoot.querySelectorAll("*").forEach(($shadowEle) => {
843
865
  queryMaxZIndex($shadowEle);
844
866
  });
845
867
  }
846
868
  }
847
869
  }
848
- target.querySelectorAll("*").forEach(($ele) => {
849
- queryMaxZIndex($ele);
870
+ target.querySelectorAll("*").forEach(($elItem) => {
871
+ queryMaxZIndex($elItem);
850
872
  });
851
873
  zIndex += deviation;
852
874
  if (zIndex >= maxZIndexCompare) {
@@ -858,6 +880,190 @@ class Utils {
858
880
  zIndex: zIndex,
859
881
  };
860
882
  }
883
+ /**
884
+ * 获取页面的坐标中最大的z-index的元素信息
885
+ *
886
+ * 其中坐标为
887
+ *
888
+ * + 左上角(宽: 1/8,高: 1/8)
889
+ * + 右上角(宽: 7/8,高: 1/8)
890
+ * + 左下角(宽: 1/8,高: 7/8)
891
+ * + 右下角(宽: 7/8,高: 7/8)
892
+ * + 中间(宽: 1/2,高: 1/2)
893
+ * @param $el 仅检测目标元素最大的z-index(自动往上层找)
894
+ * @param deviation 将对所有获取到的z-index处理偏移量(增加或减少),默认为10
895
+ */
896
+ getMaxZIndexNodeInfoFromPoint(
897
+ $el?: IFunction<HTMLElement | HTMLElement[]>,
898
+ deviation?: number
899
+ ): {
900
+ /** 处理了偏移量后的z-index值 */
901
+ zIndex: number;
902
+ /** 原始z-index值 */
903
+ originZIndex: number;
904
+ /** 拥有最大z-index的元素 */
905
+ node: HTMLElement | null;
906
+ /** 目标坐标元素 */
907
+ positionNode: HTMLElement;
908
+ /** x坐标 */
909
+ positionX: number;
910
+ /** y坐标 */
911
+ positionY: number;
912
+ }[];
913
+ /**
914
+ * 获取页面的坐标中最大的z-index的元素信息
915
+ *
916
+ * 其中坐标为
917
+ *
918
+ * + 左上角(宽: 1/8,高: 1/8)
919
+ * + 右上角(宽: 7/8,高: 1/8)
920
+ * + 左下角(宽: 1/8,高: 7/8)
921
+ * + 右下角(宽: 7/8,高: 7/8)
922
+ * + 中间(宽: 1/2,高: 1/2)
923
+ * @param deviation 将对所有获取到的z-index处理偏移量(增加或减少)
924
+ */
925
+ getMaxZIndexNodeInfoFromPoint(deviation: IFunction<number>): {
926
+ /** 处理了偏移量后的z-index值 */
927
+ zIndex: number;
928
+ /** 原始z-index值 */
929
+ originZIndex: number;
930
+ /** 拥有最大z-index的元素 */
931
+ node: HTMLElement | null;
932
+ /** 目标坐标元素 */
933
+ positionNode: HTMLElement;
934
+ /** x坐标 */
935
+ positionX: number;
936
+ /** y坐标 */
937
+ positionY: number;
938
+ }[];
939
+ getMaxZIndexNodeInfoFromPoint(
940
+ $el?: IFunction<HTMLElement | HTMLElement[] | number>,
941
+ deviation?: number
942
+ ): {
943
+ /** 处理了偏移量后的z-index值 */
944
+ zIndex: number;
945
+ /** 原始z-index值 */
946
+ originZIndex: number;
947
+ /** 拥有最大z-index的元素 */
948
+ node: HTMLElement | null;
949
+ /** 目标坐标元素 */
950
+ positionNode: HTMLElement;
951
+ /** x坐标 */
952
+ positionX: number;
953
+ /** y坐标 */
954
+ positionY: number;
955
+ }[] {
956
+ if (typeof $el === "function") {
957
+ $el = $el();
958
+ }
959
+ if (typeof $el === "number") {
960
+ deviation = $el;
961
+ $el = void 0;
962
+ }
963
+ if (typeof deviation !== "number" || Number.isNaN(deviation)) {
964
+ deviation = 10;
965
+ }
966
+ const leftTop = {
967
+ x: globalThis.innerWidth * (1 / 8),
968
+ y: globalThis.innerHeight * (1 / 8),
969
+ };
970
+ const leftBottom = {
971
+ x: globalThis.innerWidth * (1 / 8),
972
+ y: globalThis.innerHeight * (7 / 8),
973
+ };
974
+ const rightTop = {
975
+ x: globalThis.innerWidth * (7 / 8),
976
+ y: globalThis.innerHeight * (1 / 8),
977
+ };
978
+ const rightBottom = {
979
+ x: globalThis.innerWidth * (7 / 8),
980
+ y: globalThis.innerHeight * (7 / 8),
981
+ };
982
+ const center = {
983
+ x: globalThis.innerWidth / 2,
984
+ y: globalThis.innerHeight / 2,
985
+ };
986
+ const delayHandlerElementPostionList: ({ x: number; y: number } | HTMLElement)[] = [
987
+ leftTop,
988
+ leftBottom,
989
+ rightTop,
990
+ rightBottom,
991
+ center,
992
+ ];
993
+ if ($el) {
994
+ delayHandlerElementPostionList.length = 0;
995
+ if (Array.isArray($el)) {
996
+ delayHandlerElementPostionList.push(...$el);
997
+ } else {
998
+ delayHandlerElementPostionList.push($el);
999
+ }
1000
+ }
1001
+ const positionInfoList = delayHandlerElementPostionList
1002
+ .map((position) => {
1003
+ let positionNode: Element | null;
1004
+ let positionX: number;
1005
+ let positionY: number;
1006
+ if (position instanceof HTMLElement) {
1007
+ positionNode = position;
1008
+ const nodeRect = position.getBoundingClientRect();
1009
+ positionX = nodeRect.x + nodeRect.width / 2;
1010
+ positionY = nodeRect.y + nodeRect.height / 2;
1011
+ } else {
1012
+ positionNode = document.elementFromPoint(position.x, position.y);
1013
+ positionX = position.x;
1014
+ positionY = position.y;
1015
+ }
1016
+ const shadowRoot = positionNode?.shadowRoot;
1017
+ if (shadowRoot) {
1018
+ positionNode = shadowRoot.elementFromPoint(positionX, positionY);
1019
+ }
1020
+ if (positionNode instanceof HTMLStyleElement) return;
1021
+ if (positionNode instanceof HTMLScriptElement) return;
1022
+ if (positionNode instanceof HTMLMetaElement) return;
1023
+ if (positionNode instanceof HTMLHeadElement) return;
1024
+ if (!(positionNode instanceof HTMLElement)) return;
1025
+ let parent: HTMLElement | null = positionNode;
1026
+ let zIndex = 0;
1027
+ let maxZIndexNode: HTMLElement | null = null;
1028
+ while (parent) {
1029
+ const nodeStyle = globalThis.getComputedStyle(parent);
1030
+ const nodeZIndex = parseInt(nodeStyle.zIndex);
1031
+ if (nodeStyle.position !== "static" && !isNaN(nodeZIndex)) {
1032
+ if (nodeZIndex > zIndex) {
1033
+ zIndex = nodeZIndex;
1034
+ maxZIndexNode = parent;
1035
+ }
1036
+ }
1037
+ parent = parent.parentElement;
1038
+ }
1039
+ return {
1040
+ /** 处理了偏移量后的z-index值 */
1041
+ zIndex: zIndex + deviation,
1042
+ /** 原始z-index值 */
1043
+ originZIndex: zIndex,
1044
+ /** 拥有最大z-index的元素 */
1045
+ node: maxZIndexNode,
1046
+ /** 目标坐标元素 */
1047
+ positionNode: positionNode,
1048
+ /** x坐标 */
1049
+ positionX: positionX,
1050
+ /** y坐标 */
1051
+ positionY: positionY,
1052
+ };
1053
+ })
1054
+ .filter((it) => it != null);
1055
+ // 降序排序
1056
+ positionInfoList.sort((a, b) => {
1057
+ if (a.zIndex < b.zIndex) {
1058
+ return 1;
1059
+ } else if (a.zIndex > b.zIndex) {
1060
+ return -1;
1061
+ } else {
1062
+ return 0;
1063
+ }
1064
+ });
1065
+ return positionInfoList;
1066
+ }
861
1067
  /**
862
1068
  * 获取页面中最大的z-index
863
1069
  * @param deviation 获取最大的z-index值的偏移,默认是1
@@ -1542,7 +1748,6 @@ class Utils {
1542
1748
  "is",
1543
1749
  "jquery",
1544
1750
  "keydown",
1545
- "keypress",
1546
1751
  "keyup",
1547
1752
  "last",
1548
1753
  "load",
@@ -5,3 +5,7 @@ declare var unsafeWindow: Window;
5
5
  declare var mozIndexedDB: IDBFactory | null;
6
6
  declare var webkitIndexedDB: IDBFactory | null;
7
7
  declare var msIndexedDB: IDBFactory | null;
8
+
9
+ declare type IPromise<T> = T | Promise<T>;
10
+
11
+ declare type IFunction<T> = T | (() => T);