Files
LingChair/client/ui/copyToClipboard.ts
CrescentLeaf a12a8830d4 ui: 改善 复制到剪贴薄 的用户体验
* 在 Via 浏览器上, writeText 本质上被重写了, 逻辑还是 execCommand copy
* 更换 copy 为 cut
2025-10-04 11:34:56 +08:00

21 lines
658 B
TypeScript

export default function copyToClipboard(text: string) {
if (!("via" in window) && navigator.clipboard)
return navigator.clipboard.writeText(text)
return new Promise((res, rej) => {
if (document.hasFocus()) {
const a = document.createElement("textarea")
document.body.appendChild(a)
a.style.position = "fixed"
a.style.clip = "rect(0 0 0 0)"
a.style.top = "10px"
a.value = text
a.select()
document.execCommand("cut", true)
document.body.removeChild(a)
res(null)
} else {
rej()
}
})
}