@fe-free/tool 2.2.7 → 2.2.9

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @fe-free/tool
2
2
 
3
+ ## 2.2.9
4
+
5
+ ### Patch Changes
6
+
7
+ - fix: copy
8
+
9
+ ## 2.2.8
10
+
3
11
  ## 2.2.7
4
12
 
5
13
  ## 2.2.6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fe-free/tool",
3
- "version": "2.2.7",
3
+ "version": "2.2.9",
4
4
  "description": "",
5
5
  "main": "./src/index.ts",
6
6
  "author": "",
package/src/copy.ts CHANGED
@@ -1,5 +1,21 @@
1
- function copyToClipboard(text) {
2
- return navigator.clipboard.writeText(text);
1
+ async function copyToClipboard(text) {
2
+ // 优先尝试 navigator.clipboard
3
+ if (navigator.clipboard && window.isSecureContext) {
4
+ await navigator.clipboard.writeText(text);
5
+ return;
6
+ }
7
+
8
+ // 降级方案:document.execCommand
9
+ const textArea = document.createElement('textarea');
10
+ textArea.value = text;
11
+ textArea.style.position = 'fixed'; // 避免滚动
12
+ textArea.style.opacity = '0';
13
+ document.body.appendChild(textArea);
14
+ textArea.select();
15
+
16
+ document.execCommand('copy');
17
+
18
+ document.body.removeChild(textArea);
3
19
  }
4
20
 
5
21
  export { copyToClipboard };