Skip to content

Commit

Permalink
fix: 🐛 Register active-leaf-change event if it wasn't registered onLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Aug 18, 2021
1 parent ea53b8c commit 96aa5f4
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Graph } from "graphlib";
import {
addIcon,
EventRef,
MarkdownView,
Notice,
Plugin,
Expand Down Expand Up @@ -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();
Expand All @@ -108,6 +135,7 @@ export default class BreadcrumbsPlugin extends Plugin {

await this.loadSettings();

this.activeLeafChangeEventRef = undefined;
this.visited = [];

this.registerView(
Expand All @@ -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();
Expand All @@ -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 () => {
Expand Down

0 comments on commit 96aa5f4

Please sign in to comment.