ph-utils 0.11.5 → 0.12.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.
Files changed (3) hide show
  1. package/lib/dom.d.ts +24 -5
  2. package/lib/dom.js +60 -10
  3. package/package.json +1 -1
package/lib/dom.d.ts CHANGED
@@ -7,21 +7,39 @@
7
7
  * 根据选择器获取节点
8
8
  * @param {string} selector 选择器
9
9
  */
10
- export declare function elem(selector: string | HTMLElement, dom?: HTMLElement | ShadowRoot | Document): NodeListOf<HTMLElement> | HTMLElement[];
10
+ type FormatStyleParam = (string | undefined | null)[] | Record<string, boolean | string | undefined | null> | string;
11
+ type FormatClassParam = (string | undefined | null | boolean)[] | Record<string, boolean | string | undefined | null | boolean> | string;
12
+ type DocumentContext = HTMLElement | ShadowRoot | Document;
13
+ export declare function elem(selector: string | HTMLElement, dom?: DocumentContext): NodeListOf<HTMLElement> | HTMLElement[];
11
14
  /**
12
15
  * 根据选择器获取 DOM 元素。
13
16
  * @param selector - 选择器字符串或 HTMLElement 实例。
14
17
  * @param dom - 可选参数,指定在哪个 DOM 节点下查找元素,默认为 document。
15
18
  * @returns 返回匹配到的 HTMLElement 实例。
16
19
  */
17
- export declare function $(selector: string | HTMLElement, dom?: HTMLElement | ShadowRoot | Document): NodeListOf<HTMLElement> | HTMLElement[];
20
+ export declare function $(selector: string | HTMLElement, dom?: DocumentContext): NodeListOf<HTMLElement> | HTMLElement[];
21
+ /**
22
+ * 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
23
+ * @param tag - 元素标签名或 HTML 字符串。
24
+ * @param option - 元素的属性、样式、文本内容等配置。
25
+ * @param ctx - 元素的父级文档上下文。
26
+ * @returns 创建的 HTML 元素。
27
+ */
28
+ export declare function create(tag: string, option?: {
29
+ class?: FormatClassParam;
30
+ style?: FormatStyleParam;
31
+ textContent?: string;
32
+ innerHTML?: string;
33
+ outerHTML?: string;
34
+ [index: string]: any;
35
+ }, ctx?: DocumentContext): HTMLElement;
18
36
  /**
19
37
  * 根据选择器获取匹配的第一个 DOM 元素。
20
38
  * @param selector - 选择器字符串或直接的 HTMLElement。
21
39
  * @param dom - 可选的父级 DOM 元素,默认为当前文档。
22
40
  * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 undefined。
23
41
  */
