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="suggestion-item omnisearch-result">
<div class="omnisearch-result__body"> <div class="omnisearch-result__body">
{cleanContent(note?.content ?? '')} {@html cleanContent(note?.content ?? '')}
</div> </div>
</div> </div>

View File

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

View File

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

View File

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