@@ -4,10 +4,8 @@ import {APIManager} from './api/APIManager';
4
4
import { MediaTypeModel } from './models/MediaTypeModel' ;
5
5
import { dateTimeToString , markdownTable , replaceIllegalFileNameCharactersInString , UserCancelError , UserSkipError } from './utils/Utils' ;
6
6
import { OMDbAPI } from './api/apis/OMDbAPI' ;
7
- import { MediaDbAdvancedSearchModal } from './modals/MediaDbAdvancedSearchModal' ;
8
7
import { MediaDbSearchResultModal } from './modals/MediaDbSearchResultModal' ;
9
8
import { MALAPI } from './api/apis/MALAPI' ;
10
- import { MediaDbIdSearchModal } from './modals/MediaDbIdSearchModal' ;
11
9
import { WikipediaAPI } from './api/apis/WikipediaAPI' ;
12
10
import { MusicBrainzAPI } from './api/apis/MusicBrainzAPI' ;
13
11
import { MediaTypeManager } from './utils/MediaTypeManager' ;
@@ -17,12 +15,14 @@ import {PropertyMapper} from './settings/PropertyMapper';
17
15
import { YAMLConverter } from './utils/YAMLConverter' ;
18
16
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal' ;
19
17
import { PropertyMapping , PropertyMappingModel } from './settings/PropertyMapping' ;
18
+ import { ModalHelper } from './utils/ModalHelper' ;
20
19
21
20
export default class MediaDbPlugin extends Plugin {
22
21
settings : MediaDbPluginSettings ;
23
22
apiManager : APIManager ;
24
23
mediaTypeManager : MediaTypeManager ;
25
24
modelPropertyMapper : PropertyMapper ;
25
+ modalHelper : ModalHelper ;
26
26
27
27
frontMatterRexExpPattern : string = '^(---)\\n[\\s\\S]*?\\n---' ;
28
28
@@ -39,6 +39,7 @@ export default class MediaDbPlugin extends Plugin {
39
39
40
40
this . mediaTypeManager = new MediaTypeManager ( ) ;
41
41
this . modelPropertyMapper = new PropertyMapper ( this ) ;
42
+ this . modalHelper = new ModalHelper ( this ) ;
42
43
43
44
await this . loadSettings ( ) ;
44
45
// register the settings tab
@@ -91,7 +92,7 @@ export default class MediaDbPlugin extends Plugin {
91
92
// register link insert command
92
93
this . addCommand ( {
93
94
id : 'add-media-db-link' ,
94
- name : 'Add a link. ' ,
95
+ name : 'Insert link' ,
95
96
checkCallback : ( checking : boolean ) => {
96
97
if ( ! this . app . workspace . getActiveFile ( ) ) {
97
98
return false ;
@@ -111,49 +112,24 @@ export default class MediaDbPlugin extends Plugin {
111
112
* - maybe custom link syntax
112
113
*/
113
114
async createLinkWithSearchModal ( ) {
114
- let results : MediaTypeModel [ ] = [ ] ;
115
115
116
- const { advancedSearchOptions, advancedSearchModal} = await this . openMediaDbAdvancedSearchModal ( ) ;
117
- if ( ! advancedSearchOptions ) {
118
- advancedSearchModal . close ( ) ;
119
- return ;
120
- }
121
-
122
- let apiSearchResults : MediaTypeModel [ ] = undefined ;
123
- try {
124
- apiSearchResults = await this . apiManager . query ( advancedSearchOptions . query , advancedSearchOptions . apis ) ;
125
- } catch ( e ) {
126
- console . warn ( e ) ;
127
- new Notice ( e . toString ( ) ) ;
128
- advancedSearchModal . close ( ) ;
129
- return ;
130
- }
131
-
132
- advancedSearchModal . close ( ) ;
116
+ let apiSearchResults : MediaTypeModel [ ] = await this . modalHelper . openAdvancedSearchModal ( async ( advancedSearchOptions ) => {
117
+ return await this . apiManager . query ( advancedSearchOptions . query , advancedSearchOptions . apis ) ;
118
+ } ) ;
133
119
134
- const { selectRes, selectModal} = await this . openMediaDbSelectModal ( apiSearchResults , false , false ) ;
135
- if ( ! selectRes ) {
136
- selectModal . close ( ) ;
120
+ if ( ! apiSearchResults ) {
137
121
return ;
138
122
}
139
123
140
- // TODO: let's try to not query details for this
141
- try {
142
- results = await this . queryDetails ( selectRes ) ;
143
- } catch ( e ) {
144
- console . warn ( e ) ;
145
- new Notice ( e . toString ( ) ) ;
146
- selectModal . close ( ) ;
147
- return ;
148
- }
149
-
150
- selectModal . close ( ) ;
124
+ const selectResults : MediaTypeModel [ ] = await this . modalHelper . openSelectModal ( apiSearchResults , async ( selectedMediaTypeModels ) => {
125
+ return await this . queryDetails ( selectedMediaTypeModels ) ;
126
+ } ) ;
151
127
152
- if ( ! results || results . length < 1 ) {
128
+ if ( ! selectResults || selectResults . length < 1 ) {
153
129
return ;
154
130
}
155
131
156
- const link = `[${ results [ 0 ] . title } ](${ results [ 0 ] . url } )`
132
+ const link = `[${ selectResults [ 0 ] . title } ](${ selectResults [ 0 ] . url } )`
157
133
158
134
const view = this . app . workspace . getActiveViewOfType ( MarkdownView ) ;
159
135
@@ -171,71 +147,35 @@ export default class MediaDbPlugin extends Plugin {
171
147
* TODO: further refactor: extract it into own method, pass the action (api query) as lambda as well as an options object
172
148
*/
173
149
async createEntryWithAdvancedSearchModal ( ) {
174
- let results : MediaTypeModel [ ] = [ ] ;
150
+ const apiSearchResults : MediaTypeModel [ ] = await this . modalHelper . openAdvancedSearchModal ( async ( advancedSearchOptions ) => {
151
+ return await this . apiManager . query ( advancedSearchOptions . query , advancedSearchOptions . apis ) ;
152
+ } ) ;
175
153
176
- const { advancedSearchOptions, advancedSearchModal} = await this . openMediaDbAdvancedSearchModal ( ) ;
177
- if ( ! advancedSearchOptions ) {
178
- advancedSearchModal . close ( ) ;
154
+ if ( ! apiSearchResults ) {
179
155
return ;
180
156
}
181
157
182
- let apiSearchResults : MediaTypeModel [ ] = undefined ;
183
- try {
184
- apiSearchResults = await this . apiManager . query ( advancedSearchOptions . query , advancedSearchOptions . apis ) ;
185
- } catch ( e ) {
186
- console . warn ( e ) ;
187
- new Notice ( e . toString ( ) ) ;
188
- advancedSearchModal . close ( ) ;
189
- return ;
190
- }
191
-
192
- advancedSearchModal . close ( ) ;
158
+ const selectResults : MediaTypeModel [ ] = await this . modalHelper . openSelectModal ( apiSearchResults , async ( selectedMediaTypeModels ) => {
159
+ return await this . queryDetails ( selectedMediaTypeModels ) ;
160
+ } ) ;
193
161
194
- const { selectRes, selectModal} = await this . openMediaDbSelectModal ( apiSearchResults , false ) ;
195
- if ( ! selectRes ) {
196
- selectModal . close ( ) ;
162
+ if ( ! selectResults ) {
197
163
return ;
198
164
}
199
165
200
- try {
201
- results = await this . queryDetails ( selectRes ) ;
202
- } catch ( e ) {
203
- console . warn ( e ) ;
204
- new Notice ( e . toString ( ) ) ;
205
- selectModal . close ( ) ;
206
- return ;
207
- }
208
-
209
- selectModal . close ( ) ;
210
-
211
- if ( results ) {
212
- await this . createMediaDbNotes ( results ) ;
213
- }
166
+ await this . createMediaDbNotes ( selectResults ) ;
214
167
}
215
168
216
169
async createEntryWithIdSearchModal ( ) {
217
- let result : MediaTypeModel = undefined ;
170
+ const idSearchResult : MediaTypeModel = await this . modalHelper . openIdSearchModal ( async ( idSearchOptions ) => {
171
+ return await this . apiManager . queryDetailedInfoById ( idSearchOptions . query , idSearchOptions . api ) ;
172
+ } )
218
173
219
- const { idSearchOptions, idSearchModal} = await this . openMediaDbIdSearchModal ( ) ;
220
- if ( ! idSearchOptions ) {
221
- idSearchModal . close ( ) ;
174
+ if ( ! idSearchResult ) {
222
175
return ;
223
176
}
224
177
225
- try {
226
- result = await this . apiManager . queryDetailedInfoById ( idSearchOptions . query , idSearchOptions . api ) ;
227
- } catch ( e ) {
228
- console . warn ( e ) ;
229
- new Notice ( e . toString ( ) ) ;
230
- idSearchModal . close ( ) ;
231
- return ;
232
- }
233
-
234
- idSearchModal . close ( ) ;
235
-
236
- if ( result ) {
237
- await this . createMediaDbNoteFromModel ( result ) ;
238
- }
178
+ await this . createMediaDbNoteFromModel ( idSearchResult ) ;
239
179
}
240
180
241
181
async createMediaDbNotes ( models : MediaTypeModel [ ] , attachFile ?: TFile ) : Promise < void > {
@@ -517,55 +457,6 @@ export default class MediaDbPlugin extends Plugin {
517
457
const targetFile = await this . app . vault . create ( filePath , fileContent ) ;
518
458
}
519
459
520
- async openMediaDbAdvancedSearchModal ( ) : Promise < { advancedSearchOptions : { query : string , apis : string [ ] } , advancedSearchModal : MediaDbAdvancedSearchModal } > {
521
- const modal = new MediaDbAdvancedSearchModal ( this ) ;
522
- const res : { query : string , apis : string [ ] } = await new Promise ( ( resolve , reject ) => {
523
- modal . setSubmitCallback ( res => resolve ( res ) ) ;
524
- modal . setCloseCallback ( err => {
525
- if ( err ) {
526
- reject ( err ) ;
527
- }
528
- resolve ( undefined ) ;
529
- } ) ;
530
-
531
- modal . open ( ) ;
532
- } ) ;
533
- return { advancedSearchOptions : res , advancedSearchModal : modal } ;
534
- }
535
-
536
- async openMediaDbIdSearchModal ( ) : Promise < { idSearchOptions : { query : string , api : string } , idSearchModal : MediaDbIdSearchModal } > {
537
- const modal = new MediaDbIdSearchModal ( this ) ;
538
- const res : { query : string , api : string } = await new Promise ( ( resolve , reject ) => {
539
- modal . setSubmitCallback ( res => resolve ( res ) ) ;
540
- modal . setCloseCallback ( err => {
541
- if ( err ) {
542
- reject ( err ) ;
543
- }
544
- resolve ( undefined ) ;
545
- } ) ;
546
-
547
- modal . open ( ) ;
548
- } ) ;
549
- return { idSearchOptions : res , idSearchModal : modal } ;
550
- }
551
-
552
- async openMediaDbSelectModal ( resultsToDisplay : MediaTypeModel [ ] , skipButton : boolean = false , allowMultiSelect : boolean = true ) : Promise < { selectRes : MediaTypeModel [ ] , selectModal : MediaDbSearchResultModal } > {
553
- const modal = new MediaDbSearchResultModal ( this , resultsToDisplay , skipButton , allowMultiSelect ) ;
554
- const res : MediaTypeModel [ ] = await new Promise ( ( resolve , reject ) => {
555
- modal . setSubmitCallback ( res => resolve ( res ) ) ;
556
- modal . setSkipCallback ( ( ) => resolve ( [ ] ) ) ;
557
- modal . setCloseCallback ( err => {
558
- if ( err ) {
559
- reject ( err ) ;
560
- }
561
- resolve ( undefined ) ;
562
- } ) ;
563
-
564
- modal . open ( ) ;
565
- } ) ;
566
- return { selectRes : res , selectModal : modal } ;
567
- }
568
-
569
460
async loadSettings ( ) {
570
461
// console.log(DEFAULT_SETTINGS);
571
462
const diskSettings : MediaDbPluginSettings = await this . loadData ( ) ;
0 commit comments