Skip to content

Commit

Permalink
feat: new Add to View setting for Dice rollers. when enabled, all d…
Browse files Browse the repository at this point in the history
…ice roller results will be added to the Dice View
  • Loading branch information
valentine195 committed Sep 6, 2023
1 parent ccc70ea commit 70a11c4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
14 changes: 12 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ interface DiceRollerSettings {
defaultFace: number;
renderer: boolean;
renderAllDice: boolean;
addToView: boolean;
renderTime: number;
colorfulDice: boolean;
scaler: number;
Expand Down Expand Up @@ -184,6 +185,7 @@ export const DEFAULT_SETTINGS: DiceRollerSettings = {
defaultFace: 100,
renderer: false,
renderAllDice: false,
addToView: false,
renderTime: 2000,
colorfulDice: false,
scaler: 1,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 19 additions & 2 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/view/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 70a11c4

Please sign in to comment.