@@ -2,9 +2,8 @@ import {MarkdownView, Notice, parseYaml, Plugin, stringifyYaml, TFile, TFolder}
2
2
import { getDefaultSettings , MediaDbPluginSettings , MediaDbSettingTab } from './settings/Settings' ;
3
3
import { APIManager } from './api/APIManager' ;
4
4
import { MediaTypeModel } from './models/MediaTypeModel' ;
5
- import { dateTimeToString , markdownTable , replaceIllegalFileNameCharactersInString , UserCancelError , UserSkipError } from './utils/Utils' ;
5
+ import { dateTimeToString , markdownTable , replaceIllegalFileNameCharactersInString } from './utils/Utils' ;
6
6
import { OMDbAPI } from './api/apis/OMDbAPI' ;
7
- import { MediaDbSearchResultModal } from './modals/MediaDbSearchResultModal' ;
8
7
import { MALAPI } from './api/apis/MALAPI' ;
9
8
import { WikipediaAPI } from './api/apis/WikipediaAPI' ;
10
9
import { MusicBrainzAPI } from './api/apis/MusicBrainzAPI' ;
@@ -15,7 +14,7 @@ import {PropertyMapper} from './settings/PropertyMapper';
15
14
import { YAMLConverter } from './utils/YAMLConverter' ;
16
15
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal' ;
17
16
import { PropertyMapping , PropertyMappingModel } from './settings/PropertyMapping' ;
18
- import { ModalHelper } from './utils/ModalHelper' ;
17
+ import { ModalHelper , ModalResultCode } from './utils/ModalHelper' ;
19
18
20
19
export default class MediaDbPlugin extends Plugin {
21
20
settings : MediaDbPluginSettings ;
@@ -126,23 +125,23 @@ export default class MediaDbPlugin extends Plugin {
126
125
*/
127
126
async createLinkWithSearchModal ( ) {
128
127
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 ) ;
131
130
} ) ;
132
131
133
132
if ( ! apiSearchResults ) {
134
133
return ;
135
134
}
136
135
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 ) ;
139
138
} ) ;
140
139
141
140
if ( ! selectResults || selectResults . length < 1 ) {
142
141
return ;
143
142
}
144
143
145
- const link = `[${ selectResults [ 0 ] . title } ](${ selectResults [ 0 ] . url } )`
144
+ const link = `[${ selectResults [ 0 ] . title } ](${ selectResults [ 0 ] . url } )` ;
146
145
147
146
const view = this . app . workspace . getActiveViewOfType ( MarkdownView ) ;
148
147
@@ -160,16 +159,16 @@ export default class MediaDbPlugin extends Plugin {
160
159
* TODO: further refactor: extract it into own method, pass the action (api query) as lambda as well as an options object
161
160
*/
162
161
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 ) ;
165
164
} ) ;
166
165
167
166
if ( ! apiSearchResults ) {
168
167
return ;
169
168
}
170
169
171
- const selectResults : MediaTypeModel [ ] = await this . modalHelper . openSelectModal ( apiSearchResults , async ( selectedMediaTypeModels ) => {
172
- return await this . queryDetails ( selectedMediaTypeModels ) ;
170
+ const selectResults : MediaTypeModel [ ] = await this . modalHelper . openSelectModal ( { elements : apiSearchResults } , async ( selectModalData ) => {
171
+ return await this . queryDetails ( selectModalData . selected ) ;
173
172
} ) ;
174
173
175
174
if ( ! selectResults ) {
@@ -180,9 +179,9 @@ export default class MediaDbPlugin extends Plugin {
180
179
}
181
180
182
181
async createEntryWithIdSearchModal ( ) {
183
- const idSearchResult : MediaTypeModel = await this . modalHelper . openIdSearchModal ( async ( idSearchOptions ) => {
184
- return await this . apiManager . queryDetailedInfoById ( idSearchOptions . query , idSearchOptions . api ) ;
185
- } )
182
+ const idSearchResult : MediaTypeModel = await this . modalHelper . openIdSearchModal ( { } , async ( idSearchModalData ) => {
183
+ return await this . apiManager . queryDetailedInfoById ( idSearchModalData . query , idSearchModalData . api ) ;
184
+ } ) ;
186
185
187
186
if ( ! idSearchResult ) {
188
187
return ;
@@ -210,7 +209,7 @@ export default class MediaDbPlugin extends Plugin {
210
209
return detailModels ;
211
210
}
212
211
213
- async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ?: TFile , openNote ?: boolean } ) : Promise < void > {
212
+ async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ?: TFile , openNote ?: boolean } ) : Promise < void > {
214
213
try {
215
214
console . debug ( 'MDB | creating new note' ) ;
216
215
@@ -223,12 +222,15 @@ export default class MediaDbPlugin extends Plugin {
223
222
}
224
223
}
225
224
226
- private async generateMediaDbNoteContents ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ?: TFile } ) {
225
+ private async generateMediaDbNoteContents ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ?: TFile } ) {
227
226
let fileMetadata = this . modelPropertyMapper . convertObject ( mediaTypeModel . toMetaDataObject ( ) ) ;
228
227
let fileContent = '' ;
229
228
230
229
( { fileMetadata, fileContent} = await this . attachFile ( fileMetadata , fileContent , options . attachFile ) ) ;
231
- ( { fileMetadata, fileContent} = await this . attachTemplate ( fileMetadata , fileContent , options . attachTemplate ? await this . mediaTypeManager . getTemplate ( mediaTypeModel , this . app ) : '' ) ) ;
230
+ ( {
231
+ fileMetadata,
232
+ fileContent,
233
+ } = await this . attachTemplate ( fileMetadata , fileContent , options . attachTemplate ? await this . mediaTypeManager . getTemplate ( mediaTypeModel , this . app ) : '' ) ) ;
232
234
233
235
fileContent = `---\n${ this . settings . useCustomYamlStringifier ? YAMLConverter . toYaml ( fileMetadata ) : stringifyYaml ( fileMetadata ) } ---\n` + fileContent ;
234
236
return fileContent ;
@@ -417,46 +419,36 @@ export default class MediaDbPlugin extends Plugin {
417
419
continue ;
418
420
}
419
421
420
- let selectedResults : MediaTypeModel [ ] = [ ] ;
421
- const modal = new MediaDbSearchResultModal ( this , results , true ) ;
422
- try {
423
- selectedResults = await new Promise ( ( resolve , reject ) => {
424
- modal . title = `Results for \'${ title } \'` ;
425
- modal . setSubmitCallback ( res => resolve ( res ) ) ;
426
- modal . setSkipCallback ( ( ) => reject ( new UserCancelError ( 'user skipped' ) ) ) ;
427
- modal . setCloseCallback ( err => {
428
- if ( err ) {
429
- reject ( err ) ;
430
- }
431
- reject ( new UserCancelError ( 'user canceled' ) ) ;
432
- } ) ;
433
-
434
- modal . open ( ) ;
435
- } ) ;
436
- } catch ( e ) {
437
- modal . close ( ) ;
438
- if ( e instanceof UserCancelError ) {
439
- erroredFiles . push ( { filePath : file . path , error : e . message } ) ;
440
- canceled = true ;
441
- continue ;
442
- } else if ( e instanceof UserSkipError ) {
443
- erroredFiles . push ( { filePath : file . path , error : e . message } ) ;
444
- continue ;
445
- } else {
446
- erroredFiles . push ( { filePath : file . path , error : e . message } ) ;
447
- continue ;
448
- }
422
+ let { selectModalResult, selectModal} = await this . modalHelper . createSelectModal ( { elements : results , skipButton : true , modalTitle : `Results for \'${ title } \'` } ) ;
423
+
424
+ if ( selectModalResult . code === ModalResultCode . ERROR ) {
425
+ erroredFiles . push ( { filePath : file . path , error : selectModalResult . error . message } ) ;
426
+ selectModal . close ( ) ;
427
+ continue ;
428
+ }
429
+
430
+ if ( selectModalResult . code === ModalResultCode . CLOSE ) {
431
+ erroredFiles . push ( { filePath : file . path , error : 'user canceled' } ) ;
432
+ selectModal . close ( ) ;
433
+ canceled = true ;
434
+ continue ;
435
+ }
436
+
437
+ if ( selectModalResult . code === ModalResultCode . SKIP ) {
438
+ erroredFiles . push ( { filePath : file . path , error : 'user skipped' } ) ;
439
+ selectModal . close ( ) ;
440
+ continue ;
449
441
}
450
442
451
- if ( selectedResults . length === 0 ) {
443
+ if ( selectModalResult . data . selected . length === 0 ) {
452
444
erroredFiles . push ( { filePath : file . path , error : `no search results selected` } ) ;
453
445
continue ;
454
446
}
455
447
456
- const detailedResults = await this . queryDetails ( selectedResults ) ;
448
+ const detailedResults = await this . queryDetails ( selectModalResult . data . selected ) ;
457
449
await this . createMediaDbNotes ( detailedResults , appendContent ? file : null ) ;
458
450
459
- modal . close ( ) ;
451
+ selectModal . close ( ) ;
460
452
}
461
453
}
462
454
0 commit comments