@d-matrix/utils 1.11.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.
@@ -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/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.11.0",
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
- - `clipboard.writeImage(element: HTMLImageElement | null | string): void`
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,7 +147,7 @@ dom.strip('测试<em>高亮</em>测试'); // '测试高亮测试'
143
147
 
144
148
  创建`start`和`end`之间的年份数组。
145
149
 
146
- - `getRecentYears()`
150
+ - `getYears()`
147
151
 
148
152
  ```ts
149
153
  export type GetRecentYearsOptions = {