ph-utils 0.17.1 → 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/README.md +7 -7
- package/lib/dom.js +21 -19
- package/lib/theme.js +3 -9
- package/package.json +2 -2
- package/LICENSE +0 -21
- package/lib/clipboard.d.ts +0 -11
- package/lib/clipboard.js +0 -101
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
## ph-utils
|
|
2
|
-
|
|
3
|
-
整理了 js 前后端开发(web + nodejs)时常用的一些工具;[详细文档](https://gitee.com/towardly/ph/wikis/Home?sort_id=4035190)
|
|
4
|
-
|
|
5
|
-
### 包含如下工具文件
|
|
6
|
-
|
|
7
|
-
`index` 基础工具类、`date` 跟日期相关的工具类、`file` 文件操作相关工具类[**服务端**]、`server` 服务端工具类、`validator` 数据验证、`dom` 浏览器节点操作相关[**前端**]、`web` 一些只适用于前端相关的工具、`color` 颜色相关工具
|
|
1
|
+
## ph-utils
|
|
2
|
+
|
|
3
|
+
整理了 js 前后端开发(web + nodejs)时常用的一些工具;[详细文档](https://gitee.com/towardly/ph/wikis/Home?sort_id=4035190)
|
|
4
|
+
|
|
5
|
+
### 包含如下工具文件
|
|
6
|
+
|
|
7
|
+
`index` 基础工具类、`date` 跟日期相关的工具类、`file` 文件操作相关工具类[**服务端**]、`server` 服务端工具类、`validator` 数据验证、`dom` 浏览器节点操作相关[**前端**]、`web` 一些只适用于前端相关的工具、`color` 颜色相关工具
|
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
|
}
|
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
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2021 Tenny
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/lib/clipboard.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 复制数据, 可以从多种类型的数据
|
|
3
|
-
* 1. 直接复制文本: await copy("待复制的文本")
|
|
4
|
-
* 2. 复制节点上的 data-copy-text:
|
|
5
|
-
* <button data-copy-text="这是待复制的文本">复制</button>
|
|
6
|
-
* await copy(e.target) // or await copy("#a") or await copy(document.querySelector('#a'))
|
|
7
|
-
* 3. 直接复制节点本身数据: await copy('#a')
|
|
8
|
-
* @param {string | HTMLElement} source 复制源, 从中解析待复制的数据
|
|
9
|
-
* @returns {Promise<boolean>} 是否复制成功
|
|
10
|
-
*/
|
|
11
|
-
export declare function copy(source: string | HTMLElement): Promise<boolean>;
|
package/lib/clipboard.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 创建一个临时节点缓存待复制数据
|
|
3
|
-
* @param {String} value - 待复制文本
|
|
4
|
-
* @return {HTMLElement}
|
|
5
|
-
*/
|
|
6
|
-
function createFakeElement(value) {
|
|
7
|
-
const fakeElement = document.createElement("textarea");
|
|
8
|
-
fakeElement.style.border = "0";
|
|
9
|
-
fakeElement.style.padding = "0";
|
|
10
|
-
fakeElement.style.margin = "0";
|
|
11
|
-
fakeElement.style.position = "absolute";
|
|
12
|
-
fakeElement.style.left = "-9999px";
|
|
13
|
-
fakeElement.style.top = "-9999";
|
|
14
|
-
fakeElement.setAttribute("readonly", "");
|
|
15
|
-
fakeElement.value = value;
|
|
16
|
-
return fakeElement;
|
|
17
|
-
}
|
|
18
|
-
/** 通过执行 execCommand 来执行复制 */
|
|
19
|
-
function copyFromCommand(text) {
|
|
20
|
-
// 添加节点
|
|
21
|
-
const fakeEl = createFakeElement(text);
|
|
22
|
-
document.body.append(fakeEl);
|
|
23
|
-
fakeEl.focus();
|
|
24
|
-
fakeEl.select();
|
|
25
|
-
// 执行复制
|
|
26
|
-
const res = document.execCommand("copy");
|
|
27
|
-
fakeEl.remove(); // 删除节点
|
|
28
|
-
return Promise.resolve(res);
|
|
29
|
-
}
|
|
30
|
-
/** 使用 navigator.clipboard 复制 */
|
|
31
|
-
function copyFromClipboard(text) {
|
|
32
|
-
const theClipboard = navigator.clipboard;
|
|
33
|
-
if (theClipboard != null) {
|
|
34
|
-
return theClipboard
|
|
35
|
-
.writeText(text)
|
|
36
|
-
.then(() => {
|
|
37
|
-
Promise.resolve(true);
|
|
38
|
-
})
|
|
39
|
-
.catch(() => Promise.resolve(false));
|
|
40
|
-
}
|
|
41
|
-
return Promise.resolve(false);
|
|
42
|
-
}
|
|
43
|
-
/** 解析待复制的文本 */
|
|
44
|
-
function parseCopyText(source) {
|
|
45
|
-
let copyText = null; // 待复制文本
|
|
46
|
-
let sourceEl = null;
|
|
47
|
-
// 获取待复制数据
|
|
48
|
-
if (typeof source === "string") {
|
|
49
|
-
// 从节点拿数据
|
|
50
|
-
if (source.startsWith("#") || source.startsWith(".")) {
|
|
51
|
-
sourceEl = document.querySelector(source);
|
|
52
|
-
if (sourceEl == null) {
|
|
53
|
-
copyText = source;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
copyText = source;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (source instanceof HTMLElement) {
|
|
61
|
-
sourceEl = source;
|
|
62
|
-
}
|
|
63
|
-
// 从节点获取待复制数据
|
|
64
|
-
if (sourceEl != null) {
|
|
65
|
-
if (sourceEl.hasAttribute("data-copy-text")) {
|
|
66
|
-
copyText = sourceEl.getAttribute("data-copy-text");
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
const tagName = sourceEl.tagName;
|
|
70
|
-
if (tagName === "INPUT" || tagName === "TEXTAREA") {
|
|
71
|
-
copyText = sourceEl.value;
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
copyText = sourceEl.textContent;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return copyText;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* 复制数据, 可以从多种类型的数据
|
|
82
|
-
* 1. 直接复制文本: await copy("待复制的文本")
|
|
83
|
-
* 2. 复制节点上的 data-copy-text:
|
|
84
|
-
* <button data-copy-text="这是待复制的文本">复制</button>
|
|
85
|
-
* await copy(e.target) // or await copy("#a") or await copy(document.querySelector('#a'))
|
|
86
|
-
* 3. 直接复制节点本身数据: await copy('#a')
|
|
87
|
-
* @param {string | HTMLElement} source 复制源, 从中解析待复制的数据
|
|
88
|
-
* @returns {Promise<boolean>} 是否复制成功
|
|
89
|
-
*/
|
|
90
|
-
export async function copy(source) {
|
|
91
|
-
// 待复制文本
|
|
92
|
-
const copyText = parseCopyText(source);
|
|
93
|
-
if (copyText == null) {
|
|
94
|
-
return Promise.resolve(false);
|
|
95
|
-
}
|
|
96
|
-
const v = await copyFromClipboard(copyText);
|
|
97
|
-
if (v === false) {
|
|
98
|
-
return copyFromCommand(copyText);
|
|
99
|
-
}
|
|
100
|
-
return Promise.resolve(true);
|
|
101
|
-
}
|