Skip to content

Commit 861c8cb

Browse files
committed
feat: allow any monster to be saved (close #27)
1 parent badecc8 commit 861c8cb

2 files changed

Lines changed: 77 additions & 16 deletions

File tree

src/view/Statblock.svelte

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
export let context: string;
2525
export let plugin: StatBlockPlugin;
2626
export let statblock: StatblockItem[];
27-
export let canSave: boolean;
2827
2928
let canExport = monster.export ?? plugin.settings.export;
3029
let canDice =
@@ -65,20 +64,15 @@
6564
});
6665
6766
const icons = (node: HTMLElement) => {
68-
if (!canExport && !canSave && !canDice) {
69-
node.detach();
70-
return;
71-
}
7267
new ExtraButtonComponent(node).setIcon("vertical-three-dots");
7368
};
7469
const menu = new Menu(plugin.app);
75-
if (canSave)
76-
menu.addItem((item) =>
77-
item
78-
.setIcon(SAVE_SYMBOL)
79-
.setTitle("Save as Homebrew")
80-
.onClick(() => dispatch("save"))
81-
);
70+
menu.addItem((item) =>
71+
item
72+
.setIcon(SAVE_SYMBOL)
73+
.setTitle("Save as Homebrew")
74+
.onClick(() => dispatch("save"))
75+
);
8276
if (canExport)
8377
menu.addItem((item) =>
8478
item

src/view/statblock.ts

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,22 @@ export default class StatBlockRenderer extends MarkdownRenderChild {
3232
context: this.context,
3333
monster: this.monster,
3434
statblock: this.layout.blocks,
35-
plugin: this.plugin,
36-
canSave: this.canSave
35+
plugin: this.plugin
3736
}
3837
});
39-
statblock.$on("save", () => {
40-
this.plugin.saveMonster({ ...this.monster, source: "Homebrew" });
38+
statblock.$on("save", async () => {
39+
if (
40+
!this.canSave &&
41+
!(await confirmWithModal(
42+
this.plugin.app,
43+
"This will overwrite an existing monster in settings. Are you sure?"
44+
))
45+
)
46+
return;
47+
this.plugin.saveMonster({
48+
...this.monster,
49+
source: "Homebrew"
50+
});
4151
});
4252

4353
statblock.$on("export", () => {
@@ -48,3 +58,60 @@ export default class StatBlockRenderer extends MarkdownRenderChild {
4858
});
4959
}
5060
}
61+
62+
import { App, ButtonComponent, Modal } from "obsidian";
63+
64+
export async function confirmWithModal(
65+
app: App,
66+
text: string,
67+
buttons: { cta: string; secondary: string } = {
68+
cta: "Yes",
69+
secondary: "No"
70+
}
71+
): Promise<boolean> {
72+
return new Promise((resolve, reject) => {
73+
const modal = new ConfirmModal(app, text, buttons);
74+
modal.onClose = () => {
75+
resolve(modal.confirmed);
76+
};
77+
modal.open();
78+
});
79+
}
80+
81+
export class ConfirmModal extends Modal {
82+
constructor(
83+
app: App,
84+
public text: string,
85+
public buttons: { cta: string; secondary: string }
86+
) {
87+
super(app);
88+
}
89+
confirmed: boolean = false;
90+
async display() {
91+
new Promise((resolve) => {
92+
this.contentEl.empty();
93+
this.contentEl.addClass("confirm-modal");
94+
this.contentEl.createEl("p", {
95+
text: this.text
96+
});
97+
const buttonEl = this.contentEl.createDiv(
98+
"fantasy-calendar-confirm-buttons"
99+
);
100+
new ButtonComponent(buttonEl)
101+
.setButtonText(this.buttons.cta)
102+
.setCta()
103+
.onClick(() => {
104+
this.confirmed = true;
105+
this.close();
106+
});
107+
new ButtonComponent(buttonEl)
108+
.setButtonText(this.buttons.secondary)
109+
.onClick(() => {
110+
this.close();
111+
});
112+
});
113+
}
114+
onOpen() {
115+
this.display();
116+
}
117+
}

0 commit comments

Comments
 (0)