@whitesev/utils 2.11.6 → 2.11.8

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.
@@ -351,17 +351,26 @@ declare class Utils {
351
351
  /**
352
352
  * 获取页面的坐标中最大的z-index的元素信息
353
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)
354
+ * 矩阵坐标计算
361
355
  * @param $el 仅检测目标元素最大的z-index(自动往上层找)
362
356
  * @param deviation 将对所有获取到的z-index处理偏移量(增加或减少),默认为10
357
+ * @example
358
+ * Utils.getMaxZIndexNodeInfoFromPoint(document.querySelector("a"));
359
+ * @example
360
+ * Utils.getMaxZIndexNodeInfoFromPoint(document.querySelector("a"), 20);
361
+ * @example
362
+ * Utils.getMaxZIndexNodeInfoFromPoint([document.querySelector("a"), document.querySelector("div")]);
363
+ * @example
364
+ * Utils.getMaxZIndexNodeInfoFromPoint({x: 500, y: 500});
365
+ * @example
366
+ * Utils.getMaxZIndexNodeInfoFromPoint({x: 500, y: 500}, 20);
367
+ * @example
368
+ * Utils.getMaxZIndexNodeInfoFromPoint(() => {x: 500, y: 500}, 20);
363
369
  */
364
- getMaxZIndexNodeInfoFromPoint($el?: HTMLElement | HTMLElement[], deviation?: number): {
370
+ getMaxZIndexNodeInfoFromPoint($el?: IFunction<IArray<HTMLElement> | IArray<{
371
+ x: number;
372
+ y: number;
373
+ }>>, deviation?: number): {
365
374
  /** 处理了偏移量后的z-index值 */
366
375
  zIndex: number;
367
376
  /** 原始z-index值 */
@@ -378,16 +387,12 @@ declare class Utils {
378
387
  /**
379
388
  * 获取页面的坐标中最大的z-index的元素信息
380
389
  *
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)
390
+ * 矩阵坐标计算
388
391
  * @param deviation 将对所有获取到的z-index处理偏移量(增加或减少)
392
+ * @example
393
+ * Utils.getMaxZIndexNodeInfoFromPoint(20);
389
394
  */
390
- getMaxZIndexNodeInfoFromPoint(deviation: number): {
395
+ getMaxZIndexNodeInfoFromPoint(deviation: IFunction<number>): {
391
396
  /** 处理了偏移量后的z-index值 */
392
397
  zIndex: number;
393
398
  /** 原始z-index值 */
@@ -5,3 +5,9 @@ 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);
12
+
13
+ declare type IArray<T> = T | Array<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.6",
4
+ "version": "2.11.8",
5
5
  "description": "一个常用的工具库",
6
6
  "keywords": [
7
7
  "ScriptCat",
package/src/Utils.ts CHANGED
@@ -817,30 +817,33 @@ class Utils {
817
817
  * 元素是否可见
818
818
  * @param $el
819
819
  * @param $css
820
+ * @returns
821
+ * + true 可见
822
+ * + false 不可见
820
823
  */
821
- function isVisibleNode($el: Element, $css: CSSStyleDeclaration): boolean {
824
+ const isVisibleNode = function ($css: CSSStyleDeclaration, $el: Element): boolean {
822
825
  let flag = true;
823
- if (typeof $el.checkVisibility === "function") {
824
- flag = $el.checkVisibility();
826
+ if (!($el instanceof HTMLElement)) {
827
+ flag = false;
825
828
  } 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;
829
+ if (typeof $el.checkVisibility === "function") {
830
+ flag = $el.checkVisibility();
831
+ } else {
832
+ flag =
833
+ $css.position !== "static" &&
834
+ $css.display !== "none" &&
835
+ $css.visibility !== "hidden" &&
836
+ $css.opacity !== "0";
837
+ }
835
838
  }
836
839
  return flag;
837
- }
840
+ };
838
841
  /**
839
842
  * 查询元素的z-index
840
843
  * 并比较值是否是已获取的最大值
841
844
  * @param $el
842
845
  */
843
- function queryMaxZIndex($el: Element) {
846
+ const queryMaxZIndex = function ($el: Element) {
844
847
  if (typeof ignoreCallBack === "function") {
845
848
  const ignoreResult = ignoreCallBack($el);
846
849
  if (typeof ignoreResult === "boolean" && !ignoreResult) {
@@ -848,9 +851,9 @@ class Utils {
848
851
  }
849
852
  }
850
853
  /** 元素的样式 */
851
- const nodeStyle = that.windowApi.window.getComputedStyle($el);
854
+ const nodeStyle = that.windowApi.globalThis.getComputedStyle($el);
852
855
  /* 不对position为static和display为none的元素进行获取它们的z-index */
853
- if (isVisibleNode($el, nodeStyle)) {
856
+ if (isVisibleNode(nodeStyle, $el)) {
854
857
  const nodeZIndex = parseInt(nodeStyle.zIndex);
855
858
  if (!isNaN(nodeZIndex)) {
856
859
  if (nodeZIndex > zIndex) {
@@ -866,7 +869,7 @@ class Utils {
866
869
  });
867
870
  }
868
871
  }
869
- }
872
+ };
870
873
  target.querySelectorAll("*").forEach(($elItem) => {
871
874
  queryMaxZIndex($elItem);
872
875
  });
@@ -883,18 +886,24 @@ class Utils {
883
886
  /**
884
887
  * 获取页面的坐标中最大的z-index的元素信息
885
888
  *
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)
889
+ * 矩阵坐标计算
893
890
  * @param $el 仅检测目标元素最大的z-index(自动往上层找)
894
891
  * @param deviation 将对所有获取到的z-index处理偏移量(增加或减少),默认为10
892
+ * @example
893
+ * Utils.getMaxZIndexNodeInfoFromPoint(document.querySelector("a"));
894
+ * @example
895
+ * Utils.getMaxZIndexNodeInfoFromPoint(document.querySelector("a"), 20);
896
+ * @example
897
+ * Utils.getMaxZIndexNodeInfoFromPoint([document.querySelector("a"), document.querySelector("div")]);
898
+ * @example
899
+ * Utils.getMaxZIndexNodeInfoFromPoint({x: 500, y: 500});
900
+ * @example
901
+ * Utils.getMaxZIndexNodeInfoFromPoint({x: 500, y: 500}, 20);
902
+ * @example
903
+ * Utils.getMaxZIndexNodeInfoFromPoint(() => {x: 500, y: 500}, 20);
895
904
  */
896
905
  getMaxZIndexNodeInfoFromPoint(
897
- $el?: HTMLElement | HTMLElement[],
906
+ $el?: IFunction<IArray<HTMLElement> | IArray<{ x: number; y: number }>>,
898
907
  deviation?: number
899
908
  ): {
900
909
  /** 处理了偏移量后的z-index值 */
@@ -913,16 +922,12 @@ class Utils {
913
922
  /**
914
923
  * 获取页面的坐标中最大的z-index的元素信息
915
924
  *
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)
925
+ * 矩阵坐标计算
923
926
  * @param deviation 将对所有获取到的z-index处理偏移量(增加或减少)
927
+ * @example
928
+ * Utils.getMaxZIndexNodeInfoFromPoint(20);
924
929
  */
925
- getMaxZIndexNodeInfoFromPoint(deviation: number): {
930
+ getMaxZIndexNodeInfoFromPoint(deviation: IFunction<number>): {
926
931
  /** 处理了偏移量后的z-index值 */
927
932
  zIndex: number;
928
933
  /** 原始z-index值 */
@@ -937,7 +942,7 @@ class Utils {
937
942
  positionY: number;
938
943
  }[];
939
944
  getMaxZIndexNodeInfoFromPoint(
940
- $el?: HTMLElement | HTMLElement[] | number,
945
+ $el?: IFunction<IArray<HTMLElement> | number | IArray<{ x: number; y: number }>>,
941
946
  deviation?: number
942
947
  ): {
943
948
  /** 处理了偏移量后的z-index值 */
@@ -953,6 +958,9 @@ class Utils {
953
958
  /** y坐标 */
954
959
  positionY: number;
955
960
  }[] {
961
+ if (typeof $el === "function") {
962
+ $el = $el();
963
+ }
956
964
  if (typeof $el === "number") {
957
965
  deviation = $el;
958
966
  $el = void 0;
@@ -960,33 +968,34 @@ class Utils {
960
968
  if (typeof deviation !== "number" || Number.isNaN(deviation)) {
961
969
  deviation = 10;
962
970
  }
963
- const leftTop = {
964
- x: globalThis.innerWidth * (1 / 8),
965
- y: globalThis.innerHeight * (1 / 8),
966
- };
967
- const leftBottom = {
968
- x: globalThis.innerWidth * (1 / 8),
969
- y: globalThis.innerHeight * (7 / 8),
970
- };
971
- const rightTop = {
972
- x: globalThis.innerWidth * (7 / 8),
973
- y: globalThis.innerHeight * (1 / 8),
974
- };
975
- const rightBottom = {
976
- x: globalThis.innerWidth * (7 / 8),
977
- y: globalThis.innerHeight * (7 / 8),
978
- };
979
- const center = {
980
- x: globalThis.innerWidth / 2,
981
- y: globalThis.innerHeight / 2,
982
- };
983
- const delayHandlerElementPostionList: ({ x: number; y: number } | HTMLElement)[] = [
984
- leftTop,
985
- leftBottom,
986
- rightTop,
987
- rightBottom,
988
- center,
989
- ];
971
+ /** 坐标偏移 */
972
+ const positionDistance = 10;
973
+ const defaultCalcPostion: {
974
+ x: number;
975
+ y: number;
976
+ }[] = [];
977
+ const maxPostionX = globalThis.innerWidth;
978
+ const maxPostionY = globalThis.innerHeight;
979
+ const gridXCount = 8;
980
+ const gridYCount = 8;
981
+ for (let i = 0; i < gridXCount; i++) {
982
+ for (let j = 0; j < gridYCount; j++) {
983
+ const positionX = globalThis.innerWidth * (i / gridXCount) + positionDistance;
984
+ const positionY = globalThis.innerHeight * (j / gridYCount) + positionDistance;
985
+ const position: (typeof defaultCalcPostion)[0] = {
986
+ x: positionX,
987
+ y: positionY,
988
+ };
989
+ if (position.x > maxPostionX) {
990
+ position.x = maxPostionX;
991
+ }
992
+ if (position.y > maxPostionY) {
993
+ position.y = maxPostionY;
994
+ }
995
+ defaultCalcPostion.push(position);
996
+ }
997
+ }
998
+ const delayHandlerElementPostionList: ({ x: number; y: number } | HTMLElement)[] = defaultCalcPostion;
990
999
  if ($el) {
991
1000
  delayHandlerElementPostionList.length = 0;
992
1001
  if (Array.isArray($el)) {
@@ -997,55 +1006,77 @@ class Utils {
997
1006
  }
998
1007
  const positionInfoList = delayHandlerElementPostionList
999
1008
  .map((position) => {
1000
- let positionNode: Element | null;
1009
+ let $position: Element | null;
1001
1010
  let positionX: number;
1002
1011
  let positionY: number;
1003
1012
  if (position instanceof HTMLElement) {
1004
- positionNode = position;
1013
+ $position = position;
1005
1014
  const nodeRect = position.getBoundingClientRect();
1006
1015
  positionX = nodeRect.x + nodeRect.width / 2;
1007
1016
  positionY = nodeRect.y + nodeRect.height / 2;
1008
1017
  } else {
1009
- positionNode = document.elementFromPoint(position.x, position.y);
1018
+ $position = document.elementFromPoint(position.x, position.y);
1010
1019
  positionX = position.x;
1011
1020
  positionY = position.y;
1012
1021
  }
1013
- const shadowRoot = positionNode?.shadowRoot;
1022
+ const shadowRoot = $position?.shadowRoot;
1014
1023
  if (shadowRoot) {
1015
- positionNode = shadowRoot.elementFromPoint(positionX, positionY);
1024
+ // 处理ShadowRoot
1025
+ $position = shadowRoot.elementFromPoint(positionX, positionY);
1016
1026
  }
1017
- if (positionNode instanceof HTMLStyleElement) return;
1018
- if (positionNode instanceof HTMLScriptElement) return;
1019
- if (positionNode instanceof HTMLMetaElement) return;
1020
- if (positionNode instanceof HTMLHeadElement) return;
1021
- if (!(positionNode instanceof HTMLElement)) return;
1022
- let parent: HTMLElement | null = positionNode;
1027
+ if (!($position instanceof HTMLElement)) return;
1028
+ let $parent: HTMLElement | null = $position;
1023
1029
  let zIndex = 0;
1024
- let maxZIndexNode: HTMLElement | null = null;
1025
- while (parent) {
1026
- const nodeStyle = globalThis.getComputedStyle(parent);
1030
+ let $maxZIndexNode: HTMLElement | null = null;
1031
+ let rect = {
1032
+ x: 0,
1033
+ y: 0,
1034
+ width: 0,
1035
+ height: 0,
1036
+ top: 0,
1037
+ right: 0,
1038
+ bottom: 0,
1039
+ left: 0,
1040
+ } as Omit<DOMRect, "toJSON">;
1041
+ while ($parent) {
1042
+ const nodeStyle = globalThis.getComputedStyle($parent);
1027
1043
  const nodeZIndex = parseInt(nodeStyle.zIndex);
1028
1044
  if (nodeStyle.position !== "static" && !isNaN(nodeZIndex)) {
1029
1045
  if (nodeZIndex > zIndex) {
1030
1046
  zIndex = nodeZIndex;
1031
- maxZIndexNode = parent;
1047
+ $maxZIndexNode = $parent;
1032
1048
  }
1033
1049
  }
1034
- parent = parent.parentElement;
1050
+ $parent = $parent.parentElement;
1051
+ }
1052
+ if ($maxZIndexNode) {
1053
+ const maxRect = $maxZIndexNode.getBoundingClientRect();
1054
+ rect = {
1055
+ x: maxRect.x,
1056
+ y: maxRect.y,
1057
+ width: maxRect.width,
1058
+ height: maxRect.height,
1059
+ top: maxRect.top,
1060
+ right: maxRect.right,
1061
+ bottom: maxRect.bottom,
1062
+ left: maxRect.left,
1063
+ };
1035
1064
  }
1036
1065
  return {
1037
- /** 处理了偏移量后的z-index值 */
1066
+ /** 计算偏移量后的z-index值 */
1038
1067
  zIndex: zIndex + deviation,
1039
- /** 原始z-index值 */
1068
+ /** 获取到的最大的z-index值 */
1040
1069
  originZIndex: zIndex,
1041
1070
  /** 拥有最大z-index的元素 */
1042
- node: maxZIndexNode,
1071
+ node: $maxZIndexNode,
1043
1072
  /** 目标坐标元素 */
1044
- positionNode: positionNode,
1045
- /** x坐标 */
1073
+ positionNode: $position,
1074
+ /** 目标x坐标 */
1046
1075
  positionX: positionX,
1047
- /** y坐标 */
1076
+ /** 目标y坐标 */
1048
1077
  positionY: positionY,
1078
+ /** node位置信息 */
1079
+ rect,
1049
1080
  };
1050
1081
  })
1051
1082
  .filter((it) => it != null);
@@ -5,3 +5,9 @@ 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);
12
+
13
+ declare type IArray<T> = T | Array<T>;