ph-utils 0.15.2 → 0.15.4

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.
Files changed (3) hide show
  1. package/lib/dom.d.ts +4 -8
  2. package/lib/dom.js +28 -10
  3. package/package.json +1 -1
package/lib/dom.d.ts CHANGED
@@ -119,12 +119,8 @@ export declare function html(element: HTMLElement, htmlstr?: string): string | u
119
119
  * @returns
120
120
  */
121
121
  export declare function text(element: HTMLElement, textstr?: string): string | null | undefined;
122
- /**
123
- * 节点列表遍历
124
- * @param elems
125
- * @param fn 遍历到节点时的回调,回调第一个参数为遍历到的节点,第2个参数为 index;如果回调函数返回 true,则会终止遍历(break)
126
- */
127
- export declare function iterate<T>(elems: NodeList | HTMLCollection | T[], fn: (el: HTMLElement | T, index: number) => any): void;
122
+ export declare function iterate<T>(elems: T[], fn: (el: T, index: number) => any): void;
123
+ export declare function iterate(elems: NodeList | HTMLCollection, fn: (el: HTMLElement, index: number) => any): void;
128
124
  /**
129
125
  * 设置或获取节点 data-* 属性
130
126
  * @param elem
@@ -154,9 +150,9 @@ export declare function getAttr<T extends Record<string, string | number | boole
154
150
  */
155
151
  export declare function parent(el: HTMLElement): HTMLElement;
156
152
  /**
157
- * 获取隐藏节点的尺寸
153
+ * 获取隐藏节点的尺寸, 如果 parent 传空, 且 hideNode 为节点,则会通过修改原始样式方式计算
158
154
  * @param {string | HTMLElement} hideNode - The node to hide.
159
- * @param parent - 添加临时节点的父节点,默认为: body.
155
+ * @param parent - 添加临时节点的父节点, 如果传递 null, 则通过修改原始样式方式计算,默认为: body.
160
156
  * @returns The DOMRect of the element.
161
157
  */
162
158
  export declare function queryHideNodeSize(hideNode: string | HTMLElement, parent?: HTMLElement): {
package/lib/dom.js CHANGED
@@ -128,17 +128,18 @@ export function toggleClass(el, clazz) {
128
128
  * @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
129
129
  */
130
130
  export function on(element, listener, fn, option) {
131
- if (typeof option === "object" && option.eventFlag) {
132
- if (element.setAttribute) {
133
- element.setAttribute(option.eventFlag, "__stop__");
134
- }
135
- }
136
- if (element.length) {
131
+ if (element.length != null) {
137
132
  iterate(element, (elem) => {
133
+ if (typeof option === "object" && option.eventFlag) {
134
+ elem.setAttribute(option.eventFlag, "__stop__");
135
+ }
138
136
  elem.addEventListener(listener, fn, option);
139
137
  });
140
138
  }
141
139
  else if (element) {
140
+ if (typeof option === "object" && option.eventFlag) {
141
+ element.setAttribute(option.eventFlag, "__stop__");
142
+ }
142
143
  element.addEventListener(listener, fn, option);
143
144
  }
144
145
  }
@@ -149,7 +150,7 @@ export function on(element, listener, fn, option) {
149
150
  * @param fn - 要移除的事件监听器函数。
150
151
  */
151
152
  export function off(el, listener, fn, option) {
152
- if (el.length) {
153
+ if (el.length != null) {
153
154
  iterate(el, (elem) => {
154
155
  elem.removeEventListener(listener, fn, option);
155
156
  });
@@ -281,12 +282,29 @@ export function parent(el) {
281
282
  return el.parentNode;
282
283
  }
283
284
  /**
284
- * 获取隐藏节点的尺寸
285
+ * 获取隐藏节点的尺寸, 如果 parent 传空, 且 hideNode 为节点,则会通过修改原始样式方式计算
285
286
  * @param {string | HTMLElement} hideNode - The node to hide.
286
- * @param parent - 添加临时节点的父节点,默认为: body.
287
+ * @param parent - 添加临时节点的父节点, 如果传递 null, 则通过修改原始样式方式计算,默认为: body.
287
288
  * @returns The DOMRect of the element.
288
289
  */
289
290
  export function queryHideNodeSize(hideNode, parent = document.body) {
291
+ if (parent == null && typeof hideNode !== "string") {
292
+ // 保存原来的样式
293
+ const originalDisplay = hideNode.style.display;
294
+ const originalVisibility = hideNode.style.visibility;
295
+ const originalPosition = hideNode.style.position;
296
+ // 设置为可见但不可见状态,不影响布局
297
+ hideNode.style.display = "block";
298
+ hideNode.style.visibility = "hidden";
299
+ hideNode.style.position = "absolute";
300
+ // 读取高度
301
+ const rect = hideNode.getBoundingClientRect();
302
+ // 恢复原样式
303
+ hideNode.style.display = originalDisplay;
304
+ hideNode.style.visibility = originalVisibility;
305
+ hideNode.style.position = originalPosition;
306
+ return { width: rect.width, height: rect.height };
307
+ }
290
308
  // 计算折叠菜单的高度
291
309
  let $tmp = document.createElement("div");
292
310
  $tmp.style.cssText = "position:fixed;left:-1000px;top:-1000px;opacity:0;";
@@ -507,7 +525,7 @@ export function transition(el, nameOrProperties, dir = "enter", finish) {
507
525
  });
508
526
  }
509
527
  }
510
- el.addEventListener("transitionend", (e) => {
528
+ el.addEventListener("transitionend", (_e) => {
511
529
  if (status === 0) {
512
530
  status = 1;
513
531
  if (nameClass) {
package/package.json CHANGED
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "./*": "./lib/*"
70
70
  },
71
- "version": "0.15.2",
71
+ "version": "0.15.4",
72
72
  "type": "module",
73
73
  "repository": {
74
74
  "type": "git",