mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-12 12:39:52 +08:00
23 lines
710 B
TypeScript
23 lines
710 B
TypeScript
/** Copy text, falling back to execCommand for non-secure contexts (HTTP) */
|
|||
|
|
export async function copyText(text: string): Promise<boolean> {
|
||
|
|
try {
|
||
|
|
await navigator.clipboard.writeText(text)
|
||
|
|
return true
|
||
|
|
} catch {
|
||
|
|
const textarea = document.createElement("textarea")
|
||
|
|
textarea.value = text
|
||
|
|
textarea.style.position = "fixed"
|
||
|
|
textarea.style.left = "-9999px"
|
||
|
|
textarea.style.opacity = "0"
|
||
|
|
document.body.appendChild(textarea)
|
||
|
|
try {
|
||
|
|
textarea.select()
|
||
|
|
return document.execCommand("copy")
|
||
|
|
} catch {
|
||
|
|
return false
|
||
|
|
} finally {
|
||
|
|
document.body.removeChild(textarea)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|