From 4bc0d24d33743a2734238afd43c8814e1a24935d Mon Sep 17 00:00:00 2001 From: Chris Gurney Date: Sun, 8 Dec 2024 08:39:00 -0500 Subject: [PATCH] fix: prompt to create new toolbar appearing when other props change #194 --- src/main.ts | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/main.ts b/src/main.ts index 473e9752..044517eb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,6 +31,10 @@ export default class NoteToolbarPlugin extends Plugin { // track the last used callout link, for the menu URI lastCalloutLink: Element | null = null; + // track the last used file and property, to prompt if Note Toolbar property references unknown toolbar + lastFileOpened: TFile | null; + lastNtbProperty: string | undefined; + // for tracking other plugins available (for adapters and rendering edge cases) hasPlugin: { [key: string]: boolean } = { 'dataview': false, @@ -289,24 +293,31 @@ export default class NoteToolbarPlugin extends Plugin { */ metadataCacheListener = (file: TFile, data: any, cache: CachedMetadata) => { // debugLog("metadata-changed: " + file.name); - if (this.app.workspace.getActiveFile() === file) { + const activeFile = this.app.workspace.getActiveFile(); + if (activeFile === file) { this.checkAndRenderToolbar(file, cache.frontmatter); } - // FIXME: Temporarily commented out until can monitor for property and filename change // prompt to create a toolbar if it doesn't exist in the Note Toolbar property - // const notetoolbarProp: string[] = cache.frontmatter?.[this.settings.toolbarProp] ?? []; - // if (notetoolbarProp.length > 0) { - // const ignoreToolbar = notetoolbarProp.includes('none') ? true : false; - // const matchingToolbar = ignoreToolbar ? undefined : this.settingsManager.getToolbarFromProps(notetoolbarProp); - // if (!matchingToolbar && !ignoreToolbar) { - // const notice = new Notice(t('notice.warning-no-matching-toolbar', { toolbar: notetoolbarProp[0] }), 7500); - // this.registerDomEvent(notice.noticeEl, 'click', async () => { - // const newToolbar = await this.settingsManager.newToolbar(notetoolbarProp[0]); - // this.settingsManager.openToolbarSettings(newToolbar); - // }); - // } - // } + const notetoolbarProp: string[] = cache.frontmatter?.[this.settings.toolbarProp] ?? []; + if (notetoolbarProp.length > 0) { + const ignoreToolbar = notetoolbarProp.includes('none') ? true : false; + // make sure just the relevant property changed in the open file + if (this.lastFileOpened !== activeFile) this.lastNtbProperty = undefined; + if (notetoolbarProp[0] !== this.lastNtbProperty) { + const matchingToolbar = ignoreToolbar ? undefined : this.settingsManager.getToolbarFromProps(notetoolbarProp); + if (!matchingToolbar && !ignoreToolbar) { + const notice = new Notice(t('notice.warning-no-matching-toolbar', { toolbar: notetoolbarProp[0] }), 7500); + this.registerDomEvent(notice.noticeEl, 'click', async () => { + const newToolbar = await this.settingsManager.newToolbar(notetoolbarProp[0]); + this.settingsManager.openToolbarSettings(newToolbar); + }); + } + } + } + // track current state to look for future Note Toolbar property changes + this.lastNtbProperty = notetoolbarProp[0]; + this.lastFileOpened = activeFile; }; /*************************************************************************