Skip to content

Commit

Permalink
Avoid acting when no editor is in focus (e.g.: settings pages)
Browse files Browse the repository at this point in the history
  • Loading branch information
jobedom committed Mar 24, 2024
1 parent 9f368b1 commit 7dbb291
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Notice, MarkdownView, WorkspaceWindow, Scope, Plugin, PluginSettingTab, Setting } from "obsidian";
import { App, Notice, MarkdownView, Scope, Plugin, PluginSettingTab, Setting } from "obsidian";

interface HemingwayModePluginSettings {
enabled: boolean;
Expand All @@ -22,6 +22,7 @@ export default class HemingwayModePlugin extends Plugin {
settings: HemingwayModePluginSettings;
keyMapScope: Scope;
statusBar: HTMLElement;
keymapInstalled: boolean;

async onload() {
this.addSettingTab(new HemingwayModeSettingTab(this.app, this));
Expand All @@ -37,12 +38,26 @@ export default class HemingwayModePlugin extends Plugin {
});

this.app.workspace.on("active-leaf-change", async () => {
console.log("active-leaf-change");
await this.updateStatus(true);
});

await this.loadSettings();
this.buildKeyMapScope(this.settings.allowBackspace);
this.keymapInstalled = false;
await this.updateStatus(true);

this.registerInterval(
window.setInterval(async () => {
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView && this.settings.enabled) {
if (markdownView.editor.hasFocus()) {
await this.installHemingwayKeymap();
} else {
await this.uninstallHemingwayKeymap();
}
}
}, 500)
);

this.statusBar = this.addStatusBarItem();
this.statusBar.addClass("hemingway-mode-status");
Expand Down Expand Up @@ -133,11 +148,15 @@ export default class HemingwayModePlugin extends Plugin {
}

async installHemingwayKeymap() {
if (this.keymapInstalled) return;
this.app.keymap.pushScope(this.keyMapScope);
this.keymapInstalled = true;
}

async uninstallHemingwayKeymap() {
if (!this.keymapInstalled) return;
this.app.keymap.popScope(this.keyMapScope);
this.keymapInstalled = false;
}

async setupView() {
Expand Down

0 comments on commit 7dbb291

Please sign in to comment.