Skip to content

Commit

Permalink
fix: Trail stays open
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Jul 9, 2021
1 parent 55e78a9 commit b665927
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 63 deletions.
16 changes: 12 additions & 4 deletions src/TrailGrid.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ debug(settings, {maxLength, paddedTrails, transposedTrails, uniqueValuesPerCol})
{#each transposedTrails as col, i}

{#each uniqueValuesPerCol[i] as step}
<div class="breadcrumbs-trail-grid-item {resolvedClass(step, currFile)} {step === '' ? '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}
<div
class="breadcrumbs-trail-grid-item
{resolvedClass(step, currFile)}
{step === '' ? '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}
</div>
{/each}

Expand Down
57 changes: 14 additions & 43 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ const DEFAULT_SETTINGS: BreadcrumbsSettings = {
export default class BreadcrumbsPlugin extends Plugin {
settings: BreadcrumbsSettings;
matrixView: MatrixView;
trailDiv: HTMLDivElement;
// activeTrails: { file: TFile, trailDiv: HTMLDivElement }[];
// previousFile: TFile[];
visited: [string, HTMLDivElement][];
refreshIntervalID: number;
currGraphs: allGraphs;

Expand All @@ -51,22 +49,20 @@ export default class BreadcrumbsPlugin extends Plugin {

await this.loadSettings();

// this.activeTrails = [];
// this.previousFile = [this.app.workspace.getActiveFile(), this.app.workspace.getActiveFile()];
this.visited = [];

this.registerView(
VIEW_TYPE_BREADCRUMBS_MATRIX,
(leaf: WorkspaceLeaf) => (this.matrixView = new MatrixView(leaf, this))
);

this.app.workspace.onLayoutReady(async () => {
this.trailDiv = createDiv()
// this.trailDiv = createDiv()
setTimeout(async () => {
this.currGraphs = await this.initGraphs();

this.initView(VIEW_TYPE_BREADCRUMBS_MATRIX);


if (this.settings.showTrail) {
await this.drawTrail();
}
Expand All @@ -78,11 +74,6 @@ export default class BreadcrumbsPlugin extends Plugin {

await this.matrixView.draw();
if (this.settings.showTrail) {
// this.activeTrails.last().trailDiv.remove
// this.activeTrails.splice(this.activeTrails.length - 1, 1)
// this.previousFile.shift();
// this.previousFile.push(this.app.workspace.getActiveFile())
// console.log(this.previousFile)
await this.drawTrail();
}
})
Expand Down Expand Up @@ -255,31 +246,6 @@ export default class BreadcrumbsPlugin extends Plugin {
return sortedTrails;
}


fillTrailDiv(trailDiv: HTMLDivElement, breadcrumbs: string[], currFile: TFile): void {
// If a path was found
if (breadcrumbs.length > 0) {
breadcrumbs.forEach((crumb) => {
const link = trailDiv.createSpan({
text: crumb,
// A link in the trail will never be unresolved, so no need to check
cls: "internal-link breadcrumbs-link",
});
link.addEventListener("click", async (e) => {
await openOrSwitch(this.app, crumb, currFile, e);
});
trailDiv.createSpan({
text: ` ${this.settings.trailSeperator} `,
});
});
trailDiv.removeChild(trailDiv.lastChild);
}
// Otherwise don't add any links, just text
else {
trailDiv.createSpan({ text: this.settings.noPathMessage });
}
}

async drawTrail(): Promise<void> {
const { gParents, gChildren } = this.currGraphs;
const closedParents = closeImpliedLinks(gParents, gChildren)
Expand All @@ -291,24 +257,29 @@ export default class BreadcrumbsPlugin extends Plugin {
const previewView = document.querySelector(
"div.mod-active div.view-content div.markdown-preview-view"
);
previewView.querySelector('div.breadcrumbs-trail')?.remove()

const trailDiv = createDiv()
previewView.prepend(trailDiv)

this.visited.push([currFile.path, trailDiv])

this.trailDiv.className = `breadcrumbs-trail is-readable-line-width${settings.respectReadableLineLength
trailDiv.className = `breadcrumbs-trail is-readable-line-width${settings.respectReadableLineLength
? " markdown-preview-sizer markdown-preview-section"
: ""
}`

previewView.prepend(this.trailDiv);
previewView.prepend(trailDiv);

this.trailDiv.empty();
trailDiv.empty();
if (settings.trailOrTable) {
new TrailPath({
target: this.trailDiv,
target: trailDiv,
props: { sortedTrails, app: this.app, settings, currFile }
})
} else {
new TrailGrid({
target: this.trailDiv,
target: trailDiv,
props: { sortedTrails, app: this.app, settings }
})
}
Expand Down Expand Up @@ -347,7 +318,7 @@ export default class BreadcrumbsPlugin extends Plugin {
openLeaves.forEach((leaf) => leaf.detach());

// Empty trailDiv
this.trailDiv.remove();
this.visited.forEach(visit => visit[1].remove())

}
}
21 changes: 5 additions & 16 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Graph } from "graphlib";
import { parseTypedLink } from "juggl-api";
import type { App, FrontMatterCache, TFile } from "obsidian";
import type { App, FrontMatterCache, TFile, WorkspaceLeaf } from "obsidian";
import { dropHeaderOrAlias, splitLinksRegex } from "src/constants";
import type {
BreadcrumbsSettings,
Expand Down Expand Up @@ -227,19 +227,6 @@ export function closeImpliedLinks(real: Graph, implied: Graph): Graph {
return closedG
}

// This doesn't work for some reason. Even if you pass it `app`, metadatacache is undefined
// export function resolvedClass(
// app: App,
// toFile: string,
// currFile: TFile
// ):
// | "internal-link is-unresolved breadcrumbs-link"
// | "internal-link breadcrumbs-link" {
// return app.metadataCache.unresolvedLinks[currFile.path][toFile] > 0
// ? "internal-link is-unresolved breadcrumbs-link"
// : "internal-link breadcrumbs-link";
// }

export const isInVault = (app: App, note: string): boolean =>
!!app.metadataCache.getFirstLinkpathDest(
note,
Expand Down Expand Up @@ -267,15 +254,17 @@ export async function openOrSwitch(
const { workspace } = app;
const destFile = app.metadataCache.getFirstLinkpathDest(dest, currFile.path);

const openLeaves = [];
const openLeaves: WorkspaceLeaf[] = [];
// For all open leaves, if the leave's basename is equal to the link destination, rather activate that leaf instead of opening it in two panes
workspace.iterateAllLeaves((leaf) => {
if (leaf.view?.file?.basename === dest) {
openLeaves.push(leaf);
}
});

if (openLeaves.length) {

if (openLeaves.length > 0) {
console.log(openLeaves[0])
workspace.setActiveLeaf(openLeaves[0]);
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit b665927

Please sign in to comment.