Skip to content

Commit a5fdaec

Browse files
authored
fix: guard against uninitialized codemirror instance (#246)
In obsidian (>=v1.7.2?) the codemirror instance is often not unitialized at times, triggering some uncaught exceptions. This is particularly noticable when: - (w/ "Vim chord display" enabled) opening the vault w/o a note opened - closing all tabs and opening a note Add some null guards to avoid these errors
1 parent ba94e13 commit a5fdaec

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export default class VimrcPlugin extends Plugin {
153153
if (!view) return;
154154

155155
let cm = this.getCodeMirror(view);
156+
if (!cm) return;
156157
if (
157158
this.getCursorActivityHandlers(cm).some(
158159
(e: { name: string }) => e.name === "updateSelection")
@@ -180,6 +181,8 @@ export default class VimrcPlugin extends Plugin {
180181
this.currentVimStatus = vimStatus.normal;
181182
if (this.settings.displayVimMode)
182183
this.updateVimStatusBar();
184+
185+
if (!cmEditor) return;
183186
cmEditor.off('vim-mode-change', this.logVimModeChange);
184187
cmEditor.on('vim-mode-change', this.logVimModeChange);
185188

@@ -580,7 +583,9 @@ export default class VimrcPlugin extends Plugin {
580583
this.vimChordStatusBar.parentElement.insertBefore(this.vimChordStatusBar, parent.firstChild);
581584
this.vimChordStatusBar.style.marginRight = "auto";
582585

583-
let cmEditor = this.getCodeMirror(this.getActiveView());
586+
const view = this.getActiveView();
587+
if (!view) return;
588+
let cmEditor = this.getCodeMirror(view);
584589
// See https://codemirror.net/doc/manual.html#vimapi_events for events.
585590
cmEditor.off('vim-keypress', this.onVimKeypress);
586591
cmEditor.on('vim-keypress', this.onVimKeypress);

0 commit comments

Comments
 (0)