From e1ab11c0072021fa0c34522afd3f5c7e33a8500b Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Sat, 25 Apr 2026 17:20:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E6=8C=89=E9=92=AE=EF=BC=88=E5=A4=8D=E5=88=B6=E5=8E=9F=E6=96=87?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 用户和AI消息都添加复制按钮 - 复制原始文本内容(不包含Markdown格式) - 添加Toast提示显示复制成功 - 复制按钮hover时显示蓝色 --- works/ai-chat-app/www/app.js | 41 ++++++++++++++++++++++++++++++++- works/ai-chat-app/www/style.css | 28 ++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) 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 ? `
` : `