@d-matrix/utils 1.10.0 → 1.12.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/dist/clipboard.d.ts +11 -0
- package/dist/clipboard.js +46 -0
- package/dist/date.d.ts +19 -11
- package/dist/date.js +29 -10
- package/package.json +3 -1
- package/readme.md +30 -19
package/dist/clipboard.d.ts
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 复制图片到剪贴板
|
|
3
|
+
* @param element
|
|
4
|
+
* @returns
|
|
5
|
+
*/
|
|
1
6
|
export declare function writeImage(element: HTMLImageElement | null | string): Promise<unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* 复制文本到剪贴板
|
|
9
|
+
* @param text
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export declare function writeText(text: string): Promise<void>;
|
package/dist/clipboard.js
CHANGED
|
@@ -7,6 +7,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* 复制图片到剪贴板
|
|
12
|
+
* @param element
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
10
15
|
export function writeImage(element) {
|
|
11
16
|
return __awaiter(this, void 0, void 0, function* () {
|
|
12
17
|
return new Promise((resolve, reject) => {
|
|
@@ -58,3 +63,44 @@ export function writeImage(element) {
|
|
|
58
63
|
});
|
|
59
64
|
});
|
|
60
65
|
}
|
|
66
|
+
const legacyWriteText = (text) => {
|
|
67
|
+
const $textarea = document.createElement('textarea');
|
|
68
|
+
$textarea.value = text;
|
|
69
|
+
$textarea.id = '__textarea_for_clipboard__';
|
|
70
|
+
$textarea.style.cssText = 'position: fixed; width: 20px; height: 20px; opacity: 0; top: -20px;';
|
|
71
|
+
document.body.appendChild($textarea);
|
|
72
|
+
$textarea.select();
|
|
73
|
+
try {
|
|
74
|
+
const isSuccess = document.execCommand('copy');
|
|
75
|
+
if (!isSuccess && process.env.NODE_ENV === 'development') {
|
|
76
|
+
console.error('复制文本失败, 操作不被支持或未被启用');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
if (process.env.NODE_ENV === 'development') {
|
|
81
|
+
console.error('复制文本失败', error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
document.body.removeChild($textarea);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* 复制文本到剪贴板
|
|
90
|
+
* @param text
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
export function writeText(text) {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
if (!navigator.clipboard) {
|
|
96
|
+
legacyWriteText(text);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
return navigator.clipboard.writeText(text).catch((error) => {
|
|
100
|
+
if (process.env.NODE_ENV === 'development') {
|
|
101
|
+
console.error('复制文本失败', error);
|
|
102
|
+
}
|
|
103
|
+
return Promise.reject(error);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
package/dist/date.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
export interface RecentYearOption {
|
|
2
|
+
label: string;
|
|
3
|
+
value: number;
|
|
4
|
+
}
|
|
1
5
|
/**
|
|
2
6
|
* Generates an array of numbers representing a range of years between the start year and the end year.
|
|
3
7
|
*
|
|
@@ -5,16 +9,20 @@
|
|
|
5
9
|
* @param {number} end - the ending year of the range (defaults to the current year)
|
|
6
10
|
* @return {number[]} an array of numbers representing the range of years
|
|
7
11
|
*/
|
|
8
|
-
export declare
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
export declare function rangeOfYears(start: number, end?: number): number[];
|
|
13
|
+
export declare type GetRecentYearsOptions = {
|
|
14
|
+
startYear?: number;
|
|
15
|
+
recentYears?: number;
|
|
16
|
+
endYear?: number;
|
|
17
|
+
suffix?: string;
|
|
18
|
+
};
|
|
13
19
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @param
|
|
16
|
-
* @param type 控制返回值类型
|
|
17
|
-
* @param suffix 后缀,默认为'年'
|
|
20
|
+
* 获取n年, 从大到小
|
|
21
|
+
* @param options
|
|
18
22
|
*/
|
|
19
|
-
export declare function
|
|
20
|
-
|
|
23
|
+
export declare function getYears(options: GetRecentYearsOptions & {
|
|
24
|
+
type: 'number[]';
|
|
25
|
+
}): number[];
|
|
26
|
+
export declare function getYears(options: GetRecentYearsOptions & {
|
|
27
|
+
type: 'object[]';
|
|
28
|
+
}): RecentYearOption[];
|
package/dist/date.js
CHANGED
|
@@ -5,24 +5,43 @@
|
|
|
5
5
|
* @param {number} end - the ending year of the range (defaults to the current year)
|
|
6
6
|
* @return {number[]} an array of numbers representing the range of years
|
|
7
7
|
*/
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
export function rangeOfYears(start, end = new Date().getFullYear()) {
|
|
9
|
+
return Array(end - start + 1)
|
|
10
|
+
.fill(start)
|
|
11
|
+
.map((year, index) => year + index);
|
|
12
|
+
}
|
|
13
|
+
export function getYears(options) {
|
|
14
|
+
const { recentYears = 0, startYear, endYear, suffix = '年', type } = options;
|
|
15
|
+
const endY = endYear ? endYear : new Date().getFullYear();
|
|
16
|
+
let ranges = recentYears;
|
|
17
|
+
if (typeof startYear === 'number') {
|
|
18
|
+
// 包含startYear
|
|
19
|
+
ranges = endY - startYear + 1;
|
|
20
|
+
if (ranges <= 0) {
|
|
21
|
+
if (process.env.NODE_ENV === 'development') {
|
|
22
|
+
if (endYear === undefined) {
|
|
23
|
+
console.error('startYear不能大于当前年份');
|
|
24
|
+
}
|
|
25
|
+
else if (typeof endYear === 'number') {
|
|
26
|
+
console.error('endYear不能小于startYear');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
13
32
|
if (type === 'number[]') {
|
|
14
33
|
const result = [];
|
|
15
|
-
for (let i = 0; i <
|
|
16
|
-
result.push(
|
|
34
|
+
for (let i = 0; i < ranges; i++) {
|
|
35
|
+
result.push(endY - i);
|
|
17
36
|
}
|
|
18
37
|
return result;
|
|
19
38
|
}
|
|
20
39
|
if (type === 'object[]') {
|
|
21
40
|
const result = [];
|
|
22
|
-
for (let i = 0; i <
|
|
41
|
+
for (let i = 0; i < ranges; i++) {
|
|
23
42
|
result.push({
|
|
24
|
-
value:
|
|
25
|
-
label: `${
|
|
43
|
+
value: endY - i,
|
|
44
|
+
label: `${endY - i}${suffix}`,
|
|
26
45
|
});
|
|
27
46
|
}
|
|
28
47
|
return result;
|
package/package.json
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-matrix/utils",
|
|
3
3
|
"sideEffects": false,
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.12.0",
|
|
5
5
|
"description": "A dozen of utils for Front-End Development",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
9
|
"prebuild": "npm run clean",
|
|
10
|
+
"postpublish": "echo \"wait for 3 seconds, then sync cnpm\" && npm run wait3s && npm run cnpm:sync",
|
|
10
11
|
"clean": "rimraf dist",
|
|
11
12
|
"cy:open": "cypress open",
|
|
12
13
|
"cy:component": "cypress run --component --spec",
|
|
13
14
|
"cy:component:all": "cypress run --component",
|
|
14
15
|
"test:tsd": "tsd",
|
|
15
16
|
"postversion": "git push && git push --tags",
|
|
17
|
+
"wait3s": "node -e \"setTimeout(() => process.exit(0), 3000)\"",
|
|
16
18
|
"cnpm:sync": "cnpm sync %npm_package_name%"
|
|
17
19
|
},
|
|
18
20
|
"engines": {
|
package/readme.md
CHANGED
|
@@ -20,9 +20,13 @@ A dozen of utils for Front-End Development
|
|
|
20
20
|
|
|
21
21
|
### clipboard
|
|
22
22
|
|
|
23
|
-
- `
|
|
23
|
+
- `writeImage(element: HTMLImageElement | null | string): Promise<void>`
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
复制图片到剪贴板
|
|
26
|
+
|
|
27
|
+
- `writeText(text: string): Promise<void>`
|
|
28
|
+
|
|
29
|
+
复制文本到剪切板
|
|
26
30
|
|
|
27
31
|
### react
|
|
28
32
|
|
|
@@ -143,30 +147,37 @@ dom.strip('测试<em>高亮</em>测试'); // '测试高亮测试'
|
|
|
143
147
|
|
|
144
148
|
创建`start`和`end`之间的年份数组。
|
|
145
149
|
|
|
146
|
-
- `
|
|
150
|
+
- `getYears()`
|
|
147
151
|
|
|
148
152
|
```ts
|
|
149
|
-
export
|
|
150
|
-
|
|
151
|
-
|
|
153
|
+
export type GetRecentYearsOptions = {
|
|
154
|
+
// 开始年份
|
|
155
|
+
startYear?: number;
|
|
156
|
+
// 最近几年
|
|
157
|
+
recentYears?: number;
|
|
158
|
+
// 截止年份
|
|
159
|
+
endYear?: number;
|
|
160
|
+
// 后缀,默认为'年'
|
|
161
|
+
suffix?: string;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export function getYears(options: GetRecentYearsOptions & { type: 'number[]' }): number[];
|
|
165
|
+
export function getYears(options: GetRecentYearsOptions & { type: 'object[]' }): RecentYearOption[];
|
|
166
|
+
export function getYears(options: GetRecentYearsOptions & { type: 'object[]' | 'number[]' }): number[] | RecentYearOption[]
|
|
152
167
|
```
|
|
153
168
|
|
|
154
|
-
|
|
169
|
+
获取n年,`type`传`number[]`,返回`[2023, 2022, 2021]`数字数组;`type`传`object[]`,返回如下的对象数组
|
|
155
170
|
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
// { value: 2024, label: '2024年' },
|
|
163
|
-
// { value: 2023, label: '2023年' },
|
|
164
|
-
// { value: 2022, label: '2022年' },
|
|
165
|
-
// { value: 2021, label: '2021年' },
|
|
166
|
-
// { value: 2020, label: '2020年' },
|
|
167
|
-
// ]
|
|
171
|
+
```sh
|
|
172
|
+
[
|
|
173
|
+
{ value: 2023, label: '2023年' },
|
|
174
|
+
{ value: 2022, label: '2022年' },
|
|
175
|
+
{ value: 2021, label: '2021年' },
|
|
176
|
+
]
|
|
168
177
|
```
|
|
169
178
|
|
|
179
|
+
更多用法,见[测试用例](./tests/date.cy.ts)
|
|
180
|
+
|
|
170
181
|
### types
|
|
171
182
|
|
|
172
183
|
- `WithOptional<T, K extends keyof T>`
|