Skip to content

Commit

Permalink
feat: Statblocks will now attempt to self-update when the monster the…
Browse files Browse the repository at this point in the history
…y are rendering has been updated.
  • Loading branch information
valentine195 committed Apr 18, 2024
1 parent c5052be commit b7731ef
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/bestiary/bestiary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ declare module "obsidian" {
name: `fantasy-statblocks:bestiary:sorted:${T}`,
values: Array<Monster>
): void;
on(
name: `fantasy-statblocks:bestiary:creature-added`,
callback: (creature: Monster) => void
): EventRef;
on<T extends string>(
name: `fantasy-statblocks:bestiary:indexed:${T}`,
callback: () => void
Expand All @@ -29,10 +33,6 @@ declare module "obsidian" {
name: `fantasy-statblocks:bestiary:sorted:${T}`,
callback: (values: Array<Monster>) => void
): EventRef;
on(
name: `fantasy-statblocks:bestiary:creature-added`,
callback: (creature: Monster) => void
): EventRef;
}
}

Expand Down
58 changes: 38 additions & 20 deletions src/view/Statblock.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -54,7 +70,7 @@
setContext<StatBlockPlugin>("plugin", plugin);
setContext<boolean>("tryToRenderLinks", plugin.settings.tryToRenderLinks);
setContext<string>("context", context);
setContext<Partial<Monster>>("monster", monster);
setContext<Writable<Partial<Monster>>>("monster", monsterStore);
setContext<boolean>("dice", canDice);
setContext<boolean>("render", canRender);
setContext<StatBlockRenderer>("renderer", renderer);
Expand Down Expand Up @@ -177,25 +193,27 @@
class:statblock={true}
class={classes.join(" ")}
>
{#if monster}
<Bar />
{#key columns}
<ColumnContainer
{columns}
{maxColumns}
{statblock}
{ready}
{classes}
{layout}
{plugin}
on:save
on:export
/>
{/key}
<Bar />
{:else}
<span>Invalid monster.</span>
{/if}
{#key $monsterStore}
{#if $monsterStore}
<Bar />
{#key columns}
<ColumnContainer
{columns}
{maxColumns}
{statblock}
{ready}
{classes}
{layout}
{plugin}
on:save
on:export
/>
{/key}
<Bar />
{:else}
<span>Invalid monster.</span>
{/if}
{/key}
</div>
{#if icons}
<div class="icons" use:iconsEl on:click={showMenu} />
Expand Down
5 changes: 4 additions & 1 deletion src/view/ui/ColumnContainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -34,7 +35,9 @@
export let plugin: StatBlockPlugin;
const monster = getContext<Monster>("monster");
const monsterStore = getContext<Writable<Monster>>("monster");
let monster = $monsterStore;
monsterStore.subscribe((m) => (monster = m));
const ensureColon = (header: string) => {
if (/[^a-zA-Z0-9]$/.test(header)) return header;
return `${header}:`;
Expand Down
5 changes: 4 additions & 1 deletion src/view/ui/DiceHolder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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<BasicItem>("item");
let dice = getContext<boolean>("dice") && item.dice;
let monster = getContext<Monster>("monster");
const monsterStore = getContext<Writable<Monster>>("monster");
let monster = $monsterStore;
monsterStore.subscribe((m) => (monster = m));
let layout = getContext<Layout>("layout");
let plugin = getContext<StatBlockPlugin>("plugin");
Expand Down
5 changes: 4 additions & 1 deletion src/view/ui/JavaScript.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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>("monster");
const monsterStore = getContext<Writable<Monster>>("monster");
let monster = $monsterStore;
monsterStore.subscribe((m) => (monster = m));
let plugin = getContext<StatBlockPlugin>("plugin");
const render = (div: HTMLElement) => {
Expand Down
5 changes: 4 additions & 1 deletion src/view/ui/MarkdownHolder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -16,7 +17,9 @@
const renderer = getContext<StatBlockRenderer>("renderer");
let item = getContext<MarkdownableItem>("item");
let dice = getContext<boolean>("dice") && item.dice;
let monster = getContext<Monster>("monster");
const monsterStore = getContext<Writable<Monster>>("monster");
let monster = $monsterStore;
monsterStore.subscribe((m) => (monster = m));
let plugin = getContext<StatBlockPlugin>("plugin");
let layout = getContext<Layout>("layout");
Expand Down

0 comments on commit b7731ef

Please sign in to comment.