ph-utils 0.16.5 → 0.17.2
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/lib/date.js +4 -20
- package/lib/dom.d.ts +28 -1
- package/lib/dom.js +68 -19
- package/lib/theme.js +3 -9
- package/package.json +99 -99
package/lib/date.js
CHANGED
|
@@ -12,17 +12,11 @@ const ofArgs = {
|
|
|
12
12
|
const units = {
|
|
13
13
|
Date: ["date", "Date", "day", "Day", "D", "d"],
|
|
14
14
|
Month: ["Month", "month", "m"],
|
|
15
|
-
|
|
15
|
+
FullYear: ["Year", "year", "y"],
|
|
16
16
|
Hours: ["Hours", "hours", "H"],
|
|
17
17
|
Minutes: ["Minutes", "Minute", "minute", "minutes", "M"],
|
|
18
18
|
Seconds: ["Seconds", "seconds", "Second", "second", "s"],
|
|
19
|
-
Milliseconds: [
|
|
20
|
-
"Milliseconds",
|
|
21
|
-
"Millisecond",
|
|
22
|
-
"milliseconds",
|
|
23
|
-
"illisecond",
|
|
24
|
-
"S",
|
|
25
|
-
],
|
|
19
|
+
Milliseconds: ["Milliseconds", "Millisecond", "milliseconds", "illisecond", "S"],
|
|
26
20
|
};
|
|
27
21
|
/**
|
|
28
22
|
* 不足位数, 前位补 0
|
|
@@ -135,15 +129,7 @@ export function parse(date) {
|
|
|
135
129
|
*/
|
|
136
130
|
export function dateOf(date, unit, isEnd = false) {
|
|
137
131
|
/* 如果是设置某一天的开始时刻, 就需要将时、分、秒、毫秒设置为0,依次类推设置 */
|
|
138
|
-
const periods = [
|
|
139
|
-
"Year",
|
|
140
|
-
"Month",
|
|
141
|
-
"Date",
|
|
142
|
-
"Hours",
|
|
143
|
-
"Minutes",
|
|
144
|
-
"Seconds",
|
|
145
|
-
"Milliseconds",
|
|
146
|
-
];
|
|
132
|
+
const periods = ["Year", "Month", "Date", "Hours", "Minutes", "Seconds", "Milliseconds"];
|
|
147
133
|
let index = periods.indexOf(getUnit(unit || "Date"));
|
|
148
134
|
const clone = parse(date);
|
|
149
135
|
index++;
|
|
@@ -154,9 +140,7 @@ export function dateOf(date, unit, isEnd = false) {
|
|
|
154
140
|
// 设置为某个月的最后一天的日期
|
|
155
141
|
value = getLastDayOfMonth(clone);
|
|
156
142
|
}
|
|
157
|
-
Date.prototype["set" + periods[index]].apply(clone, [
|
|
158
|
-
setValues[index],
|
|
159
|
-
]);
|
|
143
|
+
Date.prototype["set" + periods[index]].apply(clone, [setValues[index]]);
|
|
160
144
|
}
|
|
161
145
|
return clone;
|
|
162
146
|
}
|
package/lib/dom.d.ts
CHANGED
|
@@ -119,7 +119,7 @@ export declare function html(element: HTMLElement, htmlstr?: string): string | u
|
|
|
119
119
|
* @param textstr 可选,如果传递该参数,则表示设置;否则表示获取
|
|
120
120
|
* @returns
|
|
121
121
|
*/
|
|
122
|
-
export declare function text(element: HTMLElement, textstr?: string): string | undefined;
|
|
122
|
+
export declare function text(element: HTMLElement, textstr?: string): string | undefined | null;
|
|
123
123
|
export declare function iterate<T>(elems: T[], fn: (el: T, index: number) => any): void;
|
|
124
124
|
export declare function iterate(elems: NodeList | HTMLCollection | NodeListOf<HTMLElement>, fn: (el: HTMLElement, index: number) => any): void;
|
|
125
125
|
/**
|
|
@@ -222,4 +222,31 @@ export declare function formatStyle(styleObj: FormatStyleParam): string;
|
|
|
222
222
|
* transition($el, [["opacity", "0", "0.3s"]], "leave", () => { $el.remove(); });
|
|
223
223
|
*/
|
|
224
224
|
export declare function transition(el: HTMLElement, nameOrProperties: string | [string, string, string][], dir?: "leave" | "enter", finish?: () => void): void;
|
|
225
|
+
/**
|
|
226
|
+
* 计算光标位置
|
|
227
|
+
*
|
|
228
|
+
* 根据输入值的变化计算新的光标位置,主要用于输入框内容变化时的光标位置调整
|
|
229
|
+
*
|
|
230
|
+
* @param start - 光标起始位置,通常为: input.selectionStart
|
|
231
|
+
* @param end - 光标结束位置(通常与起始位置相同,用于选择范围),通常为: input.selectionEnd
|
|
232
|
+
* @param oldValue - 变化前的值
|
|
233
|
+
* @param newValue - 变化后的值
|
|
234
|
+
* @returns 新的光标位置对象,包含新的起始和结束位置
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* // 删除字符时,光标向前移动
|
|
238
|
+
* calcuteCursorPosition(2, 2, "abc", "ac"); // 返回 { start: 1, end: 1 }
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* // 添加字符时,光标向后移动
|
|
242
|
+
* calcuteCursorPosition(2, 2, "ac", "abc"); // 返回 { start: 3, end: 3 }
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* // 替换字符时,保持位置
|
|
246
|
+
* calcuteCursorPosition(2, 2, "abc", "axc"); // 返回 { start: 2, end: 2 }
|
|
247
|
+
*/
|
|
248
|
+
export declare function calcuteCursorPosition(start: number, end: number, oldValue: string, newValue: string): {
|
|
249
|
+
start: number;
|
|
250
|
+
end: number;
|
|
251
|
+
};
|
|
225
252
|
export {};
|
package/lib/dom.js
CHANGED
|
@@ -36,27 +36,29 @@ export function create(tag, option = {}, ctx) {
|
|
|
36
36
|
if (option) {
|
|
37
37
|
for (const key in option) {
|
|
38
38
|
const value = option[key];
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
else {
|
|
55
|
-
if (value === true) {
|
|
56
|
-
$el.setAttribute(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;
|
|
57
54
|
}
|
|
58
55
|
else {
|
|
59
|
-
|
|
56
|
+
if (value === true) {
|
|
57
|
+
$el.setAttribute(key, "");
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
$el.setAttribute(key, value);
|
|
61
|
+
}
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
64
|
}
|
|
@@ -550,3 +552,50 @@ export function transition(el, nameOrProperties, dir = "enter", finish) {
|
|
|
550
552
|
}
|
|
551
553
|
}, { once: true });
|
|
552
554
|
}
|
|
555
|
+
/**
|
|
556
|
+
* 计算光标位置
|
|
557
|
+
*
|
|
558
|
+
* 根据输入值的变化计算新的光标位置,主要用于输入框内容变化时的光标位置调整
|
|
559
|
+
*
|
|
560
|
+
* @param start - 光标起始位置,通常为: input.selectionStart
|
|
561
|
+
* @param end - 光标结束位置(通常与起始位置相同,用于选择范围),通常为: input.selectionEnd
|
|
562
|
+
* @param oldValue - 变化前的值
|
|
563
|
+
* @param newValue - 变化后的值
|
|
564
|
+
* @returns 新的光标位置对象,包含新的起始和结束位置
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* // 删除字符时,光标向前移动
|
|
568
|
+
* calcuteCursorPosition(2, 2, "abc", "ac"); // 返回 { start: 1, end: 1 }
|
|
569
|
+
*
|
|
570
|
+
* @example
|
|
571
|
+
* // 添加字符时,光标向后移动
|
|
572
|
+
* calcuteCursorPosition(2, 2, "ac", "abc"); // 返回 { start: 3, end: 3 }
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* // 替换字符时,保持位置
|
|
576
|
+
* calcuteCursorPosition(2, 2, "abc", "axc"); // 返回 { start: 2, end: 2 }
|
|
577
|
+
*/
|
|
578
|
+
export function calcuteCursorPosition(start, end, oldValue, newValue) {
|
|
579
|
+
// 5. 【关键】计算新光标位置并恢复
|
|
580
|
+
let newStart = start;
|
|
581
|
+
let newEnd = end;
|
|
582
|
+
// 简单策略:如果新值比旧值短,说明删了字符,光标前移
|
|
583
|
+
// 更健壮的做法:对比差异,但通常可简化处理
|
|
584
|
+
if (newValue.length < oldValue.length) {
|
|
585
|
+
// 例如:用户在中间删了一个非法字符
|
|
586
|
+
newStart = Math.max(0, start - (oldValue.length - newValue.length));
|
|
587
|
+
newEnd = newStart;
|
|
588
|
+
}
|
|
589
|
+
else if (newValue.length > oldValue.length) {
|
|
590
|
+
// 插入合法字符,光标通常就在插入点后,可保持原偏移
|
|
591
|
+
// 但需防止超出长度
|
|
592
|
+
newStart = Math.min(newValue.length, start + (newValue.length - oldValue.length));
|
|
593
|
+
newEnd = newStart;
|
|
594
|
+
}
|
|
595
|
+
else {
|
|
596
|
+
// 长度不变(如替换),保持原位置(但要限制范围)
|
|
597
|
+
newStart = Math.min(newValue.length, start);
|
|
598
|
+
newEnd = newStart;
|
|
599
|
+
}
|
|
600
|
+
return { start: newStart, end: newEnd };
|
|
601
|
+
}
|
package/lib/theme.js
CHANGED
|
@@ -19,11 +19,8 @@ export async function initTheme() {
|
|
|
19
19
|
if ($themeStyle == null) {
|
|
20
20
|
$themeStyle = document.createElement("style");
|
|
21
21
|
$themeStyle.id = "theme-style";
|
|
22
|
-
$themeStyle.innerHTML =
|
|
23
|
-
|
|
24
|
-
html.light{color-scheme: light;}
|
|
25
|
-
html.dark {color-scheme: dark;}
|
|
26
|
-
`;
|
|
22
|
+
$themeStyle.innerHTML =
|
|
23
|
+
":root{color-scheme:light dark;}html.light{color-scheme: light;}html.dark {color-scheme: dark;}";
|
|
27
24
|
document.head.appendChild($themeStyle);
|
|
28
25
|
}
|
|
29
26
|
// 获取已经应用的主题设置
|
|
@@ -103,7 +100,7 @@ export function getColorTheme(defaultValue) {
|
|
|
103
100
|
const match = root.className.match(/color-([0-9a-fA-F]{6})/);
|
|
104
101
|
if (match == null) {
|
|
105
102
|
// 获取 --nt-primary-color 的值
|
|
106
|
-
let color = getComputedStyle(root).getPropertyValue("--
|
|
103
|
+
let color = getComputedStyle(root).getPropertyValue("--l-primary-color");
|
|
107
104
|
if (color === "") {
|
|
108
105
|
color = localStorage.getItem("web-theme-color");
|
|
109
106
|
}
|
|
@@ -130,13 +127,10 @@ export function initColorTheme() {
|
|
|
130
127
|
*/
|
|
131
128
|
export async function toggleColorTheme(color) {
|
|
132
129
|
const vars = [
|
|
133
|
-
`--nt-primary-color: ${color};`,
|
|
134
130
|
`--l-primary-color: ${color};`,
|
|
135
|
-
`--nt-primary-color-dark1: ${adjust(color, 1, false)};`,
|
|
136
131
|
`--l-primary-color-dark1: ${adjust(color, 1, false)};`,
|
|
137
132
|
];
|
|
138
133
|
for (let i = 1; i <= 5; i++) {
|
|
139
|
-
vars.push(`--nt-primary-color-light${i}: ${adjust(color, i)};`);
|
|
140
134
|
vars.push(`--l-primary-color-light${i}: ${adjust(color, i)};`);
|
|
141
135
|
}
|
|
142
136
|
let $style = document.getElementById("color-theme-style");
|
package/package.json
CHANGED
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ph-utils",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
"
|
|
34
|
-
"types": "./lib/
|
|
35
|
-
"import": "./lib/
|
|
36
|
-
},
|
|
37
|
-
"./
|
|
38
|
-
"types": "./lib/
|
|
39
|
-
"import": "./lib/
|
|
40
|
-
},
|
|
41
|
-
"./
|
|
42
|
-
"types": "./lib/
|
|
43
|
-
"import": "./lib/
|
|
44
|
-
},
|
|
45
|
-
"./
|
|
46
|
-
"types": "./lib/
|
|
47
|
-
"import": "./lib/
|
|
48
|
-
},
|
|
49
|
-
"./
|
|
50
|
-
"types": "./lib/
|
|
51
|
-
"import": "./lib/
|
|
52
|
-
},
|
|
53
|
-
"./
|
|
54
|
-
"types": "./lib/
|
|
55
|
-
"import": "./lib/
|
|
56
|
-
},
|
|
57
|
-
"./
|
|
58
|
-
"types": "./lib/
|
|
59
|
-
"import": "./lib/
|
|
60
|
-
},
|
|
61
|
-
"./
|
|
62
|
-
"types": "./lib/
|
|
63
|
-
"import": "./lib/
|
|
64
|
-
},
|
|
65
|
-
"./
|
|
66
|
-
"types": "./lib/
|
|
67
|
-
"import": "./lib/
|
|
68
|
-
},
|
|
69
|
-
"
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
"
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
"
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
"
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
"
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
|
|
99
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "ph-utils",
|
|
3
|
+
"version": "0.17.2",
|
|
4
|
+
"description": "js 开发工具集,前后端都可以使用(commonjs和es module)",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"date",
|
|
7
|
+
"dom",
|
|
8
|
+
"file",
|
|
9
|
+
"javascript",
|
|
10
|
+
"node",
|
|
11
|
+
"utils"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://gitee.com/towardly/ph/tree/master/packages/utils",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://gitee.com/towardly/ph/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "Tenny <tenny.shu@foxmail.com>",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https//gitee.com/towardly/ph.git",
|
|
22
|
+
"directory": "packages/utils"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"lib"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"main": "lib/index.js",
|
|
29
|
+
"module": "lib/index.js",
|
|
30
|
+
"browser": "lib/index.js",
|
|
31
|
+
"types": "lib/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./lib/index.d.ts",
|
|
35
|
+
"import": "./lib/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./file": {
|
|
38
|
+
"types": "./lib/file.d.ts",
|
|
39
|
+
"import": "./lib/file.js"
|
|
40
|
+
},
|
|
41
|
+
"./date": {
|
|
42
|
+
"types": "./lib/date.d.ts",
|
|
43
|
+
"import": "./lib/date.js"
|
|
44
|
+
},
|
|
45
|
+
"./server": {
|
|
46
|
+
"types": "./lib/server.d.ts",
|
|
47
|
+
"import": "./lib/server.js"
|
|
48
|
+
},
|
|
49
|
+
"./validator": {
|
|
50
|
+
"types": "./lib/validator.d.ts",
|
|
51
|
+
"import": "./lib/validator.js"
|
|
52
|
+
},
|
|
53
|
+
"./crypto": {
|
|
54
|
+
"types": "./lib/crypto.d.ts",
|
|
55
|
+
"import": "./lib/crypto.js"
|
|
56
|
+
},
|
|
57
|
+
"./crypto_node": {
|
|
58
|
+
"types": "./lib/crypto_node.d.ts",
|
|
59
|
+
"import": "./lib/crypto_node.js"
|
|
60
|
+
},
|
|
61
|
+
"./web": {
|
|
62
|
+
"types": "./lib/web.d.ts",
|
|
63
|
+
"import": "./lib/web.js"
|
|
64
|
+
},
|
|
65
|
+
"./dom": {
|
|
66
|
+
"types": "./lib/dom.d.ts",
|
|
67
|
+
"import": "./lib/dom.js"
|
|
68
|
+
},
|
|
69
|
+
"./copy": {
|
|
70
|
+
"types": "./lib/copy.d.ts",
|
|
71
|
+
"import": "./lib/copy.js"
|
|
72
|
+
},
|
|
73
|
+
"./storage": {
|
|
74
|
+
"types": "./lib/storage.d.ts",
|
|
75
|
+
"import": "./lib/storage.js"
|
|
76
|
+
},
|
|
77
|
+
"./color": {
|
|
78
|
+
"types": "./lib/color.d.ts",
|
|
79
|
+
"import": "./lib/color.js"
|
|
80
|
+
},
|
|
81
|
+
"./logger": {
|
|
82
|
+
"types": "./lib/logger.d.ts",
|
|
83
|
+
"import": "./lib/logger.js"
|
|
84
|
+
},
|
|
85
|
+
"./array": {
|
|
86
|
+
"types": "./lib/array.d.ts",
|
|
87
|
+
"import": "./lib/array.js"
|
|
88
|
+
},
|
|
89
|
+
"./theme": {
|
|
90
|
+
"types": "./lib/theme.d.ts",
|
|
91
|
+
"import": "./lib/theme.js"
|
|
92
|
+
},
|
|
93
|
+
"./*": "./lib/*"
|
|
94
|
+
},
|
|
95
|
+
"devDependencies": {
|
|
96
|
+
"@types/node": "^22.8.2",
|
|
97
|
+
"typescript": "^5.6.3"
|
|
98
|
+
}
|
|
99
|
+
}
|