diff --git a/www/app.js b/www/app.js index 42e311c..d674b4e 100644 --- a/www/app.js +++ b/www/app.js @@ -13,16 +13,19 @@ let conversations = []; // 对话列表 let currentConversation = null; // 当前对话 let isLoading = false; -// DOM 元素 -const appContainer = document.getElementById('app'); -const messagesContainer = document.getElementById('messagesContainer'); -const messagesDiv = document.getElementById('messages'); -const userInput = document.getElementById('userInput'); -const sendBtn = document.getElementById('sendBtn'); -const welcome = document.getElementById('welcome'); +// DOM 元素(初始为 null,在 openConversation 时重新获取) +let appContainer = null; +let messagesContainer = null; +let messagesDiv = null; +let userInput = null; +let sendBtn = null; +let welcome = null; // 初始化 document.addEventListener('DOMContentLoaded', () => { + // 初始化 appContainer + appContainer = document.getElementById('app'); + // 从本地存储加载对话列表 const saved = localStorage.getItem('conversations'); if (saved) { @@ -68,19 +71,19 @@ function showConversationList() {
- -
+
${conversations.length === 0 ? '
暂无对话记录
' : conversations.map(conv => ` -
+
${escapeHtml(conv.title)}
${conv.messages.length} 条消息 · ${formatTime(conv.updatedAt)}
-
@@ -92,6 +95,29 @@ function showConversationList() { `; appContainer.innerHTML = listHtml; + + // 绑定事件 + const newChatBtn = document.getElementById('newChatBtn'); + if (newChatBtn) { + newChatBtn.addEventListener('click', createNewConversation); + } + + const conversationList = document.getElementById('conversationList'); + if (conversationList) { + conversationList.addEventListener('click', (e) => { + const item = e.target.closest('.conversation-item'); + const deleteBtn = e.target.closest('.conv-delete-btn'); + + if (deleteBtn) { + e.stopPropagation(); + const id = deleteBtn.getAttribute('data-id'); + deleteConversation(id); + } else if (item) { + const id = item.getAttribute('data-id'); + openConversation(id); + } + }); + } } // 创建新对话 @@ -121,14 +147,14 @@ function openConversation(id) { const chatHtml = `
-

${escapeHtml(currentConversation.title)}

-
@@ -152,10 +178,8 @@ function openConversation(id) { id="userInput" placeholder="输入消息..." rows="1" - onkeydown="handleKeyDown(event)" - oninput="autoResize(this)" > -
@@ -171,9 +195,17 @@ function openConversation(id) { sendBtn = document.getElementById('sendBtn'); welcome = document.getElementById('welcome'); - // 渲染消息 - renderMessages(); - userInput.focus(); + // 绑定按钮事件 + const backBtn = document.getElementById('backBtn'); + if (backBtn) backBtn.addEventListener('click', showConversationList); + + const clearBtn = document.getElementById('clearBtn'); + if (clearBtn) clearBtn.addEventListener('click', clearCurrentChat); + + // 绑定输入事件 + userInput.addEventListener('keydown', handleKeyDown); + userInput.addEventListener('input', (e) => autoResize(e.target)); + sendBtn.addEventListener('click', sendMessage); // 绑定快捷按钮事件 document.querySelectorAll('.quick-btn').forEach(btn => { @@ -183,6 +215,10 @@ function openConversation(id) { sendMessage(); }); }); + + // 渲染消息 + renderMessages(); + userInput.focus(); } // 删除对话 @@ -424,17 +460,17 @@ function renderMessages() { const actions = isUser ? `
- - +
` : `
- - + -
`; @@ -450,6 +486,17 @@ function renderMessages() { `; }).join(''); + // 绑定消息操作按钮事件(事件委托) + messagesDiv.querySelectorAll('.copy-btn').forEach(btn => { + btn.addEventListener('click', () => copyMessage(parseInt(btn.dataset.index))); + }); + messagesDiv.querySelectorAll('.regenerate-btn').forEach(btn => { + btn.addEventListener('click', () => regenerate(parseInt(btn.dataset.index))); + }); + messagesDiv.querySelectorAll('.delete-btn').forEach(btn => { + btn.addEventListener('click', () => deleteMessage(parseInt(btn.dataset.index))); + }); + scrollToBottom(); }