Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "To get the active editor ..." #20

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { type 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