Skip to content

Commit

Permalink
refactor: ♻️ Make sure everything functions normally again
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Aug 11, 2021
1 parent 2eda709 commit 2f08de8
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 126 deletions.
17 changes: 13 additions & 4 deletions src/Components/TrailGrid.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import type BreadcrumbsPlugin from "src/main";
import {
closeImpliedLinks,
getAllXGs,
mergeGs,
normalise,
openOrSwitch,
padArray,
Expand Down Expand Up @@ -52,14 +54,21 @@
// const data: {[cell: string]: number} = {}
// allCells.forEach(cell => data[cell] = app.metadataCache.getFileCache(app.metadataCache.getFirstLinkpathDest(cell, currFile.path))?.links.length ?? 0);
const allUps = getAllXGs(plugin, "up");
const allDowns = getAllXGs(plugin, "down");
console.log({ allUps, allDowns });
const upG = mergeGs(...Object.values(allUps));
const downG = mergeGs(...Object.values(allDowns));
console.log({ upG, downG });
const closedParents = closeImpliedLinks(upG, downG);
const children: { [cell: string]: number } = {};
allCells.forEach(
(cell) =>
(children[cell] = (
closeImpliedLinks(
plugin.currGraphs.gChildren,
plugin.currGraphs.gParents
).successors(cell) ?? []
closeImpliedLinks(downG, upG).successors(cell) ?? []
).length)
);
Expand Down
82 changes: 54 additions & 28 deletions src/MatrixView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import type {
d3Graph,
} from "src/interfaces";
import type BreadcrumbsPlugin from "src/main";
import { closeImpliedLinks, copy, debug } from "src/sharedFunctions";
import {
closeImpliedLinks,
copy,
debug,
getAllXGs,
mergeGs,
} from "src/sharedFunctions";
import Lists from "./Components/Lists.svelte";
import Matrix from "./Components/Matrix.svelte";

Expand Down Expand Up @@ -46,13 +52,18 @@ export default class MatrixView extends ItemView {
callback: async () => {
const settings = this.plugin.settings;
const currFile = this.app.workspace.getActiveFile().basename;
const allPaths = this.dfsAllPaths(
closeImpliedLinks(
this.plugin.currGraphs.gChildren,
this.plugin.currGraphs.gParents
),
currFile
);

const allUps = getAllXGs(this.plugin, "up");
const allDowns = getAllXGs(this.plugin, "down");
console.log({ allUps, allDowns });

const upG = mergeGs(...Object.values(allUps));
const downG = mergeGs(...Object.values(allDowns));
console.log({ upG, downG });

const closedParents = closeImpliedLinks(upG, downG);

const allPaths = this.dfsAllPaths(closedParents, currFile);
const index = this.createIndex(currFile + "\n", allPaths, settings);
debug(settings, { index });
await copy(index);
Expand All @@ -63,17 +74,23 @@ export default class MatrixView extends ItemView {
id: "global-index",
name: "Copy a Global Index to the clipboard",
callback: async () => {
const { gParents, gChildren } = this.plugin.currGraphs;
const terminals = gParents.sinks();
const allUps = getAllXGs(this.plugin, "up");
const allDowns = getAllXGs(this.plugin, "down");
console.log({ allUps, allDowns });

const upG = mergeGs(...Object.values(allUps));
const downG = mergeGs(...Object.values(allDowns));
console.log({ upG, downG });

const closedParents = closeImpliedLinks(upG, downG);

const terminals = upG.sinks();
const settings = this.plugin.settings;

let globalIndex = "";
terminals.forEach((terminal) => {
globalIndex += terminal + "\n";
const allPaths = this.dfsAllPaths(
closeImpliedLinks(gChildren, gParents),
terminal
);
const allPaths = this.dfsAllPaths(closedParents, terminal);
globalIndex = this.createIndex(globalIndex, allPaths, settings);
});

Expand Down Expand Up @@ -239,7 +256,18 @@ export default class MatrixView extends ItemView {

async draw(): Promise<void> {
this.contentEl.empty();
const { gParents, gSiblings, gChildren } = this.plugin.currGraphs;

const allUps = getAllXGs(this.plugin, "up");
const allSames = getAllXGs(this.plugin, "same");

const allDowns = getAllXGs(this.plugin, "down");
console.log({ allUps, allDowns });

const upG = mergeGs(...Object.values(allUps));
const sameG = mergeGs(...Object.values(allSames));
const downG = mergeGs(...Object.values(allDowns));
console.log({ upG, sameG, downG });

const currFile = this.app.workspace.getActiveFile();
const settings = this.plugin.settings;

Expand All @@ -253,9 +281,9 @@ export default class MatrixView extends ItemView {
});

const [parentFieldName, siblingFieldName, childFieldName] = [
settings.showNameOrType ? settings.parentFieldName : "Parent",
settings.showNameOrType ? settings.siblingFieldName : "Sibling",
settings.showNameOrType ? settings.childFieldName : "Child",
"up",
"same",
"down",
];

let [
Expand All @@ -265,23 +293,21 @@ export default class MatrixView extends ItemView {
impliedParents,
impliedChildren,
] = [
this.squareItems(gParents, currFile),
this.squareItems(gSiblings, currFile),
this.squareItems(gChildren, currFile),
this.squareItems(gChildren, currFile, false),
this.squareItems(gParents, currFile, false),
this.squareItems(upG, currFile),
this.squareItems(sameG, currFile),
this.squareItems(downG, currFile),
this.squareItems(downG, currFile, false),
this.squareItems(upG, currFile, false),
];

// SECTION Implied Siblings
/// Notes with the same parents
const currParents = (gParents.successors(currFile.basename) ??
[]) as string[];
const currParents = (upG.successors(currFile.basename) ?? []) as string[];
let impliedSiblingsArr: internalLinkObj[] = [];

if (currParents.length) {
currParents.forEach((parent) => {
const impliedSiblings = (gParents.predecessors(parent) ??
[]) as string[];
const impliedSiblings = (upG.predecessors(parent) ?? []) as string[];

// The current note is always it's own implied sibling, so remove it from the list
const indexCurrNote = impliedSiblings.indexOf(currFile.basename);
Expand All @@ -302,7 +328,7 @@ export default class MatrixView extends ItemView {
}

/// A real sibling implies the reverse sibling
impliedSiblingsArr.push(...this.squareItems(gSiblings, currFile, false));
impliedSiblingsArr.push(...this.squareItems(sameG, currFile, false));

// !SECTION

Expand Down
87 changes: 42 additions & 45 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,31 @@ import {
VIEW_TYPE_BREADCRUMBS_STATS,
} from "src/constants";
import type {
allGraphs,
BreadcrumbsSettings,
dvFrontmatterCache,
neighbourObj,
relObj,
} from "src/interfaces";
import MatrixView from "src/MatrixView";
import StatsView from "src/StatsView";
import {
closeImpliedLinks,
debug,
getAllXGs,
getDVMetadataCache,
getNeighbourObjArr,
getObsMetadataCache,
mergeGs,
splitAndTrim,
} 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";
import { VisModal } from "src/VisModal";

const DEFAULT_SETTINGS: BreadcrumbsSettings = {
parentFieldName: "parent",
siblingFieldName: "sibling",
childFieldName: "child",
userHierarchies: [],
// parentFieldName: "parent",
// siblingFieldName: "sibling",
// childFieldName: "child",
indexNote: [""],
refreshIntervalTime: 0,
defaultView: true,
Expand Down Expand Up @@ -76,7 +77,7 @@ export default class BreadcrumbsPlugin extends Plugin {
settings: BreadcrumbsSettings;
visited: [string, HTMLDivElement][];
refreshIntervalID: number;
currGraphs: { [graph: string]: Graph };
currGraphs: { [field: string]: Graph }[];

async onload(): Promise<void> {
console.log("loading breadcrumbs plugin");
Expand Down Expand Up @@ -192,18 +193,17 @@ export default class BreadcrumbsPlugin extends Plugin {
populateGraph(
g: Graph,
currFileName: string,
relObj: relObj,
relationship: keyof relObj
fields: string[],
relationship: string
): void {
g.setNode(currFileName, relationship);
relObj[relationship].forEach((node: string) => {
g.setEdge(currFileName, node, relationship);
if (relationship === "") return;
fields.forEach((field) => {
g.setEdge(currFileName, field, relationship);
});
}

async initGraphs(): Promise<{
[graph: string]: Graph;
}> {
async initGraphs(): Promise<{ [field: string]: Graph }[]> {
debug(this.settings, "initialising graphs");
const files = this.app.vault.getMarkdownFiles();

Expand All @@ -217,46 +217,36 @@ export default class BreadcrumbsPlugin extends Plugin {

const relObjArr = await getNeighbourObjArr(this, fileFrontmatterArr);

const { parentFieldName, siblingFieldName, childFieldName } = this.settings;
const [parentFields, siblingFields, childFields] = [
splitAndTrim(parentFieldName),
splitAndTrim(siblingFieldName),
splitAndTrim(childFieldName),
];
const allFields = [parentFields, siblingFields, childFields].flat(1);
const { userHierarchies } = this.settings;
const allFields: string[] = userHierarchies
.map((hier) => Object.values(hier))
.flat()
.filter((field: string) => field !== "");

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

allFields.forEach((field) => {
graphs[field] = new Graph();
userHierarchies.forEach((hier, i) => {
const newGraphs: { [field: string]: Graph } = {};
newGraphs[hier.up] = new Graph();
newGraphs[hier.same] = new Graph();
newGraphs[hier.down] = new Graph();
graphs.push(newGraphs);
});

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

Object.keys(relObj).forEach((rel) => {
if (rel === "current") return;
this.populateGraph(graphs[rel], currFileName, relObj, rel);
relObj.hierarchies.forEach((hier, i) => {
Object.keys(hier).forEach((key) => {
const fields = hier[key];
this.populateGraph(graphs[i][key], currFileName, fields, key);
});
});
});

// 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");
console.log({ graphs });
return { ...graphs };
return graphs;
}

// !SECTION OneSource
Expand Down Expand Up @@ -373,8 +363,15 @@ export default class BreadcrumbsPlugin extends Plugin {

const settings = this.settings;

const { gParents, gChildren } = this.currGraphs;
const closedParents = closeImpliedLinks(gParents, gChildren);
const allUps = getAllXGs(this, "up");
const allDowns = getAllXGs(this, "down");
console.log({ allUps, allDowns });

const upG = mergeGs(...Object.values(allUps));
const downG = mergeGs(...Object.values(allDowns));
console.log({ upG, downG });

const closedParents = closeImpliedLinks(upG, downG);
const sortedTrails = this.getBreadcrumbs(closedParents);
debug(settings, { sortedTrails });

Expand Down
Loading

0 comments on commit 2f08de8

Please sign in to comment.