alt+up/down to navigate search history

This commit is contained in:
Simon Cambier
2022-08-26 08:54:40 +02:00
parent b73c5a9391
commit 417cc69483
3 changed files with 42 additions and 7 deletions

View File

@@ -11,7 +11,10 @@
onMount(async () => { onMount(async () => {
await tick() await tick()
elInput.focus() elInput.focus()
elInput.select() setTimeout(() => {
// tick() is not working here?
elInput.select()
}, 0)
}) })
const debouncedOnInput = debounce(() => { const debouncedOnInput = debounce(() => {

View File

@@ -1,5 +1,5 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
let lastSearch = '' let lastSearches: string[] = []
</script> </script>
<script lang="ts"> <script lang="ts">
@@ -17,10 +17,12 @@
export let modal: OmnisearchVaultModal export let modal: OmnisearchVaultModal
let selectedIndex = 0 let selectedIndex = 0
let lastSearchIndex = 0
let searchQuery: string let searchQuery: string
let resultNotes: ResultNote[] = [] let resultNotes: ResultNote[] = []
let query: Query let query: Query
$: selectedNote = resultNotes[selectedIndex] $: selectedNote = resultNotes[selectedIndex]
// $: lastSearch = lastSearches[lastSearchIndex]
$: if (searchQuery) { $: if (searchQuery) {
updateResults() updateResults()
@@ -30,7 +32,8 @@
onMount(async () => { onMount(async () => {
await reindexNotes() await reindexNotes()
searchQuery = lastSearch searchQuery = lastSearches[lastSearchIndex]
console.log(lastSearches)
eventBus.enable('vault') eventBus.enable('vault')
eventBus.on('vault', 'enter', openNoteAndCloseModal) eventBus.on('vault', 'enter', openNoteAndCloseModal)
eventBus.on('vault', 'shift-enter', createNoteAndCloseModal) eventBus.on('vault', 'shift-enter', createNoteAndCloseModal)
@@ -39,18 +42,32 @@
eventBus.on('vault', 'tab', switchToInFileModal) eventBus.on('vault', 'tab', switchToInFileModal)
eventBus.on('vault', 'arrow-up', () => moveIndex(-1)) eventBus.on('vault', 'arrow-up', () => moveIndex(-1))
eventBus.on('vault', 'arrow-down', () => moveIndex(1)) eventBus.on('vault', 'arrow-down', () => moveIndex(1))
eventBus.on('vault', 'prev-search-history', () => prevSearch())
eventBus.on('vault', 'next-search-history', () => nextSearch())
}) })
onDestroy(() => { onDestroy(() => {
eventBus.disable('vault') eventBus.disable('vault')
}) })
function prevSearch() {
if (++lastSearchIndex >= lastSearches.length) {
lastSearchIndex = 0
}
searchQuery = lastSearches[lastSearchIndex]
}
function nextSearch() {
if (--lastSearchIndex < 0) {
lastSearchIndex = lastSearches.length ? lastSearches.length - 1 : 0
}
searchQuery = lastSearches[lastSearchIndex]
}
async function updateResults() { async function updateResults() {
query = new Query(searchQuery) query = new Query(searchQuery)
resultNotes = (await getSuggestions(query)).sort( resultNotes = (await getSuggestions(query)).sort(
(a, b) => b.score - a.score (a, b) => b.score - a.score
) )
lastSearch = searchQuery
selectedIndex = 0 selectedIndex = 0
scrollIntoView() scrollIntoView()
} }
@@ -63,16 +80,21 @@
function openNoteAndCloseModal(): void { function openNoteAndCloseModal(): void {
if (!selectedNote) return if (!selectedNote) return
openNote(selectedNote) openSearchResult(selectedNote)
modal.close() modal.close()
} }
function openNoteInNewPane(): void { function openNoteInNewPane(): void {
if (!selectedNote) return if (!selectedNote) return
openNote(selectedNote, true) openSearchResult(selectedNote, true)
modal.close() modal.close()
} }
function openSearchResult(note: ResultNote, newPane = false) {
lastSearches.unshift(searchQuery)
openNote(note, newPane)
}
async function createNoteAndCloseModal(): Promise<void> { async function createNoteAndCloseModal(): Promise<void> {
try { try {
await createNote(searchQuery) await createNote(searchQuery)
@@ -146,7 +168,7 @@
</script> </script>
<div class="modal-title">Omnisearch - Vault</div> <div class="modal-title">Omnisearch - Vault</div>
<InputSearch value={lastSearch} on:input={e => (searchQuery = e.detail)} /> <InputSearch value={searchQuery} on:input={e => (searchQuery = e.detail)} />
<ModalContainer> <ModalContainer>
{#each resultNotes as result, i} {#each resultNotes as result, i}

View File

@@ -88,6 +88,16 @@ abstract class OmnisearchModal extends Modal {
e.preventDefault() e.preventDefault()
eventBus.emit('tab') // Switch context eventBus.emit('tab') // Switch context
}) })
// Search history
this.scope.register(['Alt'], 'ArrowDown', e => {
e.preventDefault()
eventBus.emit('next-search-history')
})
this.scope.register(['Alt'], 'ArrowUp', e => {
e.preventDefault()
eventBus.emit('prev-search-history')
})
} }
} }