Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/renderer/src/components/ChatInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions src/renderer/src/lib/searchHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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