Skip to content

Commit

Permalink
refactor: 🚧 Progress on multiple hierarchies
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Aug 11, 2021
1 parent b574363 commit 89a61ff
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 70 deletions.
22 changes: 8 additions & 14 deletions src/Components/Stats.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,18 @@
const { settings } = plugin;
const separator = settings.trailSeperator;
const { gParents, gSiblings, gChildren } = plugin.currGraphs;
const allRG = [gParents, gSiblings, gChildren];
const graphs = plugin.currGraphs;
const allR = Object.values(graphs);
const [pRNodes, sRNodes, cRNodes] = allRG.map((g) => g.nodes());
const allRNodes = allR.map((g) => g.nodes());
const [pRNodesStr, sRNodesStr, cRNodesStr] = [
pRNodes.join("\n"),
sRNodes.join("\n"),
cRNodes.join("\n"),
];
const allRNodesStr = allRNodes.map((rNodes) => rNodes.join("\n"));
const [pREdges, sREdges, cREdges] = allRG.map((g) => g.edges());
const allREdges = allR.map((g) => g.edges());
const [pREdgesStr, sREdgesStr, cREdgesStr] = [
pREdges.map((e) => `${e.v} → ${e.w}`).join("\n"),
sREdges.map((e) => `${e.v} → ${e.w}`).join("\n"),
cREdges.map((e) => `${e.v} → ${e.w}`).join("\n"),
];
const allREdgesStr = allREdges.map((edges) =>
edges.map((e) => `${e.v} → ${e.w}`).join("\n")
);
const [closedP, closedS, closedC] = [
closeImpliedLinks(gParents, gChildren),
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export interface neighbourObj {
children: string[];
}

export type relObj = { [key: string]: string[] } | { current: TFile };

export interface ParentObj {
current: TFile;
parents: string[];
Expand Down
64 changes: 43 additions & 21 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
BreadcrumbsSettings,
dvFrontmatterCache,
neighbourObj,
relObj,
} from "src/interfaces";
import MatrixView from "src/MatrixView";
import StatsView from "src/StatsView";
Expand All @@ -22,6 +23,7 @@ import {
getDVMetadataCache,
getNeighbourObjArr,
getObsMetadataCache,
splitAndTrim,
} from "src/sharedFunctions";
import TrailGrid from "./Components/TrailGrid.svelte";
import TrailPath from "./Components/TrailPath.svelte";
Expand Down Expand Up @@ -74,7 +76,7 @@ export default class BreadcrumbsPlugin extends Plugin {
settings: BreadcrumbsSettings;
visited: [string, HTMLDivElement][];
refreshIntervalID: number;
currGraphs: allGraphs;
currGraphs: { [graph: string]: Graph };

async onload(): Promise<void> {
console.log("loading breadcrumbs plugin");
Expand All @@ -94,12 +96,10 @@ export default class BreadcrumbsPlugin extends Plugin {
);

this.app.workspace.onLayoutReady(async () => {
// this.trailDiv = createDiv()
setTimeout(async () => {
this.currGraphs = await this.initGraphs();

this.initStatsView(VIEW_TYPE_BREADCRUMBS_STATS);

this.initMatrixView(VIEW_TYPE_BREADCRUMBS_MATRIX);

if (this.settings.showTrail) {
Expand Down Expand Up @@ -192,19 +192,17 @@ export default class BreadcrumbsPlugin extends Plugin {
populateGraph(
g: Graph,
currFileName: string,
neighbours: neighbourObj,
relationship: string
relObj: relObj,
relationship: keyof relObj
): void {
g.setNode(currFileName, relationship);
neighbours[relationship].forEach((node) => {
relObj[relationship].forEach((node: string) => {
g.setEdge(currFileName, node, relationship);
});
}

async initGraphs(): Promise<{
gParents: Graph;
gSiblings: Graph;
gChildren: Graph;
[graph: string]: Graph;
}> {
debug(this.settings, "initialising graphs");
const files = this.app.vault.getMarkdownFiles();
Expand All @@ -217,24 +215,48 @@ export default class BreadcrumbsPlugin extends Plugin {
fileFrontmatterArr = getObsMetadataCache(this.app, this.settings, files);
}

const neighbourArr = await getNeighbourObjArr(this, fileFrontmatterArr);
const relObjArr = await getNeighbourObjArr(this, fileFrontmatterArr);

const [gParents, gSiblings, gChildren] = [
new Graph(),
new Graph(),
new Graph(),
const { parentFieldName, siblingFieldName, childFieldName } = this.settings;
const [parentFields, siblingFields, childFields] = [
splitAndTrim(parentFieldName),
splitAndTrim(siblingFieldName),
splitAndTrim(childFieldName),
];
const allFields = [parentFields, siblingFields, childFields].flat(1);

const graphs: { [graph: string]: Graph } = {};

allFields.forEach((field) => {
graphs[field] = new Graph();
});

neighbourArr.forEach((neighbourObj) => {
const currFileName =
neighbourObj.current.basename || neighbourObj.current.name;
relObjArr.forEach((relObj) => {
const currFileName = relObj.current.basename || relObj.current.name;

this.populateGraph(gParents, currFileName, neighbourObj, "parents");
this.populateGraph(gSiblings, currFileName, neighbourObj, "siblings");
this.populateGraph(gChildren, currFileName, neighbourObj, "children");
Object.keys(relObj).forEach((rel) => {
if (rel === "current") return;
this.populateGraph(graphs[rel], currFileName, relObj, rel);
});
});

// const [gParents, gSiblings, gChildren] = [
// new Graph(),
// new Graph(),
// new Graph(),
// ];

// neighbourArr.forEach((neighbourObj) => {
// const currFileName =
// neighbourObj.current.basename || neighbourObj.current.name;

// this.populateGraph(gParents, currFileName, neighbourObj, "parents");
// this.populateGraph(gSiblings, currFileName, neighbourObj, "siblings");
// this.populateGraph(gChildren, currFileName, neighbourObj, "children");
// });
debug(this.settings, "graphs inited");
return { gParents, gSiblings, gChildren };
console.log({ graphs });
return { ...graphs };
}

// !SECTION OneSource
Expand Down
59 changes: 24 additions & 35 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
dvLink,
JugglLink,
neighbourObj,
relObj,
} from "src/interfaces";
import type BreadcrumbsPlugin from "src/main";
import type MatrixView from "src/MatrixView";
Expand All @@ -29,10 +30,6 @@ export function normalise(arr: number[]): number[] {
return arr.map((item) => item / max);
}

// export function flatten<T>(arr: T[]): T[] {
// return [].concat(...arr)
// }

export const isSubset = <T>(arr1: T[], arr2: T[]): boolean =>
arr1.every((value) => arr2.includes(value));

Expand Down Expand Up @@ -170,9 +167,7 @@ export async function getJugglLinks(
});

// Filter out the juggl links with no links
const filteredLinks = typedLinksArr.filter((link) =>
link.links.length ? true : false
);
const filteredLinks = typedLinksArr.filter((link) => !!link.links.length);
debug(settings, { filteredLinks });
return filteredLinks;
}
Expand Down Expand Up @@ -218,7 +213,7 @@ export const splitAndTrim = (fields: string): string[] =>
export async function getNeighbourObjArr(
plugin: BreadcrumbsPlugin,
fileFrontmatterArr: dvFrontmatterCache[]
): Promise<neighbourObj[]> {
): Promise<relObj[]> {
const { parentFieldName, siblingFieldName, childFieldName } = plugin.settings;

const [parentFields, siblingFields, childFields] = [
Expand All @@ -227,30 +222,29 @@ export async function getNeighbourObjArr(
splitAndTrim(childFieldName),
];

const allFields = [parentFields, siblingFields, childFields].flat(1);

let jugglLinks: JugglLink[] = [];
if (plugin.app.plugins.plugins.juggl !== undefined) {
jugglLinks = await getJugglLinks(plugin.app, plugin.settings);
}

const neighbourObjArr: neighbourObj[] = fileFrontmatterArr.map(
const neighbourObjArr: relObj[] = fileFrontmatterArr.map(
(fileFrontmatter) => {
let [parents, siblings, children] = [
parentFields
.map((parentField) =>
getFieldValues(fileFrontmatter, parentField, plugin.settings)
)
.flat(3),
siblingFields
.map((siblingField) =>
getFieldValues(fileFrontmatter, siblingField, plugin.settings)
)
.flat(3),
childFields
.map((childField) =>
getFieldValues(fileFrontmatter, childField, plugin.settings)
)
.flat(3),
];
const relObj: relObj = {
current: fileFrontmatter.file,
};

allFields.forEach(
(field) =>
(relObj[field] = getFieldValues(
fileFrontmatter,
field,
plugin.settings
))
);

console.log({ relObj });

if (jugglLinks.length) {
const currFileJugglLinks = jugglLinks.filter(
Expand All @@ -261,22 +255,17 @@ export async function getNeighbourObjArr(

currFileJugglLinks.forEach((jugglLink) => {
jugglLink.links.forEach((link) => {
if (parentFields.includes(link.type)) {
parents = [...parents, ...link.linksInLine];
}
if (siblingFields.includes(link.type)) {
siblings = [...siblings, ...link.linksInLine];
}
if (childFields.includes(link.type)) {
children = [...children, ...link.linksInLine];
if (allFields.includes(link.type)) {
relObj[link.type].push(...link.linksInLine);
}
});
});
}

return { current: fileFrontmatter.file, parents, siblings, children };
return { ...relObj };
}
);
console.log({ neighbourObjArr });
debug(plugin.settings, { neighbourObjArr });
return neighbourObjArr;
}
Expand Down

0 comments on commit 89a61ff

Please sign in to comment.