Refactored relevant methods to SearchHistory
This commit is contained in:
@@ -18,10 +18,7 @@ import type OmnisearchPlugin from './main'
|
|||||||
import { getNonExistingNotes } from './tools/notes'
|
import { getNonExistingNotes } from './tools/notes'
|
||||||
|
|
||||||
export class CacheManager {
|
export class CacheManager {
|
||||||
/**
|
|
||||||
* Show an empty input field next time the user opens Omnisearch modal
|
|
||||||
*/
|
|
||||||
private nextQueryIsEmpty = false
|
|
||||||
/**
|
/**
|
||||||
* The "live cache", containing all indexed vault files
|
* The "live cache", containing all indexed vault files
|
||||||
* in the form of IndexedDocuments
|
* in the form of IndexedDocuments
|
||||||
@@ -66,34 +63,6 @@ export class CacheManager {
|
|||||||
return this.documents.get(path)!
|
return this.documents.get(path)!
|
||||||
}
|
}
|
||||||
|
|
||||||
public async addToSearchHistory(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 getSearchHistory(): Promise<ReadonlyArray<string>> {
|
|
||||||
const data = (await this.plugin.database.searchHistory.toArray())
|
|
||||||
.reverse()
|
|
||||||
.map(o => o.query)
|
|
||||||
if (this.nextQueryIsEmpty) {
|
|
||||||
data.unshift('')
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function is responsible for extracting the text from a file and
|
* This function is responsible for extracting the text from a file and
|
||||||
* returning it as an `IndexedDocument` object.
|
* returning it as an `IndexedDocument` object.
|
||||||
|
|||||||
@@ -43,7 +43,7 @@
|
|||||||
const debouncedOnInput = debounce(() => {
|
const debouncedOnInput = debounce(() => {
|
||||||
// If typing a query and not executing it,
|
// If typing a query and not executing it,
|
||||||
// the next time we open the modal, the search field will be empty
|
// the next time we open the modal, the search field will be empty
|
||||||
plugin.cacheManager.addToSearchHistory('')
|
plugin.searchHistory.addToHistory('')
|
||||||
dispatch('input', value)
|
dispatch('input', value)
|
||||||
}, 300)
|
}, 300)
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -110,7 +110,7 @@
|
|||||||
|
|
||||||
async function prevSearchHistory() {
|
async function prevSearchHistory() {
|
||||||
// Filter out the empty string, if it's there
|
// Filter out the empty string, if it's there
|
||||||
const history = (await plugin.cacheManager.getSearchHistory()).filter(
|
const history = (await plugin.searchHistory.getHistory()).filter(
|
||||||
s => s
|
s => s
|
||||||
)
|
)
|
||||||
if (++historySearchIndex >= history.length) {
|
if (++historySearchIndex >= history.length) {
|
||||||
@@ -121,7 +121,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function nextSearchHistory() {
|
async function nextSearchHistory() {
|
||||||
const history = (await plugin.cacheManager.getSearchHistory()).filter(
|
const history = (await plugin.searchHistory.getHistory()).filter(
|
||||||
s => s
|
s => s
|
||||||
)
|
)
|
||||||
if (--historySearchIndex < 0) {
|
if (--historySearchIndex < 0) {
|
||||||
@@ -192,7 +192,7 @@
|
|||||||
|
|
||||||
function saveCurrentQuery() {
|
function saveCurrentQuery() {
|
||||||
if (searchQuery) {
|
if (searchQuery) {
|
||||||
plugin.cacheManager.addToSearchHistory(searchQuery)
|
plugin.searchHistory.addToHistory(searchQuery)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ export class OmnisearchVaultModal extends OmnisearchModal {
|
|||||||
.getActiveViewOfType(MarkdownView)
|
.getActiveViewOfType(MarkdownView)
|
||||||
?.editor.getSelection()
|
?.editor.getSelection()
|
||||||
|
|
||||||
plugin.cacheManager.getSearchHistory().then(history => {
|
plugin.searchHistory.getHistory().then(history => {
|
||||||
// Previously searched query (if enabled in settings)
|
// Previously searched query (if enabled in settings)
|
||||||
const previous = plugin.settings.showPreviousQueryResults
|
const previous = plugin.settings.showPreviousQueryResults
|
||||||
? history[0]
|
? history[0]
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import { logDebug } from './tools/utils'
|
|||||||
import { NotesIndexer } from './notes-indexer'
|
import { NotesIndexer } from './notes-indexer'
|
||||||
import { TextProcessor } from './tools/text-processing'
|
import { TextProcessor } from './tools/text-processing'
|
||||||
import { EmbedsRepository } from './repositories/embeds-repository'
|
import { EmbedsRepository } from './repositories/embeds-repository'
|
||||||
|
import { SearchHistory } from "./search/search-history";
|
||||||
|
|
||||||
export default class OmnisearchPlugin extends Plugin {
|
export default class OmnisearchPlugin extends Plugin {
|
||||||
// FIXME: fix the type
|
// FIXME: fix the type
|
||||||
@@ -42,6 +43,7 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
public readonly notesIndexer = new NotesIndexer(this)
|
public readonly notesIndexer = new NotesIndexer(this)
|
||||||
public readonly textProcessor = new TextProcessor(this)
|
public readonly textProcessor = new TextProcessor(this)
|
||||||
public readonly searchEngine = new SearchEngine(this)
|
public readonly searchEngine = new SearchEngine(this)
|
||||||
|
public readonly searchHistory = new SearchHistory(this)
|
||||||
|
|
||||||
public readonly embedsRepository = new EmbedsRepository(this)
|
public readonly embedsRepository = new EmbedsRepository(this)
|
||||||
|
|
||||||
|
|||||||
38
src/search/search-history.ts
Normal file
38
src/search/search-history.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import type OmnisearchPlugin from '../main'
|
||||||
|
|
||||||
|
export class SearchHistory {
|
||||||
|
/**
|
||||||
|
* Show an empty input field next time the user opens Omnisearch modal
|
||||||
|
*/
|
||||||
|
private nextQueryIsEmpty = false
|
||||||
|
|
||||||
|
constructor(private plugin: OmnisearchPlugin) {}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user