Merge pull request #1 from openapphub/fix-http-copy-fallback
fix(web): 兼容 HTTP 环境复制按钮
This commit is contained in:
commit
3f48b933a1
1 changed files with 30 additions and 3 deletions
|
|
@ -56,11 +56,38 @@ export function AssistantMessage({
|
||||||
const formattedTimestamp =
|
const formattedTimestamp =
|
||||||
timestamp !== "" ? formatMessageTime(timestamp) : ""
|
timestamp !== "" ? formatMessageTime(timestamp) : ""
|
||||||
|
|
||||||
const handleCopy = () => {
|
const handleCopy = async () => {
|
||||||
navigator.clipboard.writeText(content).then(() => {
|
const markCopied = () => {
|
||||||
setIsCopied(true)
|
setIsCopied(true)
|
||||||
setTimeout(() => setIsCopied(false), 2000)
|
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
|
const collapsedLabel = isThought
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue