Mercurial > obsidian-remember-file-state
comparison src/main.ts @ 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 | 7975d7c73f8a |
children | 114d7e6d2633 |
comparison
equal
deleted
inserted
replaced
1:f6dee41d58da | 2:f3297d90329d |
---|---|
5 Plugin, | 5 Plugin, |
6 View | 6 View |
7 } from 'obsidian'; | 7 } from 'obsidian'; |
8 | 8 |
9 import { | 9 import { |
10 EditorState, | |
10 EditorSelection | 11 EditorSelection |
11 } from '@codemirror/state'; | 12 } from '@codemirror/state'; |
12 | 13 |
13 import { | 14 import { |
14 around | 15 around |
80 | 81 |
81 this.addSettingTab(new RememberFileStatePluginSettingTab(this.app, this)); | 82 this.addSettingTab(new RememberFileStatePluginSettingTab(this.app, this)); |
82 } | 83 } |
83 | 84 |
84 onunload() { | 85 onunload() { |
85 var uninstallers = this._viewUninstallers.values(); | 86 var uninstallers = Object.values(this._viewUninstallers); |
86 console.debug(`Unregistering ${uninstallers.length} view callbacks`); | 87 console.debug(`Unregistering ${uninstallers.length} view callbacks`); |
87 uninstallers.values().forEach((cb) => cb()); | 88 uninstallers.forEach((cb) => cb()); |
88 this._viewUninstallers = {}; | 89 this._viewUninstallers = {}; |
89 | 90 |
90 this._globalUninstallers.forEach((cb) => cb()); | 91 this._globalUninstallers.forEach((cb) => cb()); |
91 } | 92 } |
92 | 93 |
126 openedFile: TFile | 127 openedFile: TFile |
127 ): Promise<void> => { | 128 ): Promise<void> => { |
128 // If `openedFile` is null, it's because the last pane was closed | 129 // If `openedFile` is null, it's because the last pane was closed |
129 // and there is now an empty pane. | 130 // and there is now an empty pane. |
130 if (openedFile) { | 131 if (openedFile) { |
131 var activeView = this.app.workspace.activeLeaf.view; | 132 var activeView = this.app.workspace.getActiveViewOfType(MarkdownView); |
132 this.registerOnUnloadFile(activeView); | 133 this.registerOnUnloadFile(activeView); |
133 | 134 |
134 if (!this._suppressNextFileOpen) { | 135 if (!this._suppressNextFileOpen) { |
135 await this.restoreFileState(openedFile, activeView); | 136 this.restoreFileState(openedFile, activeView); |
136 } else { | 137 } else { |
137 this._suppressNextFileOpen = false; | 138 this._suppressNextFileOpen = false; |
138 } | 139 } |
139 } | 140 } |
140 } | 141 } |
164 this.forgetExcessFiles(); | 165 this.forgetExcessFiles(); |
165 } | 166 } |
166 console.debug("Remember file state for:", file.path); | 167 console.debug("Remember file state for:", file.path); |
167 } | 168 } |
168 | 169 |
169 private readonly restoreFileState = async (file: TFile, view: View): Promise<void> => { | 170 private restoreFileState(file: TFile, view: View) { |
170 const existingFile = this.data.rememberedFiles.find( | 171 const existingFile = this.data.rememberedFiles.find( |
171 (curFile) => curFile.path === file.path | 172 (curFile) => curFile.path === file.path |
172 ); | 173 ); |
173 if (existingFile) { | 174 if (existingFile) { |
174 console.debug("Restoring file state for:", file.path); | 175 console.debug("Restoring file state for:", file.path); |
175 const stateData = existingFile.stateData; | 176 const stateData = existingFile.stateData; |
176 view.editor.scrollTo(stateData.scrollInfo.left, stateData.scrollInfo.top); | 177 view.editor.scrollTo(stateData.scrollInfo.left, stateData.scrollInfo.top); |
177 view.editor.cm.state.selection = EditorSelection.fromJSON(stateData.selection); | 178 var transaction = view.editor.cm.state.update({ |
179 selection: EditorSelection.fromJSON(stateData.selection)}) | |
180 view.editor.cm.dispatch(transaction); | |
178 } | 181 } |
179 } | 182 } |
180 | 183 |
181 private forgetExcessFiles() { | 184 private forgetExcessFiles() { |
182 const keepMax = this.settings.rememberMaxFiles; | 185 const keepMax = this.settings.rememberMaxFiles; |