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

This commit is contained in:
Supra4E8C
2026-02-14 03:25:33 +08:00
Unverified
parent 431ec1e0f5
commit 3a7ddfdff1
5 changed files with 38 additions and 60 deletions
+49
View File
@@ -0,0 +1,49 @@
export async function copyToClipboard(text: string): Promise<boolean> {
try {
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(text);
return true;
}
} catch {
// fallback below
}
try {
if (typeof document === 'undefined') return false;
if (!document.body) return false;
const activeElement = document.activeElement as HTMLElement | null;
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.setAttribute('readonly', '');
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
textarea.style.pointerEvents = 'none';
textarea.style.left = '-9999px';
textarea.style.top = '0';
textarea.style.width = '1px';
textarea.style.height = '1px';
textarea.style.padding = '0';
textarea.style.border = '0';
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
textarea.setSelectionRange(0, textarea.value.length);
const copied = document.execCommand('copy');
document.body.removeChild(textarea);
if (activeElement?.focus) {
try {
activeElement.focus();
} catch {
// ignore
}
}
return copied;
} catch {
return false;
}
}