Skip to content

Commit

Permalink
refactor: 🔥 Remove unnecessary functions! + Move stuff to more conven…
Browse files Browse the repository at this point in the history
…ient place
  • Loading branch information
SkepticMystic committed Nov 22, 2021
1 parent 53372f5 commit dfb27cd
Show file tree
Hide file tree
Showing 5 changed files with 19,031 additions and 19,236 deletions.
37,148 changes: 18,538 additions & 18,610 deletions main.js

Large diffs are not rendered by default.

147 changes: 7 additions & 140 deletions src/MatrixView.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { default as Graph } from "graphology";
import { cloneDeep } from "lodash";
import { ItemView, TFile, WorkspaceLeaf } from "obsidian";
import { ItemView, Notice, TFile, WorkspaceLeaf } from "obsidian";
import { blankRealNImplied, MATRIX_VIEW, TRAIL_ICON } from "src/constants";
import type {
BCSettings,
Expand All @@ -10,19 +8,14 @@ import type {
} from "src/interfaces";
import type BCPlugin from "src/main";
import {
copy,
debug,
debugGroupEnd,
debugGroupStart,
getInNeighbours,
getOppDir,
getOutNeighbours,
getRealnImplied,
getReflexiveClosure,
getSinks,
getSubInDirs,
linkClass,
makeWiki,
} from "src/sharedFunctions";
import Lists from "./Components/Lists.svelte";
import Matrix from "./Components/Matrix.svelte";
Expand Down Expand Up @@ -51,48 +44,6 @@ export default class MatrixView extends ItemView {
: 3000
);
});

this.plugin.addCommand({
id: "local-index",
name: "Copy a Local Index to the clipboard",
callback: async () => {
const { settings, mainG } = this.plugin;
const { basename } = this.app.workspace.getActiveFile();

const g = getSubInDirs(mainG, "up", "down");
const closed = getReflexiveClosure(g, settings.userHiers);
const onlyDowns = getSubInDirs(closed, "down");

const allPaths = this.dfsAllPaths(onlyDowns, basename);
const index = this.createIndex(allPaths, settings);
debug(settings, { index });
await copy(index);
},
});

this.plugin.addCommand({
id: "global-index",
name: "Copy a Global Index to the clipboard",
callback: async () => {
const { mainG, settings } = this.plugin;

const g = getSubInDirs(mainG, "up", "down");
const closed = getReflexiveClosure(g, settings.userHiers);
const onlyDowns = getSubInDirs(closed, "down");

const sinks = getSinks(mainG);

let globalIndex = "";
sinks.forEach((terminal) => {
globalIndex += terminal + "\n";
const allPaths = this.dfsAllPaths(onlyDowns, terminal);
globalIndex += this.createIndex(allPaths, settings);
});

debug(settings, { globalIndex });
await copy(globalIndex);
},
});
}

getViewType() {
Expand Down Expand Up @@ -144,102 +95,18 @@ export default class MatrixView extends ItemView {
return implieds.filter((implied) => !realTos.includes(implied.to));
}

dfsAllPaths(g: Graph, startNode: string): string[][] {
const queue: { node: string; path: string[] }[] = [
{ node: startNode, path: [] },
];
const visited = [];
const allPaths: string[][] = [];

let i = 0;
while (queue.length > 0 && i < 1000) {
i++;
const { node, path } = queue.shift();

const extPath = [node, ...path];
const succsNotVisited = g.hasNode(node)
? g.filterOutNeighbors(node, (n, a) => !visited.includes(n))
: [];
const newItems = succsNotVisited.map((n) => {
return { node: n, path: extPath };
});

visited.push(...succsNotVisited);
queue.unshift(...newItems);

// if (!g.hasNode(node) || !g.outDegree(node))
allPaths.push(extPath);
}
return allPaths;
}

createIndex(allPaths: string[][], settings: BCSettings): string {
let index = "";
const { wikilinkIndex } = settings;
const copy = cloneDeep(allPaths);
const reversed = copy.map((path) => path.reverse());
reversed.forEach((path) => path.shift());

const indent = " ";

const visited: {
[node: string]: /** The depths at which `node` was visited */ number[];
} = {};

reversed.forEach((path) => {
for (let depth = 0; depth < path.length; depth++) {
const currNode = path[depth];

// If that node has been visited before at the current depth
if (
visited.hasOwnProperty(currNode) &&
visited[currNode].includes(depth)
) {
continue;
} else {
index += `${indent.repeat(depth)}- ${makeWiki(
wikilinkIndex,
currNode
)}`;

if (settings.aliasesInIndex) {
const currFile = this.app.metadataCache.getFirstLinkpathDest(
currNode,
""
);

if (currFile !== null) {
const cache = this.app.metadataCache.getFileCache(currFile);

const alias = cache?.frontmatter?.alias ?? [];
const aliases = cache?.frontmatter?.aliases ?? [];

const allAliases: string[] = [
...[alias].flat(3),
...[aliases].flat(3),
];
if (allAliases.length) {
index += ` (${allAliases.join(", ")})`;
}
}
}

index += "\n";

if (!visited.hasOwnProperty(currNode)) visited[currNode] = [];
visited[currNode].push(depth);
}
}
});
return index;
}

getOrder = (node: string) =>
Number.parseInt(this.plugin.mainG.getNodeAttribute(node, "order"));

getHierSquares(userHiers: UserHier[], currFile: TFile, settings: BCSettings) {
const { plugin } = this;
const { mainG } = plugin;
if (!mainG) {
new Notice(
"Breadcrumbs graph was not initialised yet. Please Refresh Index"
);
return [];
}
const { basename } = currFile;
const up = getSubInDirs(mainG, "up");

Expand Down
11 changes: 10 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export interface BCSettings {
wikilinkIndex: boolean;
}

export type RawValue =
| string
| number
| dvLink
| Pos
| TFile
| undefined
| typeof Proxy;

export interface dvFrontmatterCache {
file: TFile;
[field: string]:
Expand Down Expand Up @@ -86,7 +95,7 @@ export interface JugglLink {
file: TFile;
links: {
dir: Directions | "";
type: string;
field: string;
linksInLine: string[];
}[];
}
Expand Down
Loading

0 comments on commit dfb27cd

Please sign in to comment.