From c48c571e2c3b276cf0cc67f0839d5e234e6d7b8e Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Tue, 30 Nov 2021 15:52:01 +0200 Subject: [PATCH] feat: :sparkles: Options to choose which views are oppened onload --- src/BreadcrumbsSettingTab.ts | 40 ++++++++++++++++++++++++++++++ src/constants.ts | 26 +++---------------- src/interfaces.ts | 4 +++ src/main.ts | 48 +++++++++++++++++++++++++++++++----- 4 files changed, 90 insertions(+), 28 deletions(-) diff --git a/src/BreadcrumbsSettingTab.ts b/src/BreadcrumbsSettingTab.ts index b53a2977..e2587c7f 100644 --- a/src/BreadcrumbsSettingTab.ts +++ b/src/BreadcrumbsSettingTab.ts @@ -133,6 +133,46 @@ export class BCSettingTab extends PluginSettingTab { }) ); + new Setting(generalDetails) + .setName("Open Views by Default") + .setDesc("Choose which of the views to open onload") + .addToggle((toggle) => { + toggle + .setTooltip("Matrix View") + .setValue(settings.openMatrixOnLoad) + .onChange(async (value) => { + settings.openMatrixOnLoad = value; + await plugin.saveSettings(); + }); + }) + .addToggle((toggle) => { + toggle + .setTooltip("Stats View") + .setValue(settings.openStatsOnLoad) + .onChange(async (value) => { + settings.openStatsOnLoad = value; + await plugin.saveSettings(); + }); + }) + .addToggle((toggle) => { + toggle + .setTooltip("Ducks View") + .setValue(settings.openDuckOnLoad) + .onChange(async (value) => { + settings.openDuckOnLoad = value; + await plugin.saveSettings(); + }); + }) + .addToggle((toggle) => { + toggle + .setTooltip("Down View") + .setValue(settings.openDownOnLoad) + .onChange(async (value) => { + settings.openDownOnLoad = value; + await plugin.saveSettings(); + }); + }); + new Setting(generalDetails) .setName("Enable Field Suggestor") .setDesc( diff --git a/src/constants.ts b/src/constants.ts index 2dc4d8e0..2367d20f 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,38 +1,16 @@ -import DownView from "./DownView"; -import DucksView from "./DucksView"; import type { BCSettings, Directions, Relations, UserHier, - ViewInfo, visTypes, } from "./interfaces"; -import MatrixView from "./MatrixView"; -import StatsView from "./StatsView"; export const MATRIX_VIEW = "BC-matrix"; export const STATS_VIEW = "BC-stats"; export const DUCK_VIEW = "BC-ducks"; export const DOWN_VIEW = "BC-down"; -export const VIEWS: ViewInfo[] = [ - { - plain: "Matrix", - type: MATRIX_VIEW, - constructor: MatrixView, - openOnLoad: true, - }, - { - plain: "Stats", - type: STATS_VIEW, - constructor: StatsView, - openOnLoad: true, - }, - { plain: "Duck", type: DUCK_VIEW, constructor: DucksView, openOnLoad: false }, - { plain: "Down", type: DOWN_VIEW, constructor: DownView, openOnLoad: true }, -]; - export const TRAIL_ICON = "BC-trail-icon"; export const TRAIL_ICON_SVG = ''; @@ -169,6 +147,10 @@ export const DEFAULT_SETTINGS: BCSettings = { HNUpField: "", refreshOnNoteChange: false, useAllMetadata: true, + openMatrixOnLoad: true, + openStatsOnLoad: true, + openDuckOnLoad: false, + openDownOnLoad: true, parseJugglLinksWithoutJuggl: false, showNameOrType: true, showRelationType: true, diff --git a/src/interfaces.ts b/src/interfaces.ts index f1e99a7a..6676581d 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -28,6 +28,10 @@ export interface BCSettings { limitTrailCheckboxStates: { [field: string]: boolean }; limitWriteBCCheckboxStates: { [field: string]: boolean }; noPathMessage: string; + openMatrixOnLoad: boolean; + openStatsOnLoad: boolean; + openDuckOnLoad: boolean; + openDownOnLoad: boolean; parseJugglLinksWithoutJuggl: boolean; refreshOnNoteChange: boolean; respectReadableLineLength: boolean; diff --git a/src/main.ts b/src/main.ts index d6db9cbf..715916a9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,6 +19,10 @@ import { copy, openView, } from "obsidian-community-lib/dist/utils"; +import StatsView from "./StatsView"; +import DownView from "./DownView"; +import DucksView from "./DucksView"; +import MatrixView from "./MatrixView"; import { Debugger } from "src/Debugger"; import util from "util"; import { BCSettingTab } from "./BreadcrumbsSettingTab"; @@ -35,12 +39,14 @@ import { BC_TAG_NOTE_FIELD, BC_TRAVERSE_NOTE, DEFAULT_SETTINGS, + DOWN_VIEW, dropHeaderOrAlias, + DUCK_VIEW, MATRIX_VIEW, splitLinksRegex, + STATS_VIEW, TRAIL_ICON, TRAIL_ICON_SVG, - VIEWS, } from "./constants"; import { FieldSuggestor } from "./FieldSuggestor"; import { @@ -64,6 +70,7 @@ import type { JugglLink, MyView, RawValue, + ViewInfo, } from "./interfaces"; import { createOrUpdateYaml, @@ -88,12 +95,14 @@ export default class BCPlugin extends Plugin { layoutChange: EventRef = undefined; statusBatItemEl: HTMLElement = undefined; db: Debugger; + VIEWS: ViewInfo[]; async refreshIndex() { if (!this.activeLeafChange) this.registerActiveLeafChangeEvent(); if (!this.layoutChange) this.registerLayoutChangeEvent(); this.mainG = await this.initGraphs(); - for (const view of VIEWS) await this.getActiveTYPEView(view.type)?.draw(); + for (const view of this.VIEWS) + await this.getActiveTYPEView(view.type)?.draw(); if (this.settings.showTrail) await this.drawTrail(); if (this.settings.showRefreshNotice) new Notice("Index refreshed"); } @@ -125,7 +134,7 @@ export default class BCPlugin extends Plugin { const { settings } = this; this.mainG = await this.initGraphs(); - for (const view of VIEWS) { + for (const view of this.VIEWS) { if (view.openOnLoad) await openView(this.app, view.type, view.constructor); } @@ -169,7 +178,34 @@ export default class BCPlugin extends Plugin { } } - for (const view of VIEWS) { + this.VIEWS = [ + { + plain: "Matrix", + type: MATRIX_VIEW, + constructor: MatrixView, + openOnLoad: this.settings.openMatrixOnLoad, + }, + { + plain: "Stats", + type: STATS_VIEW, + constructor: StatsView, + openOnLoad: this.settings.openStatsOnLoad, + }, + { + plain: "Duck", + type: DUCK_VIEW, + constructor: DucksView, + openOnLoad: this.settings.openDuckOnLoad, + }, + { + plain: "Down", + type: DOWN_VIEW, + constructor: DownView, + openOnLoad: this.settings.openDownOnLoad, + }, + ]; + + for (const view of this.VIEWS) { this.registerView( view.type, (leaf: WorkspaceLeaf) => new view.constructor(leaf, this) @@ -196,7 +232,7 @@ export default class BCPlugin extends Plugin { addIcon(TRAIL_ICON, TRAIL_ICON_SVG); - for (const view of VIEWS) { + for (const view of this.VIEWS) { this.addCommand({ id: `show-${view.type}-view`, name: `Open ${view.plain} View`, @@ -362,7 +398,7 @@ export default class BCPlugin extends Plugin { }; getActiveTYPEView(type: string): MyView | null { - const { constructor } = VIEWS.find((view) => view.type === type); + const { constructor } = this.VIEWS.find((view) => view.type === type); const leaves = this.app.workspace.getLeavesOfType(type); if (leaves && leaves.length >= 1) { const view = leaves[0].view;