@@ -23,12 +23,31 @@ export default class MediaDbPlugin extends Plugin {
23
23
mediaTypeManager : MediaTypeManager ;
24
24
modelPropertyMapper : ModelPropertyMapper ;
25
25
26
+ frontMatterRexExpPattern : string = '^(---)\\n[\\s\\S]*?\\n---' ;
27
+
26
28
async onload ( ) {
27
29
await this . loadSettings ( ) ;
30
+ // register the settings tab
31
+ this . addSettingTab ( new MediaDbSettingTab ( this . app , this ) ) ;
32
+
33
+
34
+ this . apiManager = new APIManager ( ) ;
35
+ // register APIs
36
+ this . apiManager . registerAPI ( new OMDbAPI ( this ) ) ;
37
+ this . apiManager . registerAPI ( new MALAPI ( this ) ) ;
38
+ this . apiManager . registerAPI ( new WikipediaAPI ( this ) ) ;
39
+ this . apiManager . registerAPI ( new MusicBrainzAPI ( this ) ) ;
40
+ this . apiManager . registerAPI ( new SteamAPI ( this ) ) ;
41
+ this . apiManager . registerAPI ( new BoardGameGeekAPI ( this ) ) ;
42
+ // this.apiManager.registerAPI(new LocGovAPI(this)); // TODO: parse data
43
+
44
+ this . mediaTypeManager = new MediaTypeManager ( this . settings ) ;
45
+ this . modelPropertyMapper = new ModelPropertyMapper ( this . settings ) ;
46
+
28
47
29
48
// add icon to the left ribbon
30
49
const ribbonIconEl = this . addRibbonIcon ( 'database' , 'Add new Media DB entry' , ( evt : MouseEvent ) =>
31
- this . createMediaDbNotes ( this . openMediaDbAdvancedSearchModal . bind ( this ) ) ,
50
+ this . createEntryWithAdvancedSearchModal ( ) ,
32
51
) ;
33
52
ribbonIconEl . addClass ( 'obsidian-media-db-plugin-ribbon-class' ) ;
34
53
@@ -46,13 +65,13 @@ export default class MediaDbPlugin extends Plugin {
46
65
this . addCommand ( {
47
66
id : 'open-media-db-search-modal' ,
48
67
name : 'Add new Media DB entry' ,
49
- callback : ( ) => this . createMediaDbNotes ( this . openMediaDbAdvancedSearchModal . bind ( this ) ) ,
68
+ callback : ( ) => this . createEntryWithAdvancedSearchModal ( ) ,
50
69
} ) ;
51
70
// register command to open id search modal
52
71
this . addCommand ( {
53
72
id : 'open-media-db-id-search-modal' ,
54
73
name : 'Add new Media DB entry by id' ,
55
- callback : ( ) => this . createMediaDbNotes ( this . openMediaDbIdSearchModal . bind ( this ) ) ,
74
+ callback : ( ) => this . createEntryWithIdSearchModal ( ) ,
56
75
} ) ;
57
76
// register command to update the open note
58
77
this . addCommand ( {
@@ -68,42 +87,65 @@ export default class MediaDbPlugin extends Plugin {
68
87
return true ;
69
88
} ,
70
89
} ) ;
90
+ }
71
91
72
- // register the settings tab
73
- this . addSettingTab ( new MediaDbSettingTab ( this . app , this ) ) ;
92
+ async createEntryWithSearchModal ( ) {
74
93
94
+ }
75
95
76
- this . apiManager = new APIManager ( ) ;
77
- // register APIs
78
- this . apiManager . registerAPI ( new OMDbAPI ( this ) ) ;
79
- this . apiManager . registerAPI ( new MALAPI ( this ) ) ;
80
- this . apiManager . registerAPI ( new WikipediaAPI ( this ) ) ;
81
- this . apiManager . registerAPI ( new MusicBrainzAPI ( this ) ) ;
82
- this . apiManager . registerAPI ( new SteamAPI ( this ) ) ;
83
- this . apiManager . registerAPI ( new BoardGameGeekAPI ( this ) ) ;
84
- // this.apiManager.registerAPI(new LocGovAPI(this)); // TODO: parse data
96
+ async createEntryWithAdvancedSearchModal ( ) {
97
+ let results : MediaTypeModel [ ] = [ ] ;
98
+ try {
99
+ const { query, apis} = await this . openMediaDbAdvancedSearchModal ( ) ;
85
100
86
- this . mediaTypeManager = new MediaTypeManager ( this . settings ) ;
87
- this . modelPropertyMapper = new ModelPropertyMapper ( this . settings ) ;
101
+ new Notice ( 'MediaDB Searching...' ) ;
102
+
103
+ const apiSearchResults = await this . apiManager . query ( query , apis ) ;
104
+ const selectResults = await this . openMediaDbSelectModal ( apiSearchResults , false ) ;
105
+ results = await this . queryDetails ( selectResults ) ;
106
+ } catch ( e ) {
107
+ console . warn ( e ) ;
108
+ new Notice ( e . toString ( ) ) ;
109
+ }
110
+
111
+ debugLog ( results ) ;
112
+ await this . createMediaDbNotes ( results ) ;
88
113
}
89
114
90
- async createMediaDbNotes ( modal : ( ) => Promise < MediaTypeModel [ ] > , attachFile ?: TFile ) : Promise < void > {
91
- let models : MediaTypeModel [ ] = [ ] ;
115
+ async createEntryWithIdSearchModal ( ) {
116
+ let result : MediaTypeModel = undefined ;
92
117
try {
93
- models = await modal ( ) ;
118
+ const { query, api} = await this . openMediaDbIdSearchModal ( ) ;
119
+
120
+ new Notice ( 'MediaDB Searching...' ) ;
121
+
122
+ result = await this . apiManager . queryDetailedInfoById ( query , api ) ;
94
123
} catch ( e ) {
95
124
console . warn ( e ) ;
96
125
new Notice ( e . toString ( ) ) ;
97
126
}
98
127
128
+ debugLog ( result ) ;
129
+ await this . createMediaDbNoteFromModel ( result ) ;
130
+ }
131
+
132
+ async createMediaDbNotes ( models : MediaTypeModel [ ] , attachFile ?: TFile ) : Promise < void > {
133
+ for ( const model of models ) {
134
+ await this . createMediaDbNoteFromModel ( model , attachFile ) ;
135
+ }
136
+ }
137
+
138
+ async queryDetails ( models : MediaTypeModel [ ] ) : Promise < MediaTypeModel [ ] > {
139
+ let detailModels : MediaTypeModel [ ] = [ ] ;
99
140
for ( const model of models ) {
100
141
try {
101
- await this . createMediaDbNoteFromModel ( await this . apiManager . queryDetailedInfo ( model ) , attachFile ) ;
142
+ detailModels . push ( await this . apiManager . queryDetailedInfo ( model ) ) ;
102
143
} catch ( e ) {
103
144
console . warn ( e ) ;
104
145
new Notice ( e . toString ( ) ) ;
105
146
}
106
147
}
148
+ return detailModels ;
107
149
}
108
150
109
151
async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , attachFile ?: TFile ) : Promise < void > {
@@ -187,23 +229,33 @@ export default class MediaDbPlugin extends Plugin {
187
229
return metadata ;
188
230
}
189
231
232
+ /**
233
+ * Creates a note in the vault.
234
+ *
235
+ * @param fileName
236
+ * @param fileContent
237
+ * @param openFile
238
+ */
190
239
async createNote ( fileName : string , fileContent : string , openFile : boolean = false ) {
191
240
fileName = replaceIllegalFileNameCharactersInString ( fileName ) ;
192
241
const filePath = `${ this . settings . folder . replace ( / \/ $ / , '' ) } /${ fileName } .md` ;
193
242
243
+ // find and possibly create the folder set in settings
194
244
const folder = this . app . vault . getAbstractFileByPath ( this . settings . folder ) ;
195
245
if ( ! folder ) {
196
246
await this . app . vault . createFolder ( this . settings . folder . replace ( / \/ $ / , '' ) ) ;
197
247
}
198
248
249
+ // find and delete file with the same name
199
250
const file = this . app . vault . getAbstractFileByPath ( filePath ) ;
200
251
if ( file ) {
201
252
await this . app . vault . delete ( file ) ;
202
253
}
203
254
255
+ // create the file
204
256
const targetFile = await this . app . vault . create ( filePath , fileContent ) ;
205
257
206
- // open file
258
+ // open newly crated file
207
259
if ( openFile ) {
208
260
const activeLeaf = this . app . workspace . getUnpinnedLeaf ( ) ;
209
261
if ( ! activeLeaf ) {
@@ -214,6 +266,10 @@ export default class MediaDbPlugin extends Plugin {
214
266
}
215
267
}
216
268
269
+ /**
270
+ * Update the active note by querying the API again.
271
+ * Tries to read the type, id and dataSource of the active note. If successful it will query the api, delete the old note and create a new one.
272
+ */
217
273
async updateActiveNote ( ) {
218
274
const activeFile : TFile = this . app . workspace . getActiveFile ( ) ;
219
275
if ( ! activeFile ) {
@@ -249,18 +305,12 @@ export default class MediaDbPlugin extends Plugin {
249
305
const erroredFiles : { filePath : string , error : string } [ ] = [ ] ;
250
306
let canceled : boolean = false ;
251
307
252
- const { selectedAPI, titleFieldName, appendContent} = await new Promise ( ( resolve , reject ) => {
253
- new MediaDbFolderImportModal ( this . app , this , ( ( selectedAPI , titleFieldName , appendContent ) => {
308
+ const { selectedAPI, titleFieldName, appendContent} = await new Promise < { selectedAPI : string , titleFieldName : string , appendContent : boolean } > ( ( resolve , reject ) => {
309
+ new MediaDbFolderImportModal ( this . app , this , ( ( selectedAPI : string , titleFieldName : string , appendContent : boolean ) => {
254
310
resolve ( { selectedAPI, titleFieldName, appendContent} ) ;
255
311
} ) ) . open ( ) ;
256
312
} ) ;
257
313
258
- const selectedAPIs = { } ;
259
- for ( const api of this . apiManager . apis ) {
260
- // @ts -ignore
261
- selectedAPIs [ api . apiName ] = api . apiName === selectedAPI ;
262
- }
263
-
264
314
for ( const child of folder . children ) {
265
315
if ( child instanceof TFile ) {
266
316
const file = child as TFile ;
@@ -279,7 +329,7 @@ export default class MediaDbPlugin extends Plugin {
279
329
280
330
let results : MediaTypeModel [ ] = [ ] ;
281
331
try {
282
- results = await this . apiManager . query ( title , selectedAPIs ) ;
332
+ results = await this . apiManager . query ( title , [ selectedAPI ] ) ;
283
333
} catch ( e ) {
284
334
erroredFiles . push ( { filePath : file . path , error : e . toString ( ) } ) ;
285
335
continue ;
@@ -292,7 +342,7 @@ export default class MediaDbPlugin extends Plugin {
292
342
let selectedResults : MediaTypeModel [ ] = [ ] ;
293
343
try {
294
344
selectedResults = await new Promise ( ( resolve , reject ) => {
295
- const searchResultModal = new MediaDbSearchResultModal ( this . app , this , results , true , ( err , res ) => {
345
+ const searchResultModal = new MediaDbSearchResultModal ( this . app , this , results , true , ( res , err ) => {
296
346
if ( err ) {
297
347
return reject ( err ) ;
298
348
}
@@ -325,47 +375,60 @@ export default class MediaDbPlugin extends Plugin {
325
375
continue ;
326
376
}
327
377
328
- await this . createMediaDbNotes ( async ( ) => selectedResults , appendContent ? file : null ) ;
378
+ const detailedResults = await this . queryDetails ( selectedResults ) ;
379
+ await this . createMediaDbNotes ( detailedResults , appendContent ? file : null ) ;
329
380
}
330
381
}
331
382
332
383
if ( erroredFiles . length > 0 ) {
333
- const title = `bulk import error report ${ dateTimeToString ( new Date ( ) ) } ` ;
334
- const filePath = `${ this . settings . folder . replace ( / \/ $ / , '' ) } /${ title } .md` ;
384
+ await this . createErroredFilesReport ( erroredFiles ) ;
385
+ }
386
+ }
335
387
336
- const table = [ [ 'file' , 'error' ] ] . concat ( erroredFiles . map ( x => [ x . filePath , x . error ] ) ) ;
337
- // console.log(table)
338
- let fileContent = `# ${ title } \n\n ${ markdownTable ( table ) } ` ;
388
+ async createErroredFilesReport ( erroredFiles : { filePath : string , error : string } [ ] ) : Promise < void > {
389
+ const title = `bulk import error report ${ dateTimeToString ( new Date ( ) ) } ` ;
390
+ const filePath = `${ this . settings . folder . replace ( / \/ $ / , '' ) } / ${ title } .md ` ;
339
391
340
- const targetFile = await this . app . vault . create ( filePath , fileContent ) ;
341
- }
392
+ const table = [ [ 'file' , 'error' ] ] . concat ( erroredFiles . map ( x => [ x . filePath , x . error ] ) ) ;
393
+ // console.log(table)
394
+ let fileContent = `# ${ title } \n\n${ markdownTable ( table ) } ` ;
395
+
396
+ const targetFile = await this . app . vault . create ( filePath , fileContent ) ;
342
397
}
343
398
344
- async openMediaDbAdvancedSearchModal ( ) : Promise < MediaTypeModel [ ] > {
345
- return new Promise ( ( ( resolve , reject ) => {
346
- new MediaDbAdvancedSearchModal ( this . app , this , ( err , results ) => {
399
+ async openMediaDbAdvancedSearchModal ( ) : Promise < { query : string , apis : string [ ] } > {
400
+ return await new Promise ( ( resolve , reject ) => {
401
+ new MediaDbAdvancedSearchModal ( this . app , this , ( res , err ) => {
347
402
if ( err ) {
348
403
return reject ( err ) ;
349
404
}
350
- new MediaDbSearchResultModal ( this . app , this , results , false , ( err2 , res ) => {
351
- if ( err2 ) {
352
- return reject ( err2 ) ;
353
- }
354
- resolve ( res ) ;
355
- } , ( ) => resolve ( [ ] ) ) . open ( ) ;
405
+ resolve ( res )
356
406
} ) . open ( ) ;
357
- } ) ) ;
407
+ } ) ;
408
+ }
409
+
410
+ async openMediaDbIdSearchModal ( ) : Promise < { query : string , api : string } > {
411
+ return await new Promise ( ( resolve , reject ) => {
412
+ new MediaDbIdSearchModal ( this . app , this , ( res , err ) => {
413
+ if ( err ) {
414
+ return reject ( err ) ;
415
+ }
416
+ resolve ( res )
417
+ } ) . open ( ) ;
418
+ } ) ;
358
419
}
359
420
360
- async openMediaDbIdSearchModal ( ) : Promise < MediaTypeModel > {
361
- return new Promise ( ( ( resolve , reject ) => {
362
- new MediaDbIdSearchModal ( this . app , this , ( err , res ) => {
421
+ async openMediaDbSelectModal ( resultsToDisplay : MediaTypeModel [ ] , skipButton : boolean = false ) : Promise < MediaTypeModel [ ] > {
422
+ return await new Promise ( ( resolve , reject ) => {
423
+ new MediaDbSearchResultModal ( this . app , this , resultsToDisplay , skipButton , ( res , err ) => {
363
424
if ( err ) {
364
425
return reject ( err ) ;
365
426
}
366
427
resolve ( res ) ;
428
+ } , ( ) => {
429
+ resolve ( [ ] )
367
430
} ) . open ( ) ;
368
- } ) ) ;
431
+ } ) ;
369
432
}
370
433
371
434
async loadSettings ( ) {
0 commit comments