): void {
on:enter={onInputEnter}
on:shift-enter={onInputShiftEnter}
on:ctrl-enter={onInputCtrlEnter}
+ on:arrow-up={() => moveIndex(-1)}
+ on:arrow-down={() => moveIndex(1)}
/>
- {#each $resultNotes as result}
-
+ {#each $resultNotes as result, i}
+ (selectedIndex = i)}
+ on:click={onClick}
+ />
{/each}
diff --git a/src/CmpNoteResult.svelte b/src/CmpNoteResult.svelte
index 260459a..d0f5cf5 100644
--- a/src/CmpNoteResult.svelte
+++ b/src/CmpNoteResult.svelte
@@ -1,21 +1,16 @@
dispatch("hover")}
+ on:click={(e) => dispatch("click")}
>
diff --git a/src/search.ts b/src/search.ts
index dad14f8..9376b09 100644
--- a/src/search.ts
+++ b/src/search.ts
@@ -7,11 +7,9 @@ import {
plugin,
resultNotes,
searchQuery,
- selectedNote,
} from './stores'
import { get } from 'svelte/store'
import { extractHeadingsFromCache, stringsToRegex, wait } from './utils'
-import { tick } from 'svelte'
let minisearchInstance: MiniSearch
@@ -96,13 +94,6 @@ function subscribeToQuery(): void {
// Save the results in the store
resultNotes.set(results)
-
- // Automatically select the first result
- const firstResult = results[0]
- if (firstResult) {
- await tick()
- selectedNote.set(firstResult)
- }
}
}
diff --git a/src/stores.ts b/src/stores.ts
index f5e1e18..2a28133 100644
--- a/src/stores.ts
+++ b/src/stores.ts
@@ -27,32 +27,6 @@ function createIndexedNotes() {
}
}
-function createSelectedNote() {
- const { subscribe, set, update } = writable(null)
- return {
- subscribe,
- set,
- next: () =>
- update(v => {
- const notes = get(resultNotes)
- if (!notes.length) return null
- let id = notes.findIndex(n => n.path === v?.path)
- if (id === -1) return notes[0] ?? null
- id = id < notes.length - 1 ? id + 1 : 0
- return notes[id] ?? null
- }),
- previous: () =>
- update(v => {
- const notes = get(resultNotes)
- if (!notes.length) return null
- let id = notes.findIndex(n => n.path === v?.path)
- if (id === -1) return notes[0] ?? null
- id = id > 0 ? id - 1 : notes.length - 1
- return notes[id] ?? null
- }),
- }
-}
-
/**
* If this field is set, the search will be limited to the given file
*/
@@ -68,11 +42,6 @@ export const searchQuery = writable('')
*/
export const resultNotes = writable([])
-/**
- * The currently selected/hovered note in the results list
- */
-export const selectedNote = createSelectedNote()
-
/**
* A reference to the plugin instance
*/
diff --git a/src/utils.ts b/src/utils.ts
index ca0a19b..500930b 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -53,21 +53,6 @@ export function getAllIndices(text: string, regex: RegExp): SearchMatch[] {
.filter(isSearchMatch)
}
-// export function getAllIndices(text: string, terms: string[]): SearchMatch[] {
-// let matches: SearchMatch[] = []
-// for (const term of terms) {
-// matches = [
-// ...matches,
-// ...[...text.matchAll(new RegExp(escapeRegex(term), 'gi'))]
-// .map(o => ({ match: o[0], index: o.index }))
-// .filter(isSearchMatch),
-// ]
-// }
-// return matches
-// // matches.sort((a, b) => b.match.length - a.match.length)
-// // return uniqBy(matches, 'index')
-// }
-
export function stringsToRegex(strings: string[]): RegExp {
return new RegExp(strings.map(escapeRegex).join('|'), 'gi')
}
@@ -93,3 +78,7 @@ export function extractHeadingsFromCache(
cache.headings?.filter(h => h.level === level).map(h => h.heading) ?? []
)
}
+
+export function loopIndex(index: number, nbItems: number): number {
+ return (index + nbItems) % nbItems
+}