diff --git a/works/ai-chat-app/www/app.js b/works/ai-chat-app/www/app.js index 2bab46d..e6921ab 100644 --- a/works/ai-chat-app/www/app.js +++ b/works/ai-chat-app/www/app.js @@ -174,6 +174,41 @@ async function regenerate(index) { await streamGenerate(userMsgIndex); } +// 复制消息(复制原文) +function copyMessage(index) { + const content = messages[index].content; + + navigator.clipboard.writeText(content).then(() => { + showToast('已复制到剪贴板'); + }).catch(err => { + console.error('复制失败:', err); + // 备用方案 + const textarea = document.createElement('textarea'); + textarea.value = content; + textarea.style.position = 'fixed'; + textarea.style.opacity = '0'; + document.body.appendChild(textarea); + textarea.select(); + document.execCommand('copy'); + document.body.removeChild(textarea); + showToast('已复制到剪贴板'); + }); +} + +// 显示提示 +function showToast(message) { + const toast = document.createElement('div'); + toast.className = 'toast'; + toast.textContent = message; + document.body.appendChild(toast); + + setTimeout(() => toast.classList.add('show'), 10); + setTimeout(() => { + toast.classList.remove('show'); + setTimeout(() => document.body.removeChild(toast), 300); + }, 2000); +} + // 删除消息 function deleteMessage(index) { if (isLoading) return; @@ -212,14 +247,18 @@ function renderMessages() { const avatar = isUser ? '👤' : '🤖'; const content = renderMarkdown(msg.content); - // 操作按钮 + // 操作按钮(复制按钮放在最前面) + const copyIcon = ``; + const actions = isUser ? `
` : `