Skip to content

Commit

Permalink
More reliable character sidebar rendering
Browse files Browse the repository at this point in the history
Fixes #469
  • Loading branch information
cwegrzyn committed Oct 31, 2024
1 parent 4dfea30 commit f6509ad
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/sidebar/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default async function renderIronVaultCharacter(
try {
const campaign = plugin.campaignManager.lastActiveCampaign();
if (!campaign) {
render(html`<p>No active campaign</p>`, containerEl);
render(html`<p>No active campaign.</p>`, containerEl);
return;
}
const context = currentActiveCharacterForCampaign(
Expand Down
11 changes: 9 additions & 2 deletions src/sidebar/sidebar-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -44,6 +44,7 @@ abstract class BaseCampaignSource extends Component {
}

protected update() {
logger.trace("BaseCampaignSource: triggering update callback");
return this.#onUpdate && this.#onUpdate();
}
}
Expand All @@ -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");
}
}

Expand Down
44 changes: 29 additions & 15 deletions src/sidebar/sidebar-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ 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";
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;

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());
Expand Down Expand Up @@ -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,
Expand All @@ -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<HTMLElement>(".content.oracle-tab")!,
);
render(
html`No active campaign.`,
this.contentEl.querySelector<HTMLElement>(".content.move-tab")!,
);
render(
html`No active campaign.`,
this.contentEl.querySelector<HTMLElement>(".content.character-tab")!,
);
}
}

Expand All @@ -104,6 +117,7 @@ export class SidebarView extends ItemView {
}

renderCharacter() {
logger.trace("SidebarView.renderCharacter: render character");
renderIronVaultCharacter(
this.contentEl.querySelector(".content.character-tab")!,
this.plugin,
Expand Down

0 comments on commit f6509ad

Please sign in to comment.