Skip to content

Commit

Permalink
fix: TrailGrid overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Jul 9, 2021
1 parent 21a4bad commit 627c64d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 24 deletions.
52 changes: 33 additions & 19 deletions src/TrailGrid.svelte
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
<script lang="ts">
import type { App,TFile } from "obsidian";
import type { App,TFile,View } from "obsidian";
import type { BreadcrumbsSettings } from "src/interfaces";
import { debug,openOrSwitch,padArray,transpose } from "src/sharedFunctions";
import { debug,openOrSwitch,padArray,runs,transpose } from "src/sharedFunctions";
export let sortedTrails: string[][]
export let app: App;
export let settings: BreadcrumbsSettings;
const currFile = app.workspace.getActiveFile();
const activeLeafView = app.workspace.activeLeaf.view;
function resolvedClass(toFile: string, currFile: TFile): string {
return app.metadataCache.unresolvedLinks[currFile.path][toFile] > 0
? "internal-link is-unresolved breadcrumbs-link"
: "internal-link breadcrumbs-link";
}
? "internal-link is-unresolved breadcrumbs-link"
: "internal-link breadcrumbs-link";
}
function hoverPreview(event: MouseEvent, view: View): void {
const targetEl = event.target as HTMLElement;
view.app.workspace.trigger("hover-link", {
event,
source: view.getViewType(),
hoverParent: view,
targetEl,
linktext: targetEl.innerText,
});
}
const maxLength = sortedTrails.last().length
const maxLength = Math.max(...sortedTrails.map(trail => trail.length))
const paddedTrails: string[][] = sortedTrails.map(trail => padArray(trail, maxLength))
const transposedTrails: string[][] = transpose(paddedTrails);
const uniqueValuesPerCol = transposedTrails.map(trail => [...new Set(trail)])
const allRuns = transposedTrails.map(runs);
debug(settings, {maxLength, paddedTrails, transposedTrails, uniqueValuesPerCol})
debug(settings, {maxLength, paddedTrails, transposedTrails, runs: allRuns})
</script>

Expand All @@ -30,19 +42,21 @@ debug(settings, {maxLength, paddedTrails, transposedTrails, uniqueValuesPerCol})
grid-template-rows: {'1fr '.repeat(sortedTrails.length)}">
{#each transposedTrails as col, i}

{#each uniqueValuesPerCol[i] as step}
{#each allRuns[i] as step}
<div
class="breadcrumbs-trail-grid-item
{resolvedClass(step, currFile)}
{step === '' ? 'breadcrumbs-filler' : ''}"
{resolvedClass(step.value, currFile)}
{step.value === '' ? 'breadcrumbs-filler' : ''}"

style="
grid-area: {col.indexOf(step) + 1} / {i + 1} /
{col.lastIndexOf(step) + 2} / {i + 2};"
on:click="{(e) =>
openOrSwitch(app, step, currFile, e)
}">
{step}
grid-area: {step.first + 1} / {i + 1} /
{step.last + 2} / {i + 2};"

on:click={(e) =>
openOrSwitch(app, step.value, currFile, e)
}
on:mouseover={(e) => hoverPreview(e,activeLeafView)}>
{step.value}
</div>
{/each}

Expand All @@ -65,7 +79,7 @@ div.breadcrumbs-trail-grid-item {
border: 1px solid var(--background-modifier-border);
align-items: center;
justify-content: center;
padding: 5px;
padding: 2px;
height: auto;
}
Expand Down
20 changes: 17 additions & 3 deletions src/TrailPath.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
<script lang="ts">
import type { App,TFile } from "obsidian";
import type { App,TFile, View } from "obsidian";
import type { BreadcrumbsSettings } from "src/interfaces";
import { openOrSwitch } from "src/sharedFunctions";
export let sortedTrails: string[][];
export let app: App;
export let settings: BreadcrumbsSettings;
export let currFile: TFile;
const activeLeafView = app.workspace.activeLeaf.view
function hoverPreview(event: MouseEvent, view: View): void {
const targetEl = event.target as HTMLElement;
view.app.workspace.trigger("hover-link", {
event,
source: view.getViewType(),
hoverParent: view,
targetEl,
linktext: targetEl.innerText,
});
}
let showAll = settings.showAll;
$: buttonText = showAll ? "Shortest" : "All";
$: trailsToShow = showAll ? sortedTrails : [sortedTrails[0]];
Expand All @@ -25,7 +38,8 @@ $: trailsToShow = showAll ? sortedTrails : [sortedTrails[0]];
{#each trail as crumb, i}
<span
class="internal-link breadcrumbs-link"
on:click={async (e) => await openOrSwitch(app, crumb, currFile, e)}>
on:click={async (e) => await openOrSwitch(app, crumb, currFile, e)}
on:mouseover={(e) => hoverPreview(e, activeLeafView)}>
{crumb}
</span>
{#if i < trail.length - 1}
Expand Down
28 changes: 27 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
neighbourObj
} from "src/interfaces";
import MatrixView from "src/MatrixView";
import { closeImpliedLinks, debug, getFileFrontmatterArr, getNeighbourObjArr, isInVault } from "src/sharedFunctions";
import { closeImpliedLinks, debug, getFileFrontmatterArr, getNeighbourObjArr } from "src/sharedFunctions";
import TrailGrid from "./TrailGrid.svelte";
import TrailPath from "./TrailPath.svelte";

Expand Down Expand Up @@ -190,6 +190,32 @@ export default class BreadcrumbsPlugin extends Plugin {
return pathsArr;
}

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

let i = 0;
while (queue.length > 0 && i < 1000) {
i++
const currPath = queue.shift();

const newNodes = (g.successors(currPath.node) ?? []) as string[];
const extPath = [currPath.node, ...currPath.path];
queue.unshift(
...newNodes.map((n: string) => {
return { node: n, path: extPath };
})
);

if (newNodes.length === 0) {
pathsArr.push(extPath);
}
}
return pathsArr
}

getBreadcrumbs(g: Graph): string[][] {
const from = this.app.workspace.getActiveFile().basename;
const paths = graphlib.alg.dijkstra(g, from);
Expand Down
16 changes: 15 additions & 1 deletion src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export async function openOrSwitch(
}
});


if (openLeaves.length > 0) {
console.log(openLeaves[0])
workspace.setActiveLeaf(openLeaves[0]);
Expand Down Expand Up @@ -299,4 +299,18 @@ export function transpose(A: any[][]): any[][] {
A.forEach(row => AT[j].push(row[j]))
}
return AT
}

export function runs(arr: string[]): { value: string, first: number, last: number }[] {
const runs: { value: string, first: number, last: number }[] = []
let i = 0;
while (i < arr.length) {
const currValue = arr[i]
runs.push({ value: currValue, first: i, last: undefined })
while (currValue === arr[i]) {
i++
}
runs.last().last = i - 1;
}
return runs
}

0 comments on commit 627c64d

Please sign in to comment.