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

Co-authored-by: openapphub <175949671+openapphub@users.noreply.github.com>
This commit is contained in:
openapphub 2026-05-06 14:44:36 +08:00 committed by GitHub
parent e3a05bd36d
commit 4d3070e849
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

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