From b7731efea9d3aaf13aead6966382d7a6c51951fb Mon Sep 17 00:00:00 2001 From: Jeremy Valentine <38669521+valentine195@users.noreply.github.com> Date: Thu, 18 Apr 2024 12:44:28 -0400 Subject: [PATCH] feat: Statblocks will now attempt to self-update when the monster they are rendering has been updated. --- src/bestiary/bestiary.ts | 8 ++--- src/view/Statblock.svelte | 58 +++++++++++++++++++----------- src/view/ui/ColumnContainer.svelte | 5 ++- src/view/ui/DiceHolder.svelte | 5 ++- src/view/ui/JavaScript.svelte | 5 ++- src/view/ui/MarkdownHolder.svelte | 5 ++- 6 files changed, 58 insertions(+), 28 deletions(-) diff --git a/src/bestiary/bestiary.ts b/src/bestiary/bestiary.ts index 25532935..85a844a8 100644 --- a/src/bestiary/bestiary.ts +++ b/src/bestiary/bestiary.ts @@ -21,6 +21,10 @@ declare module "obsidian" { name: `fantasy-statblocks:bestiary:sorted:${T}`, values: Array ): void; + on( + name: `fantasy-statblocks:bestiary:creature-added`, + callback: (creature: Monster) => void + ): EventRef; on( name: `fantasy-statblocks:bestiary:indexed:${T}`, callback: () => void @@ -29,10 +33,6 @@ declare module "obsidian" { name: `fantasy-statblocks:bestiary:sorted:${T}`, callback: (values: Array) => void ): EventRef; - on( - name: `fantasy-statblocks:bestiary:creature-added`, - callback: (creature: Monster) => void - ): EventRef; } } diff --git a/src/view/Statblock.svelte b/src/view/Statblock.svelte index 776c46da..36bb31b6 100644 --- a/src/view/Statblock.svelte +++ b/src/view/Statblock.svelte @@ -21,6 +21,9 @@ import Bar from "./ui/Bar.svelte"; import ColumnContainer from "./ui/ColumnContainer.svelte"; + import { nanoid } from "src/util/util"; + import { Bestiary } from "src/bestiary/bestiary"; + import copy from "fast-copy"; const dispatch = createEventDispatcher(); @@ -33,6 +36,19 @@ export let canSave: boolean = true; export let icons = true; + const monsterStore = writable(monster); + + plugin.registerEvent( + plugin.app.workspace.on( + "fantasy-statblocks:bestiary:creature-added", + (creature) => { + if (creature.name === monster.name) { + $monsterStore = copy(creature); + } + } + ) + ); + let maxColumns = !isNaN(Number(monster.columns ?? layout.columns)) && Number(monster.columns ?? layout.columns) > 0 @@ -54,7 +70,7 @@ setContext("plugin", plugin); setContext("tryToRenderLinks", plugin.settings.tryToRenderLinks); setContext("context", context); - setContext>("monster", monster); + setContext>>("monster", monsterStore); setContext("dice", canDice); setContext("render", canRender); setContext("renderer", renderer); @@ -177,25 +193,27 @@ class:statblock={true} class={classes.join(" ")} > - {#if monster} - - {#key columns} - - {/key} - - {:else} - Invalid monster. - {/if} + {#key $monsterStore} + {#if $monsterStore} + + {#key columns} + + {/key} + + {:else} + Invalid monster. + {/if} + {/key} {#if icons}
diff --git a/src/view/ui/ColumnContainer.svelte b/src/view/ui/ColumnContainer.svelte index b2d2cb5d..43e7796d 100644 --- a/src/view/ui/ColumnContainer.svelte +++ b/src/view/ui/ColumnContainer.svelte @@ -20,6 +20,7 @@ import type { Monster, Trait } from "index"; import { Linkifier } from "src/parser/linkify"; import Action from "./Action.svelte"; + import type { Writable } from "svelte/store"; const dispatch = createEventDispatcher(); @@ -34,7 +35,9 @@ export let plugin: StatBlockPlugin; - const monster = getContext("monster"); + const monsterStore = getContext>("monster"); + let monster = $monsterStore; + monsterStore.subscribe((m) => (monster = m)); const ensureColon = (header: string) => { if (/[^a-zA-Z0-9]$/.test(header)) return header; return `${header}:`; diff --git a/src/view/ui/DiceHolder.svelte b/src/view/ui/DiceHolder.svelte index ad672b25..84f3f248 100644 --- a/src/view/ui/DiceHolder.svelte +++ b/src/view/ui/DiceHolder.svelte @@ -9,13 +9,16 @@ import TextContent from "./TextContent.svelte"; import { parseForDice } from "src/parser/dice-parsing"; import type StatBlockPlugin from "src/main"; + import type { Writable } from "svelte/store"; export let property: string; let item = getContext("item"); let dice = getContext("dice") && item.dice; - let monster = getContext("monster"); + const monsterStore = getContext>("monster"); + let monster = $monsterStore; + monsterStore.subscribe((m) => (monster = m)); let layout = getContext("layout"); let plugin = getContext("plugin"); diff --git a/src/view/ui/JavaScript.svelte b/src/view/ui/JavaScript.svelte index b49107c6..43e868a4 100644 --- a/src/view/ui/JavaScript.svelte +++ b/src/view/ui/JavaScript.svelte @@ -5,10 +5,13 @@ import type StatBlockPlugin from "src/main"; import { getContext } from "svelte"; + import type { Writable } from "svelte/store"; export let block: JavaScriptItem; - let monster = getContext("monster"); + const monsterStore = getContext>("monster"); + let monster = $monsterStore; + monsterStore.subscribe((m) => (monster = m)); let plugin = getContext("plugin"); const render = (div: HTMLElement) => { diff --git a/src/view/ui/MarkdownHolder.svelte b/src/view/ui/MarkdownHolder.svelte index b4244afa..318a6f4d 100644 --- a/src/view/ui/MarkdownHolder.svelte +++ b/src/view/ui/MarkdownHolder.svelte @@ -8,6 +8,7 @@ import type { Monster } from "index"; import { Linkifier } from "src/parser/linkify"; import { parseForDice } from "src/parser/dice-parsing"; + import type { Writable } from "svelte/store"; export let property: string; property = Linkifier.stringifyLinks(property); @@ -16,7 +17,9 @@ const renderer = getContext("renderer"); let item = getContext("item"); let dice = getContext("dice") && item.dice; - let monster = getContext("monster"); + const monsterStore = getContext>("monster"); + let monster = $monsterStore; + monsterStore.subscribe((m) => (monster = m)); let plugin = getContext("plugin"); let layout = getContext("layout");