Skip to content

Commit

Permalink
feat(Hierarchy Note): ✨ Option to make the hierarchy note the parent …
Browse files Browse the repository at this point in the history
…of all top-level items in that hierarchy note (fix #398)
  • Loading branch information
SkepticMystic committed Jul 25, 2022
1 parent 9e3b260 commit a005409
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
24 changes: 20 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3078,6 +3078,7 @@ const DEFAULT_SETTINGS = {
gridHeatmap: false,
heatmapColour: getComputedStyle(document.body).getPropertyValue("--text-accent"),
hierarchyNotes: [""],
hierarchyNoteIsParent: false,
HNUpField: "",
indexNotes: [""],
namingSystemField: "",
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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(.*$)/);
Expand All @@ -14409,7 +14413,7 @@ async function getHierarchyNoteItems(plugin, file) {
else {
hierarchyNoteItems.push({
note,
parent: null,
parent: hierarchyNoteIsParent ? basename : null,
field,
});
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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 <code>up</code> 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."))
Expand Down
11 changes: 7 additions & 4 deletions src/AlternativeHierarchies/HierarchyNotes/HierarchyNotes.ts
Original file line number Diff line number Diff line change
@@ -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[] = [];
Expand Down Expand Up @@ -38,7 +41,7 @@ export async function getHierarchyNoteItems(plugin: BCPlugin, file: TFile) {
} else {
hierarchyNoteItems.push({
note,
parent: null,
parent: hierarchyNoteIsParent ? basename : null,
field,
});
}
Expand Down Expand Up @@ -76,7 +79,7 @@ export function addHNsToGraph(
field: targetField,
});
}

addEdgeIfNot(mainG, parent, note, {
dir: oppDir,
field: oppField,
Expand Down
13 changes: 13 additions & 0 deletions src/Settings/HierarchyNoteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/Utils/ObsidianUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

/**
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export const DEFAULT_SETTINGS: BCSettings = {
"--text-accent"
),
hierarchyNotes: [""],
hierarchyNoteIsParent: false,
HNUpField: "",
indexNotes: [""],
namingSystemField: "",
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -284,6 +286,7 @@ declare module "obsidian" {
api: MetaeditApi;
};
juggl: { settings: { typedLinkPrefix: string } };
breadcrumbs: BCPlugin
};
enabledPlugins: { has: (plugin: string) => boolean };
};
Expand Down
4 changes: 2 additions & 2 deletions src/refreshIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export async function buildMainG(plugin: BCPlugin): Promise<MultiGraph> {
if (child instanceof TFile) {
addHNsToGraph(
settings,
await getHierarchyNoteItems(plugin, child),
await getHierarchyNoteItems(child),
mainG
);
}
Expand All @@ -263,7 +263,7 @@ export async function buildMainG(plugin: BCPlugin): Promise<MultiGraph> {
if (file)
addHNsToGraph(
settings,
await getHierarchyNoteItems(plugin, file),
await getHierarchyNoteItems(file),
mainG
);
}
Expand Down

0 comments on commit a005409

Please sign in to comment.