Skip to content

Commit

Permalink
fix: prompt to create new toolbar appearing when other props change #194
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgurney committed Dec 8, 2024
1 parent c3099d7 commit 4bc0d24
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
};

/*************************************************************************
Expand Down

0 comments on commit 4bc0d24

Please sign in to comment.