Mercurial > obsidian-remember-file-state
changeset 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 |
files | src/main.ts |
diffstat | 1 files changed, 20 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main.ts Sat May 14 09:33:19 2022 -0700 +++ b/src/main.ts Thu Jul 14 14:36:44 2022 -0700 @@ -91,6 +91,9 @@ // Next unique ID to identify views without keeping references to them. private _nextUniqueViewId: number = 0; + // Remember last open file in each view. + private _lastOpenFiles: Record<string, string> = {}; + // Functions to unregister any monkey-patched view hooks on plugin unload. private _viewUninstallers: Record<string, Function> = {}; // Functions to unregister any global callbacks on plugin unload. @@ -164,6 +167,7 @@ }); console.debug(`Unregistered ${numViews} view callbacks`); this._viewUninstallers = {}; + this._lastOpenFiles = {}; // Run global unhooks. this._globalUninstallers.forEach((cb) => cb()); @@ -181,7 +185,6 @@ var filePath = view.file.path; var viewId = this.getUniqueViewId(view as unknown as ViewWithID, true); if (viewId in this._viewUninstallers) { - console.debug(`View ${viewId} is already registered`, filePath); return; } @@ -206,6 +209,7 @@ if (plugin) { console.debug(`Unregistering view ${viewId} callback`, filePath); delete plugin._viewUninstallers[viewId]; + delete plugin._lastOpenFiles[viewId]; uninstall(); } else { console.debug( @@ -224,11 +228,24 @@ if (activeView) { this.registerOnUnloadFile(activeView); + var isRealFileOpen = true; + const viewId = this.getUniqueViewId(activeView as ViewWithID); + if (viewId != undefined) { + const lastOpenFileInView = this._lastOpenFiles[viewId]; + isRealFileOpen = (lastOpenFileInView != openedFile.path); + this._lastOpenFiles[viewId] = openedFile.path; + } + // Don't restore the file state if: // - We are suppressing it explicitly (such as if the file was - // opened via clicking a hyperlink) + // opened via clicking a hyperlink) // - The file is already currently open in another pane - if (!this._suppressNextFileOpen && !this.isFileMultiplyOpen(openedFile)) { + // - The file was already opened in this pane, and we're just + // returning to it. + if (!this._suppressNextFileOpen && + !this.isFileMultiplyOpen(openedFile) && + isRealFileOpen + ) { try { this.restoreFileState(openedFile, activeView); } catch (err) {