Skip to content

Commit

Permalink
feat(List/Matrix View): ✨ Setting to list field names to check for al…
Browse files Browse the repository at this point in the history
…ternate link text

Basically, you can choose to use aliases to display notes, instead of file name
  • Loading branch information
SkepticMystic committed Sep 10, 2021
1 parent d8ca26d commit 52f0321
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 132 deletions.
192 changes: 140 additions & 52 deletions main.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions src/BreadcrumbsSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ export class BreadcrumbsSettingTab extends PluginSettingTab {
})
);

new Setting(generalDetails)
.setName('Fields used for Alternative note names (Aliases)')
.setDesc('A comma-separated list of fields you use to specify note name aliases. These fields will be checked, in order, and be used to display an alternate note title in both the list/matrix view, and trail/grid view. This field will probably be `alias` or `aliases`, but it can be anything, like `title`, for example.')
.addText(text => {
let finalValue: string;

text
.setValue(settings.altLinkFields.join(', '))
.onChange(str => {
finalValue = str
});
text.inputEl.onblur = async () => {
settings.altLinkFields = splitAndTrim(finalValue);
await plugin.saveSettings()
}
})


new Setting(generalDetails)
.setName("Use yaml or inline fields for hierarchy data")
.setDesc(
Expand Down
14 changes: 10 additions & 4 deletions src/Components/Lists.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@
class={realItem.cls}
on:click={async (e) =>
openOrSwitch(app, realItem.to, currFile, e)}
on:mouseover={(e) => hoverPreview(e, matrixView)}
on:mouseover={(e) =>
hoverPreview(e, matrixView, realItem.to)}
>
{realItem.to.split("/").last()}
{realItem.alt
? realItem.alt
: realItem.to.split("/").last()}
</div>
</li>
{/each}
Expand All @@ -59,9 +62,12 @@
class={impliedItem.cls}
on:click={async (e) =>
openOrSwitch(app, impliedItem.to, currFile, e)}
on:mouseover={(e) => hoverPreview(e, matrixView)}
on:mouseover={(e) =>
hoverPreview(e, matrixView, impliedItem.to)}
>
{impliedItem.to.split("/").last()}
{impliedItem.alt
? impliedItem.alt
: impliedItem.to.split("/").last()}
</div>
</li>
{/each}
Expand Down
14 changes: 10 additions & 4 deletions src/Components/Matrix.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
class={realItem.cls}
on:click={async (e) =>
openOrSwitch(app, realItem.to, currFile, e)}
on:mouseover={(event) => hoverPreview(event, matrixView)}
on:mouseover={(event) =>
hoverPreview(event, matrixView, realItem.to)}
>
{realItem.to.split("/").last()}
{realItem.alt
? realItem.alt
: realItem.to.split("/").last()}
</div>
</li>
{/each}
Expand All @@ -51,9 +54,12 @@
class={impliedItem.cls}
on:click={async (e) =>
openOrSwitch(app, impliedItem.to, currFile, e)}
on:mouseover={(event) => hoverPreview(event, matrixView)}
on:mouseover={(event) =>
hoverPreview(event, matrixView, impliedItem.to)}
>
{impliedItem.to.split("/").last()}
{impliedItem.alt
? impliedItem.alt
: impliedItem.to.split("/").last()}
</div>
</li>
{/each}
Expand Down
6 changes: 3 additions & 3 deletions src/Components/TrailGrid.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
: "internal-link breadcrumbs-link";
}
function hoverPreview(event: MouseEvent, view: View): void {
function hoverPreview(event: MouseEvent, view: View, to: string): void {
const targetEl = event.target as HTMLElement;
view.app.workspace.trigger("hover-link", {
event,
source: view.getViewType(),
hoverParent: view,
targetEl,
linktext: targetEl.innerText,
linktext: to,
});
}
Expand Down Expand Up @@ -123,7 +123,7 @@
).toString(16)}`
: ''}"
on:click={(e) => openOrSwitch(app, step.value, currFile, e)}
on:mouseover={(e) => hoverPreview(e, activeLeafView)}
on:mouseover={(e) => hoverPreview(e, activeLeafView, step.value)}
>
<div>{step.value}</div>
{#if step.value && settings.gridDots}
Expand Down
108 changes: 52 additions & 56 deletions src/Components/TrailPath.svelte
Original file line number Diff line number Diff line change
@@ -1,73 +1,69 @@
<script lang="ts">
import type { App,TFile, View } from "obsidian";
import type { BreadcrumbsSettings } from "src/interfaces";
import { openOrSwitch } from "src/sharedFunctions";
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;
export let sortedTrails: string[][];
export let app: App;
export let settings: BreadcrumbsSettings;
export let currFile: TFile;
const activeLeafView = app.workspace.activeLeaf.view
const activeLeafView = app.workspace.activeLeaf.view;
function hoverPreview(event: MouseEvent, view: View): void {
const targetEl = event.target as HTMLElement;
function hoverPreview(event: MouseEvent, view: View, to: string): 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]];
view.app.workspace.trigger("hover-link", {
event,
source: view.getViewType(),
hoverParent: view,
targetEl,
linktext: to,
});
}
let showAll = settings.showAll;
$: buttonText = showAll ? "Shortest" : "All";
$: trailsToShow = showAll ? sortedTrails : [sortedTrails[0]];
</script>

<span class="breadcrumbs-trail-path-container">
<div class="trails-div">
{#each trailsToShow as trail}
<div>
{#if trail.length === 0}
<span>{settings.noPathMessage}</span>
{:else}
{#each trail as crumb, i}
<span
class="internal-link breadcrumbs-link"
on:click={async (e) => await openOrSwitch(app, crumb, currFile, e)}
on:mouseover={(e) => hoverPreview(e, activeLeafView)}>
{crumb}
</span>
{#if i < trail.length - 1}
<span>{' ' + settings.trailSeperator + ' '}</span>
{/if}
{/each}
{/if}
</div>
{/each}
</div>
<div class="trails-div">
{#each trailsToShow as trail}
<div>
{#if trail.length === 0}
<span>{settings.noPathMessage}</span>
{:else}
{#each trail as crumb, i}
<span
class="internal-link breadcrumbs-link"
on:click={async (e) =>
await openOrSwitch(app, crumb, currFile, e)}
on:mouseover={(e) => hoverPreview(e, activeLeafView, crumb)}
>
{crumb}
</span>
{#if i < trail.length - 1}
<span>{" " + settings.trailSeperator + " "}</span>
{/if}
{/each}
{/if}
</div>
{/each}
</div>

{#if sortedTrails.length > 1}
{#if sortedTrails.length > 1}
<div>
<button
class="button-div"
on:click={() => showAll = !showAll}>
{buttonText}
</button>
<button class="button-div" on:click={() => (showAll = !showAll)}>
{buttonText}
</button>
</div>
{/if}
{/if}
</span>


<style>
span.breadcrumbs-trail-path-container {
span.breadcrumbs-trail-path-container {
display: flex;
justify-content: space-between;
}
</style>
}
</style>
36 changes: 26 additions & 10 deletions src/MatrixView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ export default class MatrixView extends ItemView {
return unresolvedLinks[from][to] > 0;
}

squareItems(g: Graph, currFile: TFile, realQ = true): internalLinkObj[] {
squareItems(g: Graph, currFile: TFile, settings: BreadcrumbsSettings, realQ = true): internalLinkObj[] {
let items: string[];
const altFieldsQ = !!settings.altLinkFields.length

if (realQ) {
items = (g.successors(currFile.basename) ?? []) as string[];
Expand All @@ -134,13 +135,26 @@ export default class MatrixView extends ItemView {
// TODO I don't think I need to check the length here
/// forEach won't run if it's empty anyway
if (items.length) {
items.forEach((item: string) => {
items.forEach((to: string) => {
let alt = null;
if (altFieldsQ) {
const toFile = this.app.metadataCache.getFirstLinkpathDest(to, currFile.path)
if (toFile) {
const metadata = this.app.metadataCache.getFileCache(toFile)
settings.altLinkFields.forEach(altLinkField => {
const altLink = metadata?.frontmatter?.[altLinkField]
if (altLink) { alt = altLink; return }
console.log({ alt, altLink })
})
}
}
internalLinkObjArr.push({
to: item,
to,
cls:
"internal-link breadcrumbs-link" +
(this.unresolvedQ(item, currFile.path) ? " is-unresolved" : "") +
(this.unresolvedQ(to, currFile.path) ? " is-unresolved" : "") +
(realQ ? "" : " breadcrumbs-implied"),
alt
});
});
}
Expand Down Expand Up @@ -298,11 +312,11 @@ export default class MatrixView extends ItemView {
];

let [rUp, rSame, rDown, iUp, iDown] = [
this.squareItems(currUpG, currFile),
this.squareItems(currSameG, currFile),
this.squareItems(currDownG, currFile),
this.squareItems(currDownG, currFile, false),
this.squareItems(currUpG, currFile, false),
this.squareItems(currUpG, currFile, settings),
this.squareItems(currSameG, currFile, settings),
this.squareItems(currDownG, currFile, settings),
this.squareItems(currDownG, currFile, settings, false),
this.squareItems(currUpG, currFile, settings, false),
];

// SECTION Implied Siblings
Expand Down Expand Up @@ -335,12 +349,14 @@ export default class MatrixView extends ItemView {
(this.unresolvedQ(impliedSibling, currFile.path)
? " is-unresolved"
: ""),
// TODO get alt for implied siblings
alt: null
});
});
});

/// A real sibling implies the reverse sibling
iSameArr.push(...this.squareItems(currSameG, currFile, false));
iSameArr.push(...this.squareItems(currSameG, currFile, settings, false));

// !SECTION

Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface BreadcrumbsSettings {
hierarchyNoteDownFieldName: string;
hierarchyNoteUpFieldName: string;
refreshIndexOnActiveLeafChange: boolean;
altLinkFields: string[];
useAllMetadata: boolean;
parseJugglLinksWithoutJuggl: boolean;
dvWaitTime: number;
Expand Down Expand Up @@ -96,6 +97,7 @@ export interface fileFrontmatter {
export interface internalLinkObj {
to: string;
cls: string;
alt: string | null;
}

export interface SquareProps {
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const DEFAULT_SETTINGS: BreadcrumbsSettings = {
hierarchyNoteDownFieldName: "",
hierarchyNoteUpFieldName: "",
refreshIndexOnActiveLeafChange: false,
altLinkFields: [],
useAllMetadata: true,
parseJugglLinksWithoutJuggl: false,
dvWaitTime: 5000,
Expand Down
6 changes: 3 additions & 3 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,15 @@ export const isInVault = (app: App, note: string): boolean =>
app.workspace.getActiveFile().path
);

export function hoverPreview(event: MouseEvent, matrixView: MatrixView): void {
export function hoverPreview(event: MouseEvent, matrixView: MatrixView, to: string): void {
const targetEl = event.target as HTMLElement;

matrixView.app.workspace.trigger("hover-link", {
event,
source: matrixView.getViewType(),
hoverParent: matrixView,
targetEl,
linktext: targetEl.innerText,
linktext: to,
});
}

Expand Down Expand Up @@ -673,7 +673,7 @@ export const writeBCToFile = (app: App, plugin: BreadcrumbsPlugin, currGraphs: B
succs.forEach(async succ => {
const { fieldName } = fieldG.node(succ);
if (!plugin.settings.limitWriteBCCheckboxStates[fieldName]) return

const currHier = plugin.settings.userHierarchies.filter(hier => hier[dir].includes(fieldName))[0]
let oppField: string = currHier[oppDir][0];
if (!oppField) oppField = `<Reverse>${fieldName}`
Expand Down

0 comments on commit 52f0321

Please sign in to comment.