@@ -37,6 +37,12 @@ export default class MediaDbPlugin extends Plugin {
3737 callback : ( ) => this . createMediaDbNote ( this . openMediaDbIdSearchModal . bind ( this ) ) ,
3838 } ) ;
3939
40+ this . addCommand ( {
41+ id : 'update-media-db-note' ,
42+ name : 'Update the open note, if it is a Media DB entry.' ,
43+ callback : ( ) => this . updateActiveNote ( ) ,
44+ } ) ;
45+
4046 // register the settings tab
4147 this . addSettingTab ( new MediaDbSettingTab ( this . app , this ) ) ;
4248
@@ -53,10 +59,18 @@ export default class MediaDbPlugin extends Plugin {
5359 async createMediaDbNote ( modal : ( ) => Promise < MediaTypeModel > ) : Promise < void > {
5460 try {
5561 let data : MediaTypeModel = await modal ( ) ;
56- console . log ( 'MDB | Creating new note...' ) ;
57-
5862 data = await this . apiManager . queryDetailedInfo ( data ) ;
5963
64+ await this . createMediaDbNoteFromModel ( data ) ;
65+ } catch ( e ) {
66+ console . warn ( e ) ;
67+ new Notice ( e . toString ( ) ) ;
68+ }
69+ }
70+
71+ async createMediaDbNoteFromModel ( data : MediaTypeModel ) : Promise < void > {
72+ try {
73+ console . log ( 'MDB | Creating new note...' ) ;
6074 // console.log(data);
6175
6276 let fileContent = `---\n${ data . toMetaData ( ) } ---\n` ;
@@ -86,6 +100,8 @@ export default class MediaDbPlugin extends Plugin {
86100
87101 const fileName = replaceIllegalFileNameCharactersInString ( data . getFileName ( ) ) ;
88102 const filePath = `${ this . settings . folder . replace ( / \/ $ / , '' ) } /${ fileName } .md` ;
103+
104+ await this . app . vault . delete ( this . app . vault . getAbstractFileByPath ( filePath ) ) ;
89105 const targetFile = await this . app . vault . create ( filePath , fileContent ) ;
90106
91107 // open file
@@ -95,6 +111,7 @@ export default class MediaDbPlugin extends Plugin {
95111 return ;
96112 }
97113 await activeLeaf . openFile ( targetFile , { state : { mode : 'source' } } ) ;
114+
98115 } catch ( e ) {
99116 console . warn ( e ) ;
100117 new Notice ( e . toString ( ) ) ;
@@ -122,6 +139,27 @@ export default class MediaDbPlugin extends Plugin {
122139 } ) ) ;
123140 }
124141
142+ async updateActiveNote ( ) {
143+ const activeLeaf : TFile = this . app . workspace . getActiveFile ( ) ;
144+ if ( ! activeLeaf . name ) return ;
145+
146+ let metadata = this . app . metadataCache . getFileCache ( activeLeaf ) . frontmatter ;
147+
148+ if ( ! metadata . type || ! metadata . dataSource || ! metadata . id ) {
149+ throw new Error ( 'MDB | active note is not a Media DB entry or is missing metadata' ) ;
150+ }
151+
152+ const newMetadata = await this . apiManager . queryDetailedInfo ( { dataSource : metadata . dataSource , id : metadata . id } as MediaTypeModel ) ;
153+
154+ if ( ! newMetadata ) {
155+ return ;
156+ }
157+
158+ console . log ( 'MDB | deleting old entry' ) ;
159+ await this . app . vault . delete ( activeLeaf ) ;
160+ await this . createMediaDbNoteFromModel ( newMetadata ) ;
161+ }
162+
125163 async loadSettings ( ) {
126164 this . settings = Object . assign ( { } , DEFAULT_SETTINGS , await this . loadData ( ) ) ;
127165 }
0 commit comments