@@ -6,46 +6,54 @@ import { DateParser } from './parsers/DateParser';
66import { InputFieldArgumentType , InputFieldDeclaration , InputFieldDeclarationParser , InputFieldType } from './parsers/InputFieldDeclarationParser' ;
77import { MetadataManager } from './MetadataManager' ;
88import { MetaBindBindTargetError } from './utils/MetaBindErrors' ;
9+ import { API } from './API' ;
910
1011export default class MetaBindPlugin extends Plugin {
11- // defined in `loadSettings `
12- settings : MetaBindPluginSettings = null ! ;
12+ // @ts -ignore defined in `onload `
13+ settings : MetaBindPluginSettings ;
1314
1415 // @ts -ignore defined in `onload`
1516 activeMarkdownInputFields : InputFieldMarkdownRenderChild [ ] ;
1617
1718 // @ts -ignore defined in `onload`
1819 metadataManager : MetadataManager ;
1920
21+ // @ts -ignore defined in `onload`
22+ api : API ;
23+
2024 async onload ( ) : Promise < void > {
2125 console . log ( `meta-bind | Main >> load` ) ;
2226
2327 await this . loadSettings ( ) ;
2428
29+ this . api = new API ( this ) ;
30+
2531 DateParser . dateFormat = this . settings . preferredDateFormat ;
26- InputFieldDeclarationParser . parseTemplates ( this . settings . inputTemplates ) ;
32+ this . api . parser . parseTemplates ( this . settings . inputTemplates ) ;
2733
2834 this . activeMarkdownInputFields = [ ] ;
2935 this . metadataManager = new MetadataManager ( this ) ;
3036
31- this . registerMarkdownPostProcessor ( ( element , context ) => {
32- const codeBlocks = element . querySelectorAll ( 'code' ) ;
37+ this . registerMarkdownPostProcessor ( ( el , ctx ) => {
38+ const codeBlocks = el . querySelectorAll ( 'code' ) ;
3339 for ( let index = 0 ; index < codeBlocks . length ; index ++ ) {
3440 const codeBlock = codeBlocks . item ( index ) ;
35- const text = codeBlock . innerText ;
36- const isInputField = text . startsWith ( 'INPUT[' ) && text . endsWith ( ']' ) ;
41+ const content = codeBlock . innerText ;
42+ const isInputField = content . startsWith ( 'INPUT[' ) && content . endsWith ( ']' ) ;
3743 if ( isInputField ) {
38- context . addChild ( this . buildInputFieldMarkdownRenderChild ( text , context . sourcePath , codeBlock , InputFieldMarkdownRenderChildType . INLINE_CODE_BLOCK ) ) ;
44+ const inputField = this . api . createInputFieldFromString ( content , InputFieldMarkdownRenderChildType . INLINE_CODE_BLOCK , ctx . sourcePath , codeBlock ) ;
45+ ctx . addChild ( inputField ) ;
3946 }
4047 }
4148 } , 100 ) ;
4249
4350 this . registerMarkdownCodeBlockProcessor ( 'meta-bind' , ( source , el , ctx ) => {
4451 const codeBlock = el ;
45- const text = source . replace ( / \n / g, '' ) ;
46- const isInputField = text . startsWith ( 'INPUT[' ) && text . endsWith ( ']' ) ;
52+ const content = source . replace ( / \n / g, '' ) ;
53+ const isInputField = content . startsWith ( 'INPUT[' ) && content . endsWith ( ']' ) ;
4754 if ( isInputField ) {
48- ctx . addChild ( this . buildInputFieldMarkdownRenderChild ( text , ctx . sourcePath , codeBlock , InputFieldMarkdownRenderChildType . CODE_BLOCK ) ) ;
55+ const inputField = this . api . createInputFieldFromString ( content , InputFieldMarkdownRenderChildType . CODE_BLOCK , ctx . sourcePath , codeBlock ) ;
56+ ctx . addChild ( inputField ) ;
4957 }
5058 } ) ;
5159
@@ -62,20 +70,20 @@ export default class MetaBindPlugin extends Plugin {
6270 *
6371 * @returns The render child produced.
6472 */
65- buildInputFieldMarkdownRenderChild (
66- declaration : string | InputFieldDeclaration ,
67- sourcePath : string ,
68- container : HTMLElement ,
69- renderType : InputFieldMarkdownRenderChildType = InputFieldMarkdownRenderChildType . INLINE_CODE_BLOCK
70- ) : InputFieldMarkdownRenderChild {
71- if ( typeof declaration === 'string' ) {
72- declaration = InputFieldDeclarationParser . parseString ( declaration ) ;
73- } else {
74- declaration = InputFieldDeclarationParser . parseDeclaration ( declaration ) ;
75- }
76-
77- return new InputFieldMarkdownRenderChild ( container , renderType , declaration , this , sourcePath , crypto . randomUUID ( ) ) ;
78- }
73+ // buildInputFieldMarkdownRenderChild(
74+ // declaration: string | InputFieldDeclaration,
75+ // sourcePath: string,
76+ // container: HTMLElement,
77+ // renderType: InputFieldMarkdownRenderChildType = InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK
78+ // ): InputFieldMarkdownRenderChild {
79+ // if (typeof declaration === 'string') {
80+ // declaration = InputFieldDeclarationParser.parseString(declaration);
81+ // } else {
82+ // declaration = InputFieldDeclarationParser.parseDeclaration(declaration);
83+ // }
84+ //
85+ // return new InputFieldMarkdownRenderChild(container, renderType, declaration, this, sourcePath, crypto.randomUUID());
86+ // }
7987
8088 /**
8189 * Helper method to build a declaration from some initial data or a string.
@@ -89,31 +97,31 @@ export default class MetaBindPlugin extends Plugin {
8997 *
9098 * @returns A constructed InputFieldDeclaration.
9199 */
92- buildDeclaration (
93- declarationData : string | InputFieldDeclaration | { } ,
94- inputFieldArguments ?: Record < InputFieldArgumentType , string > | { } ,
95- inputFieldType ?: InputFieldType ,
96- isBound ?: boolean ,
97- bindTarget ?: string ,
98- templateName ?: string
99- ) : InputFieldDeclaration {
100- if ( typeof declarationData === 'string' ) {
101- return InputFieldDeclarationParser . parseString ( declarationData ) ;
102- } else {
103- const declarationBase = declarationData as InputFieldDeclaration ;
104- declarationBase . inputFieldType = inputFieldType ?? declarationBase . inputFieldType ?? InputFieldType . INVALID ;
105- declarationBase . isBound = isBound ?? declarationBase . isBound ?? false ;
106- declarationBase . bindTarget = bindTarget ?? declarationBase . bindTarget ?? undefined ;
107-
108- // if the input field is bound should be determined by `isBound`
109- // `isBound` is true, `bindTarget` must be set
110- if ( declarationBase . isBound && ! declarationBase . bindTarget ) {
111- throw new MetaBindBindTargetError ( 'input field declaration is bound but bind target is undefined' ) ;
112- }
113-
114- return InputFieldDeclarationParser . parseDeclaration ( declarationBase , inputFieldArguments , templateName ) ;
115- }
116- }
100+ // buildDeclaration(
101+ // declarationData: string | InputFieldDeclaration | {},
102+ // inputFieldArguments?: Record<InputFieldArgumentType, string> | {},
103+ // inputFieldType?: InputFieldType,
104+ // isBound?: boolean,
105+ // bindTarget?: string,
106+ // templateName?: string
107+ // ): InputFieldDeclaration {
108+ // if (typeof declarationData === 'string') {
109+ // return InputFieldDeclarationParser.parseString(declarationData);
110+ // } else {
111+ // const declarationBase = declarationData as InputFieldDeclaration;
112+ // declarationBase.inputFieldType = inputFieldType ?? declarationBase.inputFieldType ?? InputFieldType.INVALID;
113+ // declarationBase.isBound = isBound ?? declarationBase.isBound ?? false;
114+ // declarationBase.bindTarget = bindTarget ?? declarationBase.bindTarget ?? undefined;
115+ //
116+ // // if the input field is bound should be determined by `isBound`
117+ // // `isBound` is true, `bindTarget` must be set
118+ // if (declarationBase.isBound && !declarationBase.bindTarget) {
119+ // throw new MetaBindBindTargetError('input field declaration is bound but bind target is undefined');
120+ // }
121+ //
122+ // return InputFieldDeclarationParser.parseDeclaration(declarationBase, inputFieldArguments, templateName);
123+ // }
124+ // }
117125
118126 onunload ( ) : void {
119127 console . log ( `meta-bind | Main >> unload` ) ;
@@ -164,7 +172,7 @@ export default class MetaBindPlugin extends Plugin {
164172 console . log ( `meta-bind | Main >> settings save` ) ;
165173
166174 DateParser . dateFormat = this . settings . preferredDateFormat ;
167- InputFieldDeclarationParser . parseTemplates ( this . settings . inputTemplates ) ;
175+ this . api . parser . parseTemplates ( this . settings . inputTemplates ) ;
168176 await this . saveData ( this . settings ) ;
169177 }
170178}
0 commit comments