Skip to content

Commit 3ceeb3c

Browse files
committed
feat: add previewModal handler to ModalHelper
1 parent 7c926f0 commit 3ceeb3c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/utils/ModalHelper.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,42 @@ export class ModalHelper {
131131
return;
132132
}
133133
}
134+
135+
async createPreviewModal(mediaTypeModel: MediaTypeModel[]): Promise<{ result: boolean, previewModal: MediaDbPreviewModal }> {
136+
//todo: handle attachFile for existing files
137+
const modal = new MediaDbPreviewModal(this.plugin, mediaTypeModel, { attachTemplate: true, attachFile: false });
138+
const booleanResult: boolean = await new Promise((resolve, reject) => {
139+
modal.setSubmitCallback(res => resolve(res));
140+
modal.setCloseCallback(err => {
141+
if (err) {
142+
reject(err);
143+
}
144+
resolve(undefined);
145+
});
146+
147+
modal.open();
148+
});
149+
return { result: booleanResult, previewModal: modal };
150+
}
151+
152+
async openPreviewModal(mediaModels: MediaTypeModel[], submitCallback: (result: boolean) => Promise<boolean>): Promise<boolean> {
153+
const { result, previewModal } = await this.createPreviewModal(mediaModels);
154+
if (!result) {
155+
previewModal.close();
156+
return;
157+
}
158+
159+
try {
160+
let callbackRes: boolean;
161+
callbackRes = await submitCallback(result);
162+
previewModal.close();
163+
return callbackRes;
164+
} catch (e) {
165+
console.warn(e);
166+
new Notice(e.toString());
167+
previewModal.close();
168+
return;
169+
}
170+
}
134171
}
135172

0 commit comments

Comments
 (0)