From 96aa5f41924ddd2c344277774f7d892fd52606c7 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Wed, 18 Aug 2021 08:28:04 +0200 Subject: [PATCH] fix: :bug: Register active-leaf-change event if it wasn't registered onLoad --- src/main.ts | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index 62bb428e..13331809 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import { Graph } from "graphlib"; import { addIcon, + EventRef, MarkdownView, Notice, Plugin, @@ -88,10 +89,36 @@ export default class BreadcrumbsPlugin extends Plugin { visited: [string, HTMLDivElement][]; refreshIntervalID: number; currGraphs: BCIndex; + activeLeafChangeEventRef: EventRef; async refreshIndex() { + if (!this.activeLeafChangeEventRef) { + console.log( + "activeLeafChangeEventRef wasn't registered onLoad, registering now" + ); + this.activeLeafChangeEventRef = this.app.workspace.on( + "active-leaf-change", + async () => { + if (this.settings.refreshIndexOnActiveLeafChange) { + // refreshIndex does everything in one + await this.refreshIndex(); + } else { + // If it is not called, active-leaf-change still needs to trigger a redraw + const activeView = this.getActiveMatrixView(); + if (activeView) { + await activeView.draw(); + } + if (this.settings.showTrail) { + await this.drawTrail(); + } + } + } + ); + + this.registerEvent(this.activeLeafChangeEventRef); + } + this.currGraphs = await this.initGraphs(); - debug(this.settings, { hierGs: this.currGraphs }); const activeView = this.getActiveMatrixView(); if (activeView) { await activeView.draw(); @@ -108,6 +135,7 @@ export default class BreadcrumbsPlugin extends Plugin { await this.loadSettings(); + this.activeLeafChangeEventRef = undefined; this.visited = []; this.registerView( @@ -130,8 +158,9 @@ export default class BreadcrumbsPlugin extends Plugin { await this.drawTrail(); } - this.registerEvent( - this.app.workspace.on("active-leaf-change", async () => { + this.activeLeafChangeEventRef = this.app.workspace.on( + "active-leaf-change", + async () => { if (this.settings.refreshIndexOnActiveLeafChange) { // refreshIndex does everything in one await this.refreshIndex(); @@ -145,9 +174,11 @@ export default class BreadcrumbsPlugin extends Plugin { await this.drawTrail(); } } - }) + } ); + this.registerEvent(this.activeLeafChangeEventRef); + // ANCHOR autorefresh interval if (this.settings.refreshIntervalTime > 0) { this.refreshIntervalID = window.setInterval(async () => {