# HG changeset patch # User Ludovic Chabant # Date 1644476382 28800 # Node ID f3297d90329d99979b0810d13de15c0227c4fb1d # Parent f6dee41d58da581c68593190cf8a322748de1458 Various fixes: - Fix calls to Object.values() - Use transactions to set the CodeMirror editor selection - Don't restore state asynchronously - Remove access to Workspace.activeLeaf diff -r f6dee41d58da -r f3297d90329d src/main.ts --- a/src/main.ts Tue Feb 08 21:54:19 2022 -0800 +++ b/src/main.ts Wed Feb 09 22:59:42 2022 -0800 @@ -7,6 +7,7 @@ } from 'obsidian'; import { + EditorState, EditorSelection } from '@codemirror/state'; @@ -82,9 +83,9 @@ } onunload() { - var uninstallers = this._viewUninstallers.values(); + var uninstallers = Object.values(this._viewUninstallers); console.debug(`Unregistering ${uninstallers.length} view callbacks`); - uninstallers.values().forEach((cb) => cb()); + uninstallers.forEach((cb) => cb()); this._viewUninstallers = {}; this._globalUninstallers.forEach((cb) => cb()); @@ -128,11 +129,11 @@ // If `openedFile` is null, it's because the last pane was closed // and there is now an empty pane. if (openedFile) { - var activeView = this.app.workspace.activeLeaf.view; + var activeView = this.app.workspace.getActiveViewOfType(MarkdownView); this.registerOnUnloadFile(activeView); if (!this._suppressNextFileOpen) { - await this.restoreFileState(openedFile, activeView); + this.restoreFileState(openedFile, activeView); } else { this._suppressNextFileOpen = false; } @@ -166,7 +167,7 @@ console.debug("Remember file state for:", file.path); } - private readonly restoreFileState = async (file: TFile, view: View): Promise => { + private restoreFileState(file: TFile, view: View) { const existingFile = this.data.rememberedFiles.find( (curFile) => curFile.path === file.path ); @@ -174,7 +175,9 @@ console.debug("Restoring file state for:", file.path); const stateData = existingFile.stateData; view.editor.scrollTo(stateData.scrollInfo.left, stateData.scrollInfo.top); - view.editor.cm.state.selection = EditorSelection.fromJSON(stateData.selection); + var transaction = view.editor.cm.state.update({ + selection: EditorSelection.fromJSON(stateData.selection)}) + view.editor.cm.dispatch(transaction); } }