@whitesev/utils 2.2.5 → 2.2.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.
package/dist/index.cjs.js CHANGED
@@ -3653,7 +3653,7 @@ class Utils {
3653
3653
  this.windowApi = new WindowApi(option);
3654
3654
  }
3655
3655
  /** 版本号 */
3656
- version = "2024.9.4";
3656
+ version = "2024.9.10";
3657
3657
  addStyle(cssText) {
3658
3658
  if (typeof cssText !== "string") {
3659
3659
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -4349,32 +4349,66 @@ class Utils {
4349
4349
  return Math.max(...newResult);
4350
4350
  }
4351
4351
  }
4352
- getMaxZIndex(deviation = 1) {
4352
+ getMaxZIndexNodeInfo(deviation = 1) {
4353
4353
  deviation = Number.isNaN(deviation) ? 1 : deviation;
4354
+ const UtilsContext = this;
4354
4355
  // 最大值2147483647
4355
- let maxZIndex = Math.pow(2, 31) - 1;
4356
+ const maxZIndex = Math.pow(2, 31) - 1;
4356
4357
  // 比较值2000000000
4357
- let maxZIndexCompare = 2 * Math.pow(10, 9);
4358
+ const maxZIndexCompare = 2 * Math.pow(10, 9);
4358
4359
  // 当前页面最大的z-index
4359
4360
  let zIndex = 0;
4360
- this.windowApi.document.querySelectorAll("*").forEach(($ele, index) => {
4361
- let nodeStyle = this.windowApi.window.getComputedStyle($ele);
4361
+ // 当前的最大z-index的元素,调试使用
4362
+ // @ts-ignore
4363
+ let maxZIndexNode = null;
4364
+ /**
4365
+ * 元素是否可见
4366
+ * @param $css
4367
+ */
4368
+ function isVisibleNode($css) {
4369
+ return $css.position !== "static" && $css.display !== "none";
4370
+ }
4371
+ /**
4372
+ * 查询元素的z-index
4373
+ * 并比较值是否是已获取的最大值
4374
+ * @param $ele
4375
+ */
4376
+ function queryMaxZIndex($ele) {
4377
+ /** 元素的样式 */
4378
+ const nodeStyle = UtilsContext.windowApi.window.getComputedStyle($ele);
4362
4379
  /* 不对position为static和display为none的元素进行获取它们的z-index */
4363
- if (nodeStyle.position !== "static" && nodeStyle.display !== "none") {
4380
+ if (isVisibleNode(nodeStyle)) {
4364
4381
  let nodeZIndex = parseInt(nodeStyle.zIndex);
4365
4382
  if (!isNaN(nodeZIndex)) {
4366
4383
  if (nodeZIndex > zIndex) {
4384
+ // 赋值到全局
4367
4385
  zIndex = nodeZIndex;
4386
+ maxZIndexNode = $ele;
4368
4387
  }
4369
4388
  }
4389
+ // 判断shadowRoot
4390
+ if ($ele.shadowRoot != null && $ele instanceof ShadowRoot) {
4391
+ $ele.shadowRoot.querySelectorAll("*").forEach(($shadowEle) => {
4392
+ queryMaxZIndex($shadowEle);
4393
+ });
4394
+ }
4370
4395
  }
4396
+ }
4397
+ this.windowApi.document.querySelectorAll("*").forEach(($ele, index) => {
4398
+ queryMaxZIndex($ele);
4371
4399
  });
4372
4400
  zIndex += deviation;
4373
4401
  if (zIndex >= maxZIndexCompare) {
4374
4402
  // 最好不要超过最大值
4375
4403
  zIndex = maxZIndex;
4376
4404
  }
4377
- return zIndex;
4405
+ return {
4406
+ node: maxZIndexNode,
4407
+ zIndex: zIndex,
4408
+ };
4409
+ }
4410
+ getMaxZIndex(deviation = 1) {
4411
+ return this.getMaxZIndexNodeInfo(deviation).zIndex;
4378
4412
  }
4379
4413
  getMinValue(...args) {
4380
4414
  let result = [...args];
@@ -6093,7 +6127,7 @@ class Utils {
6093
6127
  });
6094
6128
  return result;
6095
6129
  }
6096
- toSearchParamsStr(obj) {
6130
+ toSearchParamsStr(obj, addPrefix) {
6097
6131
  let UtilsContext = this;
6098
6132
  let searhParamsStr = "";
6099
6133
  if (Array.isArray(obj)) {
@@ -6109,8 +6143,22 @@ class Utils {
6109
6143
  else {
6110
6144
  searhParamsStr = new URLSearchParams(Object.entries(obj)).toString();
6111
6145
  }
6146
+ if (addPrefix && !searhParamsStr.startsWith("?")) {
6147
+ searhParamsStr = "?" + searhParamsStr;
6148
+ }
6112
6149
  return searhParamsStr;
6113
6150
  }
6151
+ /**
6152
+ * 将UrlSearchParams格式的字符串转为对象
6153
+ */
6154
+ searchParamStrToObj(searhParamsStr) {
6155
+ if (typeof searhParamsStr !== "string") {
6156
+ // @ts-ignore
6157
+ return {};
6158
+ }
6159
+ // @ts-ignore
6160
+ return Object.fromEntries(new URLSearchParams(searhParamsStr));
6161
+ }
6114
6162
  /**
6115
6163
  * 提供一个封装了 try-catch 的函数,可以执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
6116
6164
  * @example