ph-utils 0.17.2 → 0.19.0
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/LICENSE +21 -0
- package/lib/dom.d.ts +125 -25
- package/lib/dom.js +40 -6
- package/lib/src/array.d.ts +79 -0
- package/lib/src/array.js +212 -0
- package/lib/src/color.d.ts +55 -0
- package/lib/src/color.js +294 -0
- package/lib/src/config.d.ts +33 -0
- package/lib/src/config.js +99 -0
- package/lib/src/copy.d.ts +11 -0
- package/lib/src/copy.js +101 -0
- package/lib/src/crypto.d.ts +74 -0
- package/lib/src/crypto.js +261 -0
- package/lib/src/crypto_node.d.ts +61 -0
- package/lib/src/crypto_node.js +133 -0
- package/lib/src/date.d.ts +66 -0
- package/lib/src/date.js +202 -0
- package/lib/src/dom.d.ts +265 -0
- package/lib/src/dom.js +635 -0
- package/lib/src/file.d.ts +29 -0
- package/lib/src/file.js +54 -0
- package/lib/src/id.d.ts +68 -0
- package/lib/src/id.js +170 -0
- package/lib/src/index.d.ts +154 -0
- package/lib/src/index.js +239 -0
- package/lib/src/logger.d.ts +62 -0
- package/lib/src/logger.js +122 -0
- package/lib/src/server.d.ts +33 -0
- package/lib/src/server.js +65 -0
- package/lib/src/storage.d.ts +51 -0
- package/lib/src/storage.js +73 -0
- package/lib/src/theme.d.ts +44 -0
- package/lib/src/theme.js +156 -0
- package/lib/src/validator.d.ts +71 -0
- package/lib/src/validator.js +238 -0
- package/lib/src/web.d.ts +30 -0
- package/lib/src/web.js +100 -0
- package/package.json +2 -2
package/lib/src/dom.js
ADDED
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
export function elem(selector, dom) {
|
|
2
|
+
if (typeof selector === "string") {
|
|
3
|
+
return (dom || document).querySelectorAll(selector);
|
|
4
|
+
}
|
|
5
|
+
else {
|
|
6
|
+
return [selector];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 根据选择器获取 DOM 元素。
|
|
11
|
+
* @param selector - 选择器字符串或 HTMLElement 实例。
|
|
12
|
+
* @param dom - 可选参数,指定在哪个 DOM 节点下查找元素,默认为 document。
|
|
13
|
+
* @returns 返回匹配到的 HTMLElement 实例。
|
|
14
|
+
*/
|
|
15
|
+
export function $(selector, dom) {
|
|
16
|
+
return elem(selector, dom);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
|
|
20
|
+
* @param tag - 元素标签名或 HTML 字符串。
|
|
21
|
+
* @param option - 元素的属性、样式、文本内容等配置。所有不是指定的属性,都会通过 setAttribute 设置
|
|
22
|
+
* @param ctx - 元素的父级文档上下文。
|
|
23
|
+
* @returns 创建的 HTML 元素。
|
|
24
|
+
*/
|
|
25
|
+
export function create(tag, option = {}, children) {
|
|
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 (value != null) {
|
|
40
|
+
if (key === "class") {
|
|
41
|
+
$el.className = formatClass(value);
|
|
42
|
+
}
|
|
43
|
+
else if (key === "style") {
|
|
44
|
+
$el.style.cssText = formatStyle(value);
|
|
45
|
+
}
|
|
46
|
+
else if (key === "textContent" && value) {
|
|
47
|
+
$el.textContent = value;
|
|
48
|
+
}
|
|
49
|
+
else if (key === "innerHTML" && value) {
|
|
50
|
+
$el.innerHTML = value;
|
|
51
|
+
}
|
|
52
|
+
else if (key === "outerHTML" && value) {
|
|
53
|
+
$el.outerHTML = value;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
if (typeof value === "boolean" && value === true) {
|
|
57
|
+
$el.setAttribute(key, "");
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
$el.setAttribute(key, value);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (children) {
|
|
67
|
+
if (children instanceof HTMLElement || children instanceof DocumentFragment) {
|
|
68
|
+
$el.appendChild(children);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
let len = children.length;
|
|
72
|
+
const fragment = document.createDocumentFragment();
|
|
73
|
+
for (let i = 0; i < len; i++) {
|
|
74
|
+
const child = children[i];
|
|
75
|
+
if (child instanceof HTMLElement) {
|
|
76
|
+
fragment.appendChild(child);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (len > 0) {
|
|
80
|
+
$el.appendChild(fragment);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return $el;
|
|
86
|
+
}
|
|
87
|
+
/** 创建节点 */
|
|
88
|
+
export function $$(tag, option = {}, children) {
|
|
89
|
+
return create(tag, option, children);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 根据选择器获取匹配的第一个 DOM 元素。
|
|
93
|
+
* @param selector - 选择器字符串或直接的 HTMLElement。
|
|
94
|
+
* @param dom - 可选的父级 DOM 元素,默认为当前文档。
|
|
95
|
+
* @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 null。
|
|
96
|
+
*/
|
|
97
|
+
export function $one(selector, dom) {
|
|
98
|
+
if (typeof selector === "string") {
|
|
99
|
+
return (dom || document).querySelector(selector);
|
|
100
|
+
}
|
|
101
|
+
return selector;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 为节点添加 class
|
|
105
|
+
* @param {HTMLElement} elem 待添加 class 的节点
|
|
106
|
+
* @param {string} clazz 需要添加的 class
|
|
107
|
+
*/
|
|
108
|
+
export function addClass(elem, clazz) {
|
|
109
|
+
elem.classList.add(clazz);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 节点移除 class
|
|
113
|
+
* @param {HTMLElement} elem 待移除 class 的节点
|
|
114
|
+
* @param {string} clazz 需要移除的 class
|
|
115
|
+
*/
|
|
116
|
+
export function removeClass(elem, clazz) {
|
|
117
|
+
elem.classList.remove(clazz);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 判断节点是否包含某个 class
|
|
121
|
+
* @param elem 待判断 class 的节点
|
|
122
|
+
* @param clazz 待判断的 class
|
|
123
|
+
* @returns
|
|
124
|
+
*/
|
|
125
|
+
export function hasClass(elem, clazz) {
|
|
126
|
+
return elem.classList.contains(clazz);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 切换指定元素的类名。
|
|
130
|
+
* 如果元素已包含该类名,则移除它;否则添加它。
|
|
131
|
+
* @param el - 要操作的 HTML 元素。
|
|
132
|
+
* @param clazz - 要切换的类名。
|
|
133
|
+
*/
|
|
134
|
+
export function toggleClass(el, clazz) {
|
|
135
|
+
if (hasClass(el, clazz)) {
|
|
136
|
+
removeClass(el, clazz);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
addClass(el, clazz);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* 替换指定元素的 CSS 类
|
|
144
|
+
*
|
|
145
|
+
* 该函数用于根据给定的旧类名或类索引,将其替换为新类名。
|
|
146
|
+
* 如果旧类不存在,则不会进行替换。
|
|
147
|
+
*
|
|
148
|
+
* @param el - 目标 HTML 元素,将对其类进行操作
|
|
149
|
+
* @param oldClazz - 要替换的旧类名或类索引(基于数字时对应 classList 的索引位置)
|
|
150
|
+
* @param newClazz - 新的类名,用于替换旧的类
|
|
151
|
+
* @returns void
|
|
152
|
+
*/
|
|
153
|
+
export function replaceClass(el, oldClazz, newClazz) {
|
|
154
|
+
// 判断 oldClazz 是否为数字,若是则通过索引从 el.classList 中获取对应类名,否则直接使用字符串值
|
|
155
|
+
let old = typeof oldClazz === "number" ? el.classList.item(oldClazz) : oldClazz;
|
|
156
|
+
// 如果旧类存在,则使用 classList.replace 方法进行替换
|
|
157
|
+
if (old) {
|
|
158
|
+
el.classList.replace(old, newClazz);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 为节点添加事件处理
|
|
163
|
+
* @param {HTMLElement} element 添加事件的节点
|
|
164
|
+
* @param {string} listener 事件名称
|
|
165
|
+
* @param {function} fn 事件处理函数
|
|
166
|
+
* @param {boolean} option 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
|
|
167
|
+
*/
|
|
168
|
+
export function on(element, listener, fn, option) {
|
|
169
|
+
if (element.length != null) {
|
|
170
|
+
iterate(element, (elem) => {
|
|
171
|
+
if (typeof option === "object" && option.eventFlag) {
|
|
172
|
+
elem.setAttribute(option.eventFlag, "__stop__");
|
|
173
|
+
}
|
|
174
|
+
elem.addEventListener(listener, fn, option);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
else if (element) {
|
|
178
|
+
if (typeof option === "object" && option.eventFlag) {
|
|
179
|
+
element.setAttribute(option.eventFlag, "__stop__");
|
|
180
|
+
}
|
|
181
|
+
element.addEventListener(listener, fn, option);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 移除指定元素的事件监听器。
|
|
186
|
+
* @param el - 要移除监听器的 HTML 元素。
|
|
187
|
+
* @param listener - 事件名称。
|
|
188
|
+
* @param fn - 要移除的事件监听器函数。
|
|
189
|
+
*/
|
|
190
|
+
export function off(el, listener, fn, option) {
|
|
191
|
+
if (el.length != null) {
|
|
192
|
+
iterate(el, (elem) => {
|
|
193
|
+
elem.removeEventListener(listener, fn, option);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
else if (el) {
|
|
197
|
+
el.removeEventListener(listener, fn, option);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 判断事件是否应该继续传递。
|
|
202
|
+
* 从事件目标开始向上遍历DOM树,检查每个节点上是否存在指定的属性。
|
|
203
|
+
* 如果找到该属性且其值不为'__stop__',则返回true,表示事件可以继续传递。
|
|
204
|
+
* 否则,返回false,表示事件应该停止传递。
|
|
205
|
+
* 通常用于事件委托时判断是否继续传递事件。
|
|
206
|
+
*
|
|
207
|
+
* @param e - 触发的事件对象
|
|
208
|
+
* @param eventFlag - 需要检查的属性名
|
|
209
|
+
* @param endRoot - 可选,如果传递该参数,则表示停止遍历的节点,如果未传递,则表示遍历到文档根节点为止
|
|
210
|
+
* @returns 包含三个元素的数组:[是否继续传递事件, 属性值, 当前检查的DOM节点]
|
|
211
|
+
*/
|
|
212
|
+
export function shouldEventNext(e, eventFlag, endRoot) {
|
|
213
|
+
let target = e.target;
|
|
214
|
+
let flag = "";
|
|
215
|
+
do {
|
|
216
|
+
if ((endRoot && endRoot.isSameNode(target)) || target.tagName === "BODY") {
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
if (target.getAttribute) {
|
|
220
|
+
flag = target.getAttribute(eventFlag) || "";
|
|
221
|
+
}
|
|
222
|
+
if (flag === "") {
|
|
223
|
+
target = target.parentNode;
|
|
224
|
+
}
|
|
225
|
+
if (!target)
|
|
226
|
+
break;
|
|
227
|
+
} while (flag === "");
|
|
228
|
+
return [flag !== "__stop__" && flag !== "", flag, target];
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* 设置或获取节点的 innerHTML 属性
|
|
232
|
+
* @param element
|
|
233
|
+
* @param htmlstr 可选,如果传递该参数,则表示设置;否则表示获取
|
|
234
|
+
* @returns
|
|
235
|
+
*/
|
|
236
|
+
export function html(element, htmlstr) {
|
|
237
|
+
if (htmlstr == null) {
|
|
238
|
+
return element.innerHTML;
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
element.innerHTML = htmlstr;
|
|
242
|
+
return undefined;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* 设置或获取节点的 textContent 属性
|
|
247
|
+
* @param element
|
|
248
|
+
* @param textstr 可选,如果传递该参数,则表示设置;否则表示获取
|
|
249
|
+
* @returns
|
|
250
|
+
*/
|
|
251
|
+
export function text(element, textstr) {
|
|
252
|
+
if (textstr == null) {
|
|
253
|
+
return element.textContent;
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
element.textContent = textstr;
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* 节点列表遍历
|
|
262
|
+
* @param elems
|
|
263
|
+
* @param fn 遍历到节点时的回调,回调第一个参数为遍历到的节点,第2个参数为 index;如果回调函数返回 true,则会终止遍历(break)
|
|
264
|
+
*/
|
|
265
|
+
export function iterate(elems, fn) {
|
|
266
|
+
for (let i = 0, len = elems.length; i < len; i++) {
|
|
267
|
+
let r = fn(elems[i], i);
|
|
268
|
+
if (r === true) {
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 设置或获取节点 data-* 属性
|
|
275
|
+
* @param elem
|
|
276
|
+
* @param key data- 后面跟随的值
|
|
277
|
+
* @param value 如果传递该值表示获取;否则表示设置
|
|
278
|
+
* @returns
|
|
279
|
+
*/
|
|
280
|
+
export function attr(elem, key, value) {
|
|
281
|
+
if (value != null) {
|
|
282
|
+
elem.setAttribute("data-" + key, value);
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
return elem.getAttribute("data-" + key);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
export function getAttr(el, key, defaultValue) {
|
|
289
|
+
const value = el.getAttribute(key);
|
|
290
|
+
if (defaultValue == null)
|
|
291
|
+
return value;
|
|
292
|
+
const valueType = typeof defaultValue;
|
|
293
|
+
if (value == null)
|
|
294
|
+
return defaultValue;
|
|
295
|
+
// 类型转换
|
|
296
|
+
if (valueType === "bigint" || valueType === "number") {
|
|
297
|
+
if (value === "")
|
|
298
|
+
return defaultValue;
|
|
299
|
+
return Number(value);
|
|
300
|
+
}
|
|
301
|
+
if (valueType === "boolean") {
|
|
302
|
+
if (value === "" || value === "1" || value === "true" || value === key) {
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
if (valueType === "object") {
|
|
308
|
+
if (value === "")
|
|
309
|
+
return defaultValue;
|
|
310
|
+
return JSON.parse(value);
|
|
311
|
+
}
|
|
312
|
+
return value;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* 获取指定节点的父节点
|
|
316
|
+
* @param el
|
|
317
|
+
* @returns
|
|
318
|
+
*/
|
|
319
|
+
export function parent(el) {
|
|
320
|
+
return el.parentNode;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* 获取隐藏节点的尺寸, 如果 parent 传空, 且 hideNode 为节点,则会通过修改原始样式方式计算
|
|
324
|
+
* @param {string | HTMLElement} hideNode - The node to hide.
|
|
325
|
+
* @param parent - 添加临时节点的父节点, 如果传递 null, 则通过修改原始样式方式计算,默认为: body.
|
|
326
|
+
* @returns The DOMRect of the element.
|
|
327
|
+
*/
|
|
328
|
+
export function queryHideNodeSize(hideNode, parent = document.body) {
|
|
329
|
+
if (parent == null && typeof hideNode !== "string") {
|
|
330
|
+
// 保存原来的样式
|
|
331
|
+
const originalDisplay = hideNode.style.display;
|
|
332
|
+
const originalVisibility = hideNode.style.visibility;
|
|
333
|
+
const originalPosition = hideNode.style.position;
|
|
334
|
+
// 设置为可见但不可见状态,不影响布局
|
|
335
|
+
hideNode.style.position = "absolute";
|
|
336
|
+
hideNode.style.visibility = "hidden";
|
|
337
|
+
hideNode.style.display = "block";
|
|
338
|
+
// 读取高度
|
|
339
|
+
const rect = hideNode.getBoundingClientRect();
|
|
340
|
+
// 恢复原样式
|
|
341
|
+
hideNode.style.display = originalDisplay;
|
|
342
|
+
hideNode.style.visibility = originalVisibility;
|
|
343
|
+
hideNode.style.position = originalPosition;
|
|
344
|
+
return { width: rect.width, height: rect.height };
|
|
345
|
+
}
|
|
346
|
+
// 计算折叠菜单的高度
|
|
347
|
+
let $tmp = document.createElement("div");
|
|
348
|
+
$tmp.style.cssText = "position:fixed;left:-1000px;top:-1000px;opacity:0;";
|
|
349
|
+
let $tmpInner = document.createElement("div");
|
|
350
|
+
$tmpInner.style.cssText = "position:relative;";
|
|
351
|
+
if (typeof hideNode === "string") {
|
|
352
|
+
$tmpInner.innerHTML = hideNode;
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
$tmpInner.appendChild(hideNode.cloneNode(true));
|
|
356
|
+
}
|
|
357
|
+
$tmp.appendChild($tmpInner);
|
|
358
|
+
(parent || document.body).appendChild($tmp);
|
|
359
|
+
let rect = $tmpInner.children[0].getBoundingClientRect();
|
|
360
|
+
(parent || document.body).removeChild($tmp);
|
|
361
|
+
return { width: rect.width, height: rect.height };
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* 判断元素是否在父元素的可视区域内。
|
|
365
|
+
*
|
|
366
|
+
* @param el 要检查的元素
|
|
367
|
+
* @param parent 元素的父元素,默认为document.body
|
|
368
|
+
* @param direction 检查的方向,默认为"horizontal"
|
|
369
|
+
* @returns 如果元素在父元素的可视区域内,则返回true;否则返回false。
|
|
370
|
+
*/
|
|
371
|
+
export function isVisible(el, parent = null, direction = "horizontal") {
|
|
372
|
+
if (parent == null) {
|
|
373
|
+
parent = el.offsetParent;
|
|
374
|
+
}
|
|
375
|
+
// 获取父元素的边界信息
|
|
376
|
+
const containerRect = parent.getBoundingClientRect();
|
|
377
|
+
// 获取元素的边界信息
|
|
378
|
+
const elementRect = el.getBoundingClientRect();
|
|
379
|
+
// 根据检查方向,确定元素的起始和结束位置
|
|
380
|
+
// 元素的上、下边界
|
|
381
|
+
let elStart = direction === "horizontal" ? elementRect.left : elementRect.top;
|
|
382
|
+
let elEnd = direction === "horizontal" ? elementRect.right : elementRect.bottom;
|
|
383
|
+
// 根据检查方向,确定父元素的起始和结束位置
|
|
384
|
+
// 容器的可视区域的上、下边界
|
|
385
|
+
let containerStart = direction === "horizontal" ? containerRect.left : containerRect.top;
|
|
386
|
+
let containerEnd = direction === "horizontal" ? containerRect.right : containerRect.bottom;
|
|
387
|
+
// 判断元素是否在父元素的可视区域内
|
|
388
|
+
// 判断元素是否完全在容器的可视区域内
|
|
389
|
+
return elStart >= containerStart && elEnd <= containerEnd;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* 判断当前设备是否为移动设备。
|
|
393
|
+
*
|
|
394
|
+
* 本函数通过检查用户代理字符串和屏幕尺寸来判断设备是否为移动设备。
|
|
395
|
+
* 这对于需要根据设备类型进行不同布局或功能调整的应用非常有用。
|
|
396
|
+
*
|
|
397
|
+
* @returns {boolean} 如果设备是移动设备,则返回true;否则返回false。
|
|
398
|
+
*/
|
|
399
|
+
export function isMobile() {
|
|
400
|
+
// 通过正则表达式匹配用户代理字符串,判断是否为移动设备
|
|
401
|
+
// 检查是否为移动设备
|
|
402
|
+
const isMobile = /Mobi|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
|
403
|
+
// 获取窗口的 innerWidth 和 innerHeight,用于判断屏幕尺寸
|
|
404
|
+
// 获取屏幕宽度和高度
|
|
405
|
+
const screenWidth = window.innerWidth;
|
|
406
|
+
const screenHeight = window.innerHeight;
|
|
407
|
+
// 判断屏幕宽度或高度是否小于等于800或600,用于进一步确认是否为移动设备
|
|
408
|
+
const isScreenMobile = screenWidth <= 800 || screenHeight <= 600;
|
|
409
|
+
// 如果是移动设备或屏幕尺寸符合移动设备特征,则返回true
|
|
410
|
+
return isMobile || isScreenMobile;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* 格式化类名,支持数组和对象两种形式。
|
|
414
|
+
* - 数组形式:数组中的每个元素代表一个类名,非空元素将被添加到结果字符串中。
|
|
415
|
+
* - 对象形式:对象的键代表类名,值为真(非空、非undefined、非null)时,键将被添加到结果字符串中。
|
|
416
|
+
* @param classObj - 类名对象或数组
|
|
417
|
+
* @returns 格式化后的类名字符串
|
|
418
|
+
*/
|
|
419
|
+
export function formatClass(classObj) {
|
|
420
|
+
let classes = "";
|
|
421
|
+
if (Array.isArray(classObj)) {
|
|
422
|
+
for (let i = 0, len = classObj.length; i < len; i++) {
|
|
423
|
+
const item = classObj[i];
|
|
424
|
+
if (item) {
|
|
425
|
+
classes += `${item} `;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
else if (typeof classObj === "string") {
|
|
430
|
+
classes = classObj;
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
for (const key in classObj) {
|
|
434
|
+
if (classObj[key]) {
|
|
435
|
+
classes += `${key} `;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return classes.trim();
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* 将样式对象格式化为 CSS 样式字符串。
|
|
443
|
+
* @param styleObj - 样式对象,可以是字符串数组或键值对对象。
|
|
444
|
+
* @returns 格式化后的 CSS 样式字符串。
|
|
445
|
+
*/
|
|
446
|
+
export function formatStyle(styleObj) {
|
|
447
|
+
let styleStr = "";
|
|
448
|
+
if (Array.isArray(styleObj)) {
|
|
449
|
+
for (let i = 0, len = styleObj.length; i < len; i++) {
|
|
450
|
+
const item = styleObj[i];
|
|
451
|
+
if (item) {
|
|
452
|
+
styleStr += `${item};`;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
else if (typeof styleObj === "string") {
|
|
457
|
+
styleStr = styleObj;
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
for (const key in styleObj) {
|
|
461
|
+
const value = styleObj[key];
|
|
462
|
+
if (value) {
|
|
463
|
+
styleStr += `${key}:${value};`;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return styleStr;
|
|
468
|
+
}
|
|
469
|
+
function toggleCssProperty(el, properties, method = "set") {
|
|
470
|
+
for (let i = 0, len = properties.length; i < len; i++) {
|
|
471
|
+
const rec = properties[i];
|
|
472
|
+
if (method === "set") {
|
|
473
|
+
el.style.setProperty(rec[0], rec[1]);
|
|
474
|
+
}
|
|
475
|
+
else {
|
|
476
|
+
el.style.removeProperty(rec[0]);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* 执行元素的过渡动画。
|
|
482
|
+
*
|
|
483
|
+
* 如果是名称类似于 Vue.js 的过渡动画,通过添加 `*-active`、`*-from|to` class 类名。
|
|
484
|
+
*
|
|
485
|
+
* @param el - 需要执行过渡动画的 HTML 元素。
|
|
486
|
+
* @param nameOrProperties - 过渡动画的名称或属性数组。可以是字符串表示的动画名称,或者是包含属性名称、初始值或目标值、持续时间。eg. [['opacity', '0.5']] | 'nt-opacity'
|
|
487
|
+
* @param dir - 过渡动画的方向,"leave" 表示离开,"enter" 表示进入。默认值为 "enter"。
|
|
488
|
+
* @param finish - 过渡动画结束时的回调函数。
|
|
489
|
+
*
|
|
490
|
+
* @example <caption>执行 `nt-opacity` 名称动画</caption>
|
|
491
|
+
* // css
|
|
492
|
+
* .nt-opacity-enter-active,
|
|
493
|
+
* .nt-opacity-leave-active {
|
|
494
|
+
* transition: opacity 0.3s ease;
|
|
495
|
+
* }
|
|
496
|
+
* .nt-opacity-enter-from,
|
|
497
|
+
* .nt-opacity-leave-to {
|
|
498
|
+
* opacity: 0;
|
|
499
|
+
* }
|
|
500
|
+
* // js
|
|
501
|
+
* transition($el, "nt-opacity", "enter");
|
|
502
|
+
*
|
|
503
|
+
* @example <caption>执行 `style` 属性动画</caption>
|
|
504
|
+
* transition($el, [["opacity", "0", "0.3s"]], "enter");
|
|
505
|
+
*
|
|
506
|
+
* @example <caption>动画结束后移除节点</caption>
|
|
507
|
+
* transition($el, [["opacity", "0", "0.3s"]], "leave", () => { $el.remove(); });
|
|
508
|
+
*/
|
|
509
|
+
export function transition(el, nameOrProperties, dir = "enter", finish) {
|
|
510
|
+
const p = dir === "enter" ? "from" : "to";
|
|
511
|
+
let nameClass = "", activeClass = "";
|
|
512
|
+
/** 动画状态, -1 - 准备, 0 - 进行中, 1 - 完成 */
|
|
513
|
+
let status = -1;
|
|
514
|
+
const trans = [];
|
|
515
|
+
if (typeof nameOrProperties === "string") {
|
|
516
|
+
nameClass = `${nameOrProperties}-${dir}-${p}`;
|
|
517
|
+
activeClass = `${nameOrProperties}-${dir}-active`;
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
for (let i = 0, len = nameOrProperties.length; i < len; i++) {
|
|
521
|
+
const rec = nameOrProperties[i];
|
|
522
|
+
if (rec.length >= 3) {
|
|
523
|
+
trans.push(`${rec[0]} ${rec[2]}`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
status = 0;
|
|
528
|
+
if (dir === "enter") {
|
|
529
|
+
if (nameClass) {
|
|
530
|
+
el.classList.add(nameClass);
|
|
531
|
+
setTimeout(() => {
|
|
532
|
+
el.classList.add(activeClass);
|
|
533
|
+
requestAnimationFrame(() => {
|
|
534
|
+
el.classList.remove(nameClass);
|
|
535
|
+
});
|
|
536
|
+
}, 0);
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
toggleCssProperty(el, nameOrProperties, "set");
|
|
540
|
+
setTimeout(() => {
|
|
541
|
+
if (trans.length > 0) {
|
|
542
|
+
el.style.setProperty("transition", trans.join(", "));
|
|
543
|
+
}
|
|
544
|
+
requestAnimationFrame(() => {
|
|
545
|
+
toggleCssProperty(el, nameOrProperties, "remove");
|
|
546
|
+
});
|
|
547
|
+
}, 0);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
else {
|
|
551
|
+
if (nameClass) {
|
|
552
|
+
el.classList.add(activeClass);
|
|
553
|
+
requestAnimationFrame(() => {
|
|
554
|
+
el.classList.add(nameClass);
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
else {
|
|
558
|
+
if (trans.length > 0) {
|
|
559
|
+
el.style.setProperty("transition", trans.join(", "));
|
|
560
|
+
}
|
|
561
|
+
requestAnimationFrame(() => {
|
|
562
|
+
toggleCssProperty(el, nameOrProperties, "set");
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
el.addEventListener("transitionend", () => {
|
|
567
|
+
if (status === 0) {
|
|
568
|
+
status = 1;
|
|
569
|
+
if (nameClass) {
|
|
570
|
+
el.classList.remove(activeClass);
|
|
571
|
+
requestAnimationFrame(() => {
|
|
572
|
+
el.classList.remove(nameClass);
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
else {
|
|
576
|
+
if (trans) {
|
|
577
|
+
el.style.removeProperty("transition");
|
|
578
|
+
}
|
|
579
|
+
requestAnimationFrame(() => {
|
|
580
|
+
toggleCssProperty(el, nameOrProperties, "remove");
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
if (finish) {
|
|
584
|
+
finish();
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}, { once: true });
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* 计算光标位置
|
|
591
|
+
*
|
|
592
|
+
* 根据输入值的变化计算新的光标位置,主要用于输入框内容变化时的光标位置调整
|
|
593
|
+
*
|
|
594
|
+
* @param start - 光标起始位置,通常为: input.selectionStart
|
|
595
|
+
* @param end - 光标结束位置(通常与起始位置相同,用于选择范围),通常为: input.selectionEnd
|
|
596
|
+
* @param oldValue - 变化前的值
|
|
597
|
+
* @param newValue - 变化后的值
|
|
598
|
+
* @returns 新的光标位置对象,包含新的起始和结束位置
|
|
599
|
+
*
|
|
600
|
+
* @example
|
|
601
|
+
* // 删除字符时,光标向前移动
|
|
602
|
+
* calcuteCursorPosition(2, 2, "abc", "ac"); // 返回 { start: 1, end: 1 }
|
|
603
|
+
*
|
|
604
|
+
* @example
|
|
605
|
+
* // 添加字符时,光标向后移动
|
|
606
|
+
* calcuteCursorPosition(2, 2, "ac", "abc"); // 返回 { start: 3, end: 3 }
|
|
607
|
+
*
|
|
608
|
+
* @example
|
|
609
|
+
* // 替换字符时,保持位置
|
|
610
|
+
* calcuteCursorPosition(2, 2, "abc", "axc"); // 返回 { start: 2, end: 2 }
|
|
611
|
+
*/
|
|
612
|
+
export function calcuteCursorPosition(start, end, oldValue, newValue) {
|
|
613
|
+
// 5. 【关键】计算新光标位置并恢复
|
|
614
|
+
let newStart = start;
|
|
615
|
+
let newEnd = end;
|
|
616
|
+
// 简单策略:如果新值比旧值短,说明删了字符,光标前移
|
|
617
|
+
// 更健壮的做法:对比差异,但通常可简化处理
|
|
618
|
+
if (newValue.length < oldValue.length) {
|
|
619
|
+
// 例如:用户在中间删了一个非法字符
|
|
620
|
+
newStart = Math.max(0, start - (oldValue.length - newValue.length));
|
|
621
|
+
newEnd = newStart;
|
|
622
|
+
}
|
|
623
|
+
else if (newValue.length > oldValue.length) {
|
|
624
|
+
// 插入合法字符,光标通常就在插入点后,可保持原偏移
|
|
625
|
+
// 但需防止超出长度
|
|
626
|
+
newStart = Math.min(newValue.length, start + (newValue.length - oldValue.length));
|
|
627
|
+
newEnd = newStart;
|
|
628
|
+
}
|
|
629
|
+
else {
|
|
630
|
+
// 长度不变(如替换),保持原位置(但要限制范围)
|
|
631
|
+
newStart = Math.min(newValue.length, start);
|
|
632
|
+
newEnd = newStart;
|
|
633
|
+
}
|
|
634
|
+
return { start: newStart, end: newEnd };
|
|
635
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 读取文件内容
|
|
3
|
+
* @example <caption>1. 读取JSON文件, 内容为字符串列表</caption>
|
|
4
|
+
* read<string[]>('a.json', []);
|
|
5
|
+
* @example <caption>2. 读取JSON文件, 内容为对象</caption>
|
|
6
|
+
* read<{ name: string }>('b.json', {});
|
|
7
|
+
* @param filepath 文件路径
|
|
8
|
+
* @param defaultValue 文件不存在时默认值, 不传则抛异常, 如果传递的是对象形式则会将结果转换为 JSON
|
|
9
|
+
* @returns 文件内容
|
|
10
|
+
*/
|
|
11
|
+
export declare function read<T>(filepath: string, defaultValue?: T): Promise<T>;
|
|
12
|
+
/**
|
|
13
|
+
* 写入 JSON 格式的数据到文件
|
|
14
|
+
* @param file 待写入的文件
|
|
15
|
+
* @param data 待写入的数据
|
|
16
|
+
* @param opts 写入配置
|
|
17
|
+
* @property opts.json 是否写入 JSON 格式的数据,写入数据时对数据进行 JSON 格式化,默认为:true
|
|
18
|
+
* @property opts.format 是否在写入 json 数据时,将 JSON 数据格式化2个空格写入, 默认为 true
|
|
19
|
+
*/
|
|
20
|
+
export declare function write(file: string, data: any, opts?: {
|
|
21
|
+
json: boolean;
|
|
22
|
+
format: boolean;
|
|
23
|
+
}): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* 根据文件的 stat 获取文件的 etag
|
|
26
|
+
* @param filePath 文件地址
|
|
27
|
+
* @returns file stat etag
|
|
28
|
+
*/
|
|
29
|
+
export declare function statTag(filePath: string): Promise<string>;
|