fix(clipboard): add fallback helper and unify copy actions

This commit is contained in:
Supra4E8C
2026-02-14 03:25:33 +08:00
parent 431ec1e0f5
commit 3a7ddfdff1
5 changed files with 38 additions and 60 deletions

View File

@@ -1,28 +0,0 @@
export const copyToClipboard = async (text: string): Promise<boolean> => {
try {
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(text);
return true;
}
} catch {
// fallback below
}
try {
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
textarea.style.left = '-9999px';
textarea.style.top = '0';
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
const copied = document.execCommand('copy');
document.body.removeChild(textarea);
return copied;
} catch {
return false;
}
};