Skip to content

Commit

Permalink
feat(WriteBCToFile): ✨ New Command: Write Breadcrumbs to current file
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Sep 3, 2021
1 parent ea29908 commit c46c95d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"Vis View",
"getFieldValues",
"Path View",
"Hierarchy Note"
"Hierarchy Note",
"WriteBCToFile"
]
}
25 changes: 25 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [""],
Expand Down Expand Up @@ -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;
}
}
};
};
}
Expand All @@ -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();
Expand Down Expand Up @@ -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()
);
Expand Down
65 changes: 63 additions & 2 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ import {
Notice,
Pos,
TFile,
WorkspaceLeaf,
WorkspaceLeaf
} from "obsidian";
import { DIRECTIONS, dropHeaderOrAlias, splitLinksRegex } from "src/constants";
import type {
BCIndex,
BreadcrumbsSettings,
Directions,
dvFrontmatterCache,
dvLink,
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);
}
Expand Down Expand Up @@ -602,3 +605,61 @@ export function hierToStr(hier: userHierarchy) {
export function removeDuplicates<T>(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 = `<Reverse>${fieldName}`

createOrUpdateYaml(oppField, succ, file, frontmatter, api)
})
})
})
})
}

0 comments on commit c46c95d

Please sign in to comment.