@@ -6,6 +6,7 @@ import {Notice} from 'obsidian';
66import MediaDbPlugin from '../main' ;
77import { MediaDbPreviewModal } from 'src/modals/MediaDbPreviewModal' ;
88import { CreateNoteOptions } from './Utils' ;
9+ import { MediaDbSearchModal } from '../modals/MediaDbSearchModal' ;
910
1011
1112export enum ModalResultCode {
@@ -15,6 +16,17 @@ export enum ModalResultCode {
1516 ERROR ,
1617}
1718
19+ /**
20+ * Object containing the data {@link ModalHelper.createSearchModal} returns.
21+ * On {@link ModalResultCode.SUCCESS} this contains {@link SearchModalData}.
22+ * On {@link ModalResultCode.ERROR} this contains a reference to that error.
23+ */
24+ export interface SearchModalResult {
25+ code : ModalResultCode . SUCCESS | ModalResultCode . CLOSE | ModalResultCode . ERROR ,
26+ data ?: SearchModalData ,
27+ error ?: Error ,
28+ }
29+
1830/**
1931 * Object containing the data {@link ModalHelper.createAdvancedSearchModal} returns.
2032 * On {@link ModalResultCode.SUCCESS} this contains {@link AdvancedSearchModalData}.
@@ -59,6 +71,16 @@ export interface PreviewModalResult {
5971 error ?: Error ,
6072}
6173
74+ /**
75+ * The data the search modal returns.
76+ * - query: the query string
77+ * - types: the selected APIs
78+ */
79+ export interface SearchModalData {
80+ query : string ,
81+ types : string [ ] ,
82+ }
83+
6284/**
6385 * The data the advanced search modal returns.
6486 * - query: the query string
@@ -95,6 +117,18 @@ export interface PreviewModalData {
95117 confirmed : boolean ,
96118}
97119
120+ /**
121+ * Options for the search modal.
122+ * - modalTitle: the title of the modal
123+ * - preselectedTypes: a list of preselected Types
124+ * - prefilledSearchString: prefilled query
125+ */
126+ export interface SearchModalOptions {
127+ modalTitle ?: string ,
128+ preselectedTypes ?: string [ ] ,
129+ prefilledSearchString ?: string ,
130+ }
131+
98132/**
99133 * Options for the advanced search modal.
100134 * - modalTitle: the title of the modal
@@ -144,6 +178,12 @@ export interface PreviewModalOptions {
144178 createNoteOptions ?: CreateNoteOptions ,
145179}
146180
181+ export const SEARCH_MODAL_DEFAULT_OPTIONS : SearchModalOptions = {
182+ modalTitle : 'Media DB Search' ,
183+ preselectedTypes : [ ] ,
184+ prefilledSearchString : '' ,
185+ } ;
186+
147187export const ADVANCED_SEARCH_MODAL_DEFAULT_OPTIONS : AdvancedSearchModalOptions = {
148188 modalTitle : 'Media DB Advanced Search' ,
149189 preselectedAPIs : [ ] ,
@@ -180,6 +220,68 @@ export class ModalHelper {
180220 this . plugin = plugin ;
181221 }
182222
223+ /**
224+ * Creates an {@link MediaDbSearchModal}, then sets callbacks and awaits them,
225+ * returning either the user input once submitted or nothing once closed.
226+ * The modal needs ot be manually closed by calling `close()` on the modal reference.
227+ *
228+ * @param searchModalOptions the options for the modal, see {@link SEARCH_MODAL_DEFAULT_OPTIONS}
229+ * @returns the user input or nothing and a reference to the modal.
230+ */
231+ async createSearchModal ( searchModalOptions : SearchModalOptions ) : Promise < { searchModalResult : SearchModalResult , searchModal : MediaDbSearchModal } > {
232+ const modal = new MediaDbSearchModal ( this . plugin , searchModalOptions ) ;
233+ const res : SearchModalResult = await new Promise ( ( resolve , reject ) => {
234+ modal . setSubmitCallback ( res => resolve ( { code : ModalResultCode . SUCCESS , data : res } ) ) ;
235+ modal . setCloseCallback ( err => {
236+ if ( err ) {
237+ resolve ( { code : ModalResultCode . ERROR , error : err } ) ;
238+ }
239+ resolve ( { code : ModalResultCode . CLOSE } ) ;
240+ } ) ;
241+
242+ modal . open ( ) ;
243+ } ) ;
244+ return { searchModalResult : res , searchModal : modal } ;
245+ }
246+
247+ /**
248+ * Opens an {@link MediaDbSearchModal} and awaits its result,
249+ * then executes the `submitCallback` returning the callbacks result and closing the modal.
250+ *
251+ * @param searchModalOptions the options for the modal, see {@link SEARCH_MODAL_DEFAULT_OPTIONS}
252+ * @param submitCallback the callback that gets executed after the modal has been submitted, but after it has been closed
253+ * @returns the user input or nothing and a reference to the modal.
254+ */
255+ async openSearchModal ( searchModalOptions : SearchModalOptions , submitCallback : ( searchModalData : SearchModalData ) => Promise < MediaTypeModel [ ] > ) : Promise < MediaTypeModel [ ] > {
256+ const { searchModalResult, searchModal} = await this . createSearchModal ( searchModalOptions ) ;
257+ console . debug ( `MDB | searchModal closed with code ${ searchModalResult . code } ` )
258+
259+ if ( searchModalResult . code === ModalResultCode . ERROR ) {
260+ // there was an error in the modal itself
261+ console . warn ( searchModalResult . error ) ;
262+ new Notice ( searchModalResult . error . toString ( ) ) ;
263+ searchModal . close ( ) ;
264+ return undefined ;
265+ }
266+
267+ if ( searchModalResult . code === ModalResultCode . CLOSE ) {
268+ // modal is already being closed
269+ return undefined ;
270+ }
271+
272+ try {
273+ let callbackRes : MediaTypeModel [ ] ;
274+ callbackRes = await submitCallback ( searchModalResult . data ) ;
275+ searchModal . close ( ) ;
276+ return callbackRes ;
277+ } catch ( e ) {
278+ console . warn ( e ) ;
279+ new Notice ( e . toString ( ) ) ;
280+ searchModal . close ( ) ;
281+ return undefined ;
282+ }
283+ }
284+
183285 /**
184286 * Creates an {@link MediaDbAdvancedSearchModal}, then sets callbacks and awaits them,
185287 * returning either the user input once submitted or nothing once closed.
@@ -214,6 +316,7 @@ export class ModalHelper {
214316 */
215317 async openAdvancedSearchModal ( advancedSearchModalOptions : AdvancedSearchModalOptions , submitCallback : ( advancedSearchModalData : AdvancedSearchModalData ) => Promise < MediaTypeModel [ ] > ) : Promise < MediaTypeModel [ ] > {
216318 const { advancedSearchModalResult, advancedSearchModal} = await this . createAdvancedSearchModal ( advancedSearchModalOptions ) ;
319+ console . debug ( `MDB | advencedSearchModal closed with code ${ advancedSearchModalResult . code } ` )
217320
218321 if ( advancedSearchModalResult . code === ModalResultCode . ERROR ) {
219322 // there was an error in the modal itself
@@ -275,6 +378,7 @@ export class ModalHelper {
275378 */
276379 async openIdSearchModal ( idSearchModalOptions : IdSearchModalOptions , submitCallback : ( idSearchModalData : IdSearchModalData ) => Promise < MediaTypeModel > ) : Promise < MediaTypeModel > {
277380 const { idSearchModalResult, idSearchModal} = await this . createIdSearchModal ( idSearchModalOptions ) ;
381+ console . debug ( `MDB | idSearchModal closed with code ${ idSearchModalResult . code } ` )
278382
279383 if ( idSearchModalResult . code === ModalResultCode . ERROR ) {
280384 // there was an error in the modal itself
@@ -337,6 +441,7 @@ export class ModalHelper {
337441 */
338442 async openSelectModal ( selectModalOptions : SelectModalOptions , submitCallback : ( selectModalData : SelectModalData ) => Promise < MediaTypeModel [ ] > ) : Promise < MediaTypeModel [ ] > {
339443 const { selectModalResult, selectModal} = await this . createSelectModal ( selectModalOptions ) ;
444+ console . debug ( `MDB | selectModal closed with code ${ selectModalResult . code } ` )
340445
341446 if ( selectModalResult . code === ModalResultCode . ERROR ) {
342447 // there was an error in the modal itself
@@ -388,6 +493,7 @@ export class ModalHelper {
388493
389494 async openPreviewModal ( previewModalOptions : PreviewModalOptions , submitCallback : ( previewModalData : PreviewModalData ) => Promise < boolean > ) : Promise < boolean > {
390495 const { previewModalResult, previewModal} = await this . createPreviewModal ( previewModalOptions ) ;
496+ console . debug ( `MDB | previewModal closed with code ${ previewModalResult . code } ` )
391497
392498 if ( previewModalResult . code === ModalResultCode . ERROR ) {
393499 // there was an error in the modal itself
0 commit comments