diff --git a/package.json b/package.json index 572555a..ff0d677 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scambier.obsidian-search", - "version": "1.9.0-beta.1", + "version": "1.9.0-beta.2", "description": "A search engine for Obsidian", "main": "dist/main.js", "scripts": { diff --git a/src/cache-manager.ts b/src/cache-manager.ts index ba5f0de..1065165 100644 --- a/src/cache-manager.ts +++ b/src/cache-manager.ts @@ -1,4 +1,4 @@ -import { Notice, TFile } from 'obsidian' +import { Notice } from 'obsidian' import type { DocumentRef, IndexedDocument } from './globals' import { database } from './database' import type { AsPlainObject } from 'minisearch' @@ -11,13 +11,11 @@ import { isFilePDF, isFilePlaintext, makeMD5, - removeDiacritics + removeDiacritics, } from './tools/utils' -import { getImageText, getPdfText } from "obsidian-text-extract"; +import { getImageText, getPdfText } from 'obsidian-text-extract' -async function getIndexedDocument( - path: string -): Promise { +async function getIndexedDocument(path: string): Promise { const file = app.vault.getFiles().find(f => f.path === path) if (!file) throw new Error(`Invalid file path: "${path}"`) let content: string @@ -76,12 +74,20 @@ class CacheManager { */ private nextQueryIsEmpty = false + /** + * The "live cache", containing all indexed vault files + * in the form of IndexedDocuments + */ private documents: Map = new Map() public async addToLiveCache(path: string): Promise { const doc = await getIndexedDocument(path) this.documents.set(path, doc) - console.log(path) + // console.log(path) + } + + public removeFromLiveCache(path: string): void { + this.documents.delete(path) } public async getDocument(path: string): Promise { diff --git a/src/components/InputSearch.svelte b/src/components/InputSearch.svelte index fa839e2..72b23c4 100644 --- a/src/components/InputSearch.svelte +++ b/src/components/InputSearch.svelte @@ -32,7 +32,7 @@ // the next time we open the modal, the search field will be empty cacheManager.addToSearchHistory('') dispatch('input', value) - }, 200) + }, 500)
diff --git a/src/components/ModalVault.svelte b/src/components/ModalVault.svelte index ad240f0..216d540 100644 --- a/src/components/ModalVault.svelte +++ b/src/components/ModalVault.svelte @@ -3,13 +3,10 @@ import { onDestroy, onMount, tick } from 'svelte' import InputSearch from './InputSearch.svelte' import ModalContainer from './ModalContainer.svelte' - import { eventBus, indexingStep, IndexingStepType, type ResultNote } from 'src/globals' + import { eventBus, indexingStep, IndexingStepType, type ResultNote, } from 'src/globals' import { createNote, openNote } from 'src/tools/notes' - import { getCtrlKeyLabel, getExtension, isFilePDF, loopIndex } from 'src/tools/utils' - import { - OmnisearchInFileModal, - type OmnisearchVaultModal, - } from 'src/components/modals' + import { getCtrlKeyLabel, getExtension, isFilePDF, loopIndex, } from 'src/tools/utils' + import { OmnisearchInFileModal, type OmnisearchVaultModal, } from 'src/components/modals' import ResultItemVault from './ResultItemVault.svelte' import { Query } from 'src/search/query' import { settings } from '../settings' @@ -25,12 +22,18 @@ let resultNotes: ResultNote[] = [] let query: Query let indexingStepDesc = '' + let searching = true $: selectedNote = resultNotes[selectedIndex] $: searchQuery = searchQuery ?? previousQuery $: if (searchQuery) { - updateResults() + resultNotes = [] + searching = true + updateResults().then(() => { + searching = false + }) } else { + searching = false resultNotes = [] } $: { @@ -246,11 +249,13 @@ on:mousemove="{_ => (selectedIndex = i)}" on:click="{onClick}" /> {/each} - {#if !resultNotes.length && searchQuery} -
+
+ {#if !resultNotes.length && searchQuery && !searching} We found 0 result for your search here. -
- {/if} + {:else if searching} + Searching... + {/if} +
diff --git a/src/main.ts b/src/main.ts index 57141fb..32301a4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,13 +6,7 @@ import { import { loadSettings, settings, SettingsTab, showExcerpt } from './settings' import { eventBus, EventNames, indexingStep, IndexingStepType } from './globals' import api from './tools/api' -import { - isFileImage, - isFileIndexable, - isFilePDF, - isFilePlaintext, - wait, -} from './tools/utils' +import { isFileIndexable, isFilePlaintext } from './tools/utils' import { OmnisearchCache } from './database' import * as NotesIndex from './notes-index' import { searchEngine } from './search/omnisearch' @@ -59,23 +53,28 @@ export default class OmnisearchPlugin extends Plugin { app.workspace.onLayoutReady(async () => { // Listeners to keep the search index up-to-date this.registerEvent( - this.app.vault.on('create', file => { + this.app.vault.on('create', async file => { + cacheManager.addToLiveCache(file.path) searchEngine.addFromPaths([file.path], false) }) ) this.registerEvent( this.app.vault.on('delete', file => { + cacheManager.removeFromLiveCache(file.path) searchEngine.removeFromPaths([file.path]) }) ) this.registerEvent( this.app.vault.on('modify', async file => { + cacheManager.addToLiveCache(file.path) NotesIndex.markNoteForReindex(file) }) ) this.registerEvent( this.app.vault.on('rename', async (file, oldPath) => { if (file instanceof TFile && isFilePlaintext(file.path)) { + cacheManager.removeFromLiveCache(oldPath) + cacheManager.addToLiveCache(file.path) searchEngine.removeFromPaths([oldPath]) await searchEngine.addFromPaths([file.path], false) } @@ -115,7 +114,7 @@ async function populateIndex(): Promise { const files = app.vault.getFiles().filter(f => isFileIndexable(f.path)) // Map documents in the background - files.forEach(f => cacheManager.addToLiveCache(f.path)) + // Promise.all(files.map(f => cacheManager.addToLiveCache(f.path))) if (!Platform.isIosApp) { console.time('Omnisearch - Loading index from cache') diff --git a/src/search/omnisearch.ts b/src/search/omnisearch.ts index f4c3bd4..30c41da 100644 --- a/src/search/omnisearch.ts +++ b/src/search/omnisearch.ts @@ -1,8 +1,4 @@ -import MiniSearch, { - type AsPlainObject, - type Options, - type SearchResult, -} from 'minisearch' +import MiniSearch, { type Options, type SearchResult } from 'minisearch' import type { DocumentRef, IndexedDocument, @@ -183,6 +179,8 @@ export class Omnisearch { }) } + results = results.slice(0, 50) + const documents = await Promise.all( results.map(async result => await cacheManager.getDocument(result.id)) ) @@ -212,11 +210,9 @@ export class Omnisearch { } // FIXME: // Dedupe results - clutch for https://github.com/scambier/obsidian-omnisearch/issues/129 - results = results - .filter( - (result, index, arr) => arr.findIndex(t => t.id === result.id) === index - ) - .slice(0, 50) + results = results.filter( + (result, index, arr) => arr.findIndex(t => t.id === result.id) === index + ) this.previousResults = results