Skip to content

Commit

Permalink
feat(API): ✨ Expose more methods on API
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Feb 12, 2022
1 parent db644e0 commit 3ab66ea
Show file tree
Hide file tree
Showing 10 changed files with 31,451 additions and 31,362 deletions.
62,632 changes: 31,325 additions & 31,307 deletions main.js

Large diffs are not rendered by default.

24 changes: 20 additions & 4 deletions src/API.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { MultiGraph } from "graphology";
import type { App } from "obsidian";
import { ARROW_DIRECTIONS, DIRECTIONS } from "./constants";
import type { BCAPII, Directions } from "./interfaces";
import type BCPlugin from "./main";
import { getMatrixNeighbours } from "./Views/MatrixView";
import {
buildObsGraph,
dfsAllPaths,
Expand All @@ -11,20 +14,33 @@ import {
export class BCAPI implements BCAPII {
app: App;
plugin: BCPlugin;
mainG: MultiGraph;
closedG: MultiGraph;

public constructor(app: App, plugin: BCPlugin) {
this.app = app;
this.plugin = plugin;
this.mainG = this.plugin.mainG;
this.closedG = this.plugin.closedG;
}

public DIRECTIONS = DIRECTIONS;
public ARROW_DIRECTIONS = ARROW_DIRECTIONS;

public buildObsGraph = () => buildObsGraph(this.app);

public getSubInDirs = (dirs: Directions[], g = this.plugin.mainG) =>
public getSubInDirs = (dirs: Directions[], g = this.mainG) =>
getSubInDirs(g, ...dirs);

public getSubForFields = (fields: string[], g = this.plugin.mainG) =>
public getSubForFields = (fields: string[], g = this.mainG) =>
getSubForFields(g, fields);

public dfsAllPaths = (fromNode: string, g = this.plugin.mainG) =>
dfsAllPaths(g, fromNode);
public dfsAllPaths = (
fromNode = this.app.workspace.getActiveFile()?.basename,
g = this.mainG
) => dfsAllPaths(g, fromNode);

public getMatrixNeighbours = (
fromNode = this.app.workspace.getActiveFile()?.basename
) => getMatrixNeighbours(this.plugin, fromNode);
}
5 changes: 4 additions & 1 deletion src/Codeblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import { getFieldInfo, getFields, getOppDir } from "./Utils/HierUtils";
import { createJuggl } from "./Visualisations/Juggl";

export function getCodeblockCB(plugin: BCPlugin) {
const { settings } = plugin;
const { settings, db } = plugin;
return (
source: string,
el: HTMLElement,
ctx: MarkdownPostProcessorContext
) => {
db.start2G("Codeblock");
const parsedSource = parseCodeBlockSource(source);
const err = codeblockError(plugin, parsedSource);

Expand Down Expand Up @@ -101,6 +102,8 @@ export function getCodeblockCB(plugin: BCPlugin) {
);
break;
}

db.end2G();
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/Utils/HierUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export const getOppDir = (dir: Directions): Directions => {
}
};

/**
* Get the hierarchy and direction that `field` is in
* */
export function getFieldInfo(userHiers: UserHier[], field: string) {
let fieldDir: Directions;
let fieldHier: UserHier;
Expand Down
42 changes: 25 additions & 17 deletions src/Utils/graphUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import type { Attributes } from "graphology-types";
import { info } from "loglevel";
import type { App } from "obsidian";
import type BCPlugin from "../../main";
import { BC_I_REFLEXIVE, BC_ORDER, blankRealNImplied } from "../constants";
import {
BC_I_REFLEXIVE,
BC_ORDER,
blankRealNImplied,
DIRECTIONS,
} from "../constants";
import type {
BCSettings,
Directions,
Expand All @@ -17,10 +22,6 @@ import type {
import { getFieldInfo, getOppDir, getOppFields } from "./HierUtils";
import { getBaseFromMDPath } from "./ObsidianUtils";

// TODO - this is a hack to get the graph to work with the approvals
// I shouldn't need
const DIRECTIONS = ["up", "same", "down", "next", "prev"];

// This function takes the real & implied graphs for a given relation, and returns a new graphs with both.
// It makes implied relations real
// TODO use reflexiveClosure instead
Expand All @@ -44,12 +45,14 @@ export function removeUnlinkedNodes(g: MultiGraph) {

/**
* Return a subgraph of all nodes & edges with `dirs.includes(a.dir)`
* @param {MultiGraph} main
*
* Filter the given graph to only include edges in the given directions.
* @param {MultiGraph} g
* @param {Directions} dir
*/
export function getSubInDirs(main: MultiGraph, ...dirs: Directions[]) {
export function getSubInDirs(g: MultiGraph, ...dirs: Directions[]) {
const sub = new MultiGraph();
main?.forEachEdge((k, a, s, t) => {
g?.forEachEdge((k, a, s, t) => {
if (dirs.includes(a.dir)) {
//@ts-ignore
addNodesIfNot(sub, [s, t], { order: a.order });
Expand All @@ -60,13 +63,15 @@ export function getSubInDirs(main: MultiGraph, ...dirs: Directions[]) {
}

/**
* Return a subgraph of all nodes & edges with `files.includes(a.field)`
* @param {MultiGraph} main
* Return a subgraph of all nodes & edges with `fields.includes(a.field)`.
*
* Filter the given graph to only include edges with the given fields.
* @param {MultiGraph} g
* @param {string[]} fields
*/
export function getSubForFields(main: MultiGraph, fields: string[]) {
export function getSubForFields(g: MultiGraph, fields: string[]) {
const sub = new MultiGraph();
main.forEachEdge((k, a, s, t) => {
g.forEachEdge((k, a, s, t) => {
if (fields.includes(a.field)) {
//@ts-ignore
addNodesIfNot(sub, [s, t], { order: a.order });
Expand Down Expand Up @@ -143,9 +148,13 @@ export const getInNeighbours = (g: MultiGraph, node: string) =>
g.hasNode(node) ? g.inNeighbors(node) : [];

/**
* Get the hierarchy and direction that `field` is in
* */

* Finds all paths from a starting node to all other sinks in a graph.
*
*
* @param {MultiGraph} g - The graph to search
* @param {string} start - The starting node
* @returns An array of arrays. Each array is a path.
*/
export function dfsAllPaths(g: MultiGraph, start: string): string[][] {
const queue: NodePath[] = [{ node: start, path: [] }];
const visited: { [note: string]: number } = {};
Expand Down Expand Up @@ -329,7 +338,7 @@ export function getRealnImplied(
if (s === currNode && (edgeDir === currDir || edgeDir === oppDir)) {
const arr = realsnImplieds[edgeDir].reals;
if (arr.findIndex((item) => item.to === t) === -1) {
arr.push({ to: t, real: true, field, implied });
arr.push({ to: t, field, implied });
}
}
// Implieds
Expand All @@ -339,7 +348,6 @@ export function getRealnImplied(
if (arr.findIndex((item) => item.to === s) === -1) {
arr.push({
to: s,
real: false,
field: oppField,
implied,
});
Expand Down
51 changes: 25 additions & 26 deletions src/Views/MatrixView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ import { splitAndTrim } from "../Utils/generalUtils";
import { getOppDir, getOppFields } from "../Utils/HierUtils";
import { getDVApi, linkClass } from "../Utils/ObsidianUtils";

export function getMatrixNeighbours(plugin: BCPlugin, currNode: string) {
const { closedG, settings } = plugin;
const { userHiers } = settings;
const neighbours = blankRealNImplied();
if (!closedG) return neighbours;

closedG.forEachEdge(currNode, (k, a, s, t) => {
const { field, dir, implied } = a as EdgeAttr;

if (s === currNode) {
neighbours[dir].reals.push({ to: t, field, implied });
} else {
neighbours[getOppDir(dir)].implieds.push({
to: s,
field: getOppFields(userHiers, field, dir)[0],
implied,
});
}
});

return neighbours;
}
export default class MatrixView extends ItemView {
plugin: BCPlugin;
private view: MLContainer;
Expand Down Expand Up @@ -126,29 +148,6 @@ export default class MatrixView extends ItemView {
getOrder = (node: string) =>
Number.parseInt(this.plugin.mainG.getNodeAttribute(node, "order"));

getMatrixNeighbours(plugin: BCPlugin, currNode: string) {
const { closedG, settings } = plugin;
const { userHiers } = settings;
const neighbours = blankRealNImplied();
if (!closedG) return neighbours;

closedG.forEachEdge(currNode, (k, a, s, t) => {
const { field, dir, implied } = a as EdgeAttr;

if (s === currNode) {
neighbours[dir].reals.push({ to: t, field, implied });
} else {
neighbours[getOppDir(dir)].implieds.push({
to: s,
field: getOppFields(userHiers, field, dir)[0],
implied,
});
}
});

return neighbours;
}

sortItemsAlpha = (a: internalLinkObj, b: internalLinkObj) => {
const { sortByNameShowAlias, alphaSortAsc } = this.plugin.settings;
const aToSort = (sortByNameShowAlias ? a.to : a.alt ?? a.to).toLowerCase();
Expand All @@ -168,15 +167,15 @@ export default class MatrixView extends ItemView {

const { basename } = currFile;
if (!mainG.hasNode(basename)) return [];
const realsnImplieds = this.getMatrixNeighbours(plugin, basename);
const realsnImplieds = getMatrixNeighbours(plugin, basename);

return userHiers.map((hier) => {
const filteredRealNImplied: {
const filteredRealNImplied = blankRealNImplied() as unknown as {
[dir in Directions]: {
reals: internalLinkObj[];
implieds: internalLinkObj[];
};
} = blankRealNImplied();
};

const resultsFilter = (
item: SquareItem,
Expand Down
3 changes: 1 addition & 2 deletions src/Views/TrailView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,12 @@ function getNextNPrev(plugin: BCPlugin, currNode: string) {
const { dir, field, implied } = a as EdgeAttr;
if (dir !== "next" && dir !== "prev") return;
if (s === currNode) {
nextNPrev[dir].reals.push({ field, to: t, real: true, implied });
nextNPrev[dir].reals.push({ field, to: t, implied });
} else {
const oppField = getOppFields(userHiers, field, dir)[0];
nextNPrev[getOppDir(dir)].implieds.push({
field: oppField,
to: s,
real: false,
implied,
});
}
Expand Down
8 changes: 7 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
BCSettings,
Directions,
RealNImplied,
Relations,
UserHier,
visTypes,
Expand Down Expand Up @@ -38,7 +39,12 @@ export const VISTYPES: visTypes[] = [
"Radial Tree",
];

/* All 5 possible directions. */
export const DIRECTIONS = ["up", "same", "down", "next", "prev"] as const;

/**
* An arrow for each {@link DIRECTIONS} value.
*/
export const ARROW_DIRECTIONS: { [dir in Directions]: string } = {
up: "↑",
same: "↔",
Expand Down Expand Up @@ -115,7 +121,7 @@ export const blankDirObjs = (): { [dir in Directions]: {} } => {
};
};

export const blankRealNImplied = () => {
export const blankRealNImplied = (): RealNImplied => {
return {
up: { reals: [], implieds: [] },
down: { reals: [], implieds: [] },
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export {
getSubForFields,
dfsAllPaths,
} from "./Utils/graphUtils";

export { DIRECTIONS, ARROW_DIRECTIONS } from "./constants";

export { BCAPI } from "./API";
41 changes: 37 additions & 4 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ export type HierData = {

export type SquareItem = {
to: string;
real: boolean;
field: string;
implied?: string;
};
Expand Down Expand Up @@ -331,8 +330,42 @@ export interface EdgeAttr {
}

export interface BCAPII {
mainG: MultiGraph;
closedG: MultiGraph;

/** Build the obsidian graph as a graphology MultiGraph */
buildObsGraph: () => MultiGraph;
getSubInDirs: (dirs: Directions[], g?: MultiGraph) => MultiGraph;
getSubForFields: (fields: Directions[], g?: MultiGraph) => MultiGraph;
dfsAllPaths: (fromNode: string, g?: MultiGraph) => string[][];

/**
* Return a subgraph of all nodes & edges with `dirs.includes(a.dir)`
*
* Filter the given graph to only include edges in the given directions.
* @param {MultiGraph} g - The graph to search. Defaults to `plugin.mainG`
* @param {Directions} dir - An array of directions to look for.
*/
getSubInDirs: (dirs: Directions[], g: MultiGraph) => MultiGraph;

/**
* Return a subgraph of all nodes & edges with `fields.includes(a.field)`.
*
* Filter the given graph to only include edges with the given fields.
* @param {MultiGraph} g - The graph to search. Defaults to `plugin.mainG`
* @param {string[]} fields - An array of fields to look for.
*/
getSubForFields: (fields: Directions[], g: MultiGraph) => MultiGraph;

/**
* Finds all paths from a starting node to all other sinks in a graph.
*
*
* @param {MultiGraph} g - The graph to search. Defaults to `plugin.mainG`
* @param {string} fromNode - The starting node
* @returns An array of arrays. Each array is a path.
*/
dfsAllPaths: (fromNode: string, g: MultiGraph) => string[][];

/** Get the Breadcrumb neighbours of the current note, split by `direction` and `real/implied`
* @param {string} fromNode - The starting node
*/
getMatrixNeighbours: (fromNode: string) => RealNImplied;
}

0 comments on commit 3ab66ea

Please sign in to comment.