9b30f681c6
- React+antd+zustand,注册表驱动路由/菜单/权限 - Tauri 桌面壳、后端托管 SPA、跨标签页消息队列
28 lines
731 B
TypeScript
28 lines
731 B
TypeScript
/** Copy text to clipboard with fallback for non-secure contexts. */
|
|
export async function copyText(text: string): Promise<void> {
|
|
if (navigator.clipboard && window.isSecureContext) {
|
|
await navigator.clipboard.writeText(text);
|
|
return;
|
|
}
|
|
|
|
const textarea = document.createElement("textarea");
|
|
textarea.value = text;
|
|
textarea.setAttribute("readonly", "");
|
|
textarea.style.position = "absolute";
|
|
textarea.style.left = "-9999px";
|
|
document.body.appendChild(textarea);
|
|
|
|
let copied = false;
|
|
try {
|
|
textarea.focus();
|
|
textarea.select();
|
|
copied = document.execCommand("copy");
|
|
} finally {
|
|
document.body.removeChild(textarea);
|
|
}
|
|
|
|
if (!copied) {
|
|
throw new Error("Failed to copy text");
|
|
}
|
|
}
|