Skip to content

Commit

Permalink
feat(DucksView): ✨ Add Duck view to show notes which don't have any b…
Browse files Browse the repository at this point in the history
…readcrumbs
  • Loading branch information
SkepticMystic committed Nov 19, 2021
1 parent 0d1ec3e commit 2d0b2b2
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 48 deletions.
29 changes: 29 additions & 0 deletions src/Components/Ducks.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<script lang="ts">
import type { App } from "obsidian";
import { hoverPreview, openOrSwitch } from "obsidian-community-lib";
import type DucksView from "src/DucksView";
import type BCPlugin from "src/main";
export let plugin: BCPlugin;
export let app: App;
export let ducksView: DucksView;
const { main } = plugin.currGraphs;
const files = app.vault.getMarkdownFiles();
const fileNames = files.map((file) => file.basename);
const ducks = fileNames.filter((name) => !main.neighbors(name).length);
</script>

<div class="BC-Ducks markdown-preview-view">
<h6>Notes without Breadcrumbs</h6>
{#each ducks as duck}
<div
on:click={async (e) => await openOrSwitch(app, duck, e)}
on:mouseover={(e) => hoverPreview(e, ducksView, duck)}
>
<a class="internal-link">{duck}</a>
</div>
{/each}
</div>
50 changes: 50 additions & 0 deletions src/DucksView.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ItemView, WorkspaceLeaf } from "obsidian";
import { STATS_VIEW } from "src/constants";
import type BCPlugin from "src/main";
import Ducks from "./Components/Ducks.svelte";

