fix(web): 兼容 HTTP 环境复制按钮

This commit is contained in:
jin xm 2026-04-29 17:37:17 +08:00
parent 62d0e34ec9
commit 15609abe7a

View file

@ -56,11 +56,38 @@ export function AssistantMessage({
const formattedTimestamp =
timestamp !== "" ? formatMessageTime(timestamp) : ""
const handleCopy = () => {
navigator.clipboard.writeText(content).then(() => {
const handleCopy = async () => {
const markCopied = () => {
setIsCopied(true)
setTimeout(() => setIsCopied(false), 2000)
})
}
try {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(content)
markCopied()
return
}
} catch {
// HTTP 或受限环境下可能不支持 Clipboard API继续走降级方案
}
const textArea = document.createElement("textarea")
textArea.value = content
textArea.setAttribute("readonly", "")
textArea.style.position = "fixed"
textArea.style.left = "-9999px"
document.body.appendChild(textArea)
textArea.select()
try {
const copied = document.execCommand("copy")
if (copied) {
markCopied()
}
} finally {
document.body.removeChild(textArea)
}
}
const collapsedLabel = isThought