feat(FN-4951): complete Step 1 — add shared clipboard utility
Fusion-Task-Id: FN-4951 Fusion-Task-Lineage: 25de66db-c8cf-463b-86f2-efff1bc22683
This commit is contained in:
41
packages/dashboard/app/utils/copyToClipboard.ts
Normal file
41
packages/dashboard/app/utils/copyToClipboard.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
export async function copyTextToClipboard(text: string): Promise<boolean> {
|
||||
if (
|
||||
typeof navigator !== "undefined" &&
|
||||
typeof window !== "undefined" &&
|
||||
window.isSecureContext !== false &&
|
||||
navigator.clipboard?.writeText
|
||||
) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return true;
|
||||
} catch {
|
||||
// Fall through to execCommand fallback.
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof document === "undefined" || !document.body) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.setAttribute("readonly", "");
|
||||
textarea.style.position = "fixed";
|
||||
textarea.style.left = "-9999px";
|
||||
textarea.style.top = "0";
|
||||
textarea.style.opacity = "0";
|
||||
textarea.style.pointerEvents = "none";
|
||||
|
||||
document.body.appendChild(textarea);
|
||||
|
||||
try {
|
||||
textarea.focus();
|
||||
textarea.select();
|
||||
textarea.setSelectionRange(0, text.length);
|
||||
return document.execCommand("copy");
|
||||
} catch {
|
||||
return false;
|
||||
} finally {
|
||||
textarea.remove();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user