Re-trigger the search when the query or singleFileSearch change

This commit is contained in:
Simon Cambier
2022-04-18 10:59:09 +02:00
parent 6cb113b87e
commit 3168a89070
4 changed files with 14 additions and 18 deletions

View File

@@ -30,6 +30,6 @@ function cleanContent(content: string): string {
<div class="suggestion-item omnisearch-result">
<div class="omnisearch-result__body">
{cleanContent(note?.content ?? '')}
{@html cleanContent(note?.content ?? '')}
</div>
</div>

View File

@@ -10,8 +10,8 @@ const dispatch = createEventDispatcher()
onMount(async () => {
await tick()
elInput.focus()
elInput.select()
elInput.value = $searchQuery
elInput.select()
})
const debouncedOnInput = debounce(() => $searchQuery = inputValue, 100)

View File

@@ -1,18 +1,9 @@
<script lang="ts">
import { onMount, tick } from "svelte"
import CmpInput from "./CmpInput.svelte"
import CmpNoteInternalResult from "./CmpInfileResult.svelte"
import CmpNoteResult from "./CmpNoteResult.svelte"
import type { ResultNote } from "./globals"
import { openNote } from "./notes"
import { getSuggestions } from "./search"
import {
inFileSearch,
modal,
plugin,
resultNotes,
searchQuery,
selectedNote,
} from "./stores"
$: firstResult = $resultNotes[0]

View File

@@ -3,7 +3,7 @@ import MiniSearch, { type SearchResult } from 'minisearch'
import type { IndexedNote, ResultNote, SearchMatch } from './globals'
import {
indexedNotes,
inFileSearch,
inFileSearch as singleFileSearch,
plugin,
resultNotes,
searchQuery,
@@ -75,15 +75,20 @@ function search(query: string): SearchResult[] {
}
/**
* Subscribe to the searchQuery store,
* and automatically triggers a search when the query changes
* Automatically re-trigger the search when the query or the
* inFileSearch changes
*/
function subscribeToQuery(): void {
searchQuery.subscribe(async q => {
singleFileSearch.subscribe(async file => {
triggerQuery(get(searchQuery))
})
searchQuery.subscribe(triggerQuery)
async function triggerQuery(q: string): Promise<void> {
// If we're in "single file" mode, the search results array
// will contain a single result, related to this file
const results = get(inFileSearch)
? getSuggestions(q, { singleFile: get(inFileSearch) })
const results = get(singleFileSearch)
? getSuggestions(q, { singleFile: get(singleFileSearch) })
: getSuggestions(q)
console.log(results)
@@ -96,7 +101,7 @@ function subscribeToQuery(): void {
await tick()
selectedNote.set(firstResult)
}
})
}
}
/**