export default class DucksView extends ItemView {
private plugin: BCPlugin;
private view: Ducks;

constructor(leaf: WorkspaceLeaf, plugin: BCPlugin) {
super(leaf);
this.plugin = plugin;
}

async onload(): Promise<void> {
super.onload();
await this.plugin.saveSettings();
this.app.workspace.onLayoutReady(async () => {
await this.draw();
});
}

getViewType() {
return STATS_VIEW;
}
getDisplayText() {
return "Breadcrumbs Ducks";
}

// TODO Duck icon
icon = "info";

async onOpen(): Promise<void> {}

onClose(): Promise<void> {
if (this.view) {
this.view.$destroy();
}
return Promise.resolve();
}

async draw(): Promise<void> {
this.contentEl.empty();

this.view = new Ducks({
target: this.contentEl,
props: { plugin: this.plugin, app: this.app, ducksView: this },
});
}
}
96 changes: 48 additions & 48 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ import {
TFile,
WorkspaceLeaf,
} from "obsidian";
import { openView, wait } from "obsidian-community-lib/dist/utils";
import {
addFeatherIcon,
openView,
wait,
} from "obsidian-community-lib/dist/utils";
import { BCSettingTab } from "src/BreadcrumbsSettingTab";
import {
blankDirObjs,
blankDirUndef,
DEFAULT_SETTINGS,
DIRECTIONS,
DUCK_VIEW,
MATRIX_VIEW,
STATS_VIEW,
MyView,
TRAIL_ICON,
TRAIL_ICON_SVG,
VIEWS,
} from "src/constants";
import type {
BCIndex,
Expand All @@ -28,7 +34,6 @@ import type {
dvFrontmatterCache,
HierarchyGraphs,
} from "src/interfaces";
import MatrixView from "src/MatrixView";
import {
addEdgeIfNot,
addNodeIfNot,
Expand All @@ -44,13 +49,11 @@ import {
getOppDir,
getOutNeighbours,
getPrevNext,
iterateAllGs,
mergeGs,
oppFields,
removeDuplicates,
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";
Expand All @@ -68,9 +71,11 @@ export default class BCPlugin extends Plugin {
if (!this.activeLeafChange) this.registerActiveLeafEvent();

this.currGraphs = await this.initGraphs();
const activeView = this.getActiveMatrixView();
const activeMatrix = this.getActiveTYPEView(MATRIX_VIEW);
const activeDucks = this.getActiveTYPEView(DUCK_VIEW);

if (activeView) await activeView.draw();
if (activeMatrix) await activeMatrix.draw();
if (activeDucks) await activeDucks.draw();
if (this.settings.showTrail) await this.drawTrail();

new Notice("Index refreshed");
Expand All @@ -83,7 +88,7 @@ export default class BCPlugin extends Plugin {
if (this.settings.refreshIndexOnActiveLeafChange) {
await this.refreshIndex();
} else {
const activeView = this.getActiveMatrixView();
const activeView = this.getActiveTYPEView(MATRIX_VIEW);
if (activeView) await activeView.draw();
if (this.settings.showBCs) await this.drawTrail();
}
Expand All @@ -96,8 +101,10 @@ export default class BCPlugin extends Plugin {
const { settings } = this;
this.currGraphs = await this.initGraphs();

await openView(this.app, MATRIX_VIEW, MatrixView);
await openView(this.app, STATS_VIEW, StatsView);
for (const view of VIEWS) {
if (view.openOnLoad)
await openView(this.app, view.type, view.constructor);
}

if (settings.showBCs) await this.drawTrail();

Expand All @@ -109,7 +116,7 @@ export default class BCPlugin extends Plugin {
this.currGraphs = await this.initGraphs();
if (settings.showBCs) await this.drawTrail();

const activeView = this.getActiveMatrixView();
const activeView = this.getActiveTYPEView(MATRIX_VIEW);
if (activeView) await activeView.draw();
}, settings.refreshIntervalTime * 1000);
this.registerInterval(this.refreshIntervalID);
Expand All @@ -127,14 +134,12 @@ export default class BCPlugin extends Plugin {
});
});

this.registerView(
STATS_VIEW,
(leaf: WorkspaceLeaf) => new StatsView(leaf, this)
);
this.registerView(
MATRIX_VIEW,
(leaf: WorkspaceLeaf) => new MatrixView(leaf, this)
);
for (const view of VIEWS) {
this.registerView(
view.type,
(leaf: WorkspaceLeaf) => new view.constructor(leaf, this)
);
}

this.app.workspace.onLayoutReady(async () => {
if (this.app.plugins.enabledPlugins.has("dataview")) {
Expand All @@ -153,29 +158,19 @@ export default class BCPlugin extends Plugin {

addIcon(TRAIL_ICON, TRAIL_ICON_SVG);

this.addCommand({
id: "show-breadcrumbs-matrix-view",
name: "Open Matrix View",
//@ts-ignore
checkCallback: async (checking: boolean) => {
if (checking) {
return this.app.workspace.getLeavesOfType(MATRIX_VIEW).length === 0;
}
await openView(this.app, MATRIX_VIEW, MatrixView);
},
});

this.addCommand({
id: "show-breadcrumbs-stats-view",
name: "Open Stats View",
//@ts-ignore
checkCallback: async (checking: boolean) => {
if (checking) {
return this.app.workspace.getLeavesOfType(STATS_VIEW).length === 0;
}
await openView(this.app, STATS_VIEW, StatsView);
},
});
for (const view of VIEWS) {
this.addCommand({
id: `show-${view.type}-view`,
name: `Open ${view.plain} View`,
//@ts-ignore
checkCallback: async (checking: boolean) => {
if (checking) {
return this.app.workspace.getLeavesOfType(view.type).length === 0;
}
await openView(this.app, view.type, view.constructor);
},
});
}

this.addCommand({
id: "Refresh-Breadcrumbs-Index",
Expand Down Expand Up @@ -250,11 +245,12 @@ export default class BCPlugin extends Plugin {
this.addSettingTab(new BCSettingTab(this.app, this));
}

getActiveMatrixView(): MatrixView | null {
const leaves = this.app.workspace.getLeavesOfType(MATRIX_VIEW);
getActiveTYPEView(type: string): MyView | null {
const { constructor } = VIEWS.find((view) => view.type === type);
const leaves = this.app.workspace.getLeavesOfType(type);
if (leaves && leaves.length >= 1) {
const view = leaves[0].view;
if (view instanceof MatrixView) {
if (view instanceof constructor) {
return view;
}
}
Expand Down Expand Up @@ -365,11 +361,14 @@ export default class BCPlugin extends Plugin {
dir: Directions,
fieldName: string
): void {
//@ts-ignore
addNodeIfNot(g, currFileName, { dir, fieldName });

if (fieldName === "") return;
fieldValues.forEach((value) => {
//@ts-ignore
addNodeIfNot(g, value, { dir, fieldName });
//@ts-ignore
addEdgeIfNot(g, currFileName, value, { dir, fieldName });
});
}
Expand Down Expand Up @@ -411,6 +410,7 @@ export default class BCPlugin extends Plugin {
if (fieldName === "" || !row[fieldName]) return;

addNodeIfNot(g, row[fieldName]);
//@ts-ignore
addEdgeIfNot(g, row.file, row[fieldName], { dir, fieldName });
});
}
Expand Down Expand Up @@ -541,6 +541,7 @@ export default class BCPlugin extends Plugin {

hierarchyNotesArr.forEach((adjListItem) => {
adjListItem.children.forEach((child) => {
//@ts-ignore
addNodeIfNot(gUp, adjListItem.note, { dir: "up" });
gUp.addEdge(child, adjListItem.note, {
dir: "up",
Expand All @@ -556,6 +557,7 @@ export default class BCPlugin extends Plugin {

hierarchyNotesArr.forEach((adjListItem) => {
adjListItem.children.forEach((child) => {
//@ts-ignore
addNodeIfNot(gDown, adjListItem.note, { dir: "down" });
gDown.addEdge(adjListItem.note, child, {
dir: "down",
Expand Down Expand Up @@ -782,9 +784,7 @@ export default class BCPlugin extends Plugin {

onunload(): void {
console.log("unloading");
[MATRIX_VIEW, STATS_VIEW].forEach((type) =>
this.app.workspace.detachLeavesOfType(type)
);
VIEWS.forEach((view) => this.app.workspace.detachLeavesOfType(view.type));

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

0 comments on commit 2d0b2b2

Please sign in to comment.