From 52ce4c639142f30c68247858dfe13f6d0271416c Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Sun, 24 Apr 2022 09:38:31 +0200 Subject: [PATCH] #21 Index files after obsidian is correctly loaded --- src/main.ts | 52 +++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/main.ts b/src/main.ts index 3beb00e..91fbf71 100644 --- a/src/main.ts +++ b/src/main.ts @@ -34,32 +34,34 @@ export default class OmnisearchPlugin extends Plugin { }, }) - // Listeners to keep the search index up-to-date - this.registerEvent( - this.app.vault.on('create', file => { - addToIndex(file) - }), - ) - this.registerEvent( - this.app.vault.on('delete', file => { - removeFromIndex(file) - }), - ) - this.registerEvent( - this.app.vault.on('modify', async file => { - removeFromIndex(file) - await addToIndex(file) - }), - ) - this.registerEvent( - this.app.vault.on('rename', async (file, oldPath) => { - if (file instanceof TFile && file.path.endsWith('.md')) { - removeFromIndexByPath(oldPath) + app.workspace.onLayoutReady(async () => { + // Listeners to keep the search index up-to-date + this.registerEvent( + this.app.vault.on('create', file => { + addToIndex(file) + }), + ) + this.registerEvent( + this.app.vault.on('delete', file => { + removeFromIndex(file) + }), + ) + this.registerEvent( + this.app.vault.on('modify', async file => { + removeFromIndex(file) await addToIndex(file) - } - }), - ) + }), + ) + this.registerEvent( + this.app.vault.on('rename', async (file, oldPath) => { + if (file instanceof TFile && file.path.endsWith('.md')) { + removeFromIndexByPath(oldPath) + await addToIndex(file) + } + }), + ) - initGlobalSearchIndex() + await initGlobalSearchIndex() + }) } }