@fe-free/tool 2.2.8 → 2.2.10
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 +8 -0
- package/package.json +1 -1
- package/src/copy.ts +18 -2
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/copy.ts
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
|
-
function copyToClipboard(text) {
|
|
2
|
-
|
|
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 };
|