1- import { CachedMetadata , MarkdownPostProcessorContext , Plugin , TFile } from 'obsidian' ;
1+ import { CachedMetadata , editorEditorField , MarkdownPostProcessorContext , Plugin , TFile } from 'obsidian' ;
22import { DEFAULT_SETTINGS , MetaBindPluginSettings , MetaBindSettingTab } from './settings/Settings' ;
33import { InputFieldMarkdownRenderChild , InputFieldMarkdownRenderChildType } from './InputFieldMarkdownRenderChild' ;
4- import { getFileName , isPath , isTruthy , removeFileEnding } from './utils/Utils' ;
4+ import { getFileName , isPath , isTruthy , MetaBindBindTargetError , removeFileEnding } from './utils/Utils' ;
55import { Logger } from './utils/Logger' ;
66import { DateParser } from './parsers/DateParser' ;
77import { InputFieldArgumentType , InputFieldDeclaration , InputFieldDeclarationParser , InputFieldType } from './parsers/InputFieldDeclarationParser' ;
@@ -57,12 +57,12 @@ export default class MetaBindPlugin extends Plugin {
5757 }
5858
5959 /**
60- * Accessable function for building an input field.
60+ * Accessible function for building an input field.
6161 *
62- * @param {string|InputFieldDeclaration } declaration The field declaration string or data .
63- * @param {string } sourcePath The path of the file the element is being inserted into
64- * @param {HTMLElement } container The element to fill with the input element
65- * @param {InputFieldMarkdownRenderChildType } renderType Inline or Code Block
62+ * @param {string|InputFieldDeclaration } declaration The input field declaration as a string or object .
63+ * @param {string } sourcePath The path of the file the element will be inserted into.
64+ * @param {HTMLElement } container The container element for the input element.
65+ * @param {InputFieldMarkdownRenderChildType } renderType Inline or Code Block.
6666 *
6767 * @returns The render child produced.
6868 */
@@ -72,16 +72,19 @@ export default class MetaBindPlugin extends Plugin {
7272 container : HTMLElement ,
7373 renderType : InputFieldMarkdownRenderChildType = InputFieldMarkdownRenderChildType . INLINE_CODE_BLOCK
7474 ) : InputFieldMarkdownRenderChild {
75- let error = undefined ;
75+ let error : string | undefined ;
7676
7777 try {
7878 if ( typeof declaration === 'string' ) {
7979 declaration = InputFieldDeclarationParser . parseString ( declaration ) ;
8080 } else {
8181 declaration = InputFieldDeclarationParser . parseDeclaration ( declaration ) ;
8282 }
83- } catch ( error ) {
84- console . warn ( error ) ;
83+ } catch ( e : any ) {
84+ if ( e instanceof Error ) {
85+ error = e . message ;
86+ console . warn ( e ) ;
87+ }
8588 }
8689
8790 return new InputFieldMarkdownRenderChild ( container , renderType , declaration as InputFieldDeclaration , this , sourcePath , error ) ;
@@ -91,7 +94,7 @@ export default class MetaBindPlugin extends Plugin {
9194 * Helper method to build a declaration from some initial data or a string.
9295 *
9396 * @param {string | InputFieldDeclaration | {} } declarationData The base declaration data or a string to parse for it. Can also be an empty object with the other arguments provided to fill it.
94- * @param {Record<InputFieldArgumentType, string> | {} | undefined } inputFieldArguments (Optional) The arguments, indexed by name.
97+ * @param {Record<InputFieldArgumentType, string> | {} | undefined } inputFieldArguments (Optional) The input field arguments, indexed by argument name.
9598 * @param {InputFieldType | undefined } inputFieldType (Optional) The input field type if not provided in the base object.
9699 * @param {boolean | undefined } isBound (Optional) If the field should try to be bound to a bindTarget.
97100 * @param {string | undefined } bindTarget (Optional) The bind target of the field.
@@ -110,12 +113,18 @@ export default class MetaBindPlugin extends Plugin {
110113 if ( typeof declarationData === 'string' ) {
111114 return InputFieldDeclarationParser . parseString ( declarationData ) ;
112115 } else {
113- const fullBase = declarationData as InputFieldDeclaration ;
114- fullBase . inputFieldType = inputFieldType ?? fullBase . inputFieldType ?? InputFieldType . INVALID ;
115- fullBase . isBound = isBound ?? fullBase . isBound ?? false ?? isTruthy ( bindTarget ) ;
116- fullBase . bindTarget = bindTarget ?? fullBase . bindTarget ?? undefined ;
116+ const declarationBase = declarationData as InputFieldDeclaration ;
117+ declarationBase . inputFieldType = inputFieldType ?? declarationBase . inputFieldType ?? InputFieldType . INVALID ;
118+ declarationBase . isBound = isBound ?? declarationBase . isBound ?? false ;
119+ declarationBase . bindTarget = bindTarget ?? declarationBase . bindTarget ?? undefined ;
120+
121+ // if the input field is bound should be determined by `isBound`
122+ // `isBound` is true, `bindTarget` must be set
123+ if ( declarationBase . isBound && ! declarationBase . bindTarget ) {
124+ throw new MetaBindBindTargetError ( 'input field declaration is bound but bind target is undefined' ) ;
125+ }
117126
118- return InputFieldDeclarationParser . parseDeclaration ( fullBase , inputFieldArguments , templateName ) ;
127+ return InputFieldDeclarationParser . parseDeclaration ( declarationBase , inputFieldArguments , templateName ) ;
119128 }
120129 }
121130
0 commit comments