@@ -12,45 +12,84 @@ export class InputField extends MarkdownRenderChild {
1212 boundMetadataField : string ;
1313 file : TFile ;
1414
15+ limitInterval : NodeJS . Timer ;
16+ intervalCounter : number ;
17+ valueQueue : any [ ] ;
18+
1519 constructor ( containerEl : HTMLElement , fullDeclaration : string , plugin : MetaBindPlugin , filePath : string ) {
1620 super ( containerEl ) ;
1721
1822 //console.log(this, 2)
1923
2024 this . error = '' ;
25+ this . declaration = fullDeclaration ;
26+ this . plugin = plugin ;
27+
28+ this . valueQueue = [ ] ;
29+ this . intervalCounter = 0 ;
30+ this . limitInterval = setInterval ( this . incrementInterval . bind ( this ) , 10 ) ;
2131
2232 const regExp = new RegExp ( / \[ .* ?\] / ) ;
2333 let declaration = regExp . exec ( fullDeclaration ) [ 0 ] ;
2434 declaration = declaration . replace ( '[' , '' ) . replace ( ']' , '' ) ;
25-
2635 let declarationParts : string [ ] = declaration . split ( ':' ) ;
2736 let boundTo : string = declarationParts [ 1 ] ?? '' ;
37+
2838 this . isBound = ! ! boundTo ;
39+ this . inputFieldType = declarationParts [ 0 ] . toLowerCase ( ) ;
40+
2941 if ( this . isBound ) {
3042 let boundToParts = boundTo . split ( '#' ) ;
3143 if ( boundToParts . length === 1 ) { // same file
3244 this . boundMetadataField = boundTo ;
33- this . file = plugin . getFileByName ( filePath ) ;
45+ const files = plugin . getFilesByName ( filePath ) ;
46+ if ( files . length === 0 ) {
47+ this . error = 'file not fond.' ;
48+ return ;
49+ } else if ( files . length === 1 ) {
50+ this . file = files [ 0 ] ;
51+ } else {
52+ this . error = 'multiple files found. please specify the file path.' ;
53+ return ;
54+ }
3455 } else if ( boundToParts . length === 2 ) {
3556 this . boundMetadataField = boundToParts [ 1 ] ;
36- this . file = plugin . getFileByName ( boundToParts [ 0 ] ) ;
57+ const files = plugin . getFilesByName ( boundToParts [ 0 ] ) ;
58+ if ( files . length === 0 ) {
59+ this . error = 'file not fond.' ;
60+ return ;
61+ } else if ( files . length === 1 ) {
62+ this . file = files [ 0 ] ;
63+ } else {
64+ this . error = 'multiple files found. please specify the file path.' ;
65+ return ;
66+ }
3767 } else {
38- this . error = 'invalid binding' ;
68+ this . error = 'invalid binding.' ;
69+ return ;
3970 }
40-
4171 this . metaData = plugin . getMetaDataForFile ( this . file ) ;
4272 }
4373
44- this . declaration = fullDeclaration ;
45- this . inputFieldType = declarationParts [ 0 ] . toLowerCase ( ) ;
46- this . plugin = plugin ;
47-
4874 // console.log(this, 3)
4975 }
5076
77+ // use this interval to reduce writing operations
78+ async incrementInterval ( ) {
79+ this . intervalCounter += 1 ;
80+
81+ if ( this . intervalCounter >= 20 && this . valueQueue . length > 0 ) {
82+ // console.log(this.valueQueue.at(-1))
83+ await this . plugin . updateMetaData ( this . boundMetadataField , this . valueQueue . at ( - 1 ) , this . file ) ;
84+ this . valueQueue = [ ] ;
85+ this . intervalCounter = 0 ;
86+ }
87+
88+ }
89+
5190 async updateMetaData ( value : any ) {
5291 if ( this . isBound ) {
53- await this . plugin . updateMetaData ( this . boundMetadataField , value , this . file ) ;
92+ this . valueQueue . push ( value ) ;
5493 }
5594 }
5695
@@ -61,14 +100,15 @@ export class InputField extends MarkdownRenderChild {
61100 }
62101
63102 onload ( ) {
64- // console.log(this, 1)
103+ console . log ( 'load' , this ) ;
65104
66105 const container = this . containerEl . createDiv ( ) ;
67106 container . addClass ( 'meta-bind-plugin-input-wrapper' ) ;
68107
69108 if ( this . error ) {
70- container . innerText = ` \`Error ${ this . error } \`` ;
71- this . containerEl . replaceWith ( container ) ;
109+ container . innerText = `Error: ${ this . error } ` ;
110+ container . addClass ( 'meta-bind-plugin-error' ) ;
111+ this . containerEl . appendChild ( container ) ;
72112 return ;
73113 }
74114
@@ -92,6 +132,14 @@ export class InputField extends MarkdownRenderChild {
92132 } ) ;
93133 }
94134
95- this . containerEl . replaceWith ( container ) ;
135+ this . containerEl . empty ( ) ;
136+ this . containerEl . appendChild ( container ) ;
137+ }
138+
139+ onunload ( ) {
140+ super . onunload ( ) ;
141+
142+ console . log ( 'unload' , this ) ;
143+ clearInterval ( this . limitInterval ) ;
96144 }
97145}
0 commit comments