From e2962ec021f6191ebdfc6ee012b50598dcd69707 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Sun, 13 Oct 2024 17:49:06 +0200 Subject: [PATCH] Refactored relevant methods to SearchHistory --- src/cache-manager.ts | 33 +-------------------------- src/components/InputSearch.svelte | 2 +- src/components/ModalVault.svelte | 6 ++--- src/components/modals.ts | 2 +- src/main.ts | 2 ++ src/search/search-history.ts | 38 +++++++++++++++++++++++++++++++ 6 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 src/search/search-history.ts diff --git a/src/cache-manager.ts b/src/cache-manager.ts index 32b6b4d..598eccf 100644 --- a/src/cache-manager.ts +++ b/src/cache-manager.ts @@ -18,10 +18,7 @@ import type OmnisearchPlugin from './main' import { getNonExistingNotes } from './tools/notes' 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 * in the form of IndexedDocuments @@ -66,34 +63,6 @@ export class CacheManager { return this.documents.get(path)! } - public async addToSearchHistory(query: string): Promise { - 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> { - 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 * returning it as an `IndexedDocument` object. diff --git a/src/components/InputSearch.svelte b/src/components/InputSearch.svelte index a0860e7..85c84e7 100644 --- a/src/components/InputSearch.svelte +++ b/src/components/InputSearch.svelte @@ -43,7 +43,7 @@ const debouncedOnInput = debounce(() => { // If typing a query and not executing it, // the next time we open the modal, the search field will be empty - plugin.cacheManager.addToSearchHistory('') + plugin.searchHistory.addToHistory('') dispatch('input', value) }, 300) diff --git a/src/components/ModalVault.svelte b/src/components/ModalVault.svelte index 2cb4d83..73637ff 100644 --- a/src/components/ModalVault.svelte +++ b/src/components/ModalVault.svelte @@ -110,7 +110,7 @@ async function prevSearchHistory() { // 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 ) if (++historySearchIndex >= history.length) { @@ -121,7 +121,7 @@ } async function nextSearchHistory() { - const history = (await plugin.cacheManager.getSearchHistory()).filter( + const history = (await plugin.searchHistory.getHistory()).filter( s => s ) if (--historySearchIndex < 0) { @@ -192,7 +192,7 @@ function saveCurrentQuery() { if (searchQuery) { - plugin.cacheManager.addToSearchHistory(searchQuery) + plugin.searchHistory.addToHistory(searchQuery) } } diff --git a/src/components/modals.ts b/src/components/modals.ts index e70642f..3ffd6f2 100644 --- a/src/components/modals.ts +++ b/src/components/modals.ts @@ -163,7 +163,7 @@ export class OmnisearchVaultModal extends OmnisearchModal { .getActiveViewOfType(MarkdownView) ?.editor.getSelection() - plugin.cacheManager.getSearchHistory().then(history => { + plugin.searchHistory.getHistory().then(history => { // Previously searched query (if enabled in settings) const previous = plugin.settings.showPreviousQueryResults ? history[0] diff --git a/src/main.ts b/src/main.ts index f2586d9..a43fd7c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -29,6 +29,7 @@ import { logDebug } from './tools/utils' import { NotesIndexer } from './notes-indexer' import { TextProcessor } from './tools/text-processing' import { EmbedsRepository } from './repositories/embeds-repository' +import { SearchHistory } from "./search/search-history"; export default class OmnisearchPlugin extends Plugin { // FIXME: fix the type @@ -42,6 +43,7 @@ export default class OmnisearchPlugin extends Plugin { public readonly notesIndexer = new NotesIndexer(this) public readonly textProcessor = new TextProcessor(this) public readonly searchEngine = new SearchEngine(this) + public readonly searchHistory = new SearchHistory(this) public readonly embedsRepository = new EmbedsRepository(this) diff --git a/src/search/search-history.ts b/src/search/search-history.ts new file mode 100644 index 0000000..ce58309 --- /dev/null +++ b/src/search/search-history.ts @@ -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 { + 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> { + const data = (await this.plugin.database.searchHistory.toArray()) + .reverse() + .map(o => o.query) + if (this.nextQueryIsEmpty) { + data.unshift('') + } + return data + } +}