#306 - Reworked search field auto-fill

This commit is contained in:
Simon Cambier
2023-11-01 10:29:41 +01:00
parent 4a4d322f33
commit beb4f191dc
3 changed files with 30 additions and 17 deletions

View File

@@ -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(() => {

View File

@@ -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()
}
}
}