obsidian-locator/src/search/search-history.ts
2025-06-21 13:22:54 +02:00

39 lines
1.0 KiB
TypeScript

import type LocatorPlugin from '../main'
export class SearchHistory {
/**
* Show an empty input field next time the user opens Locator modal
*/
private nextQueryIsEmpty = false
constructor(private plugin: LocatorPlugin) {}
public async addToHistory(query: string): Promise<void> {
if (!query) {
this.nextQueryIsEmpty = true
return
}
this.nextQueryIsEmpty = false
const database = this.plugin.database
let history = await database.searchHistory.toArray()
history = history.filter(s => s.query !== query).reverse()
history.unshift({ query })
history = history.slice(0, 10)
await database.searchHistory.clear()
await database.searchHistory.bulkAdd(history)
}
/**
* @returns The search history, in reverse chronological order
*/
public async getHistory(): Promise<ReadonlyArray<string>> {
const data = (await this.plugin.database.searchHistory.toArray())
.reverse()
.map(o => o.query)
if (this.nextQueryIsEmpty) {
data.unshift('')
}
return data
}
}