Skip to content

Commit

Permalink
feat(nextPrev): ✨ Show fieldName
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Nov 18, 2021
1 parent 65ed6df commit b2e10d4
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 38 deletions.
8 changes: 5 additions & 3 deletions src/Components/NextPrev.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import { openOrSwitch } from "obsidian-community-lib";
import { linkClass } from "src/sharedFunctions";
import type BCPlugin from "src/main";
import type { PrevNext } from "src/interfaces";
export let app: App;
export let plugin: BCPlugin;
export let next: { to: string; real: boolean }[];
export let prev: { to: string; real: boolean }[];
export let next: PrevNext[];
export let prev: PrevNext[];
</script>

<div class="BC-NextPrev-Container">
Expand All @@ -18,6 +19,7 @@
on:click={async (e) => openOrSwitch(app, p.to, e)}
class={linkClass(app, p.to, p.real)}
>
<strong>{p.fieldName}</strong>
{p.to}
</div>
{/each}
Expand All @@ -31,7 +33,7 @@
on:click={async (e) => openOrSwitch(app, n.to, e)}
class="{linkClass(app, n.to, n.real)} BC-next"
>
{n.to}
{n.to} <strong>{n.fieldName}</strong>
</div>
{/each}
</span>
Expand Down
5 changes: 1 addition & 4 deletions src/Components/UserHierarchies.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
<div class="GA-Buttons">
<button
aria-label="Add New Hierarchy"
on:click={async () => {
currHiers = [...currHiers, blankUserHier()];
// await update(currHiers);
}}
on:click={async () => (currHiers = [...currHiers, blankUserHier()])}
>
<div class="icon">
<FaPlus />
Expand Down
16 changes: 9 additions & 7 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ export interface BCSettings {
export interface dvFrontmatterCache {
file: TFile;
[field: string]:
| string
| string[]
| string[][]
| dvLink
| dvLink[]
| Pos
| TFile;
| string
| string[]
| string[][]
| dvLink
| dvLink[]
| Pos
| TFile;
}

export type Directions = "up" | "same" | "down" | "next" | "prev";
Expand Down Expand Up @@ -204,3 +204,5 @@ export type MergedGraphs = {
export type ClosedGraphs = {
[dir in Directions]: Graph;
};

export type PrevNext = { to: string; real: boolean; fieldName: string };
31 changes: 9 additions & 22 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Notice,
Plugin,
TFile,
WorkspaceLeaf,
WorkspaceLeaf
} from "obsidian";
import { openView, wait } from "obsidian-community-lib/dist/utils";
import { BCSettingTab } from "src/BreadcrumbsSettingTab";
Expand All @@ -17,14 +17,14 @@ import {
MATRIX_VIEW,
STATS_VIEW,
TRAIL_ICON,
TRAIL_ICON_SVG,
TRAIL_ICON_SVG
} from "src/constants";
import type {
BCIndex,
BCSettings,
Directions,
dvFrontmatterCache,
HierarchyGraphs,
HierarchyGraphs
} from "src/interfaces";
import MatrixView from "src/MatrixView";
import {
Expand All @@ -41,17 +41,18 @@ import {
getObsMetadataCache,
getOppDir,
getOutNeighbours,
getPrevNext,
iterateAllGs,
mergeGs,
oppFields,
removeDuplicates,
writeBCToFile,
writeBCToFile
} from "src/sharedFunctions";
import StatsView from "src/StatsView";
import { VisModal } from "src/VisModal";
import NextPrev from "./Components/NextPrev.svelte";
import TrailGrid from "./Components/TrailGrid.svelte";
import TrailPath from "./Components/TrailPath.svelte";
import NextPrev from "./Components/NextPrev.svelte";

export default class BCPlugin extends Plugin {
settings: BCSettings;
Expand Down Expand Up @@ -684,24 +685,10 @@ export default class BCPlugin extends Plugin {
debug(settings, { sortedTrails });

const { basename } = currFile;
const { main } = this.currGraphs;

const next: { to: string; real: boolean }[] = [];
const prev: { to: string; real: boolean }[] = [];
main.forEachEdge(basename, (k, a, s, t) => {
if (a.dir === "next" && s === basename) {
next.push({ to: t, real: true });
}
if (a.dir === "prev" && t === basename) {
next.push({ to: s, real: false });
}
if (a.dir === "prev" && s === basename) {
prev.push({ to: t, real: true });
}
if (a.dir === "next" && t === basename) {
prev.push({ to: s, real: false });
}
});
const { rPrev, rNext, iPrev, iNext } = getPrevNext(this, basename);
const next = [...rNext, ...iNext];
const prev = [...rPrev, ...iPrev];

const noItems =
sortedTrails.length === 0 && next.length === 0 && prev.length === 0;
Expand Down
64 changes: 62 additions & 2 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Graph from "graphology";
import type NodeAttributes from "graphology";
import Graph from "graphology";
import { parseTypedLink } from "juggl-api";
import {
App,
Expand All @@ -9,7 +9,12 @@ import {
TFile,
WorkspaceLeaf,
} from "obsidian";
import { DIRECTIONS, dropHeaderOrAlias, splitLinksRegex } from "src/constants";
import {
ARROW_DIRECTIONS,
DIRECTIONS,
dropHeaderOrAlias,
splitLinksRegex,
} from "src/constants";
import type {
BCIndex,
BCSettings,
Expand All @@ -19,6 +24,7 @@ import type {
HierarchyFields,
HierarchyGraphs,
JugglLink,
PrevNext,
userHierarchy,
} from "src/interfaces";
import type BCPlugin from "src/main";
Expand Down Expand Up @@ -748,6 +754,30 @@ export function oppFields(
);
}

/**
* Get the hierarchy and direction that `field` is in
* */
export function getFieldInfo(userHierarchies: userHierarchy[], field: string) {
let fieldDir: Directions;
let fieldHier: userHierarchy;
DIRECTIONS.forEach((dir) => {
userHierarchies.forEach((hier) => {
if (hier[dir].includes(field)) {
fieldDir = dir;
fieldHier = hier;
return;
}
});
});
return { fieldHier, fieldDir };
}

export function getOppFields(userHierarchies: userHierarchy[], field: string) {
const { fieldHier, fieldDir } = getFieldInfo(userHierarchies, field);
const oppDir = getOppDir(fieldDir);
return fieldHier[oppDir];
}

export function addNodeIfNot(g: Graph, node: string, attr?: NodeAttributes) {
if (!g.hasNode(node)) g.addNode(node, attr);
}
Expand Down Expand Up @@ -785,3 +815,33 @@ export const getOutNeighbours = (g: Graph, node: string): string[] =>
g.hasNode(node) ? g.outNeighbors(node) : [];
export const getInNeighbours = (g: Graph, node: string): string[] =>
g.hasNode(node) ? g.inNeighbors(node) : [];

export function getPrevNext(plugin: BCPlugin, currNode: string) {
const [rPrev, rNext, iPrev, iNext]: PrevNext[][] = [[], [], [], []];
const { userHierarchies } = plugin.settings;

plugin.currGraphs.main.forEachEdge(currNode, (k, a, s, t) => {
const { fieldName } = a;
if (a.dir === "next" && s === currNode) {
rNext.push({ to: t, real: true, fieldName });
}
if (a.dir === "prev" && t === currNode) {
iNext.push({
to: s,
real: false,
fieldName: getOppFields(userHierarchies, fieldName)[0],
});
}
if (a.dir === "prev" && s === currNode) {
rPrev.push({ to: t, real: true, fieldName });
}
if (a.dir === "next" && t === currNode) {
iPrev.push({
to: s,
real: false,
fieldName: getOppFields(userHierarchies, fieldName)[0],
});
}
});
return { rPrev, rNext, iPrev, iNext };
}

0 comments on commit b2e10d4

Please sign in to comment.