From c46c95d08d7bfb46075c5b88ed67552ac58941b6 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Fri, 3 Sep 2021 17:45:02 +0200 Subject: [PATCH] feat(WriteBCToFile): :sparkles: New Command: Write Breadcrumbs to current file --- .vscode/settings.json | 3 +- src/main.ts | 25 ++++++++++++++++ src/sharedFunctions.ts | 65 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 61e96040..7f0211c3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,6 +8,7 @@ "Vis View", "getFieldValues", "Path View", - "Hierarchy Note" + "Hierarchy Note", + "WriteBCToFile" ] } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 838378a1..dc355597 100644 --- a/src/main.ts +++ b/src/main.ts @@ -35,12 +35,15 @@ import { getObsMetadataCache, mergeGs, removeDuplicates, + writeBCToFile, + } from "src/sharedFunctions"; import StatsView from "src/StatsView"; import { VisModal } from "src/VisModal"; import TrailGrid from "./Components/TrailGrid.svelte"; import TrailPath from "./Components/TrailPath.svelte"; + const DEFAULT_SETTINGS: BreadcrumbsSettings = { userHierarchies: [], indexNote: [""], @@ -86,6 +89,17 @@ declare module "obsidian" { plugins: { dataview: { api: any }; juggl: any; + metaedit: { + api: { + getAutopropFunction: () => any; + getUpdateFunction: () => any; + getFileFromTFileOrPath: () => any; + getGetPropertyValueFunction: () => any; + getGetFilesWithPropertyFunction: () => any; + getCreateYamlPropertyFunction: () => any; + getGetPropertiesInFile: () => any; + } + } }; }; } @@ -107,6 +121,7 @@ export default class BreadcrumbsPlugin extends Plugin { this.activeLeafChangeEventRef = this.app.workspace.on( "active-leaf-change", async () => { + if (this.settings.refreshIndexOnActiveLeafChange) { // refreshIndex does everything in one await this.refreshIndex(); @@ -299,6 +314,16 @@ export default class BreadcrumbsPlugin extends Plugin { callback: async () => await this.refreshIndex(), }); + this.addCommand({ + id: "Write-Breadcrumbs-to-Current-File", + name: "Write Breadcrumbs to Current File", + callback: () => { + const currFile = this.app.workspace.getActiveFile(); + writeBCToFile(this.app, this, this.currGraphs, currFile) + }, + }); + + this.addRibbonIcon("dice", "Breadcrumbs Visualisation", () => new VisModal(this.app, this).open() ); diff --git a/src/sharedFunctions.ts b/src/sharedFunctions.ts index 728b48b8..2130a5db 100644 --- a/src/sharedFunctions.ts +++ b/src/sharedFunctions.ts @@ -7,10 +7,11 @@ import { Notice, Pos, TFile, - WorkspaceLeaf, + WorkspaceLeaf } from "obsidian"; import { DIRECTIONS, dropHeaderOrAlias, splitLinksRegex } from "src/constants"; import type { + BCIndex, BreadcrumbsSettings, Directions, dvFrontmatterCache, @@ -18,11 +19,13 @@ import type { HierarchyFields, HierarchyGraphs, JugglLink, - userHierarchy, + userHierarchy } from "src/interfaces"; import type BreadcrumbsPlugin from "src/main"; import type MatrixView from "src/MatrixView"; + + export function sum(arr: number[]): number { return arr.reduce((a, b) => a + b); } @@ -602,3 +605,61 @@ export function hierToStr(hier: userHierarchy) { export function removeDuplicates(arr: T[]) { return [...new Set(arr)]; } + +export const createOrUpdateYaml = async ( + key: string, + value: string, + file: TFile, + frontmatter: FrontMatterCache | undefined, + api: { [fun: string]: (...args: any) => any } +) => { + if (!api) { + new Notice('Metaedit must be enabled for this function to work'); + return + } + + let valueStr = value.toString() + + if (!frontmatter || frontmatter[key] === undefined) { + await api.createYamlProperty(key, `['${valueStr}']`, file); + } else if ([...[frontmatter[key]]].flat(3).includes(valueStr)) { + return + } + else { + const oldValueFlat: string[] = [...[frontmatter[key]]].flat(4); + const newValue = [...oldValueFlat, valueStr].map(val => `'${val}'`); + await api.update(key, `[${newValue.join(", ")}]`, file); + } + +} + +export const writeBCToFile = (app: App, plugin: BreadcrumbsPlugin, currGraphs: BCIndex, file: TFile) => { + + const frontmatter = app.metadataCache.getFileCache(file)?.frontmatter; + const { api } = app.plugins.plugins.metaedit + + currGraphs.hierGs.forEach(hier => { + DIRECTIONS.forEach(dir => { + let oppDir: Directions; + if (dir === 'up') oppDir = 'down'; + if (dir === 'down') oppDir = 'up'; + if (dir === 'same') oppDir = 'same'; + + Object.keys(hier[dir]).forEach(field => { + + + const fieldG = hier[dir][field]; + const succs = fieldG.predecessors(file.basename) as string[]; + + succs.forEach(succ => { + const { fieldName } = fieldG.node(succ); + const currHier = plugin.settings.userHierarchies.filter(hier => hier[dir].includes(fieldName))[0] + let oppField: string = currHier[oppDir][0]; + if (!oppField) oppField = `${fieldName}` + + createOrUpdateYaml(oppField, succ, file, frontmatter, api) + }) + }) + }) + }) +} \ No newline at end of file