diff --git a/assets/styles.css b/assets/styles.css index 4f920d8..cb111c8 100644 --- a/assets/styles.css +++ b/assets/styles.css @@ -8,6 +8,10 @@ font-weight: bold; } +.omnisearch-result__counter { + font-size: 0.7rem; +} + .omnisearch-result__body { white-space: normal; font-size: small; diff --git a/src/CmpNoteResult.svelte b/src/CmpNoteResult.svelte new file mode 100644 index 0000000..4a633b4 --- /dev/null +++ b/src/CmpNoteResult.svelte @@ -0,0 +1,20 @@ + + +
+ + {@html title} + + + {nbMatches} + {#if nbMatches > 1}matches{:else}match{/if} + +
+ {@html content} +
+
+ diff --git a/src/Component.svelte b/src/Component.svelte deleted file mode 100644 index 38520a9..0000000 --- a/src/Component.svelte +++ /dev/null @@ -1,11 +0,0 @@ - - -
My number is {variable}!
- - diff --git a/src/modal.ts b/src/modal.ts index 9c5c105..e0ec1fb 100644 --- a/src/modal.ts +++ b/src/modal.ts @@ -1,12 +1,13 @@ import { MarkdownView, SuggestModal, TFile } from 'obsidian' import type { ResultNote } from './globals' import type OmnisearchPlugin from './main' -import Component from './Component.svelte' +import CmpNoteResult from './CmpNoteResult.svelte' import { escapeHTML, escapeRegex, getAllIndexes, highlighter } from './utils' +import { selectedNoteId } from './store' +import { get } from 'svelte/store' export class OmnisearchModal extends SuggestModal { private plugin: OmnisearchPlugin - private selectedNoteId?: string private mutationObserver?: MutationObserver constructor(plugin: OmnisearchPlugin) { @@ -27,7 +28,7 @@ export class OmnisearchModal extends SuggestModal { } async onKeydown(ev: KeyboardEvent): Promise { - const noteId = this.selectedNoteId + const noteId = get(selectedNoteId) if (ev.key !== 'Enter' || !noteId) return if (ev.ctrlKey || ev.metaKey) { @@ -66,9 +67,11 @@ export class OmnisearchModal extends SuggestModal { (event.target as HTMLDivElement).classList.contains('is-selected'), ) const id = - (record?.target as HTMLElement)?.getAttribute('data-note-id') ?? null + (record?.target?.firstChild as HTMLElement)?.getAttribute( + 'data-note-id', + ) ?? null if (id) { - this.selectedNoteId = id + selectedNoteId.set(id) } }) this.mutationObserver.observe(modalEl, { @@ -79,8 +82,7 @@ export class OmnisearchModal extends SuggestModal { onOpen(): void { this.inputEl.focus() - this.setupObserver(this.modalEl) - + this.inputEl.onkeydown = this.onKeydown.bind(this) // Reload last search, if any if (this.plugin.lastSearch) { const event = new Event('input', { @@ -93,7 +95,7 @@ export class OmnisearchModal extends SuggestModal { this.inputEl.spellcheck = false } - this.inputEl.onkeydown = this.onKeydown.bind(this) + this.setupObserver(this.modalEl) } onClose(): void { @@ -110,7 +112,12 @@ export class OmnisearchModal extends SuggestModal { prefix: true, fuzzy: term => (term.length > 4 ? 0.2 : false), combineWith: 'AND', - boost: { basename: 2, headings1: 1.5, headings2: 1.3, headings3: 1.1 }, + boost: { + basename: 2, + headings1: 1.5, + headings2: 1.3, + headings3: 1.1, + }, }) .sort((a, b) => b.score - a.score) .slice(0, 50) @@ -164,20 +171,15 @@ export class OmnisearchModal extends SuggestModal { } renderSuggestion(value: ResultNote, el: HTMLElement): void { - const component = new Component({ + new CmpNoteResult({ target: el, - props: { variable: 1 }, + props: { + id: value.path, + title: value.basename, + content: value.content, + nbMatches: value.matches.length, + }, }) - el.setAttribute('data-note-id', value.path) - el.addClass('omnisearch-result') - - // title - const title = el.createEl('div', { cls: 'omnisearch-result__title' }) - title.innerHTML = value.basename - - // body - const body = el.createEl('div', { cls: 'omnisearch-result__body' }) - body.innerHTML = value.content } async onChooseSuggestion(item: ResultNote): Promise { diff --git a/src/store.ts b/src/store.ts new file mode 100644 index 0000000..f35ebd3 --- /dev/null +++ b/src/store.ts @@ -0,0 +1,3 @@ +import { writable } from 'svelte/store' + +export const selectedNoteId = writable() diff --git a/src/utils.ts b/src/utils.ts index e3286a4..c4b9be1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,11 @@ -import { CachedMetadata } from 'obsidian' +import type { CachedMetadata } from 'obsidian' import { isSearchMatch, regexLineSplit, regexWikilink, regexYaml, - SearchMatch, } from './globals' +import type { SearchMatch } from './globals' export function highlighter(str: string): string { return '' + str + ''