diff --git a/src/renderer/src/components/ChatInput.vue b/src/renderer/src/components/ChatInput.vue index 3923b54b2..6833e8efc 100644 --- a/src/renderer/src/components/ChatInput.vue +++ b/src/renderer/src/components/ChatInput.vue @@ -925,9 +925,17 @@ function onKeydown(e: KeyboardEvent) { handleEditorEnter(e) e.preventDefault() } + if (e.code === 'ArrowUp') { const contentEditableDiv = e.target as HTMLDivElement if (isCursorInFirstLine(contentEditableDiv)) { + const currentContent = editor.getText().trim() + + // 如果当前有内容,先将其插入到搜索历史的当前位置 + if (currentContent) { + searchHistory.insertAtCurrent(currentContent) + } + const previousSearch = searchHistory.getPrevious() if (previousSearch !== null) { editor.commands.setContent(previousSearch) @@ -937,6 +945,13 @@ function onKeydown(e: KeyboardEvent) { } else if (e.code === 'ArrowDown') { const contentEditableDiv = e.target as HTMLDivElement if (isCursorInLastLine(contentEditableDiv)) { + const currentContent = editor.getText().trim() + + // 如果当前有内容,先将其插入到搜索历史的当前位置 + if (currentContent) { + searchHistory.insertAtCurrent(currentContent) + } + const nextSearch = searchHistory.getNext() if (nextSearch !== null) { editor.commands.setContent(nextSearch) diff --git a/src/renderer/src/lib/searchHistory.ts b/src/renderer/src/lib/searchHistory.ts index 297ba3b24..ebb6d6c71 100644 --- a/src/renderer/src/lib/searchHistory.ts +++ b/src/renderer/src/lib/searchHistory.ts @@ -45,6 +45,40 @@ export class SearchHistory { this.currentIndex = -1 // Reset index when clearing history console.log('Search history cleared') } + + insertAtCurrent(query: string) { + if (!query || query.trim() === '') return + + const trimmedQuery = query.trim() + + // 如果当前索引在历史记录末尾,直接添加 + if (this.currentIndex >= this.history.length) { + this.addSearch(trimmedQuery) + return + } + + // 检查是否与当前位置的内容相同,避免重复 + if (this.history[this.currentIndex] === trimmedQuery) { + return + } + + // 检查是否与最后一条记录相同,避免重复 + if (this.history[this.history.length - 1] === trimmedQuery) { + return + } + + // 如果 history 已经满了,移除最旧的记录 + if (this.history.length >= this.maxHistorySize) { + this.history.shift() + this.currentIndex = Math.max(0, this.currentIndex - 1) + } + + // 在当前索引位置插入新内容 + this.history.splice(this.currentIndex, 0, trimmedQuery) + this.currentIndex = this.history.length // 重置索引到末尾 + + console.log('Search history inserted at current position:', this.history) + } } export const searchHistory = new SearchHistory(100) // Create a new instance with a maximum size of 100