From 70a11c4743b384e465d6736079218bbdd53736d9 Mon Sep 17 00:00:00 2001 From: Jeremy Valentine <38669521+valentine195@users.noreply.github.com> Date: Wed, 6 Sep 2023 12:35:19 -0400 Subject: [PATCH] feat: new `Add to View` setting for Dice rollers. when enabled, all dice roller results will be added to the Dice View --- src/main.ts | 14 ++++++++++++-- src/settings/settings.ts | 21 +++++++++++++++++++-- src/view/view.ts | 2 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main.ts b/src/main.ts index eb8136d..3781c8c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -152,6 +152,7 @@ interface DiceRollerSettings { defaultFace: number; renderer: boolean; renderAllDice: boolean; + addToView: boolean; renderTime: number; colorfulDice: boolean; scaler: number; @@ -184,6 +185,7 @@ export const DEFAULT_SETTINGS: DiceRollerSettings = { defaultFace: 100, renderer: false, renderAllDice: false, + addToView: false, renderTime: 2000, colorfulDice: false, scaler: 1, @@ -400,8 +402,12 @@ export default class DiceRollerPlugin extends Plugin { content, ctx.sourcePath ); - if (roller instanceof StackRoller && roller.shouldRender) { - roller.hasRunOnce = true; + if (roller instanceof StackRoller) { + if (roller.shouldRender) roller.hasRunOnce = true; + roller.on("new-result", () => { + if (this.data.addToView) + this.view?.addResult(roller); + }); } modPromises.push( @@ -485,6 +491,10 @@ export default class DiceRollerPlugin extends Plugin { } if (roller instanceof StackRoller) { roller.shouldRender = shouldRender; + roller.on("new-result", () => { + if (this.data.addToView) + this.view?.addResult(roller); + }); } node.replaceWith(roller.containerEl); diff --git a/src/settings/settings.ts b/src/settings/settings.ts index 960c70f..e130ef7 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -190,10 +190,12 @@ export default class SettingTab extends PluginSettingTab { }); new Setting(containerEl) .setName("Auto Roll dice") - .setDesc("On initial display, should dice be rolled or displayed empty.") + .setDesc( + "On initial display, should dice be rolled or displayed empty." + ) .addDropdown((d) => { d.addOption(ExpectedValue.None, "Empty") - .addOption(ExpectedValue.Roll, "Rolled") + .addOption(ExpectedValue.Roll, "Rolled") .setValue(this.plugin.data.initialDisplay) .onChange((v: ExpectedValue) => { this.plugin.data.initialDisplay = v; @@ -228,6 +230,21 @@ export default class SettingTab extends PluginSettingTab { this.plugin.saveSettings(); }); }); + new Setting(containerEl) + .setName("Add Rolls to Dice View") + .setDesc( + createFragment((e) => { + e.createSpan({ + text: "Dice rolled in notes will be added to the Dice View's Results section." + }); + }) + ) + .addToggle((t) => { + t.setValue(this.plugin.data.addToView).onChange((v) => { + this.plugin.data.addToView = v; + this.plugin.saveSettings(); + }); + }); } buildTables(containerEl: HTMLDivElement) { containerEl.empty(); diff --git a/src/view/view.ts b/src/view/view.ts index ec372f3..f4eeabf 100644 --- a/src/view/view.ts +++ b/src/view/view.ts @@ -328,7 +328,7 @@ export default class DiceView extends ItemView { "aria-label": roller.resultText } }) - .appendChild(roller.containerEl); + .appendChild(roller.containerEl.cloneNode(true)); const context = result.createDiv("result-context");