Skip to content

Commit

Permalink
fix: Deregister CodeMirror extension on unload
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersismail committed Feb 12, 2023
1 parent cc4854b commit a3e98fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
11 changes: 9 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { StateField } from "@codemirror/state";
import { Editor, MarkdownView, Plugin } from "obsidian";
import { startSession, abortSession } from "./perilous";
import {
Expand All @@ -9,6 +10,7 @@ import { log } from "./utilities";

export default class PerilousWritingPlugin extends Plugin {
settings: PerilousWritingSettings;
cmExtensions: Array<StateField<unknown>> = [];

async onload() {
await this.loadSettings();
Expand All @@ -18,15 +20,15 @@ export default class PerilousWritingPlugin extends Plugin {
id: "perilous-writing-short-session",
name: `Begin short session`,
editorCallback: (editor: Editor, view: MarkdownView) => {
startSession(editor, view, this, "short");
startSession(editor, view, this, this.cmExtensions, "short");
},
});

this.addCommand({
id: "perilous-writing-long-session",
name: `Begin long session`,
editorCallback: (editor: Editor, view: MarkdownView) => {
startSession(editor, view, this, "long");
startSession(editor, view, this, this.cmExtensions, "long");
},
});

Expand All @@ -35,6 +37,11 @@ export default class PerilousWritingPlugin extends Plugin {

onunload() {
abortSession();
// Remove the CodeMirror transaction handler, if it still exists.
if (this.cmExtensions.length > 0) {
this.cmExtensions.pop();
this.app.workspace.updateOptions();
}
log("Unloaded plugin");
}

Expand Down
24 changes: 11 additions & 13 deletions src/perilous/perilous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function startSession(
editor: Editor,
view: MarkdownView,
plugin: PerilousWritingPlugin,
cmExtensions: Array<StateField<unknown>>,
sessionType: SessionType
) {
const sessionLength =
Expand Down Expand Up @@ -68,23 +69,21 @@ export function startSession(
log("Removed: CodeMirror hook; &c.");
},
};
const cmExtensions: Array<StateField<unknown>> = [];

const scheduler = new Scheduler(sessionLength, hooks);

scheduler.initialise((eventCallback: () => void) => {
const onInputKeypress = StateField.define({
create: () => null,
update: (_, transaction) => {
if (!transaction.docChanged) {
return null;
}
if (transaction.isUserEvent("input")) {
eventCallback();
}
create: () => null,
update: (_, transaction) => {
if (!transaction.docChanged) {
return null;
},
})
}
if (transaction.isUserEvent("input")) {
eventCallback();
}
return null;
},
});
cmExtensions.push(onInputKeypress);
plugin.registerEditorExtension(cmExtensions);
log("Registered CodeMirror hook");
Expand All @@ -93,7 +92,6 @@ export function startSession(

export function abortSession() {
removeProgressBar();
// TODO: Remove CodeMirror event logger
}

function getSnapshot(editor: Editor): [string, EditorPosition] {
Expand Down

0 comments on commit a3e98fa

Please sign in to comment.