diff --git a/src/cache-manager.ts b/src/cache-manager.ts index 3503b23..06f79fd 100644 --- a/src/cache-manager.ts +++ b/src/cache-manager.ts @@ -217,6 +217,9 @@ class CacheManager { await database.searchHistory.bulkAdd(history) } + /** + * @returns The search history, in reverse chronological order + */ public async getSearchHistory(): Promise> { const data = (await database.searchHistory.toArray()) .reverse() diff --git a/src/components/ModalVault.svelte b/src/components/ModalVault.svelte index 5f5ea9a..5c3ef27 100644 --- a/src/components/ModalVault.svelte +++ b/src/components/ModalVault.svelte @@ -102,9 +102,6 @@ eventBus.on('vault', Action.PrevSearchHistory, prevSearchHistory) eventBus.on('vault', Action.NextSearchHistory, nextSearchHistory) await NotesIndex.refreshIndex() - if (settings.showPreviousQueryResults) { - previousQuery = (await cacheManager.getSearchHistory())[0] - } }) onDestroy(() => { diff --git a/src/components/modals.ts b/src/components/modals.ts index eaeef5d..ab17381 100644 --- a/src/components/modals.ts +++ b/src/components/modals.ts @@ -4,6 +4,7 @@ import ModalVault from './ModalVault.svelte' import ModalInFile from './ModalInFile.svelte' import { Action, eventBus, EventNames, isInputComposition } from '../globals' import { settings } from '../settings' +import { cacheManager } from 'src/cache-manager' abstract class OmnisearchModal extends Modal { protected constructor(app: App) { @@ -142,25 +143,37 @@ abstract class OmnisearchModal extends Modal { } export class OmnisearchVaultModal extends OmnisearchModal { + /** + * Instanciate the Omnisearch vault modal + * @param app + * @param query The query to pre-fill the search field with + */ constructor(app: App, query?: string) { super(app) - // Get selected text - const selection = app.workspace.getActiveViewOfType(MarkdownView)?.editor.getSelection() + // Selected text in the editor + const selectedText = app.workspace + .getActiveViewOfType(MarkdownView) + ?.editor.getSelection() - const cmp = new ModalVault({ - target: this.modalEl, - props: { - modal: this, - previousQuery: selection ?? query, - }, + cacheManager.getSearchHistory().then(history => { + // Previously searched query (if enabled in settings) + const previous = settings.showPreviousQueryResults ? history[0] : null + + // Instantiate and display the Svelte component + const cmp = new ModalVault({ + target: this.modalEl, + props: { + modal: this, + previousQuery: query || selectedText || previous || '', + }, + }) + this.onClose = () => { + // Since the component is manually created, + // we also need to manually destroy it + cmp.$destroy() + } }) - - this.onClose = () => { - // Since the component is manually created, - // we also need to manually destroy it - cmp.$destroy() - } } }