Skip to content

Commit

Permalink
refactor: change how to get active editor
Browse files Browse the repository at this point in the history
  • Loading branch information
udus122 committed Jun 13, 2024
1 parent 8ff60d7 commit 9228523
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ export default class Main extends Plugin {
saveCommandDefinition.callback = () => {
this.originalSaveCallback();

if (this.settings.updateOnSave) {
const editor = this.app.workspace.activeEditor?.editor;

if (this.settings.updateOnSave && editor) {
const operator = new Operator(this.app as UnsafeApp);
operator.updateActiveFile();
operator.updateActiveFile(editor);
}
};
}
Expand Down
13 changes: 2 additions & 11 deletions src/operations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Editor, MarkdownView, type TFile } from "obsidian";
import { Editor, type TFile } from "obsidian";
import { Replacer, UnsafeApp } from "./types";
import { createReplacerFromContent } from "./dataview-publisher";
import { DataviewApi } from "obsidian-dataview";
Expand All @@ -13,8 +13,7 @@ export class Operator {
this.dv = getDataviewAPI(app);
}

async updateActiveFile() {
const editor = this.getEditor();
async updateActiveFile(editor: Editor) {
const cursor = editor.getCursor();
const content = editor.getValue();

Expand All @@ -25,14 +24,6 @@ export class Operator {
editor.setCursor(cursor);
}

private getEditor(): Editor {
const activeLeaf = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeLeaf) {
throw new Error("No active leaf found");
}
return activeLeaf.editor;
}

updateFromSource(source: string) {
const targetTfiles = this.retrieveTfilesFromSource(source);

Expand Down

0 comments on commit 9228523

Please sign in to comment.