@@ -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