WIP in-file search
This commit is contained in:
@@ -11,6 +11,7 @@ onMount(async () => {
|
||||
await tick()
|
||||
input.focus()
|
||||
input.select()
|
||||
input.value = $searchQuery
|
||||
})
|
||||
|
||||
const debouncedOnInput = debounce(() => $searchQuery = inputValue, 100)
|
||||
@@ -41,6 +42,9 @@ function moveNoteSelection(ev: KeyboardEvent): void {
|
||||
} else if (ev.shiftKey) {
|
||||
// Create a new note
|
||||
dispatch("shift-enter", $selectedNote)
|
||||
} else if (ev.altKey) {
|
||||
// Create a new note
|
||||
dispatch("alt-enter", $selectedNote)
|
||||
} else {
|
||||
// Open in current pane
|
||||
dispatch("enter", $selectedNote)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
<script lang="ts">
|
||||
import { tick } from "svelte"
|
||||
import { onMount, tick } from "svelte"
|
||||
import CmpInput from "./CmpInput.svelte"
|
||||
import CmpNoteInternalResult from "./CmpNoteInternalResult.svelte"
|
||||
import CmpNoteResult from "./CmpNoteResult.svelte"
|
||||
import type { ResultNote } from "./globals"
|
||||
import { openNote } from "./notes"
|
||||
import { getSuggestions } from "./search"
|
||||
import {
|
||||
inFileSearch,
|
||||
modal,
|
||||
plugin,
|
||||
resultNotes,
|
||||
@@ -13,8 +15,18 @@ import {
|
||||
selectedNote,
|
||||
} from "./stores"
|
||||
|
||||
$: firstResult = $resultNotes[0]
|
||||
|
||||
searchQuery.subscribe(async (q) => {
|
||||
const results = getSuggestions(q)
|
||||
// If we're in "single file" mode, the search results array
|
||||
// will contain a single result, related to this file
|
||||
const results = $inFileSearch
|
||||
? getSuggestions(q, { singleFile: $inFileSearch })
|
||||
: getSuggestions(q)
|
||||
|
||||
console.log(results)
|
||||
|
||||
// Search on the whole vault
|
||||
resultNotes.set(results)
|
||||
const firstResult = results[0]
|
||||
if (firstResult) {
|
||||
@@ -57,34 +69,48 @@ function onInputShiftEnter(event: CustomEvent<ResultNote>): void {
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="modal-title">Omnisearch</div>
|
||||
<CmpInput
|
||||
on:enter={onInputEnter}
|
||||
on:shift-enter={onInputShiftEnter}
|
||||
on:ctrl-enter={onInputCtrlEnter}
|
||||
on:enter={onInputEnter}
|
||||
on:shift-enter={onInputShiftEnter}
|
||||
on:ctrl-enter={onInputCtrlEnter}
|
||||
/>
|
||||
|
||||
<div class="prompt-results">
|
||||
{#each $resultNotes as result}
|
||||
<CmpNoteResult selected={result === $selectedNote} note={result} />
|
||||
{/each}
|
||||
</div>
|
||||
<div class="prompt-instructions">
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↑↓</span><span>to navigate</span>
|
||||
<div class="modal-content">
|
||||
<div class="prompt-results">
|
||||
{#if $inFileSearch}
|
||||
<!-- In-file results -->
|
||||
{#if firstResult}
|
||||
{#each firstResult.matches as match}
|
||||
<CmpNoteInternalResult {match} />
|
||||
{/each}
|
||||
{:else}
|
||||
We found 0 result for your search here.
|
||||
{/if}
|
||||
{:else}
|
||||
<!-- Multi-files results -->
|
||||
{#each $resultNotes as result}
|
||||
<CmpNoteResult selected={result === $selectedNote} note={result} />
|
||||
{/each}
|
||||
{/if}
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↵</span><span>to open</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">ctrl ↵</span><span
|
||||
>to open in a new pane</span
|
||||
>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">shift ↵</span><span>to create</span
|
||||
>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">esc</span><span>to dismiss</span>
|
||||
<div class="prompt-instructions">
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↑↓</span><span>to navigate</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">↵</span><span>to open</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">ctrl ↵</span>
|
||||
<span>to open in a new pane</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">shift ↵</span>
|
||||
<span>to create</span>
|
||||
</div>
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">esc</span><span>to dismiss</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
11
src/CmpNoteInternalResult.svelte
Normal file
11
src/CmpNoteInternalResult.svelte
Normal file
@@ -0,0 +1,11 @@
|
||||
<script lang="ts">
|
||||
import type { SearchMatch } from "./globals"
|
||||
|
||||
export let match: SearchMatch
|
||||
</script>
|
||||
|
||||
<div class="suggestion-item omnisearch-result">
|
||||
<div class="omnisearch-result__body">
|
||||
{JSON.stringify(match)}
|
||||
</div>
|
||||
</div>
|
||||
24
src/main.ts
24
src/main.ts
@@ -1,9 +1,9 @@
|
||||
import { Plugin, TFile } from 'obsidian'
|
||||
import { MarkdownView, Plugin, TFile } from 'obsidian'
|
||||
import { OmnisearchModal } from './modal'
|
||||
import { plugin } from './stores'
|
||||
import {
|
||||
addToIndex,
|
||||
instantiateMinisearch,
|
||||
initGlobalSearchIndex,
|
||||
removeFromIndex,
|
||||
removeFromIndexByPath,
|
||||
} from './search'
|
||||
@@ -12,18 +12,34 @@ export default class OmnisearchPlugin extends Plugin {
|
||||
async onload(): Promise<void> {
|
||||
plugin.set(this)
|
||||
|
||||
await instantiateMinisearch()
|
||||
await initGlobalSearchIndex()
|
||||
|
||||
// Commands to display Omnisearch modal
|
||||
this.addCommand({
|
||||
id: 'show-modal',
|
||||
name: 'Open Omnisearch',
|
||||
name: 'Vault search',
|
||||
// hotkeys: [{ modifiers: ['Mod'], key: 'o' }],
|
||||
callback: () => {
|
||||
new OmnisearchModal(this).open()
|
||||
},
|
||||
})
|
||||
|
||||
this.addCommand({
|
||||
id: 'show-modal-infile',
|
||||
name: 'In-file search',
|
||||
// hotkeys: [{ modifiers: ['Mod'], key: 'o' }],
|
||||
checkCallback: (checking: boolean) => {
|
||||
const view = this.app.workspace.getActiveViewOfType(MarkdownView)
|
||||
if (view) {
|
||||
if (!checking) {
|
||||
new OmnisearchModal(this, view.file).open()
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
})
|
||||
|
||||
// Listeners to keep the search index up-to-date
|
||||
this.registerEvent(
|
||||
this.app.vault.on('create', file => {
|
||||
|
||||
14
src/modal.ts
14
src/modal.ts
@@ -1,13 +1,19 @@
|
||||
import { Modal } from 'obsidian'
|
||||
import { Modal, TFile } from 'obsidian'
|
||||
import type OmnisearchPlugin from './main'
|
||||
import CmpModal from './CmpModal.svelte'
|
||||
import { modal } from './stores'
|
||||
import { inFileSearch, modal } from './stores'
|
||||
|
||||
export class OmnisearchModal extends Modal {
|
||||
constructor(plugin: OmnisearchPlugin) {
|
||||
constructor(plugin: OmnisearchPlugin, file?: TFile) {
|
||||
super(plugin.app)
|
||||
// Remove all the default modal's children (except the close button)
|
||||
// so that we can more easily customize it
|
||||
const closeEl = this.containerEl.find('.modal-close-button')
|
||||
this.modalEl.replaceChildren()
|
||||
this.modalEl.append(closeEl)
|
||||
this.modalEl.addClass('omnisearch-modal', 'prompt')
|
||||
this.modalEl.replaceChildren() // Remove all the default Modal's children
|
||||
|
||||
inFileSearch.set(file ?? null)
|
||||
|
||||
modal.set(this)
|
||||
|
||||
|
||||
@@ -1,21 +1,15 @@
|
||||
import { Notice, TFile, type TAbstractFile } from 'obsidian'
|
||||
import MiniSearch from 'minisearch'
|
||||
import MiniSearch, { type SearchResult } from 'minisearch'
|
||||
import type { IndexedNote, ResultNote, SearchMatch } from './globals'
|
||||
import { indexedNotes, plugin } from './stores'
|
||||
import { get } from 'svelte/store'
|
||||
import {
|
||||
escapeRegex,
|
||||
extractHeadingsFromCache,
|
||||
getAllIndices,
|
||||
stringsToRegex,
|
||||
wait,
|
||||
} from './utils'
|
||||
import { extractHeadingsFromCache, stringsToRegex, wait } from './utils'
|
||||
|
||||
let minisearch: MiniSearch<IndexedNote>
|
||||
let minisearchInstance: MiniSearch<IndexedNote>
|
||||
|
||||
export async function instantiateMinisearch(): Promise<void> {
|
||||
export async function initGlobalSearchIndex(): Promise<void> {
|
||||
indexedNotes.set({})
|
||||
minisearch = new MiniSearch({
|
||||
minisearchInstance = new MiniSearch({
|
||||
idField: 'path',
|
||||
fields: ['basename', 'content', 'headings1', 'headings2', 'headings3'],
|
||||
})
|
||||
@@ -54,23 +48,34 @@ export function getMatches(text: string, reg: RegExp): SearchMatch[] {
|
||||
return matches
|
||||
}
|
||||
|
||||
export function getSuggestions(query: string): ResultNote[] {
|
||||
const results = minisearch
|
||||
.search(query, {
|
||||
prefix: true,
|
||||
fuzzy: term => (term.length > 4 ? 0.2 : false),
|
||||
combineWith: 'AND',
|
||||
boost: {
|
||||
basename: 2,
|
||||
headings1: 1.5,
|
||||
headings2: 1.3,
|
||||
headings3: 1.1,
|
||||
},
|
||||
})
|
||||
.sort((a, b) => b.score - a.score)
|
||||
.slice(0, 50)
|
||||
// console.log(`Omnisearch - Results for "${query}"`)
|
||||
// console.log(results)
|
||||
function search(query: string): SearchResult[] {
|
||||
return minisearchInstance.search(query, {
|
||||
prefix: true,
|
||||
fuzzy: term => (term.length > 4 ? 0.2 : false),
|
||||
combineWith: 'AND',
|
||||
boost: {
|
||||
basename: 2,
|
||||
headings1: 1.5,
|
||||
headings2: 1.3,
|
||||
headings3: 1.1,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function getSuggestions(
|
||||
query: string,
|
||||
options?: Partial<{ singleFile: TFile }>,
|
||||
): ResultNote[] {
|
||||
let results = search(query)
|
||||
if (options?.singleFile) {
|
||||
const file = options.singleFile
|
||||
const result = results.find(r => r.id === file.path)
|
||||
if (result) results = [result]
|
||||
else results = []
|
||||
}
|
||||
else {
|
||||
results = results.sort((a, b) => b.score - a.score).slice(0, 50)
|
||||
}
|
||||
|
||||
const suggestions = results.map(result => {
|
||||
const note = indexedNotes.get(result.id)
|
||||
@@ -80,17 +85,11 @@ export function getSuggestions(query: string): ResultNote[] {
|
||||
const words = Object.keys(result.match)
|
||||
const matches = getMatches(note.content, stringsToRegex(words))
|
||||
const resultNote: ResultNote = {
|
||||
// searchResult: result,
|
||||
foundWords: words,
|
||||
occurence: 0,
|
||||
matches,
|
||||
...note,
|
||||
}
|
||||
// if (note.basename === 'Search') {
|
||||
// console.log('=======')
|
||||
// console.log(result)
|
||||
// console.log(resultNote)
|
||||
// }
|
||||
return resultNote
|
||||
})
|
||||
|
||||
@@ -127,7 +126,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
|
||||
? extractHeadingsFromCache(fileCache, 3).join(' ')
|
||||
: '',
|
||||
}
|
||||
minisearch.add(note)
|
||||
minisearchInstance.add(note)
|
||||
indexedNotes.add(note)
|
||||
}
|
||||
catch (e) {
|
||||
@@ -146,7 +145,7 @@ export function removeFromIndex(file: TAbstractFile): void {
|
||||
export function removeFromIndexByPath(path: string): void {
|
||||
const note = indexedNotes.get(path)
|
||||
if (note) {
|
||||
minisearch.remove(note)
|
||||
minisearchInstance.remove(note)
|
||||
indexedNotes.remove(path)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { TFile } from 'obsidian'
|
||||
import { get, writable } from 'svelte/store'
|
||||
import type { IndexedNote, ResultNote } from './globals'
|
||||
import type OmnisearchPlugin from './main'
|
||||
@@ -52,9 +53,37 @@ function createSelectedNote() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If this field is set, the search will be limited to the given file
|
||||
*/
|
||||
export const inFileSearch = writable<TFile | null>(null)
|
||||
|
||||
/**
|
||||
* The current search query
|
||||
*/
|
||||
export const searchQuery = writable<string>('')
|
||||
|
||||
/**
|
||||
* The search results list, according to the current search query
|
||||
*/
|
||||
export const resultNotes = writable<ResultNote[]>([])
|
||||
export const plugin = writable<OmnisearchPlugin>()
|
||||
export const modal = writable<OmnisearchModal>()
|
||||
|
||||
/**
|
||||
* The currently selected/hovered note in the results list
|
||||
*/
|
||||
export const selectedNote = createSelectedNote()
|
||||
|
||||
/**
|
||||
* A reference to the plugin instance
|
||||
*/
|
||||
export const plugin = writable<OmnisearchPlugin>()
|
||||
|
||||
/**
|
||||
* A reference to the modal instance
|
||||
*/
|
||||
export const modal = writable<OmnisearchModal>()
|
||||
|
||||
/**
|
||||
* The entire list of indexed notes, constantly kept up-to-date.
|
||||
*/
|
||||
export const indexedNotes = createIndexedNotes()
|
||||
|
||||
@@ -5,7 +5,6 @@ import {
|
||||
regexYaml,
|
||||
} from './globals'
|
||||
import type { SearchMatch } from './globals'
|
||||
import { uniqBy } from 'lodash-es'
|
||||
|
||||
export function highlighter(str: string): string {
|
||||
return '<span class="search-result-file-matched-text">' + str + '</span>'
|
||||
|
||||
Reference in New Issue
Block a user