@@ -2,9 +2,8 @@ import {MarkdownView, Notice, parseYaml, Plugin, stringifyYaml, TFile, TFolder}
22import { getDefaultSettings , MediaDbPluginSettings , MediaDbSettingTab } from './settings/Settings' ;
33import { APIManager } from './api/APIManager' ;
44import { MediaTypeModel } from './models/MediaTypeModel' ;
5- import { dateTimeToString , markdownTable , replaceIllegalFileNameCharactersInString , UserCancelError , UserSkipError } from './utils/Utils' ;
5+ import { dateTimeToString , markdownTable , replaceIllegalFileNameCharactersInString } from './utils/Utils' ;
66import { OMDbAPI } from './api/apis/OMDbAPI' ;
7- import { MediaDbSearchResultModal } from './modals/MediaDbSearchResultModal' ;
87import { MALAPI } from './api/apis/MALAPI' ;
98import { WikipediaAPI } from './api/apis/WikipediaAPI' ;
109import { MusicBrainzAPI } from './api/apis/MusicBrainzAPI' ;
@@ -15,7 +14,7 @@ import {PropertyMapper} from './settings/PropertyMapper';
1514import { YAMLConverter } from './utils/YAMLConverter' ;
1615import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal' ;
1716import { PropertyMapping , PropertyMappingModel } from './settings/PropertyMapping' ;
18- import { ModalHelper } from './utils/ModalHelper' ;
17+ import { ModalHelper , ModalResultCode } from './utils/ModalHelper' ;
1918
2019export default class MediaDbPlugin extends Plugin {
2120 settings : MediaDbPluginSettings ;
@@ -126,23 +125,23 @@ export default class MediaDbPlugin extends Plugin {
126125 */
127126 async createLinkWithSearchModal ( ) {
128127
129- let apiSearchResults : MediaTypeModel [ ] = await this . modalHelper . openAdvancedSearchModal ( async ( advancedSearchOptions ) => {
130- return await this . apiManager . query ( advancedSearchOptions . query , advancedSearchOptions . apis ) ;
128+ let apiSearchResults : MediaTypeModel [ ] = await this . modalHelper . openAdvancedSearchModal ( { } , async ( advancedSearchModalData ) => {
129+ return await this . apiManager . query ( advancedSearchModalData . query , advancedSearchModalData . apis ) ;
131130 } ) ;
132131
133132 if ( ! apiSearchResults ) {
134133 return ;
135134 }
136135
137- const selectResults : MediaTypeModel [ ] = await this . modalHelper . openSelectModal ( apiSearchResults , async ( selectedMediaTypeModels ) => {
138- return await this . queryDetails ( selectedMediaTypeModels ) ;
136+ const selectResults : MediaTypeModel [ ] = await this . modalHelper . openSelectModal ( { elements : apiSearchResults , multiSelect : false } , async ( selectModalData ) => {
137+ return await this . queryDetails ( selectModalData . selected ) ;
139138 } ) ;
140139
141140 if ( ! selectResults || selectResults . length < 1 ) {
142141 return ;
143142 }
144143
145- const link = `[${ selectResults [ 0 ] . title } ](${ selectResults [ 0 ] . url } )`
144+ const link = `[${ selectResults [ 0 ] . title } ](${ selectResults [ 0 ] . url } )` ;
146145
147146 const view = this . app . workspace . getActiveViewOfType ( MarkdownView ) ;
148147
@@ -160,8 +159,8 @@ export default class MediaDbPlugin extends Plugin {
160159 * TODO: further refactor: extract it into own method, pass the action (api query) as lambda as well as an options object
161160 */
162161 async createEntryWithAdvancedSearchModal ( ) {
163- const apiSearchResults : MediaTypeModel [ ] = await this . modalHelper . openAdvancedSearchModal ( async ( advancedSearchOptions ) => {
164- return await this . apiManager . query ( advancedSearchOptions . query , advancedSearchOptions . apis ) ;
162+ let apiSearchResults : MediaTypeModel [ ] = await this . modalHelper . openAdvancedSearchModal ( { } , async ( advancedSearchModalData ) => {
163+ return await this . apiManager . query ( advancedSearchModalData . query , advancedSearchModalData . apis ) ;
165164 } ) ;
166165
167166 if ( ! apiSearchResults ) {
@@ -227,7 +226,7 @@ export default class MediaDbPlugin extends Plugin {
227226 return detailModels ;
228227 }
229228
230- async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ?: TFile , openNote ?: boolean } ) : Promise < void > {
229+ async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ?: TFile , openNote ?: boolean } ) : Promise < void > {
231230 try {
232231 console . debug ( 'MDB | creating new note' ) ;
233232
@@ -245,7 +244,10 @@ export default class MediaDbPlugin extends Plugin {
245244 let fileContent = '' ;
246245
247246 ( { fileMetadata, fileContent} = await this . attachFile ( fileMetadata , fileContent , options . attachFile ) ) ;
248- ( { fileMetadata, fileContent} = await this . attachTemplate ( fileMetadata , fileContent , options . attachTemplate ? await this . mediaTypeManager . getTemplate ( mediaTypeModel , this . app ) : '' ) ) ;
247+ ( {
248+ fileMetadata,
249+ fileContent,
250+ } = await this . attachTemplate ( fileMetadata , fileContent , options . attachTemplate ? await this . mediaTypeManager . getTemplate ( mediaTypeModel , this . app ) : '' ) ) ;
249251
250252 fileContent = `---\n${ this . settings . useCustomYamlStringifier ? YAMLConverter . toYaml ( fileMetadata ) : stringifyYaml ( fileMetadata ) } ---\n` + fileContent ;
251253 return fileContent ;
@@ -434,46 +436,36 @@ export default class MediaDbPlugin extends Plugin {
434436 continue ;
435437 }
436438
437- let selectedResults : MediaTypeModel [ ] = [ ] ;
438- const modal = new MediaDbSearchResultModal ( this , results , true ) ;
439- try {
440- selectedResults = await new Promise ( ( resolve , reject ) => {
441- modal . title = `Results for \'${ title } \'` ;
442- modal . setSubmitCallback ( res => resolve ( res ) ) ;
443- modal . setSkipCallback ( ( ) => reject ( new UserCancelError ( 'user skipped' ) ) ) ;
444- modal . setCloseCallback ( err => {
445- if ( err ) {
446- reject ( err ) ;
447- }
448- reject ( new UserCancelError ( 'user canceled' ) ) ;
449- } ) ;
450-
451- modal . open ( ) ;
452- } ) ;
453- } catch ( e ) {
454- modal . close ( ) ;
455- if ( e instanceof UserCancelError ) {
456- erroredFiles . push ( { filePath : file . path , error : e . message } ) ;
457- canceled = true ;
458- continue ;
459- } else if ( e instanceof UserSkipError ) {
460- erroredFiles . push ( { filePath : file . path , error : e . message } ) ;
461- continue ;
462- } else {
463- erroredFiles . push ( { filePath : file . path , error : e . message } ) ;
464- continue ;
465- }
439+ let { selectModalResult, selectModal} = await this . modalHelper . createSelectModal ( { elements : results , skipButton : true , modalTitle : `Results for \'${ title } \'` } ) ;
440+
441+ if ( selectModalResult . code === ModalResultCode . ERROR ) {
442+ erroredFiles . push ( { filePath : file . path , error : selectModalResult . error . message } ) ;
443+ selectModal . close ( ) ;
444+ continue ;
445+ }
446+
447+ if ( selectModalResult . code === ModalResultCode . CLOSE ) {
448+ erroredFiles . push ( { filePath : file . path , error : 'user canceled' } ) ;
449+ selectModal . close ( ) ;
450+ canceled = true ;
451+ continue ;
452+ }
453+
454+ if ( selectModalResult . code === ModalResultCode . SKIP ) {
455+ erroredFiles . push ( { filePath : file . path , error : 'user skipped' } ) ;
456+ selectModal . close ( ) ;
457+ continue ;
466458 }
467459
468- if ( selectedResults . length === 0 ) {
460+ if ( selectModalResult . data . selected . length === 0 ) {
469461 erroredFiles . push ( { filePath : file . path , error : `no search results selected` } ) ;
470462 continue ;
471463 }
472464
473- const detailedResults = await this . queryDetails ( selectedResults ) ;
465+ const detailedResults = await this . queryDetails ( selectModalResult . data . selected ) ;
474466 await this . createMediaDbNotes ( detailedResults , appendContent ? file : null ) ;
475467
476- modal . close ( ) ;
468+ selectModal . close ( ) ;
477469 }
478470 }
479471
0 commit comments