diff --git a/src/sidebar/character.ts b/src/sidebar/character.ts index 50e7592..e8e9e6d 100644 --- a/src/sidebar/character.ts +++ b/src/sidebar/character.ts @@ -16,7 +16,7 @@ export default async function renderIronVaultCharacter( try { const campaign = plugin.campaignManager.lastActiveCampaign(); if (!campaign) { - render(html`
No active campaign
`, containerEl); + render(html`No active campaign.
`, containerEl); return; } const context = currentActiveCharacterForCampaign( diff --git a/src/sidebar/sidebar-block.ts b/src/sidebar/sidebar-block.ts index 83a1c61..6db560a 100644 --- a/src/sidebar/sidebar-block.ts +++ b/src/sidebar/sidebar-block.ts @@ -4,12 +4,12 @@ import { CampaignManager } from "campaigns/manager"; import { UnsubscribeFunction } from "emittery"; import IronVaultPlugin from "index"; import { html, render } from "lit-html"; +import { rootLogger } from "logger"; import { Component, MarkdownRenderChild, Vault } from "obsidian"; import renderIronVaultMoves from "./moves"; import renderIronVaultOracles from "./oracles"; -// These are intended for folks who want to get the stuff that's usualy in the -// sidebar, but embedded in a note directly. +const logger = rootLogger.getLogger("sidebar-block"); export default function registerSidebarBlocks(plugin: IronVaultPlugin) { plugin.registerMarkdownCodeBlockProcessor( @@ -44,6 +44,7 @@ abstract class BaseCampaignSource extends Component { } protected update() { + logger.trace("BaseCampaignSource: triggering update callback"); return this.#onUpdate && this.#onUpdate(); } } @@ -54,12 +55,18 @@ export class ActiveCampaignWatch extends BaseCampaignSource { } onload() { + logger.trace("ActiveCampaignWatch.onload: regstering watch"); this.registerEvent( this.campaignManager.on("active-campaign-changed", () => this.update()), ); if (this.campaignManager.lastActiveCampaign()) { + logger.trace( + "ActiveCampaignWatch.onload: found last active campaign; triggering update", + ); setTimeout(() => this.update()); + } else { + logger.trace("ActiveCampaignWatch.onload: no active campaign"); } } diff --git a/src/sidebar/sidebar-view.ts b/src/sidebar/sidebar-view.ts index d9d1f29..dc3097f 100644 --- a/src/sidebar/sidebar-view.ts +++ b/src/sidebar/sidebar-view.ts @@ -2,6 +2,7 @@ import { html, render } from "lit-html"; import { debounce, ItemView, WorkspaceLeaf } from "obsidian"; import IronVaultPlugin from "index"; +import { rootLogger } from "logger"; import renderIronVaultCharacter from "./character"; import renderIronVaultMoves from "./moves"; import renderIronVaultOracles from "./oracles"; @@ -9,6 +10,8 @@ import { ActiveCampaignWatch } from "./sidebar-block"; export const SIDEBAR_VIEW_TYPE = "iron-vault-sidebar-view"; +const logger = rootLogger.getLogger("sidebar-view"); + export class SidebarView extends ItemView { plugin: IronVaultPlugin; campaignSource: ActiveCampaignWatch; @@ -16,6 +19,7 @@ export class SidebarView extends ItemView { constructor(leaf: WorkspaceLeaf, plugin: IronVaultPlugin) { super(leaf); this.plugin = plugin; + this.renderCharacter = debounce(this.renderCharacter.bind(this), 100, true); this.campaignSource = this.addChild( new ActiveCampaignWatch(plugin.campaignManager), ).onUpdate(() => this.refresh()); @@ -56,34 +60,30 @@ export class SidebarView extends ItemView { `; render(tpl, this.contentEl); - // We separate these out so they can do their own dynamic state stuff. - const renderCharacter = debounce(() => this.renderCharacter(), 100, true); - - this.registerEvent( - this.plugin.campaignManager.on("active-campaign-changed", () => { - renderCharacter(); - }), - ); - this.registerEvent( this.plugin.campaignManager.on( "active-campaign-settings-changed", ({ key }) => { if (key === "activeCharacter") { - renderCharacter(); + this.renderCharacter(); } }, ), ); - this.registerEvent(this.plugin.characters.on("changed", renderCharacter)); + this.registerEvent( + // TODO: probably this should be limited to just the current character, although + // how often would we change the non-active character? + this.plugin.characters.on("changed", this.renderCharacter), + ); - this.refresh(); + this.refresh(true); } - refresh() { + refresh(initial: boolean = false) { const dataContext = this.campaignSource.campaignContext; - if (dataContext) { + if (!initial && dataContext) { + logger.trace("SidebarView.refresh: refreshing from context"); renderIronVaultOracles( this.contentEl.querySelector(".content.oracle-tab")!, this.plugin, @@ -94,8 +94,21 @@ export class SidebarView extends ItemView { this.plugin, dataContext, ); + this.renderCharacter(); } else { - // I guess render something else? + logger.trace("SidebarView.refresh: no active campaign"); + render( + html`No active campaign.`, + this.contentEl.querySelector