Skip to content

Commit

Permalink
editor bugfix round 2
Browse files Browse the repository at this point in the history
+ fix: Local vars instead of class vars & revert to get editor from view
  • Loading branch information
ALegendsTale committed Jul 3, 2024
1 parent cbdf41c commit 7a1705d
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/paletteMRC.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Editor, MarkdownPostProcessorContext, MarkdownRenderChild, MarkdownView, Notice } from "obsidian";
import { MarkdownPostProcessorContext, MarkdownRenderChild, MarkdownView, Notice } from "obsidian";
import ColorPalette, { urlRegex } from "main";
import { ColorPaletteSettings } from "settings";
import { Palette, PaletteSettings, Status } from "palette";
Expand All @@ -12,7 +12,6 @@ export class PaletteMRC extends MarkdownRenderChild {
input: string;
palette: Palette
context: MarkdownPostProcessorContext;
editor: Editor | undefined;
editModeChanges: { colors: string[], settings: Partial<PaletteSettings> | undefined };

constructor(plugin: ColorPalette, containerEl: HTMLElement, input: string, context: MarkdownPostProcessorContext) {
Expand All @@ -38,10 +37,6 @@ 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 @@ -150,14 +145,15 @@ export class PaletteMRC extends MarkdownRenderChild {
* Gets the palette codeblock input
*/
public getPaletteInput() {
let editor = this.plugin.app.workspace.getActiveViewOfType(MarkdownView)?.editor;
// Palette line start & line end
const paletteSection = this.context.getSectionInfo(this.palette.containerEl);
if(paletteSection && this.editor) return {
if(paletteSection && editor) return {
lines: {
lineStart: paletteSection.lineStart,
lineEnd: paletteSection.lineEnd
},
input: this.editor.getRange(
input: editor.getRange(
{line: paletteSection.lineStart, ch: 0},
{line: paletteSection.lineEnd + 1, ch: 0}
)
Expand All @@ -171,10 +167,11 @@ export class PaletteMRC extends MarkdownRenderChild {
* Sets the palette codeblock input with `replacement`
*/
public setPaletteInput(replacement: string) {
let editor = this.plugin.app.workspace.getActiveViewOfType(MarkdownView)?.editor;
// Palette line start & line end
const paletteSection = this.context.getSectionInfo(this.palette.containerEl);
if(paletteSection && this.editor) {
this.editor.replaceRange(replacement, {line: paletteSection.lineStart, ch: 0}, {line: paletteSection.lineEnd + 1, ch: 0});
if(paletteSection && editor) {
editor.replaceRange(replacement, {line: paletteSection.lineStart, ch: 0}, {line: paletteSection.lineEnd + 1, ch: 0});
return {
lineStart: paletteSection.lineStart,
lineEnd: paletteSection.lineEnd
Expand Down

0 comments on commit 7a1705d

Please sign in to comment.