@@ -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,8 +159,8 @@ 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 ) {
@@ -227,7 +226,7 @@ export default class MediaDbPlugin extends Plugin {
227
226
return detailModels ;
228
227
}
229
228
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 > {
231
230
try {
232
231
console . debug ( 'MDB | creating new note' ) ;
233
232
@@ -245,7 +244,10 @@ export default class MediaDbPlugin extends Plugin {
245
244
let fileContent = '' ;
246
245
247
246
( { 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 ) : '' ) ) ;
249
251
250
252
fileContent = `---\n${ this . settings . useCustomYamlStringifier ? YAMLConverter . toYaml ( fileMetadata ) : stringifyYaml ( fileMetadata ) } ---\n` + fileContent ;
251
253
return fileContent ;
@@ -434,46 +436,36 @@ export default class MediaDbPlugin extends Plugin {
434
436
continue ;
435
437
}
436
438
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 ;
466
458
}
467
459
468
- if ( selectedResults . length === 0 ) {
460
+ if ( selectModalResult . data . selected . length === 0 ) {
469
461
erroredFiles . push ( { filePath : file . path , error : `no search results selected` } ) ;
470
462
continue ;
471
463
}
472
464
473
- const detailedResults = await this . queryDetails ( selectedResults ) ;
465
+ const detailedResults = await this . queryDetails ( selectModalResult . data . selected ) ;
474
466
await this . createMediaDbNotes ( detailedResults , appendContent ? file : null ) ;
475
467
476
- modal . close ( ) ;
468
+ selectModal . close ( ) ;
477
469
}
478
470
}
479
471
0 commit comments