@@ -11,6 +11,7 @@ import { parsePath, traverseObjectByPath } from '@opd-libs/opd-utils-lib/lib/Obj
1111import { ShowcaseInputFieldArgument } from './inputFieldArguments/ShowcaseInputFieldArgument' ;
1212import { TitleInputFieldArgument } from './inputFieldArguments/TitleInputFieldArgument' ;
1313import { isTruthy } from './utils/Utils' ;
14+ import { Listener , Signal } from './utils/Signal' ;
1415
1516export enum RenderChildType {
1617 INLINE = 'inline' ,
@@ -30,12 +31,19 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
3031 inputFieldDeclaration : InputFieldDeclaration ;
3132 bindTargetFile : TFile | undefined ;
3233 bindTargetMetadataPath : string [ ] | undefined ;
33-
34- intervalCounter : number ;
35- metadataValueUpdateQueue : any [ ] ;
36- inputFieldValueUpdateQueue : any [ ] ;
37-
38- constructor ( containerEl : HTMLElement , type : RenderChildType , declaration : InputFieldDeclaration , plugin : MetaBindPlugin , filePath : string , uuid : string ) {
34+ private metadataManagerReadSignalListener : Listener < any > | undefined ;
35+
36+ // maybe 2: in/out
37+ /**
38+ * Signal to write to the input field
39+ */
40+ public writeSignal : Signal < any > ;
41+ /**
42+ * Signal to read from the input field
43+ */
44+ public readSignal : Signal < any > ;
45+
46+ constructor ( containerEl : HTMLElement , renderChildType : RenderChildType , declaration : InputFieldDeclaration , plugin : MetaBindPlugin , filePath : string , uuid : string ) {
3947 super ( containerEl ) ;
4048
4149 if ( ! declaration . error ) {
@@ -47,24 +55,22 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
4755 this . filePath = filePath ;
4856 this . uuid = uuid ;
4957 this . plugin = plugin ;
50- this . renderChildType = type ;
58+ this . renderChildType = renderChildType ;
5159 this . fullDeclaration = declaration . fullDeclaration ;
52-
53- this . metadataValueUpdateQueue = [ ] ;
54- this . inputFieldValueUpdateQueue = [ ] ;
55- this . intervalCounter = 0 ;
5660 this . inputFieldDeclaration = declaration ;
5761
62+ this . writeSignal = new Signal < any > ( undefined ) ;
63+ this . readSignal = new Signal < any > ( undefined ) ;
64+
5865 if ( ! this . error ) {
5966 try {
6067 if ( this . inputFieldDeclaration . isBound ) {
6168 this . parseBindTarget ( ) ;
6269 }
6370
6471 this . inputField = InputFieldFactory . createInputField ( this . inputFieldDeclaration . inputFieldType , {
65- type : type ,
72+ renderChildType : renderChildType ,
6673 inputFieldMarkdownRenderChild : this ,
67- onValueChanged : this . updateMetadataManager . bind ( this ) ,
6874 } ) ;
6975 } catch ( e : any ) {
7076 this . error = e . message ;
@@ -113,35 +119,31 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
113119 }
114120
115121 registerSelfToMetadataManager ( ) : MetadataFileCache | undefined {
122+ // if bind target is invalid, return
116123 if ( ! this . inputFieldDeclaration ?. isBound || ! this . bindTargetFile || ! this . bindTargetMetadataPath || this . bindTargetMetadataPath ?. length === 0 ) {
117124 return ;
118125 }
119126
120- return this . plugin . metadataManager . register (
121- this . bindTargetFile ,
122- value => {
123- if ( ! this . inputField ) {
124- throw new MetaBindInternalError ( 'inputField is undefined, can not update inputField' ) ;
125- }
127+ this . metadataManagerReadSignalListener = this . readSignal . registerListener ( { callback : this . updateMetadataManager . bind ( this ) } ) ;
126128
127- if ( ! this . inputField . isEqualValue ( value ) ) {
128- this . inputField . setValue ( value ) ;
129- }
130- } ,
131- this . bindTargetMetadataPath ,
132- this . uuid
133- ) ;
129+ return this . plugin . metadataManager . register ( this . bindTargetFile , this . writeSignal , this . bindTargetMetadataPath , this . uuid ) ;
134130 }
135131
136132 unregisterSelfFromMetadataManager ( ) : void {
133+ // if bind target is invalid, return
137134 if ( ! this . inputFieldDeclaration ?. isBound || ! this . bindTargetFile || ! this . bindTargetMetadataPath || this . bindTargetMetadataPath ?. length === 0 ) {
138135 return ;
139136 }
140137
138+ if ( this . metadataManagerReadSignalListener ) {
139+ this . readSignal . unregisterListener ( this . metadataManagerReadSignalListener ) ;
140+ }
141+
141142 this . plugin . metadataManager . unregister ( this . bindTargetFile , this . uuid ) ;
142143 }
143144
144145 updateMetadataManager ( value : any ) : void {
146+ // if bind target is invalid, return
145147 if ( ! this . inputFieldDeclaration ?. isBound || ! this . bindTargetFile || ! this . bindTargetMetadataPath || this . bindTargetMetadataPath ?. length === 0 ) {
146148 return ;
147149 }
@@ -179,32 +181,31 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
179181 ) ;
180182 }
181183
184+ hasValidBindTarget ( ) : boolean {
185+ return isTruthy ( this . inputFieldDeclaration ?. isBound ) && isTruthy ( this . bindTargetFile ) && isTruthy ( this . bindTargetMetadataPath ) && this . bindTargetMetadataPath ?. length !== 0 ;
186+ }
187+
182188 async onload ( ) : Promise < void > {
183189 console . log ( 'meta-bind | InputFieldMarkdownRenderChild >> load' , this ) ;
184190
191+ this . containerEl . addClass ( 'meta-bind-plugin-input' ) ;
192+
185193 const container : HTMLDivElement = createDiv ( ) ;
186194 container . addClass ( 'meta-bind-plugin-input-wrapper' ) ;
187- this . containerEl . addClass ( 'meta-bind-plugin-input' ) ;
188195
189196 if ( this . error ) {
190- this . containerEl . empty ( ) ;
191- this . containerEl . createEl ( 'span' , { text : this . fullDeclaration , cls : 'meta-bind-code' } ) ;
192- container . innerText = ` -> ERROR: ${ this . error } ` ;
193- container . addClass ( 'meta-bind-plugin-error' ) ;
194- this . containerEl . appendChild ( container ) ;
197+ this . renderError ( this . error ) ;
195198 return ;
196199 }
197200
198201 if ( ! this . inputField ) {
199- this . containerEl . empty ( ) ;
200- this . containerEl . createEl ( 'span' , { text : this . fullDeclaration , cls : 'meta-bind-code' } ) ;
201- container . innerText = ` -> ERROR: ${ new MetaBindInternalError ( 'input field is undefined and error is empty' ) . message } ` ;
202- container . addClass ( 'meta-bind-plugin-error' ) ;
203- this . containerEl . appendChild ( container ) ;
202+ this . renderError ( new MetaBindInternalError ( 'input field is undefined and error is empty' ) . message ) ;
204203 return ;
205204 }
206205
207- this . metadataCache = this . registerSelfToMetadataManager ( ) ;
206+ if ( this . hasValidBindTarget ( ) ) {
207+ this . metadataCache = this . registerSelfToMetadataManager ( ) ;
208+ }
208209 this . plugin . registerInputFieldMarkdownRenderChild ( this ) ;
209210
210211 this . inputField . render ( container ) ;
@@ -236,6 +237,20 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
236237 }
237238 }
238239
240+ renderError ( message : string ) : void {
241+ this . containerEl . empty ( ) ;
242+
243+ if ( this . renderChildType === RenderChildType . BLOCK ) {
244+ const cardContainer : HTMLDivElement = this . containerEl . createDiv ( { cls : 'meta-bind-plugin-card' } ) ;
245+
246+ cardContainer . createEl ( 'code' , { text : ` ${ this . fullDeclaration } ` } ) ;
247+ cardContainer . createEl ( 'span' , { text : message , cls : 'meta-bind-plugin-error' } ) ;
248+ } else {
249+ this . containerEl . createEl ( 'code' , { text : ` ${ this . fullDeclaration } ` } ) ;
250+ this . containerEl . createEl ( 'code' , { text : `-> ${ message } ` , cls : 'meta-bind-plugin-error' } ) ;
251+ }
252+ }
253+
239254 onunload ( ) : void {
240255 console . log ( 'meta-bind | InputFieldMarkdownRenderChild >> unload' , this ) ;
241256
@@ -244,7 +259,7 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
244259 this . unregisterSelfFromMetadataManager ( ) ;
245260
246261 this . containerEl . empty ( ) ;
247- this . containerEl . remove ( ) ;
262+ this . containerEl . createEl ( 'span' , { text : 'unloaded meta bind input field' , cls : 'meta-bind-plugin-error' } ) ;
248263
249264 super . onunload ( ) ;
250265 }
0 commit comments