From 4669df8c73c026e1fd0fb04905b841d79cff6e14 Mon Sep 17 00:00:00 2001 From: Jeremy Valentine <38669521+valentine195@users.noreply.github.com> Date: Thu, 7 Mar 2024 16:05:07 -0500 Subject: [PATCH] feat: Adds ability to open note-based creatures directly from the bestiary in settings --- index.ts | 1 + src/api/api.ts | 2 +- src/settings/creatures/Creature.svelte | 29 ++++++++++++++++++------- src/settings/creatures/Creatures.svelte | 2 +- src/typings/obsidian-extended.d.ts | 11 +++++++++- src/watcher/watcher.ts | 1 + 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/index.ts b/index.ts index 59cb921b..f65f7205 100644 --- a/index.ts +++ b/index.ts @@ -96,6 +96,7 @@ export interface Monster { forceColumns?: boolean; note?: string; + path?: string; mtime?: number; /* Extensions */ diff --git a/src/api/api.ts b/src/api/api.ts index 63fa56ba..d3323bfe 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -4,8 +4,8 @@ import type { Component } from "obsidian"; import type { HomebrewCreature } from "obsidian-overload"; import { Bestiary } from "src/bestiary/bestiary"; import type StatBlockPlugin from "src/main"; -import { buildLoader } from "src/util"; import StatBlockRenderer from "src/view/statblock"; + declare global { interface Window { FantasyStatblocks: API; diff --git a/src/settings/creatures/Creature.svelte b/src/settings/creatures/Creature.svelte index bc6fb875..1b446b3e 100644 --- a/src/settings/creatures/Creature.svelte +++ b/src/settings/creatures/Creature.svelte @@ -6,9 +6,11 @@ import { Bestiary } from "src/bestiary/bestiary"; import { getContext } from "../layout/context"; import { NameFilter } from "./filters/filters"; + import { createEventDispatcher } from "svelte"; export let item: Monster; + const dispatch = createEventDispatcher<{ close: void }>(); const plugin = getContext("plugin"); let content: Setting; const buildSetting = (node: HTMLElement) => { @@ -33,14 +35,7 @@ stringify(item.source, 0, ", ", false) ); } - content.addExtraButton((b) => { - b.setIcon("info") - .setTooltip("View") - .onClick(() => { - const modal = new ViewMonsterModal(plugin, item as Monster); - modal.open(); - }); - }); + if (Bestiary.isLocal(item.name)) { content .addExtraButton((b) => { @@ -58,7 +53,25 @@ await plugin.deleteMonster(item.name); }); }); + } else if (item.path) { + const file = plugin.app.vault.getFileByPath(item.path); + if (!file) return; + content.addExtraButton((b) => { + b.setIcon("file-symlink").onClick(async () => { + const leaf = plugin.app.workspace.getLeaf(); + await leaf.openFile(file); + plugin.app.setting.close(); + }); + }); } + content.addExtraButton((b) => { + b.setIcon("info") + .setTooltip("View") + .onClick(() => { + const modal = new ViewMonsterModal(plugin, item as Monster); + modal.open(); + }); + }); }; $: { diff --git a/src/settings/creatures/Creatures.svelte b/src/settings/creatures/Creatures.svelte index 4740382d..9669c66c 100644 --- a/src/settings/creatures/Creatures.svelte +++ b/src/settings/creatures/Creatures.svelte @@ -78,7 +78,7 @@
{#each $sliced as item (item.name)} - + {/each}
diff --git a/src/typings/obsidian-extended.d.ts b/src/typings/obsidian-extended.d.ts index d308a215..7fff084a 100644 --- a/src/typings/obsidian-extended.d.ts +++ b/src/typings/obsidian-extended.d.ts @@ -18,6 +18,9 @@ declare module "obsidian" { listCommands: () => Command[]; executeCommandById: (id: string) => boolean; }; + setting: { + close: () => void; + }; } interface MetadataCache { initialized: boolean; @@ -32,6 +35,12 @@ declare module "obsidian" { name: T, data: StatblockEvents[T] ): void; - trigger(name: "link-hover", popover: object, target: HTMLElement, link: string, source: string); + trigger( + name: "link-hover", + popover: object, + target: HTMLElement, + link: string, + source: string + ); } } diff --git a/src/watcher/watcher.ts b/src/watcher/watcher.ts index db9a7e00..49fffcb8 100644 --- a/src/watcher/watcher.ts +++ b/src/watcher/watcher.ts @@ -122,6 +122,7 @@ class WatcherClass extends Component { let { monster, path } = evt.data.data; let update = Bestiary.hasCreature(monster.name); + monster.path = path; Bestiary.addEphemeralCreature(monster);