24
- export declare function $one(selector: string | HTMLElement, dom?: HTMLElement | ShadowRoot | Document): HTMLElement;
42
+ export declare function $one(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement | undefined;
25
43
  /**
26
44
  * 为节点添加 class
27
45
  * @param {HTMLElement} elem 待添加 class 的节点
@@ -151,13 +169,13 @@ export declare function isMobile(): boolean;
151
169
  * @param classObj - 类名对象或数组
152
170
  * @returns 格式化后的类名字符串
153
171
  */
154
- export declare function formatClass(classObj: (boolean | string | undefined | null)[] | Record<string, boolean | string | undefined | null>): string;
172
+ export declare function formatClass(classObj: (boolean | string | undefined | null)[] | Record<string, boolean | string | undefined | null> | string): string;
155
173
  /**
156
174
  * 将样式对象格式化为 CSS 样式字符串。
157
175
  * @param styleObj - 样式对象,可以是字符串数组或键值对对象。
158
176
  * @returns 格式化后的 CSS 样式字符串。
159
177
  */
160
- export declare function formatStyle(styleObj: (string | undefined | null | "")[] | Record<string, string | undefined | null>): string;
178
+ export declare function formatStyle(styleObj: FormatStyleParam): string;
161
179
  /**
162
180
  * 对指定的 HTML 元素应用显示过渡效果。
163
181
  * @param target - 要应用过渡效果的 HTML 元素。
@@ -181,3 +199,4 @@ export declare function startTransition(target: HTMLElement, properties: [string
181
199
  * @param finish - 过渡结束后可选的回调函数。
182
200
  */
183
201
  export declare function endTransition(target: HTMLElement, properties: [string, string][], finish?: () => void): void;
202
+ export {};
package/lib/dom.js CHANGED
@@ -1,12 +1,3 @@
1
- /**
2
- * web(浏览器端) DOM 文件操作
3
- * 现今不推荐在使用这种方式,现在开发前端的时候,推荐使用一些成熟的框架例如:React、Preact、Vue、Angular、Svelte、Ember、Knockout等
4
- * 在使用这些框架的时候额外的一些不可避免的 dom 操作时才使用这个工具;如果确实需要使用原生开发推荐使用 jquery 或者想要更精简的话可以使用 https://github.com/finom/bala 封装
5
- */
6
- /**
7
- * 根据选择器获取节点
8
- * @param {string} selector 选择器
9
- */
10
1
  export function elem(selector, dom) {
11
2
  if (typeof selector === "string") {
12
3
  return (dom || document).querySelectorAll(selector);
@@ -24,6 +15,58 @@ export function elem(selector, dom) {
24
15
  export function $(selector, dom) {
25
16
  return elem(selector, dom);
26
17
  }
18
+ /**
19
+ * 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
20
+ * @param tag - 元素标签名或 HTML 字符串。
21
+ * @param option - 元素的属性、样式、文本内容等配置。
22
+ * @param ctx - 元素的父级文档上下文。
23
+ * @returns 创建的 HTML 元素。
24
+ */
25
+ export function create(tag, option = {}, ctx) {
26
+ let $el;
27
+ if (tag.startsWith("<") && tag.endsWith(">")) {
28
+ const parser = new DOMParser();
29
+ const doc = parser.parseFromString(tag, "text/html");
30
+ $el = doc.body.firstElementChild;
31
+ }
32
+ else {
33
+ $el = document.createElement(tag);
34
+ }
35
+ if ($el) {
36
+ if (option) {
37
+ for (const key in option) {
38
+ const value = option[key];
39
+ if (key === "class") {
40
+ $el.className = formatClass(value);
41
+ }
42
+ else if (key === "style") {
43
+ $el.style.cssText = formatStyle(value);
44
+ }
45
+ else if (key === "textContent" && value) {
46
+ $el.textContent = value;
47
+ }
48
+ else if (key === "innerHTML" && value) {
49
+ $el.innerHTML = value;
50
+ }
51
+ else if (key === "outerHTML" && value) {
52
+ $el.outerHTML = value;
53
+ }
54
+ else {
55
+ if (value === true) {
56
+ $el.setAttribute(key, "");
57
+ }
58
+ else if (typeof value === "string") {
59
+ $el.setAttribute(key, value);
60
+ }
61
+ }
62
+ }
63
+ }
64
+ if (ctx) {
65
+ ctx.appendChild($el);
66
+ }
67
+ }
68
+ return $el;
69
+ }
27
70
  /**
28
71
  * 根据选择器获取匹配的第一个 DOM 元素。
29
72
  * @param selector - 选择器字符串或直接的 HTMLElement。
@@ -31,7 +74,8 @@ export function $(selector, dom) {
31
74
  * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 undefined。
32
75
  */
33
76
  export function $one(selector, dom) {
34
- return elem(selector, dom)[0];
77
+ const els = elem(selector, dom);
78
+ return els.length > 0 ? els[0] : undefined;
35
79
  }
36
80
  /**
37
81
  * 为节点添加 class
@@ -298,6 +342,9 @@ export function formatClass(classObj) {
298
342
  }
299
343
  }
300
344
  }
345
+ else if (typeof classObj === "string") {
346
+ classes = classObj;
347
+ }
301
348
  else {
302
349
  for (const key in classObj) {
303
350
  if (classObj[key]) {
@@ -322,6 +369,9 @@ export function formatStyle(styleObj) {
322
369
  }
323
370
  }
324
371
  }
372
+ else if (typeof styleObj === "string") {
373
+ styleStr = styleObj;
374
+ }
325
375
  else {
326
376
  for (const key in styleObj) {
327
377
  const value = styleObj[key];
package/package.json CHANGED
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "./*": "./lib/*"
70
70
  },
71
- "version": "0.11.5",
71
+ "version": "0.12.1",
72
72
  "type": "module",
73
73
  "repository": {
74
74
  "type": "git",