#306 - Reworked search field auto-fill
This commit is contained in:
@@ -217,6 +217,9 @@ class CacheManager {
|
|||||||
await database.searchHistory.bulkAdd(history)
|
await database.searchHistory.bulkAdd(history)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns The search history, in reverse chronological order
|
||||||
|
*/
|
||||||
public async getSearchHistory(): Promise<ReadonlyArray<string>> {
|
public async getSearchHistory(): Promise<ReadonlyArray<string>> {
|
||||||
const data = (await database.searchHistory.toArray())
|
const data = (await database.searchHistory.toArray())
|
||||||
.reverse()
|
.reverse()
|
||||||
|
|||||||
@@ -102,9 +102,6 @@
|
|||||||
eventBus.on('vault', Action.PrevSearchHistory, prevSearchHistory)
|
eventBus.on('vault', Action.PrevSearchHistory, prevSearchHistory)
|
||||||
eventBus.on('vault', Action.NextSearchHistory, nextSearchHistory)
|
eventBus.on('vault', Action.NextSearchHistory, nextSearchHistory)
|
||||||
await NotesIndex.refreshIndex()
|
await NotesIndex.refreshIndex()
|
||||||
if (settings.showPreviousQueryResults) {
|
|
||||||
previousQuery = (await cacheManager.getSearchHistory())[0]
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import ModalVault from './ModalVault.svelte'
|
|||||||
import ModalInFile from './ModalInFile.svelte'
|
import ModalInFile from './ModalInFile.svelte'
|
||||||
import { Action, eventBus, EventNames, isInputComposition } from '../globals'
|
import { Action, eventBus, EventNames, isInputComposition } from '../globals'
|
||||||
import { settings } from '../settings'
|
import { settings } from '../settings'
|
||||||
|
import { cacheManager } from 'src/cache-manager'
|
||||||
|
|
||||||
abstract class OmnisearchModal extends Modal {
|
abstract class OmnisearchModal extends Modal {
|
||||||
protected constructor(app: App) {
|
protected constructor(app: App) {
|
||||||
@@ -142,25 +143,37 @@ abstract class OmnisearchModal extends Modal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class OmnisearchVaultModal extends OmnisearchModal {
|
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) {
|
constructor(app: App, query?: string) {
|
||||||
super(app)
|
super(app)
|
||||||
|
|
||||||
// Get selected text
|
// Selected text in the editor
|
||||||
const selection = app.workspace.getActiveViewOfType(MarkdownView)?.editor.getSelection()
|
const selectedText = app.workspace
|
||||||
|
.getActiveViewOfType(MarkdownView)
|
||||||
|
?.editor.getSelection()
|
||||||
|
|
||||||
|
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({
|
const cmp = new ModalVault({
|
||||||
target: this.modalEl,
|
target: this.modalEl,
|
||||||
props: {
|
props: {
|
||||||
modal: this,
|
modal: this,
|
||||||
previousQuery: selection ?? query,
|
previousQuery: query || selectedText || previous || '',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
this.onClose = () => {
|
this.onClose = () => {
|
||||||
// Since the component is manually created,
|
// Since the component is manually created,
|
||||||
// we also need to manually destroy it
|
// we also need to manually destroy it
|
||||||
cmp.$destroy()
|
cmp.$destroy()
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user