@@ -18,15 +18,18 @@ import { Signal } from '../utils/Signal';
1818import { type BindTargetScope } from '../metadata/BindTargetScope' ;
1919import { MetaBindTable } from '../metaBindTable/MetaBindTable' ;
2020import { InputFieldFactory } from '../inputFields/InputFieldFactory' ;
21- import { type JsViewFieldDeclaration , type UnvalidatedViewFieldDeclaration , type ViewFieldDeclaration } from '../parsers/viewFieldParser/ViewFieldDeclaration' ;
21+ import {
22+ type JsViewFieldDeclaration ,
23+ type UnvalidatedViewFieldDeclaration ,
24+ type ViewFieldDeclaration ,
25+ } from '../parsers/viewFieldParser/ViewFieldDeclaration' ;
2226import { ViewFieldFactory } from '../viewFields/ViewFieldFactory' ;
2327import { getUUID } from '../utils/Utils' ;
2428
2529export class API implements IAPI {
2630 public plugin : MetaBindPlugin ;
2731 public readonly inputField : InputFieldAPI ;
2832
29- // public inputFieldParser: InputFieldDeclarationParser;
3033 public readonly inputFieldParser : InputFieldDeclarationParser ;
3134 public readonly viewFieldParser : ViewFieldDeclarationParser ;
3235 public readonly bindTargetParser : BindTargetParser ;
@@ -47,32 +50,54 @@ export class API implements IAPI {
4750 this . viewFieldFactory = new ViewFieldFactory ( this . plugin ) ;
4851 }
4952
53+ /**
54+ * Creates an input field from an unvalidated declaration.
55+ *
56+ * @param unvalidatedDeclaration the unvalidated declaration
57+ * @param renderType whether to render the input field inline or as a block
58+ * @param filePath the file path that the input field is in or an empty string if it is not in a file
59+ * @param containerEl the container to mount the input field to
60+ * @param component component for lifecycle management
61+ * @param scope optional bind target scope
62+ */
5063 public createInputField (
5164 unvalidatedDeclaration : UnvalidatedInputFieldDeclaration ,
5265 renderType : RenderChildType ,
5366 filePath : string ,
5467 containerEl : HTMLElement ,
5568 component : Component | MarkdownPostProcessorContext ,
69+ scope ?: BindTargetScope | undefined ,
5670 ) : InputFieldMDRC | ExcludedMDRC {
5771 if ( this . plugin . isFilePathExcluded ( filePath ) ) {
5872 return this . createExcludedField ( containerEl , filePath , component ) ;
5973 }
6074
61- const declaration = this . inputFieldParser . validateDeclaration ( unvalidatedDeclaration ) ;
75+ const declaration = this . inputFieldParser . validateDeclaration ( unvalidatedDeclaration , scope ) ;
6276
6377 const inputField = new InputFieldMDRC ( containerEl , renderType , declaration , this . plugin , filePath , getUUID ( ) ) ;
6478 component . addChild ( inputField ) ;
6579
6680 return inputField ;
6781 }
6882
83+ /**
84+ * Creates an input field from a string.
85+ * This will parse the string and create the input field.
86+ *
87+ * @param fullDeclaration the string
88+ * @param renderType whether to render the input field inline or as a block
89+ * @param filePath the file path that the input field is in or an empty string if it is not in a file
90+ * @param containerEl the container to mount the input field to
91+ * @param component component for lifecycle management
92+ * @param scope optional bind target scope
93+ */
6994 public createInputFieldFromString (
7095 fullDeclaration : string ,
7196 renderType : RenderChildType ,
7297 filePath : string ,
7398 containerEl : HTMLElement ,
7499 component : Component | MarkdownPostProcessorContext ,
75- scope : BindTargetScope | undefined ,
100+ scope ? : BindTargetScope | undefined ,
76101 ) : InputFieldMDRC | ExcludedMDRC {
77102 if ( this . plugin . isFilePathExcluded ( filePath ) ) {
78103 return this . createExcludedField ( containerEl , filePath , component ) ;
@@ -86,25 +111,47 @@ export class API implements IAPI {
86111 return inputField ;
87112 }
88113
114+ /**
115+ * Creates a view field from a string.
116+ * This will parse the string and create the view field.
117+ *
118+ * @param fullDeclaration the string
119+ * @param renderType whether to render the view field inline or as a block
120+ * @param filePath the file path that the view field is in or an empty string if it is not in a file
121+ * @param containerEl the container to mount the view field to
122+ * @param component component for lifecycle management
123+ * @param scope optional bind target scope
124+ */
89125 public createViewFieldFromString (
90126 fullDeclaration : string ,
91127 renderType : RenderChildType ,
92128 filePath : string ,
93129 containerEl : HTMLElement ,
94130 component : Component | MarkdownPostProcessorContext ,
131+ scope ?: BindTargetScope | undefined ,
95132 ) : ViewFieldMDRC | ExcludedMDRC {
96133 if ( this . plugin . isFilePathExcluded ( filePath ) ) {
97134 return this . createExcludedField ( containerEl , filePath , component ) ;
98135 }
99136
100- const declaration : ViewFieldDeclaration = this . viewFieldParser . parseString ( fullDeclaration ) ;
137+ const declaration : ViewFieldDeclaration = this . viewFieldParser . parseString ( fullDeclaration , scope ) ;
101138
102139 const viewField = new ViewFieldMDRC ( containerEl , renderType , declaration , this . plugin , filePath , getUUID ( ) ) ;
103140 component . addChild ( viewField ) ;
104141
105142 return viewField ;
106143 }
107144
145+ /**
146+ * Creates a js view field from a string.
147+ * This will parse the string and create the js view field.
148+ *
149+ * @param fullDeclaration the string containing the header and the code
150+ * @param renderType whether to render the js view field inline or as a block
151+ * @param filePath the file path that the js view field is in or an empty string if it is not in a file
152+ * @param containerEl the container to mount the js view field to
153+ * @param component component for lifecycle management
154+ */
108155 public createJsViewFieldFromString (
109156 fullDeclaration : string ,
110157 renderType : RenderChildType ,
@@ -124,7 +171,18 @@ export class API implements IAPI {
124171 return viewField ;
125172 }
126173
127- public createExcludedField ( containerEl : HTMLElement , filePath : string , component : Component | MarkdownPostProcessorContext ) : ExcludedMDRC {
174+ /**
175+ * An excluded field will render a message that the file was excluded in the plugin settings.
176+ *
177+ * @param containerEl the container to mount the field to
178+ * @param filePath the file path that the field should be in or an empty string if it is not in a file
179+ * @param component component for lifecycle management
180+ */
181+ public createExcludedField (
182+ containerEl : HTMLElement ,
183+ filePath : string ,
184+ component : Component | MarkdownPostProcessorContext ,
185+ ) : ExcludedMDRC {
128186 const excludedField = new ExcludedMDRC ( containerEl , RenderChildType . INLINE , this . plugin , filePath , getUUID ( ) ) ;
129187 component . addChild ( excludedField ) ;
130188
@@ -138,26 +196,48 @@ export class API implements IAPI {
138196 /**
139197 * Registers a signal to a metadata property and returns a callback to unregister.
140198 *
141- * @param signal
142- * @param filePath
143- * @param metadataPath
144- * @param listenToChildren
199+ * @param signal a signal that will be updated with new metadata
200+ * @param filePath the file path of the metadata to listen to
201+ * @param metadataPath the object path of the metadata to listen to (e.g. ['task', '0', 'completed'])
202+ * @param listenToChildren whether to listen to updates of the children of the metadata path, useful when listening to arrays or objects
203+ * @param onDelete callback that will be called when the metadata becomes unavailable
145204 */
146- public listenToMetadata ( signal : Signal < unknown > , filePath : string , metadataPath : string [ ] , listenToChildren : boolean = false ) : ( ) => void {
205+ public listenToMetadata (
206+ signal : Signal < unknown > ,
207+ filePath : string ,
208+ metadataPath : string [ ] ,
209+ listenToChildren : boolean = false ,
210+ onDelete ?: ( ) => void ,
211+ ) : ( ) => void {
147212 const uuid = getUUID ( ) ;
148213
149- const subscription = this . plugin . metadataManager . subscribe ( uuid , signal , {
150- filePath : filePath ,
151- metadataPath : metadataPath ,
152- listenToChildren : listenToChildren ,
153- boundToLocalScope : false ,
154- } ) ;
214+ const subscription = this . plugin . metadataManager . subscribe (
215+ uuid ,
216+ signal ,
217+ {
218+ filePath : filePath ,
219+ metadataPath : metadataPath ,
220+ listenToChildren : listenToChildren ,
221+ boundToLocalScope : false ,
222+ } ,
223+ onDelete ?? ( ( ) : void => { } ) ,
224+ ) ;
155225
156226 return ( ) => {
157227 subscription . unsubscribe ( ) ;
158228 } ;
159229 }
160230
231+ /**
232+ * Creates an editable table.
233+ *
234+ * @param containerEl the container to mount the table to
235+ * @param filePath the file path that the table is in or an empty string if it is not in a file
236+ * @param component component for lifecycle management
237+ * @param bindTarget the bind target of the table, it will be available to all input fields in the columns of the table as local scope
238+ * @param tableHead the head of the table
239+ * @param columns the columns of the table
240+ */
161241 public createTable (
162242 containerEl : HTMLElement ,
163243 filePath : string ,
@@ -166,7 +246,16 @@ export class API implements IAPI {
166246 tableHead : string [ ] ,
167247 columns : ( UnvalidatedInputFieldDeclaration | UnvalidatedViewFieldDeclaration ) [ ] ,
168248 ) : MetaBindTable {
169- const table = new MetaBindTable ( containerEl , RenderChildType . INLINE , this . plugin , filePath , getUUID ( ) , bindTarget , tableHead , columns ) ;
249+ const table = new MetaBindTable (
250+ containerEl ,
251+ RenderChildType . INLINE ,
252+ this . plugin ,
253+ filePath ,
254+ getUUID ( ) ,
255+ bindTarget ,
256+ tableHead ,
257+ columns ,
258+ ) ;
170259 component . addChild ( table ) ;
171260
172261 return table ;
0 commit comments