1
- import { parseYaml , Plugin , stringifyYaml , TFile } from 'obsidian' ;
1
+ import { CachedMetadata , FrontMatterCache , parseYaml , Plugin , stringifyYaml , TFile } from 'obsidian' ;
2
2
import { DEFAULT_SETTINGS , MetaBindPluginSettings , MetaBindSettingTab } from './settings/Settings' ;
3
3
import { InputFieldMarkdownRenderChild , InputFieldMarkdownRenderChildType } from './InputFieldMarkdownRenderChild' ;
4
4
import { getFileName , isPath , removeFileEnding } from './utils/Utils' ;
@@ -64,11 +64,9 @@ export default class MetaBindPlugin extends Plugin {
64
64
}
65
65
} ) ;
66
66
67
- this . registerEvent ( this . app . vault . on ( 'modify' , async abstractFile => {
68
- if ( abstractFile instanceof TFile ) {
69
- await this . updateMarkdownInputFieldsOnFileChange ( abstractFile ) ;
70
- }
71
- } ) ) ;
67
+ this . app . metadataCache . on ( 'changed' , async ( file : TFile , data : string , cache : CachedMetadata ) => {
68
+ await this . updateMarkdownInputFieldsOnMetadataCacheChange ( file , cache ) ;
69
+ } ) ;
72
70
73
71
this . addSettingTab ( new MetaBindSettingTab ( this . app , this ) ) ;
74
72
}
@@ -87,7 +85,7 @@ export default class MetaBindPlugin extends Plugin {
87
85
this . activeMarkdownInputFields = this . activeMarkdownInputFields . filter ( x => x . uid !== inputFieldMarkdownRenderChild . uid ) ;
88
86
}
89
87
90
- async updateMarkdownInputFieldsOnFileChange ( file : TFile ) : Promise < void > {
88
+ async updateMarkdownInputFieldsOnMetadataCacheChange ( file : TFile , cache : CachedMetadata ) : Promise < void > {
91
89
let metadata : any = undefined ;
92
90
93
91
for ( const activeMarkdownInputField of this . activeMarkdownInputFields ) {
@@ -97,7 +95,7 @@ export default class MetaBindPlugin extends Plugin {
97
95
98
96
if ( activeMarkdownInputField . bindTargetFile . path === file . path ) {
99
97
if ( metadata === undefined ) {
100
- metadata = await this . getMetaDataForFile ( file ) ;
98
+ metadata = await this . getMetadataFromFileCache ( cache . frontmatter ) ;
101
99
}
102
100
activeMarkdownInputField . updateValue ( metadata [ activeMarkdownInputField . bindTargetMetadataField ] ) ;
103
101
}
@@ -192,6 +190,19 @@ export default class MetaBindPlugin extends Plugin {
192
190
return metadata ;
193
191
}
194
192
193
+ getMetadataFromFileCache ( cache : FrontMatterCache | undefined ) {
194
+ let metadata = cache as object ;
195
+
196
+ if ( metadata ) {
197
+ metadata = Object . assign ( { } , metadata ) ; // copy
198
+ // @ts -ignore
199
+ delete metadata . position ;
200
+ } else {
201
+ metadata = { } ;
202
+ }
203
+ return metadata ;
204
+ }
205
+
195
206
async loadSettings ( ) : Promise < void > {
196
207
this . settings = Object . assign ( { } , DEFAULT_SETTINGS , await this . loadData ( ) ) ;
197
208
}
0 commit comments