浏览代码

fix: 创建 clipboard.ts 工具函数文件

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
mengboxin137-blip 6 小时之前
父节点
当前提交
200df6e383
共有 1 个文件被更改,包括 17 次插入0 次删除
  1. 17 0
      frontend/utils/clipboard.ts

+ 17 - 0
frontend/utils/clipboard.ts

@@ -0,0 +1,17 @@
+export async function copyToClipboard(text: string): Promise<boolean> {
+  try {
+    if (navigator.clipboard?.writeText) {
+      await navigator.clipboard.writeText(text);
+      return true;
+    }
+  } catch {}
+  const textarea = document.createElement('textarea');
+  textarea.value = text;
+  textarea.style.position = 'fixed';
+  textarea.style.opacity = '0';
+  document.body.appendChild(textarea);
+  textarea.select();
+  const ok = document.execCommand('copy');
+  document.body.removeChild(textarea);
+  return ok;
+}