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
This commit is contained in:
Bao 2024-10-31 09:02:36 -05:00 committed by GitHub
parent ba94e13835
commit a5fdaec56c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -153,6 +153,7 @@ export default class VimrcPlugin extends Plugin {
if (!view) return;
let cm = this.getCodeMirror(view);
if (!cm) return;
if (
this.getCursorActivityHandlers(cm).some(
(e: { name: string }) => e.name === "updateSelection")
@ -180,6 +181,8 @@ export default class VimrcPlugin extends Plugin {
this.currentVimStatus = vimStatus.normal;
if (this.settings.displayVimMode)
this.updateVimStatusBar();
if (!cmEditor) return;
cmEditor.off('vim-mode-change', this.logVimModeChange);
cmEditor.on('vim-mode-change', this.logVimModeChange);
@ -580,7 +583,9 @@ export default class VimrcPlugin extends Plugin {
this.vimChordStatusBar.parentElement.insertBefore(this.vimChordStatusBar, parent.firstChild);
this.vimChordStatusBar.style.marginRight = "auto";
let cmEditor = this.getCodeMirror(this.getActiveView());
const view = this.getActiveView();
if (!view) return;
let cmEditor = this.getCodeMirror(view);
// See https://codemirror.net/doc/manual.html#vimapi_events for events.
cmEditor.off('vim-keypress', this.onVimKeypress);
cmEditor.on('vim-keypress', this.onVimKeypress);