1- import { parseYaml , Plugin , stringifyYaml , TFile } from 'obsidian' ;
1+ import { CachedMetadata , FrontMatterCache , parseYaml , Plugin , stringifyYaml , TFile } from 'obsidian' ;
22import { DEFAULT_SETTINGS , MetaBindPluginSettings , MetaBindSettingTab } from './settings/Settings' ;
33import { InputFieldMarkdownRenderChild , InputFieldMarkdownRenderChildType } from './InputFieldMarkdownRenderChild' ;
44import { getFileName , isPath , removeFileEnding } from './utils/Utils' ;
@@ -64,11 +64,9 @@ export default class MetaBindPlugin extends Plugin {
6464 }
6565 } ) ;
6666
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+ } ) ;
7270
7371 this . addSettingTab ( new MetaBindSettingTab ( this . app , this ) ) ;
7472 }
@@ -87,7 +85,7 @@ export default class MetaBindPlugin extends Plugin {
8785 this . activeMarkdownInputFields = this . activeMarkdownInputFields . filter ( x => x . uid !== inputFieldMarkdownRenderChild . uid ) ;
8886 }
8987
90- async updateMarkdownInputFieldsOnFileChange ( file : TFile ) : Promise < void > {
88+ async updateMarkdownInputFieldsOnMetadataCacheChange ( file : TFile , cache : CachedMetadata ) : Promise < void > {
9189 let metadata : any = undefined ;
9290
9391 for ( const activeMarkdownInputField of this . activeMarkdownInputFields ) {
@@ -97,7 +95,7 @@ export default class MetaBindPlugin extends Plugin {
9795
9896 if ( activeMarkdownInputField . bindTargetFile . path === file . path ) {
9997 if ( metadata === undefined ) {
100- metadata = await this . getMetaDataForFile ( file ) ;
98+ metadata = await this . getMetadataFromFileCache ( cache . frontmatter ) ;
10199 }
102100 activeMarkdownInputField . updateValue ( metadata [ activeMarkdownInputField . bindTargetMetadataField ] ) ;
103101 }
@@ -192,6 +190,19 @@ export default class MetaBindPlugin extends Plugin {
192190 return metadata ;
193191 }
194192
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+
195206 async loadSettings ( ) : Promise < void > {
196207 this . settings = Object . assign ( { } , DEFAULT_SETTINGS , await this . loadData ( ) ) ;
197208 }
0 commit comments