changeset 2:f3297d90329d

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
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 09 Feb 2022 22:59:42 -0800
parents f6dee41d58da
children f669172572a6
files src/main.ts
diffstat 1 files changed, 9 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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<void> => {
+	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);
 		}
 	}