Skip to content

Commit

Permalink
feat: allow any monster to be saved (close #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Jan 6, 2022
1 parent badecc8 commit 861c8cb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 16 deletions.
18 changes: 6 additions & 12 deletions src/view/Statblock.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
export let context: string;
export let plugin: StatBlockPlugin;
export let statblock: StatblockItem[];
export let canSave: boolean;
let canExport = monster.export ?? plugin.settings.export;
let canDice =
Expand Down Expand Up @@ -65,20 +64,15 @@
});
const icons = (node: HTMLElement) => {
if (!canExport && !canSave && !canDice) {
node.detach();
return;
}
new ExtraButtonComponent(node).setIcon("vertical-three-dots");
};
const menu = new Menu(plugin.app);
if (canSave)
menu.addItem((item) =>
item
.setIcon(SAVE_SYMBOL)
.setTitle("Save as Homebrew")
.onClick(() => dispatch("save"))
);
menu.addItem((item) =>
item
.setIcon(SAVE_SYMBOL)
.setTitle("Save as Homebrew")
.onClick(() => dispatch("save"))
);
if (canExport)
menu.addItem((item) =>
item
Expand Down
75 changes: 71 additions & 4 deletions src/view/statblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,22 @@ export default class StatBlockRenderer extends MarkdownRenderChild {
context: this.context,
monster: this.monster,
statblock: this.layout.blocks,
plugin: this.plugin,
canSave: this.canSave
plugin: this.plugin
}
});
statblock.$on("save", () => {
this.plugin.saveMonster({ ...this.monster, source: "Homebrew" });
statblock.$on("save", async () => {
if (
!this.canSave &&
!(await confirmWithModal(
this.plugin.app,
"This will overwrite an existing monster in settings. Are you sure?"
))
)
return;
this.plugin.saveMonster({
...this.monster,
source: "Homebrew"
});
});

statblock.$on("export", () => {
Expand All @@ -48,3 +58,60 @@ export default class StatBlockRenderer extends MarkdownRenderChild {
});
}
}

import { App, ButtonComponent, Modal } from "obsidian";

export async function confirmWithModal(
app: App,
text: string,
buttons: { cta: string; secondary: string } = {
cta: "Yes",
secondary: "No"
}
): Promise<boolean> {
return new Promise((resolve, reject) => {
const modal = new ConfirmModal(app, text, buttons);
modal.onClose = () => {
resolve(modal.confirmed);
};
modal.open();
});
}

export class ConfirmModal extends Modal {
constructor(
app: App,
public text: string,
public buttons: { cta: string; secondary: string }
) {
super(app);
}
confirmed: boolean = false;
async display() {
new Promise((resolve) => {
this.contentEl.empty();
this.contentEl.addClass("confirm-modal");
this.contentEl.createEl("p", {
text: this.text
});
const buttonEl = this.contentEl.createDiv(
"fantasy-calendar-confirm-buttons"
);
new ButtonComponent(buttonEl)
.setButtonText(this.buttons.cta)
.setCta()
.onClick(() => {
this.confirmed = true;
this.close();
});
new ButtonComponent(buttonEl)
.setButtonText(this.buttons.secondary)
.onClick(() => {
this.close();
});
});
}
onOpen() {
this.display();
}
}

0 comments on commit 861c8cb

Please sign in to comment.