Skip to content

Commit b1a4e32

Browse files
committed
update and rename some stuff
1 parent 00d1f47 commit b1a4e32

File tree

5 files changed

+77
-34
lines changed

5 files changed

+77
-34
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules
33
package-lock.json
44
exampleVault
5+
main.js

src/InputFieldMarkdownRenderChild.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import MetaBindPlugin from './main';
33
import { AbstractInputField } from './inputFields/AbstractInputField';
44
import { InputFieldFactory } from './inputFields/InputFieldFactory';
55
import { InputFieldArgumentType, InputFieldDeclaration, InputFieldDeclarationParser } from './parsers/InputFieldDeclarationParser';
6-
import { isTruthy, MetaBindBindTargetError, MetaBindInternalError } from './utils/Utils';
6+
import { isFalsy, isTruthy, MetaBindBindTargetError, MetaBindInternalError } from './utils/Utils';
77
import { AbstractInputFieldArgument } from './inputFieldArguments/AbstractInputFieldArgument';
88
import { ClassInputFieldArgument } from './inputFieldArguments/ClassInputFieldArgument';
99
import { getFrontmatterOfTFile, updateOrInsertFieldInTFile } from '@opd-libs/opd-metadata-lib/lib/API';
@@ -47,12 +47,10 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
4747
this.intervalCounter = 0;
4848
this.inputFieldDeclaration = declaration;
4949

