Mercurial > obsidian-remember-file-state
changeset 52:586f857a98dd
Move unregistering of all views into a reusable function.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 02 Oct 2023 10:05:02 -0700 |
parents | e932f1b73133 |
children | f2e066bbe343 |
files | src/main.ts |
diffstat | 1 files changed, 33 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main.ts Mon Oct 02 10:04:13 2023 -0700 +++ b/src/main.ts Mon Oct 02 10:05:02 2023 -0700 @@ -163,30 +163,12 @@ } onunload() { - // Run view uninstallers on all current views. - var numViews: number = 0; - this.app.workspace.getLeavesOfType("markdown").forEach( - (leaf: WorkspaceLeaf) => { - const filePath = (leaf.view as MarkdownView).file.path; - const viewId = this.getUniqueViewId(leaf.view as ViewWithID); - if (viewId != undefined) { - var uninstaller = this._viewUninstallers[viewId]; - if (uninstaller) { - console.debug(`RememberFileState: uninstalling hooks for view ${viewId}`, filePath); - uninstaller(leaf.view); - ++numViews; - } else { - console.debug("RememberFileState: found markdown view without an uninstaller!", filePath); - } - // Clear the ID so we don't get confused if the plugin - // is re-enabled later. - this.clearUniqueViewId(leaf.view as ViewWithID); - } else { - console.debug("RememberFileState: found markdown view without an ID!", filePath); - } - }); - console.debug(`RememberFileState: unregistered ${numViews} view callbacks`); - this._viewUninstallers = {}; + console.log("RememberFileState: unloading plugin"); + + // Unregister unload callbacks on all views. + this.unregisterAllViews(); + + // Forget which files are opened in which views. this._lastOpenFiles = {}; // Run global unhooks. @@ -263,6 +245,33 @@ }); } + private readonly unregisterAllViews = function() { + // Run view uninstallers on all current views. + var numViews: number = 0; + this.app.workspace.getLeavesOfType("markdown").forEach( + (leaf: WorkspaceLeaf) => { + const filePath = (leaf.view as MarkdownView).file.path; + const viewId = this.getUniqueViewId(leaf.view as ViewWithID); + if (viewId != undefined) { + var uninstaller = this._viewUninstallers[viewId]; + if (uninstaller) { + console.debug(`RememberFileState: uninstalling hooks for view ${viewId}`, filePath); + uninstaller(leaf.view); + ++numViews; + } else { + console.debug("RememberFileState: found markdown view without an uninstaller!", filePath); + } + // Clear the ID so we don't get confused if the plugin + // is re-enabled later. + this.clearUniqueViewId(leaf.view as ViewWithID); + } else { + console.debug("RememberFileState: found markdown view without an ID!", filePath); + } + }); + console.debug(`RememberFileState: unregistered ${numViews} view callbacks`); + this._viewUninstallers = {}; + } + private readonly onFileOpen = async ( openedFile: TFile ): Promise<void> => {