From a005409afe5e5872dfd3b584a3108d79b717da01 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Mon, 25 Jul 2022 08:02:45 +0200 Subject: [PATCH] feat(Hierarchy Note): :sparkles: Option to make the hierarchy note the parent of all top-level items in that hierarchy note (fix #398) --- main.js | 24 +++++++++++++++---- .../HierarchyNotes/HierarchyNotes.ts | 11 +++++---- src/Settings/HierarchyNoteSettings.ts | 13 ++++++++++ src/Utils/ObsidianUtils.ts | 2 ++ src/constants.ts | 1 + src/interfaces.ts | 3 +++ src/refreshIndex.ts | 4 ++-- 7 files changed, 48 insertions(+), 10 deletions(-) diff --git a/main.js b/main.js index cb567fd9..c21dbab0 100644 --- a/main.js +++ b/main.js @@ -3078,6 +3078,7 @@ const DEFAULT_SETTINGS = { gridHeatmap: false, heatmapColour: getComputedStyle(document.body).getPropertyValue("--text-accent"), hierarchyNotes: [""], + hierarchyNoteIsParent: false, HNUpField: "", indexNotes: [""], namingSystemField: "", @@ -5189,6 +5190,7 @@ function escapeRegex(string) { return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); } +const getSettings = () => app.plugins.plugins.breadcrumbs.settings; const getCurrFile = () => app.workspace.getActiveFile(); /** * Get basename from a **Markdown** `path` @@ -14381,10 +14383,12 @@ function addFolderNotesToGraph(plugin, folderNotes, frontms, mainG) { }); } -async function getHierarchyNoteItems(plugin, file) { +async function getHierarchyNoteItems(file) { const { listItems } = app.metadataCache.getFileCache(file); if (!listItems) return []; + const basename = getDVBasename(file); + const { hierarchyNoteIsParent } = getSettings(); const lines = (await app.vault.cachedRead(file)).split("\n"); const hierarchyNoteItems = []; const afterBulletReg = new RegExp(/\s*[+*-]\s(.*$)/); @@ -14409,7 +14413,7 @@ async function getHierarchyNoteItems(plugin, file) { else { hierarchyNoteItems.push({ note, - parent: null, + parent: hierarchyNoteIsParent ? basename : null, field, }); } @@ -34012,14 +34016,14 @@ async function buildMainG(plugin) { continue; for (const child of folder.children) { if (child instanceof obsidian.TFile) { - addHNsToGraph(settings, await getHierarchyNoteItems(plugin, child), mainG); + addHNsToGraph(settings, await getHierarchyNoteItems(child), mainG); } } } else { const file = app.metadataCache.getFirstLinkpathDest(noteOrFolder, ""); if (file) - addHNsToGraph(settings, await getHierarchyNoteItems(plugin, file), mainG); + addHNsToGraph(settings, await getHierarchyNoteItems(file), mainG); } } } @@ -37178,6 +37182,18 @@ function addHierarchyNoteSettings(plugin, alternativeHierarchyDetails) { await plugin.saveSettings(); }; }); + new obsidian.Setting(hierarchyNoteDetails) + .setName('Hierarchy note is parent of top-level items') + .setDesc('Should the actual hierarchy note be treated as the parent of all the top-level items in the list? ✅ = Yes, ❌ = No') + .addToggle((toggle) => { + toggle + .setValue(settings.hierarchyNoteIsParent) + .onChange(async (value) => { + settings.hierarchyNoteIsParent = value; + await plugin.saveSettings(); + await refreshIndex(plugin); + }); + }); new obsidian.Setting(hierarchyNoteDetails) .setName("Default Hierarchy Note Field") .setDesc(fragWithHTML("By default, hierarchy notes use the first up field in your hierarchies. Choose a different one to use by default. If you don't want to choose a default, select the blank option at the bottom of the list.")) diff --git a/src/AlternativeHierarchies/HierarchyNotes/HierarchyNotes.ts b/src/AlternativeHierarchies/HierarchyNotes/HierarchyNotes.ts index 8a4e14bc..c6459ceb 100644 --- a/src/AlternativeHierarchies/HierarchyNotes/HierarchyNotes.ts +++ b/src/AlternativeHierarchies/HierarchyNotes/HierarchyNotes.ts @@ -1,14 +1,17 @@ import type { MultiGraph } from "graphology"; import type { TFile } from "obsidian"; +import { getDVBasename, getSettings } from "../../Utils/ObsidianUtils"; import type { BCSettings, HierarchyNoteItem } from "../../interfaces"; -import type BCPlugin from "../../main"; import { addEdgeIfNot, addNodesIfNot } from "../../Utils/graphUtils"; import { getFieldInfo, getFields, getOppDir, getOppFields } from "../../Utils/HierUtils"; -export async function getHierarchyNoteItems(plugin: BCPlugin, file: TFile) { +export async function getHierarchyNoteItems(file: TFile) { const { listItems } = app.metadataCache.getFileCache(file); if (!listItems) return []; + const basename = getDVBasename(file) + const { hierarchyNoteIsParent } = getSettings(); + const lines = (await app.vault.cachedRead(file)).split("\n"); const hierarchyNoteItems: HierarchyNoteItem[] = []; @@ -38,7 +41,7 @@ export async function getHierarchyNoteItems(plugin: BCPlugin, file: TFile) { } else { hierarchyNoteItems.push({ note, - parent: null, + parent: hierarchyNoteIsParent ? basename : null, field, }); } @@ -76,7 +79,7 @@ export function addHNsToGraph( field: targetField, }); } - + addEdgeIfNot(mainG, parent, note, { dir: oppDir, field: oppField, diff --git a/src/Settings/HierarchyNoteSettings.ts b/src/Settings/HierarchyNoteSettings.ts index 76af595c..3e9dedca 100644 --- a/src/Settings/HierarchyNoteSettings.ts +++ b/src/Settings/HierarchyNoteSettings.ts @@ -35,6 +35,19 @@ export function addHierarchyNoteSettings( }; }); + new Setting(hierarchyNoteDetails) + .setName('Hierarchy note is parent of top-level items') + .setDesc('Should the actual hierarchy note be treated as the parent of all the top-level items in the list? ✅ = Yes, ❌ = No') + .addToggle((toggle) => { + toggle + .setValue(settings.hierarchyNoteIsParent) + .onChange(async (value) => { + settings.hierarchyNoteIsParent = value + await plugin.saveSettings(); + await refreshIndex(plugin); + }) + }) + new Setting(hierarchyNoteDetails) .setName("Default Hierarchy Note Field") .setDesc( diff --git a/src/Utils/ObsidianUtils.ts b/src/Utils/ObsidianUtils.ts index 8d7bca71..186cba09 100644 --- a/src/Utils/ObsidianUtils.ts +++ b/src/Utils/ObsidianUtils.ts @@ -14,6 +14,8 @@ import type { MetaeditApi } from "../interfaces"; import type BCPlugin from "../main"; import { splitAndTrim } from "./generalUtils"; +export const getSettings = () => app.plugins.plugins.breadcrumbs.settings + export const getCurrFile = (): TFile | null => app.workspace.getActiveFile() /** diff --git a/src/constants.ts b/src/constants.ts index 18e2edac..9dc84649 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -353,6 +353,7 @@ export const DEFAULT_SETTINGS: BCSettings = { "--text-accent" ), hierarchyNotes: [""], + hierarchyNoteIsParent: false, HNUpField: "", indexNotes: [""], namingSystemField: "", diff --git a/src/interfaces.ts b/src/interfaces.ts index cacc0cd5..ba74bb89 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -3,6 +3,7 @@ import type { IJugglSettings, JugglLayouts } from "juggl-api"; import type { LogLevel } from "loglevel"; import type { DateTime } from "luxon"; import type { Constructor, Pos, TFile } from "obsidian"; +import type BCPlugin from "./main"; import type { CODEBLOCK_FIELDS, CODEBLOCK_TYPES, @@ -41,6 +42,7 @@ export interface BCSettings { gridHeatmap: boolean; heatmapColour: string; hierarchyNotes: string[]; + hierarchyNoteIsParent: boolean; HNUpField: string; /** WARNING: The defaults for this feature are all `false`! */ impliedRelations: { @@ -284,6 +286,7 @@ declare module "obsidian" { api: MetaeditApi; }; juggl: { settings: { typedLinkPrefix: string } }; + breadcrumbs: BCPlugin }; enabledPlugins: { has: (plugin: string) => boolean }; }; diff --git a/src/refreshIndex.ts b/src/refreshIndex.ts index 29203dfd..a3fce346 100644 --- a/src/refreshIndex.ts +++ b/src/refreshIndex.ts @@ -253,7 +253,7 @@ export async function buildMainG(plugin: BCPlugin): Promise { if (child instanceof TFile) { addHNsToGraph( settings, - await getHierarchyNoteItems(plugin, child), + await getHierarchyNoteItems(child), mainG ); } @@ -263,7 +263,7 @@ export async function buildMainG(plugin: BCPlugin): Promise { if (file) addHNsToGraph( settings, - await getHierarchyNoteItems(plugin, file), + await getHierarchyNoteItems(file), mainG ); }