Skip to content

Commit

Permalink
feat(API): ✨ Add various hierarchy utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Feb 12, 2022
1 parent 32d9dae commit 9b1bb8f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
12 changes: 11 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6696,7 +6696,7 @@ function getFieldInfo(userHiers, field) {
function getOppFields(userHiers, field, dir) {
// If the field ends with `>`, it is already the opposite field we need (coming from `getOppFallback`)
if (field.endsWith(">"))
return field.slice(0, -4);
return [field.slice(0, -4)];
const oppFields = [fallbackOppField(field, dir)];
const { fieldHier, fieldDir } = getFieldInfo(userHiers, field);
if (!fieldHier || !fieldDir)
Expand Down Expand Up @@ -36030,11 +36030,21 @@ class BCAPI {
this.getSubForFields = (fields, g = this.mainG) => getSubForFields(g, fields);
this.dfsAllPaths = (fromNode, g) => { var _a; if (fromNode === void 0) { fromNode = (_a = this.app.workspace.getActiveFile()) === null || _a === void 0 ? void 0 : _a.basename; } if (g === void 0) { g = this.mainG; } return dfsAllPaths(g, fromNode); };
this.getMatrixNeighbours = (fromNode) => { var _a; if (fromNode === void 0) { fromNode = (_a = this.app.workspace.getActiveFile()) === null || _a === void 0 ? void 0 : _a.basename; } return getMatrixNeighbours(this.plugin, fromNode); };
this.getOppDir = (dir) => getOppDir(dir);
this.getOppFields = (field) => {
const { fieldDir } = getFieldInfo(this.plugin.settings.userHiers, field);
return getOppFields(this.plugin.settings.userHiers, field, fieldDir);
};
this.getFieldInfo = (field) => getFieldInfo(this.plugin.settings.userHiers, field);
this.getFields = (dir) => getFields(this.plugin.settings.userHiers, dir !== null && dir !== void 0 ? dir : "all");
this.app = app;
this.plugin = plugin;
this.mainG = this.plugin.mainG;
this.closedG = this.plugin.closedG;
}
iterateHiers(cb) {
iterateHiers(this.plugin.settings.userHiers, cb);
}
}

/* src\Components\ModifyHNItemComp.svelte generated by Svelte v3.35.0 */
Expand Down
27 changes: 26 additions & 1 deletion src/API.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MultiGraph } from "graphology";
import type { App } from "obsidian";
import { ARROW_DIRECTIONS, DIRECTIONS } from "./constants";
import type { BCAPII, Directions } from "./interfaces";
import type { BCAPII, Directions, UserHier } from "./interfaces";
import type BCPlugin from "./main";
import { getMatrixNeighbours } from "./Views/MatrixView";
import {
Expand All @@ -10,6 +10,13 @@ import {
getSubForFields,
getSubInDirs,
} from "./Utils/graphUtils";
import {
getFieldInfo,
getFields,
getOppDir,
getOppFields,
iterateHiers,
} from "./Utils/HierUtils";

export class BCAPI implements BCAPII {
app: App;
Expand Down Expand Up @@ -43,4 +50,22 @@ export class BCAPI implements BCAPII {
public getMatrixNeighbours = (
fromNode = this.app.workspace.getActiveFile()?.basename
) => getMatrixNeighbours(this.plugin, fromNode);

public getOppDir = (dir: Directions) => getOppDir(dir);

public getOppFields = (field: string) => {
const { fieldDir } = getFieldInfo(this.plugin.settings.userHiers, field);
return getOppFields(this.plugin.settings.userHiers, field, fieldDir);
};

public getFieldInfo = (field: string) =>
getFieldInfo(this.plugin.settings.userHiers, field);
public getFields = (dir?: Directions) =>
getFields(this.plugin.settings.userHiers, dir ?? "all");

public iterateHiers(
cb: (hier: UserHier, dir: Directions, field: string) => void
) {
iterateHiers(this.plugin.settings.userHiers, cb);
}
}
2 changes: 1 addition & 1 deletion src/Utils/HierUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function getOppFields(
dir: Directions
) {
// If the field ends with `>`, it is already the opposite field we need (coming from `getOppFallback`)
if (field.endsWith(">")) return field.slice(0, -4);
if (field.endsWith(">")) return [field.slice(0, -4)];

const oppFields = [fallbackOppField(field, dir)];
const { fieldHier, fieldDir } = getFieldInfo(userHiers, field);
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export type {
RealNImplied,
Directions,
SquareItem,
UserHier
} from "./interfaces";
26 changes: 25 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export interface BCAPII {
* @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;
getSubForFields: (fields: string[], g?: MultiGraph) => MultiGraph;

/**
* Finds all paths from a starting node to all other sinks in a graph.
Expand All @@ -371,4 +371,28 @@ export interface BCAPII {
* @param {string} fromNode - The starting node. Defaults to the currently active note.
*/
getMatrixNeighbours: (fromNode?: string) => RealNImplied;

/** Get the direction opposite to `dir` */
getOppDir: (dir: Directions) => Directions;

/** Get all fields in the direction opposite to the direction of `field` */
getOppFields: (field: string) => string[];

/**
* Get the hierarchy and direction that `field` is in
* @returns {{ fieldHier: UserHier; fieldDir: Directions }}
* */
getFieldInfo(field: string): { fieldHier: UserHier; fieldDir: Directions };

/**
* Get all the fields in `dir`.
* Returns all fields by default.
* @param {Directions} dir
*/
getFields(dir?: Directions | ""): string[];

/** Iterate over all user hierarchies, running `cb` on each new field */
iterateHiers: (
cb: (hier: UserHier, dir: Directions, field: string) => void
) => void;
}

0 comments on commit 9b1bb8f

Please sign in to comment.