50-
if (isTruthy(error)) {
51-
console.warn(error);
52-
} else {
53-
this.uid = this.plugin.markDownInputFieldIndex;
54-
this.plugin.markDownInputFieldIndex += 1;
50+
this.uid = this.plugin.markDownInputFieldIndex;
51+
this.plugin.markDownInputFieldIndex += 1;
5552

53+
if (!error) {
5654
try {
5755
if (this.inputFieldDeclaration.isBound) {
5856
this.parseBindTarget();
@@ -78,9 +76,9 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
7876
throw new MetaBindInternalError('inputFieldDeclaration is undefined, can not parse bind target');
7977
}
8078

81-
const bindTargetParts = this.inputFieldDeclaration.bindTarget.split('#');
82-
let bindTargetFileName;
83-
let bindTargetMetadataFieldName;
79+
const bindTargetParts: string[] = this.inputFieldDeclaration.bindTarget.split('#');
80+
let bindTargetFileName: string;
81+
let bindTargetMetadataFieldName: string;
8482

8583
if (bindTargetParts.length === 1) {
8684
// the bind target is in the same file

src/inputFieldArguments/InputFieldArgumentFactory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { MaxValueInputFieldArgument } from './MaxValueInputFieldArgument';
77
import { OptionInputFieldArgument } from './OptionInputFieldArgument';
88
import { TitleInputFieldArgument } from './TitleInputFieldArgument';
99
import { AlignRightInputFieldArgument } from './AlignRightInputFieldArgument';
10+
import { AbstractInputFieldArgument } from './AbstractInputFieldArgument';
1011

1112
export class InputFieldArgumentFactory {
12-
static createInputFieldArgument(argumentIdentifier: string) {
13+
static createInputFieldArgument(argumentIdentifier: string): AbstractInputFieldArgument {
1314
if (argumentIdentifier === InputFieldArgumentType.CLASS) {
1415
return new ClassInputFieldArgument();
1516
} else if (argumentIdentifier === InputFieldArgumentType.ADD_LABELS) {

src/main.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { CachedMetadata, MarkdownPostProcessorContext, Plugin, TFile } from 'obsidian';
1+
import { CachedMetadata, editorEditorField, MarkdownPostProcessorContext, Plugin, TFile } from 'obsidian';
22
import { DEFAULT_SETTINGS, MetaBindPluginSettings, MetaBindSettingTab } from './settings/Settings';
33
import { InputFieldMarkdownRenderChild, InputFieldMarkdownRenderChildType } from './InputFieldMarkdownRenderChild';
4-
import { getFileName, isPath, isTruthy, removeFileEnding } from './utils/Utils';
4+
import { getFileName, isPath, isTruthy, MetaBindBindTargetError, removeFileEnding } from './utils/Utils';
55
import { Logger } from './utils/Logger';
66
import { DateParser } from './parsers/DateParser';
77
import { 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

src/parsers/InputFieldDeclarationParser.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export enum InputFieldType {
1515
TIME = 'time',
1616
DATE_PICKER = 'date_picker',
1717
NUMBER = 'number',
18+
1819
INVALID = 'invalid',
1920
}
2021

@@ -31,11 +32,39 @@ export enum InputFieldArgumentType {
3132
}
3233

3334
export interface InputFieldDeclaration {
35+
/**
36+
* The full declaration of the input field including the "INPUT[]".
37+
* e.g.
38+
* INPUT[input_type(argument_name(value)):bind_target]
39+
*/
3440
fullDeclaration: string;
41+
/**
42+
* Trimmed declaration of the input field including without the "INPUT[]".
43+
* e.g.
44+
* input_type(argument_name(value)):bind_target
45+
*/
3546
declaration: string;
47+
/**
48+
* The type of the input field.
49+
* e.g.
50+
* input_type
51+
*/
3652
inputFieldType: InputFieldType;
53+
/**
54+
* Whether the input field is bound.
55+
* e.g.
56+
* true
57+
*/
3758
isBound: boolean;
59+
/**
60+
* The frontmatter field the input field is bound to.
61+
* e.g.
62+
* bind_target
63+
*/
3864
bindTarget: string;
65+
/**
66+
* A collection of the input field arguments.
67+
*/
3968
argumentContainer: InputFieldArgumentContainer;
4069
}
4170

@@ -65,7 +94,7 @@ export class InputFieldDeclarationParser {
6594
fullDeclaration.inputFieldType = InputFieldDeclarationParser.getInputFieldType(fullDeclaration.inputFieldType);
6695

6796
// template check:
68-
let useTemplate = isTruthy(templateName) && typeof templateName === 'string';
97+
let useTemplate: boolean = isTruthy(templateName) && typeof templateName === 'string';
6998
if (useTemplate) {
7099
InputFieldDeclarationParser.applyTemplate(fullDeclaration, templateName);
71100
}
@@ -237,15 +266,20 @@ export class InputFieldDeclarationParser {
237266
return InputFieldType.INVALID;
238267
}
239268

240-
static applyTemplate(fullDeclaration: InputFieldDeclaration, templateName: string | null | undefined) {
269+
static applyTemplate(inputFieldDeclaration: InputFieldDeclaration, templateName: string | null | undefined) {
270+
if (!templateName) {
271+
return;
272+
}
273+
241274
const template = InputFieldDeclarationParser.templates.filter(x => x.identifier === templateName).first()?.template;
242-
if (template) {
243-
fullDeclaration.bindTarget = fullDeclaration.bindTarget || template.bindTarget;
244-
fullDeclaration.isBound = fullDeclaration.isBound || template.isBound;
245-
fullDeclaration.inputFieldType = fullDeclaration.inputFieldType === InputFieldType.INVALID ? template.inputFieldType : fullDeclaration.inputFieldType || template.inputFieldType;
246-
fullDeclaration.argumentContainer = template.argumentContainer.mergeByOverride(fullDeclaration.argumentContainer);
247-
} else {
275+
if (!template) {
248276
throw new MetaBindParsingError(`unknown template name \'${templateName}\'`);
249277
}
278+
279+
inputFieldDeclaration.bindTarget = inputFieldDeclaration.bindTarget || template.bindTarget;
280+
inputFieldDeclaration.isBound = inputFieldDeclaration.isBound || template.isBound;
281+
inputFieldDeclaration.inputFieldType =
282+
(inputFieldDeclaration.inputFieldType === InputFieldType.INVALID ? template.inputFieldType : inputFieldDeclaration.inputFieldType) || template.inputFieldType;
283+
inputFieldDeclaration.argumentContainer = template.argumentContainer.mergeByOverride(inputFieldDeclaration.argumentContainer);
250284
}
251285
}

0 commit comments

Comments
 (0)