+
${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 = `
@@ -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
? `
-
${copyIcon}
-
+ ${copyIcon}
+
`
: `
-
${copyIcon}
-
+ ${copyIcon}
+
-
+
`;
@@ -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();
}