diff --git a/www/app.js b/www/app.js index 143aae4..8a01ed6 100644 --- a/www/app.js +++ b/www/app.js @@ -115,6 +115,36 @@ function showConversationList() { + + +
+ + + + + `; + 置顶 + + + + `; @@ -126,6 +156,71 @@ function showConversationList() { newChatBtn.addEventListener('click', createNewConversation); } + // 搜索功能 + const searchToggleBtn = document.getElementById('searchToggleBtn'); + const searchBar = document.getElementById('searchBar'); + const searchInput = document.getElementById('searchInput'); + const searchCloseBtn = document.getElementById('searchCloseBtn'); + const searchResults = document.getElementById('searchResults'); + + if (searchToggleBtn) { + searchToggleBtn.addEventListener('click', () => { + if (searchBar) { + searchBar.classList.add('show'); + if (searchInput) { + searchInput.focus(); + } + } + }); + } + + if (searchCloseBtn) { + searchCloseBtn.addEventListener('click', () => { + hideSearchBar(); + }); + } + + if (searchInput) { + searchInput.addEventListener('input', (e) => { + const keyword = e.target.value.trim(); + if (keyword) { + searchConversations(keyword); + } else { + if (searchResults) searchResults.innerHTML = ''; + } + }); + + searchInput.addEventListener('keydown', (e) => { + if (e.key === 'Escape') { + hideSearchBar(); + } + }); + } + + // 点击搜索结果 + if (searchResults) { + searchResults.addEventListener('click', (e) => { + const item = e.target.closest('.search-result-item'); + if (item) { + const id = item.getAttribute('data-id'); + hideSearchBar(); + openConversation(id); + } + }); + } + + function hideSearchBar() { + if (searchBar) { + searchBar.classList.remove('show'); + } + if (searchInput) { + searchInput.value = ''; + } + if (searchResults) { + searchResults.innerHTML = ''; + } + } + const conversationList = document.getElementById('conversationList'); const actionMenu = document.getElementById('actionMenu'); let longPressTimer = null; @@ -224,6 +319,52 @@ function sortConversations() { }); } +// 搜索对话 +function searchConversations(keyword) { + const searchResults = document.getElementById('searchResults'); + if (!searchResults) return; + + keyword = keyword.toLowerCase(); + + // 搜索标题和消息内容 + const results = conversations.filter(conv => { + // 搜索标题 + if (conv.title.toLowerCase().includes(keyword)) return true; + + // 搜索消息内容 + if (conv.messages.some(m => m.content.toLowerCase().includes(keyword))) return true; + + return false; + }); + + if (results.length === 0) { + searchResults.innerHTML = '