Mercurial > obsidian-remember-file-state
comparison src/main.ts @ 28:fbaf7c7126be
Don't restore file state if we're just switching to another pane
To do this we have to track the opened file path in the new editor view
and check if it matches the last file path we knew for that view.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 14 Jul 2022 14:36:44 -0700 |
parents | 3d0ac176118f |
children | 66cada11efb8 |
comparison
equal
deleted
inserted
replaced
27:3d0ac176118f | 28:fbaf7c7126be |
---|---|
88 | 88 |
89 // Don't restore state on the next file being opened. | 89 // Don't restore state on the next file being opened. |
90 private _suppressNextFileOpen: boolean = false; | 90 private _suppressNextFileOpen: boolean = false; |
91 // Next unique ID to identify views without keeping references to them. | 91 // Next unique ID to identify views without keeping references to them. |
92 private _nextUniqueViewId: number = 0; | 92 private _nextUniqueViewId: number = 0; |
93 | |
94 // Remember last open file in each view. | |
95 private _lastOpenFiles: Record<string, string> = {}; | |
93 | 96 |
94 // Functions to unregister any monkey-patched view hooks on plugin unload. | 97 // Functions to unregister any monkey-patched view hooks on plugin unload. |
95 private _viewUninstallers: Record<string, Function> = {}; | 98 private _viewUninstallers: Record<string, Function> = {}; |
96 // Functions to unregister any global callbacks on plugin unload. | 99 // Functions to unregister any global callbacks on plugin unload. |
97 private _globalUninstallers: Function[] = []; | 100 private _globalUninstallers: Function[] = []; |
162 console.debug("Found markdown view without an ID!", filePath); | 165 console.debug("Found markdown view without an ID!", filePath); |
163 } | 166 } |
164 }); | 167 }); |
165 console.debug(`Unregistered ${numViews} view callbacks`); | 168 console.debug(`Unregistered ${numViews} view callbacks`); |
166 this._viewUninstallers = {}; | 169 this._viewUninstallers = {}; |
170 this._lastOpenFiles = {}; | |
167 | 171 |
168 // Run global unhooks. | 172 // Run global unhooks. |
169 this._globalUninstallers.forEach((cb) => cb()); | 173 this._globalUninstallers.forEach((cb) => cb()); |
170 } | 174 } |
171 | 175 |
179 | 183 |
180 private readonly registerOnUnloadFile = function(view: MarkdownView) { | 184 private readonly registerOnUnloadFile = function(view: MarkdownView) { |
181 var filePath = view.file.path; | 185 var filePath = view.file.path; |
182 var viewId = this.getUniqueViewId(view as unknown as ViewWithID, true); | 186 var viewId = this.getUniqueViewId(view as unknown as ViewWithID, true); |
183 if (viewId in this._viewUninstallers) { | 187 if (viewId in this._viewUninstallers) { |
184 console.debug(`View ${viewId} is already registered`, filePath); | |
185 return; | 188 return; |
186 } | 189 } |
187 | 190 |
188 console.debug(`Registering callback on view ${viewId}`, filePath); | 191 console.debug(`Registering callback on view ${viewId}`, filePath); |
189 const _this = this; | 192 const _this = this; |
204 // @ts-ignore | 207 // @ts-ignore |
205 var plugin: RememberFileStatePlugin = app.plugins.getPlugin("obsidian-remember-file-state"); | 208 var plugin: RememberFileStatePlugin = app.plugins.getPlugin("obsidian-remember-file-state"); |
206 if (plugin) { | 209 if (plugin) { |
207 console.debug(`Unregistering view ${viewId} callback`, filePath); | 210 console.debug(`Unregistering view ${viewId} callback`, filePath); |
208 delete plugin._viewUninstallers[viewId]; | 211 delete plugin._viewUninstallers[viewId]; |
212 delete plugin._lastOpenFiles[viewId]; | |
209 uninstall(); | 213 uninstall(); |
210 } else { | 214 } else { |
211 console.debug( | 215 console.debug( |
212 "Plugin obsidian-remember-file-state has been unloaded, ignoring unregister"); | 216 "Plugin obsidian-remember-file-state has been unloaded, ignoring unregister"); |
213 } | 217 } |
222 if (openedFile) { | 226 if (openedFile) { |
223 var activeView: MarkdownView = this.app.workspace.getActiveViewOfType(MarkdownView); | 227 var activeView: MarkdownView = this.app.workspace.getActiveViewOfType(MarkdownView); |
224 if (activeView) { | 228 if (activeView) { |
225 this.registerOnUnloadFile(activeView); | 229 this.registerOnUnloadFile(activeView); |
226 | 230 |
231 var isRealFileOpen = true; | |
232 const viewId = this.getUniqueViewId(activeView as ViewWithID); | |
233 if (viewId != undefined) { | |
234 const lastOpenFileInView = this._lastOpenFiles[viewId]; | |
235 isRealFileOpen = (lastOpenFileInView != openedFile.path); | |
236 this._lastOpenFiles[viewId] = openedFile.path; | |
237 } | |
238 | |
227 // Don't restore the file state if: | 239 // Don't restore the file state if: |
228 // - We are suppressing it explicitly (such as if the file was | 240 // - We are suppressing it explicitly (such as if the file was |
229 // opened via clicking a hyperlink) | 241 // opened via clicking a hyperlink) |
230 // - The file is already currently open in another pane | 242 // - The file is already currently open in another pane |
231 if (!this._suppressNextFileOpen && !this.isFileMultiplyOpen(openedFile)) { | 243 // - The file was already opened in this pane, and we're just |
244 // returning to it. | |
245 if (!this._suppressNextFileOpen && | |
246 !this.isFileMultiplyOpen(openedFile) && | |
247 isRealFileOpen | |
248 ) { | |
232 try { | 249 try { |
233 this.restoreFileState(openedFile, activeView); | 250 this.restoreFileState(openedFile, activeView); |
234 } catch (err) { | 251 } catch (err) { |
235 console.error("Couldn't restore file state: ", err); | 252 console.error("Couldn't restore file state: ", err); |
236 } | 253 } |