diff --git a/src/BreadcrumbsSettingTab.ts b/src/BreadcrumbsSettingTab.ts index 9666e09e..546dcb0d 100644 --- a/src/BreadcrumbsSettingTab.ts +++ b/src/BreadcrumbsSettingTab.ts @@ -90,8 +90,8 @@ export class BreadcrumbsSettingTab extends PluginSettingTab { if (plugin.settings.showTrail) { await plugin.drawTrail(); } - if (plugin.getActiveView()) { - await plugin.getActiveView().draw(); + if (plugin.getActiveMatrixView()) { + await plugin.getActiveMatrixView().draw(); } }, num * 1000); plugin.registerInterval(plugin.refreshIntervalID); @@ -131,7 +131,7 @@ export class BreadcrumbsSettingTab extends PluginSettingTab { .onChange(async (value) => { plugin.settings.showNameOrType = value; await plugin.saveSettings(); - await plugin.getActiveView().draw(); + await plugin.getActiveMatrixView().draw(); }) ); @@ -146,7 +146,7 @@ export class BreadcrumbsSettingTab extends PluginSettingTab { .onChange(async (value) => { plugin.settings.showRelationType = value; await plugin.saveSettings(); - await plugin.getActiveView().draw(); + await plugin.getActiveMatrixView().draw(); }) ); @@ -297,7 +297,7 @@ export class BreadcrumbsSettingTab extends PluginSettingTab { plugin.settings.trailSeperator = value; await plugin.saveSettings(); // BUG This doesn't seem to work... you still have to switch notes for it to redraw - await plugin.getActiveView().draw(); + await plugin.getActiveMatrixView().draw(); }) ); diff --git a/src/Components/Stats.svelte b/src/Components/Stats.svelte new file mode 100644 index 00000000..e69de29b diff --git a/src/MatrixView.ts b/src/MatrixView.ts index 7d2d8778..405613d7 100644 --- a/src/MatrixView.ts +++ b/src/MatrixView.ts @@ -84,11 +84,10 @@ export default class MatrixView extends ItemView { }); } - getViewType(): string { + getViewType() { return VIEW_TYPE_BREADCRUMBS_MATRIX; } - - getDisplayText(): string { + getDisplayText() { return "Breadcrumbs Matrix"; } @@ -241,7 +240,6 @@ export default class MatrixView extends ItemView { async draw(): Promise { this.contentEl.empty(); - // this.currGraphs = this.plugin.currGraphs; const { gParents, gSiblings, gChildren } = this.plugin.currGraphs; const currFile = this.app.workspace.getActiveFile(); const settings = this.plugin.settings; diff --git a/src/StatsView.ts b/src/StatsView.ts new file mode 100644 index 00000000..244d2065 --- /dev/null +++ b/src/StatsView.ts @@ -0,0 +1,86 @@ +import type { Graph } from "graphlib"; +import { ItemView, WorkspaceLeaf } from "obsidian"; +import { + DATAVIEW_INDEX_DELAY, + VIEW_TYPE_BREADCRUMBS_STATS, +} from "src/constants"; +import type BreadcrumbsPlugin from "src/main"; +import Stats from "./Components/Stats.svelte"; + +export default class StatsView extends ItemView { + private plugin: BreadcrumbsPlugin; + private view: Stats; + + constructor(leaf: WorkspaceLeaf, plugin: BreadcrumbsPlugin) { + super(leaf); + this.plugin = plugin; + } + + async onload(): Promise { + super.onload(); + await this.plugin.saveSettings(); + this.app.workspace.onLayoutReady(async () => { + setTimeout(async () => await this.draw(), DATAVIEW_INDEX_DELAY); + }); + } + + getViewType() { + return VIEW_TYPE_BREADCRUMBS_STATS; + } + getDisplayText() { + return "Breadcrumbs Stats"; + } + + icon = "bullet-list-glyph"; + + async onOpen(): Promise { + await this.plugin.saveSettings(); + } + + onClose(): Promise { + if (this.view) { + this.view.$destroy(); + } + return Promise.resolve(); + } + + // ANCHOR Remove duplicate implied links + + dfsAllPaths(g: Graph, startNode: string): string[][] { + const queue: { node: string; path: string[] }[] = [ + { node: startNode, path: [] }, + ]; + const pathsArr: string[][] = []; + + let i = 0; + while (queue.length > 0 && i < 1000) { + i++; + const currPath = queue.shift(); + + const newNodes = (g.successors(currPath.node) ?? []) as string[]; + const extPath = [currPath.node, ...currPath.path]; + queue.unshift( + ...newNodes.map((n: string) => { + return { node: n, path: extPath }; + }) + ); + + if (newNodes.length === 0) { + pathsArr.push(extPath); + } + } + return pathsArr; + } + + async draw(): Promise { + this.contentEl.empty(); + // const { gParents, gSiblings, gChildren } = this.plugin.currGraphs; + // const currFile = this.app.workspace.getActiveFile(); + // const settings = this.plugin.settings; + + this.view = new Stats({ + target: this.contentEl, + props: {}, + }); + } +} diff --git a/src/constants.ts b/src/constants.ts index 0eff183f..645996f6 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,7 +1,9 @@ export const VIEW_TYPE_BREADCRUMBS_MATRIX = "breadcrumbs-matrix"; +export const VIEW_TYPE_BREADCRUMBS_STATS = "breadcrumbs-stats"; export const TRAIL_ICON = "breadcrumbs-trail-icon"; -export const TRAIL_ICON_SVG = '' +export const TRAIL_ICON_SVG = + ''; export const splitLinksRegex = new RegExp(/\[\[(.+?)\]\]/g); export const dropHeaderOrAlias = new RegExp(/\[\[([^#|]+)\]\]/); diff --git a/src/main.ts b/src/main.ts index 9a381155..1be5a49e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,6 +6,7 @@ import { TRAIL_ICON, TRAIL_ICON_SVG, VIEW_TYPE_BREADCRUMBS_MATRIX, + VIEW_TYPE_BREADCRUMBS_STATS, } from "src/constants"; import type { allGraphs, @@ -14,6 +15,7 @@ import type { neighbourObj, } from "src/interfaces"; import MatrixView from "src/MatrixView"; +import StatsView from "src/StatsView"; import { closeImpliedLinks, debug, @@ -75,6 +77,11 @@ export default class BreadcrumbsPlugin extends Plugin { this.visited = []; + this.registerView( + VIEW_TYPE_BREADCRUMBS_STATS, + (leaf: WorkspaceLeaf) => new StatsView(leaf, this) + ); + this.registerView( VIEW_TYPE_BREADCRUMBS_MATRIX, (leaf: WorkspaceLeaf) => new MatrixView(leaf, this) @@ -85,7 +92,9 @@ export default class BreadcrumbsPlugin extends Plugin { setTimeout(async () => { this.currGraphs = await this.initGraphs(); - this.initView(VIEW_TYPE_BREADCRUMBS_MATRIX); + this.initStatsView(VIEW_TYPE_BREADCRUMBS_STATS); + + this.initMatrixView(VIEW_TYPE_BREADCRUMBS_MATRIX); if (this.settings.showTrail) { await this.drawTrail(); @@ -95,7 +104,7 @@ export default class BreadcrumbsPlugin extends Plugin { this.app.workspace.on("active-leaf-change", async () => { this.currGraphs = await this.initGraphs(); debug(this.settings, this.currGraphs); - const activeView = this.getActiveView(); + const activeView = this.getActiveMatrixView(); if (activeView) { await activeView.draw(); } @@ -112,7 +121,7 @@ export default class BreadcrumbsPlugin extends Plugin { if (this.settings.showTrail) { await this.drawTrail(); } - const activeView = this.getActiveView(); + const activeView = this.getActiveMatrixView(); if (activeView) { await activeView.draw(); } @@ -134,14 +143,28 @@ export default class BreadcrumbsPlugin extends Plugin { .length === 0 ); } - this.initView(VIEW_TYPE_BREADCRUMBS_MATRIX); + this.initMatrixView(VIEW_TYPE_BREADCRUMBS_MATRIX); + }, + }); + + this.addCommand({ + id: "show-breadcrumbs-stats-view", + name: "Open Stats View", + checkCallback: (checking: boolean) => { + if (checking) { + return ( + this.app.workspace.getLeavesOfType(VIEW_TYPE_BREADCRUMBS_STATS) + .length === 0 + ); + } + this.initStatsView(VIEW_TYPE_BREADCRUMBS_STATS); }, }); this.addSettingTab(new BreadcrumbsSettingTab(this.app, this)); } - getActiveView(): MatrixView | null { + getActiveMatrixView(): MatrixView | null { const leaves = this.app.workspace.getLeavesOfType( VIEW_TYPE_BREADCRUMBS_MATRIX ); @@ -374,7 +397,7 @@ export default class BreadcrumbsPlugin extends Plugin { } } - initView = async (type: string): Promise => { + initMatrixView = async (type: string): Promise => { let leaf: WorkspaceLeaf = null; for (leaf of this.app.workspace.getLeavesOfType(type)) { if (leaf.view instanceof MatrixView) { @@ -385,7 +408,22 @@ export default class BreadcrumbsPlugin extends Plugin { } (leaf ?? this.app.workspace.getRightLeaf(false)).setViewState({ type, - active: true, + active: false, + }); + }; + + initStatsView = async (type: string): Promise => { + let leaf: WorkspaceLeaf = null; + for (leaf of this.app.workspace.getLeavesOfType(type)) { + if (leaf.view instanceof StatsView) { + return; + } + await leaf.setViewState({ type: "empty" }); + break; + } + (leaf ?? this.app.workspace.getRightLeaf(false)).setViewState({ + type, + active: false, }); };