Skip to content

Commit

Permalink
bugfix for editor instance
Browse files Browse the repository at this point in the history
+ fix: editor instance wasn't loading on startup until palette reloaded
+ Notices for undefined editor
  • Loading branch information
ALegendsTale committed Jul 3, 2024
1 parent 6e35bf5 commit 5a2358b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/paletteMRC.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Editor, MarkdownPostProcessorContext, MarkdownRenderChild, MarkdownView } from "obsidian";
import { Editor, MarkdownPostProcessorContext, MarkdownRenderChild, MarkdownView, Notice } from "obsidian";
import ColorPalette, { urlRegex } from "main";
import { ColorPaletteSettings } from "settings";
import { Palette, PaletteSettings, Status } from "palette";
Expand All @@ -21,7 +21,6 @@ export class PaletteMRC extends MarkdownRenderChild {
this.pluginSettings = plugin.settings;
this.input = input;
this.context = context;
this.editor = this.plugin.app.workspace.getActiveViewOfType(MarkdownView)?.editor;
}

onload(): void {
Expand All @@ -39,6 +38,10 @@ export class PaletteMRC extends MarkdownRenderChild {
paletteMenu.showAtMouseEvent(e);
}
})

this.plugin.app.workspace.on('editor-change', (editor, info) => {
this.editor = editor;
})
}

unload(): void {
Expand Down Expand Up @@ -158,7 +161,10 @@ export class PaletteMRC extends MarkdownRenderChild {
{line: paletteSection.lineStart, ch: 0},
{line: paletteSection.lineEnd + 1, ch: 0}
)
};
}
else {
this.createNotice('The editor has not fully loaded yet.');
}
}

/**
Expand All @@ -174,5 +180,16 @@ export class PaletteMRC extends MarkdownRenderChild {
lineEnd: paletteSection.lineEnd
};
}
else {
this.createNotice('The editor has not fully loaded yet.');
}
}

/**
* Creates a new notice using pre-set settings
* @param message Message to display
*/
public createNotice(message: string) {
new Notice(message, this.pluginSettings.noticeDuration);
}
}

0 comments on commit 5a2358b

Please sign in to comment.