ph-utils 0.8.4 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/array.js +3 -2
- package/lib/theme.d.ts +42 -0
- package/lib/theme.js +139 -0
- package/package.json +5 -1
package/lib/array.js
CHANGED
@@ -6,9 +6,10 @@
|
|
6
6
|
* @returns
|
7
7
|
*/
|
8
8
|
export function order(arr, order = "asc", orderKey = null) {
|
9
|
-
const collator = new Intl.Collator();
|
9
|
+
const collator = new Intl.Collator(undefined, { numeric: true });
|
10
10
|
return arr.sort((a, b) => {
|
11
|
-
let aValue = a
|
11
|
+
let aValue = a;
|
12
|
+
let bValue = b;
|
12
13
|
if (typeof a === "object" && orderKey != null) {
|
13
14
|
// @ts-ignore
|
14
15
|
aValue = a[orderKey];
|
package/lib/theme.d.ts
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
/** 获取当前系统的主题 */
|
2
|
+
export declare function getSystemTheme(): "dark" | "light" | "auto";
|
3
|
+
/**
|
4
|
+
* 初始化主题, 让网页能够适应系统主题, 同时根据缓存的主题切换主题
|
5
|
+
* @returns 当前应用的主题
|
6
|
+
*/
|
7
|
+
export declare function initTheme(): Promise<"dark" | "light" | "auto" | undefined>;
|
8
|
+
/**
|
9
|
+
* 切换主题, 通常用于预览
|
10
|
+
* @param theme 切换的主题
|
11
|
+
* @returns 切换后的主题
|
12
|
+
*/
|
13
|
+
export declare function toggleTheme(theme?: "light" | "dark" | "auto"): Promise<"dark" | "light" | "auto" | undefined>;
|
14
|
+
/** 获取当前主题 */
|
15
|
+
export declare function getTheme(): string;
|
16
|
+
/**
|
17
|
+
* 应用主题
|
18
|
+
* @param theme 待应用的主题
|
19
|
+
* @param cache 是否缓存应用的主题, 让应用下一次启动的时候, 可以应用主题, 默认: true
|
20
|
+
* @returns 应用的主题
|
21
|
+
*/
|
22
|
+
export declare function applyTheme(theme?: "light" | "dark" | "auto", cache?: boolean): Promise<"dark" | "light" | "auto" | undefined>;
|
23
|
+
/** 获取当前主题色 */
|
24
|
+
export declare function getColorTheme(defaultValue?: string): string | undefined;
|
25
|
+
/**
|
26
|
+
* 初始化主题色, 让网页能够适应系统主题色, 同时根据缓存的主题色切换主题色
|
27
|
+
* @returns 当前应用的主题色
|
28
|
+
*/
|
29
|
+
export declare function initColorTheme(): Promise<string> | null;
|
30
|
+
/**
|
31
|
+
* 切换主题色, 通常用于预览
|
32
|
+
* @param color 待切换的主题色
|
33
|
+
* @returns 切换后的主题色
|
34
|
+
*/
|
35
|
+
export declare function toggleColorTheme(color: string): Promise<string>;
|
36
|
+
/**
|
37
|
+
* 应用主题色
|
38
|
+
* @param color 主题色
|
39
|
+
* @param cache 是否缓存主题色, 让应用下一次启动的时候, 可以应用主题色, 默认: true
|
40
|
+
* @returns 切换后的主题色
|
41
|
+
*/
|
42
|
+
export declare function applyColorTheme(color: string, cache?: boolean): Promise<string>;
|
package/lib/theme.js
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
import { adjust } from "./color.js";
|
2
|
+
/** 获取当前系统的主题 */
|
3
|
+
export function getSystemTheme() {
|
4
|
+
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
5
|
+
return "dark";
|
6
|
+
}
|
7
|
+
else if (window.matchMedia("(prefers-color-scheme: light)").matches) {
|
8
|
+
return "light";
|
9
|
+
}
|
10
|
+
return "auto";
|
11
|
+
}
|
12
|
+
/**
|
13
|
+
* 初始化主题, 让网页能够适应系统主题, 同时根据缓存的主题切换主题
|
14
|
+
* @returns 当前应用的主题
|
15
|
+
*/
|
16
|
+
export async function initTheme() {
|
17
|
+
// 让网页能够适应系统主题
|
18
|
+
let $themeStyle = document.getElementById("theme-style");
|
19
|
+
if ($themeStyle == null) {
|
20
|
+
$themeStyle = document.createElement("style");
|
21
|
+
$themeStyle.id = "theme-style";
|
22
|
+
$themeStyle.innerHTML = `
|
23
|
+
:root{color-scheme:light dark;}
|
24
|
+
html.light{color-scheme: light;}
|
25
|
+
html.dark {color-scheme: dark;}
|
26
|
+
`;
|
27
|
+
document.head.appendChild($themeStyle);
|
28
|
+
}
|
29
|
+
// 获取已经应用的主题设置
|
30
|
+
const cacheTheme = localStorage.getItem("web-theme-appearance");
|
31
|
+
return toggleTheme(cacheTheme);
|
32
|
+
}
|
33
|
+
/**
|
34
|
+
* 切换主题, 通常用于预览
|
35
|
+
* @param theme 切换的主题
|
36
|
+
* @returns 切换后的主题
|
37
|
+
*/
|
38
|
+
export async function toggleTheme(theme) {
|
39
|
+
const classList = document.documentElement.classList;
|
40
|
+
if (theme === "light") {
|
41
|
+
classList.add("light");
|
42
|
+
classList.remove("dark");
|
43
|
+
}
|
44
|
+
else if (theme === "dark") {
|
45
|
+
classList.add("dark");
|
46
|
+
classList.remove("light");
|
47
|
+
}
|
48
|
+
else {
|
49
|
+
classList.remove("light", "dark");
|
50
|
+
}
|
51
|
+
return theme;
|
52
|
+
}
|
53
|
+
/** 获取当前主题 */
|
54
|
+
export function getTheme() {
|
55
|
+
// 1. 从根节点获取
|
56
|
+
let theme = document.documentElement.className.match(/light|dark/);
|
57
|
+
if (theme != null) {
|
58
|
+
theme = theme[0];
|
59
|
+
}
|
60
|
+
if (theme == null) {
|
61
|
+
theme = localStorage.getItem("web-theme-appearance");
|
62
|
+
}
|
63
|
+
if (theme == null) {
|
64
|
+
theme = getSystemTheme();
|
65
|
+
}
|
66
|
+
return theme;
|
67
|
+
}
|
68
|
+
/**
|
69
|
+
* 应用主题
|
70
|
+
* @param theme 待应用的主题
|
71
|
+
* @param cache 是否缓存应用的主题, 让应用下一次启动的时候, 可以应用主题, 默认: true
|
72
|
+
* @returns 应用的主题
|
73
|
+
*/
|
74
|
+
export async function applyTheme(theme, cache = true) {
|
75
|
+
if (cache === true) {
|
76
|
+
localStorage.setItem("web-theme-appearance", theme == null ? "auto" : theme);
|
77
|
+
}
|
78
|
+
return toggleTheme(theme);
|
79
|
+
}
|
80
|
+
/** 获取当前主题色 */
|
81
|
+
export function getColorTheme(defaultValue) {
|
82
|
+
const root = document.documentElement;
|
83
|
+
const match = root.className.match(/color-([0-9a-fA-F]{6})/);
|
84
|
+
if (match == null) {
|
85
|
+
// 获取 --nt-primary-color 的值
|
86
|
+
let color = getComputedStyle(root).getPropertyValue("--nt-primary-color");
|
87
|
+
if (color === "") {
|
88
|
+
color = localStorage.getItem("web-theme-color");
|
89
|
+
}
|
90
|
+
return color ? color : defaultValue;
|
91
|
+
}
|
92
|
+
return `#${match[1]}`;
|
93
|
+
}
|
94
|
+
/**
|
95
|
+
* 初始化主题色, 让网页能够适应系统主题色, 同时根据缓存的主题色切换主题色
|
96
|
+
* @returns 当前应用的主题色
|
97
|
+
*/
|
98
|
+
export function initColorTheme() {
|
99
|
+
// 获取缓存主题色
|
100
|
+
const color = localStorage.getItem("web-theme-color");
|
101
|
+
if (color != null) {
|
102
|
+
return toggleColorTheme(color);
|
103
|
+
}
|
104
|
+
return color;
|
105
|
+
}
|
106
|
+
/**
|
107
|
+
* 切换主题色, 通常用于预览
|
108
|
+
* @param color 待切换的主题色
|
109
|
+
* @returns 切换后的主题色
|
110
|
+
*/
|
111
|
+
export async function toggleColorTheme(color) {
|
112
|
+
const vars = [
|
113
|
+
`--nt-primary-color: ${color};`,
|
114
|
+
`--nt-primary-color-dark1: ${adjust(color, 1, false)};`,
|
115
|
+
];
|
116
|
+
for (let i = 1; i <= 5; i++) {
|
117
|
+
vars.push(`--nt-primary-color-light${i}: ${adjust(color, i)};`);
|
118
|
+
}
|
119
|
+
let $style = document.getElementById("color-theme-style");
|
120
|
+
if ($style == null) {
|
121
|
+
$style = document.createElement("style");
|
122
|
+
$style.id = "color-theme-style";
|
123
|
+
document.head.appendChild($style);
|
124
|
+
}
|
125
|
+
$style.innerHTML = `:root{${vars.join("")}}`;
|
126
|
+
return color;
|
127
|
+
}
|
128
|
+
/**
|
129
|
+
* 应用主题色
|
130
|
+
* @param color 主题色
|
131
|
+
* @param cache 是否缓存主题色, 让应用下一次启动的时候, 可以应用主题色, 默认: true
|
132
|
+
* @returns 切换后的主题色
|
133
|
+
*/
|
134
|
+
export function applyColorTheme(color, cache = true) {
|
135
|
+
if (cache === true) {
|
136
|
+
localStorage.setItem("web-theme-color", color.substring(1));
|
137
|
+
}
|
138
|
+
return toggleColorTheme(color);
|
139
|
+
}
|
package/package.json
CHANGED
@@ -58,9 +58,13 @@
|
|
58
58
|
"types": "./lib/array.d.ts",
|
59
59
|
"import": "./lib/array.js"
|
60
60
|
},
|
61
|
+
"./theme": {
|
62
|
+
"types": "./lib/theme.d.ts",
|
63
|
+
"import": "./lib/theme.js"
|
64
|
+
},
|
61
65
|
"./*": "./lib/*"
|
62
66
|
},
|
63
|
-
"version": "0.
|
67
|
+
"version": "0.9.1",
|
64
68
|
"type": "module",
|
65
69
|
"repository": {
|
66
70
|
"type": "git",
|