Skip to content

Commit 5e03f1f

Browse files
committed
comments fixes part 1
1 parent 054210a commit 5e03f1f

File tree

4 files changed

+81
-126
lines changed

4 files changed

+81
-126
lines changed

exampleVault/.obsidian/plugins/obsidian-meta-bind-plugin/main.js

Lines changed: 29 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/InputFieldMarkdownRenderChild.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
3434
metadataValueUpdateQueue: any[];
3535
inputFieldValueUpdateQueue: any[];
3636

37-
constructor(
38-
containerEl: HTMLElement,
39-
type: InputFieldMarkdownRenderChildType,
40-
declaration: InputFieldDeclaration,
41-
plugin: MetaBindPlugin,
42-
filePath: string,
43-
error?: string
44-
) {
37+
constructor(containerEl: HTMLElement, type: InputFieldMarkdownRenderChildType, declaration: InputFieldDeclaration, plugin: MetaBindPlugin, filePath: string, error?: string) {
4538
super(containerEl);
4639

4740
this.error = error || '';
@@ -54,11 +47,12 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
5447
this.inputFieldValueUpdateQueue = [];
5548
this.intervalCounter = 0;
5649
this.inputFieldDeclaration = declaration;
57-
50+
5851
if (isTruthy(error)) {
5952
console.warn(error);
6053
} else {
61-
this.uid = this.plugin.markDownInputFieldIndex++;
54+
this.uid = this.plugin.markDownInputFieldIndex;
55+
this.plugin.markDownInputFieldIndex += 1;
6256

6357
try {
6458
if (this.inputFieldDeclaration.isBound) {

src/main.ts

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,7 @@ export default class MetaBindPlugin extends Plugin {
3333
const text = codeBlock.innerText;
3434
const isInputField = text.startsWith('INPUT[') && text.endsWith(']');
3535
if (isInputField) {
36-
context.addChild(
37-
this.buildInputFieldMarkdownRenderChild(
38-
text,
39-
context.sourcePath,
40-
codeBlock,
41-
InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK
42-
)
43-
);
36+
context.addChild(this.buildInputFieldMarkdownRenderChild(text, context.sourcePath, codeBlock, InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK));
4437
}
4538
}
4639
});
@@ -50,12 +43,7 @@ export default class MetaBindPlugin extends Plugin {
5043
const text = source.replace(/\n/g, '');
5144
const isInputField = text.startsWith('INPUT[') && text.endsWith(']');
5245
if (isInputField) {
53-
ctx.addChild(this.buildInputFieldMarkdownRenderChild(
54-
text,
55-
ctx.sourcePath,
56-
codeBlock,
57-
InputFieldMarkdownRenderChildType.CODE_BLOCK
58-
));
46+
ctx.addChild(this.buildInputFieldMarkdownRenderChild(text, ctx.sourcePath, codeBlock, InputFieldMarkdownRenderChildType.CODE_BLOCK));
5947
}
6048
});
6149

@@ -70,71 +58,64 @@ export default class MetaBindPlugin extends Plugin {
7058

7159
/**
7260
* Accessable function for building an input field.
73-
*
61+
*
7462
* @param {string|InputFieldDeclaration} declaration The field declaration string or data.
7563
* @param {string} sourcePath The path of the file the element is being inserted into
76-
* @param {HTMLElement} container The element to fill with the input element
64+
* @param {HTMLElement} container The element to fill with the input element
7765
* @param {InputFieldMarkdownRenderChildType} renderType Inline or Code Block
78-
*
66+
*
7967
* @returns The render child produced.
8068
*/
8169
buildInputFieldMarkdownRenderChild(
8270
declaration: string | InputFieldDeclaration,
8371
sourcePath: string,
8472
container: HTMLElement,
8573
renderType: InputFieldMarkdownRenderChildType = InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK
86-
) : InputFieldMarkdownRenderChild {
87-
var error = undefined;
74+
): InputFieldMarkdownRenderChild {
75+
let error = undefined;
8876

8977
try {
9078
if (typeof declaration === 'string') {
9179
declaration = InputFieldDeclarationParser.parseString(declaration);
9280
} else {
9381
declaration = InputFieldDeclarationParser.parseDeclaration(declaration);
9482
}
95-
} catch (error) { }
96-
97-
return new InputFieldMarkdownRenderChild(
98-
container,
99-
renderType,
100-
declaration as InputFieldDeclaration,
101-
this,
102-
sourcePath,
103-
error
104-
);
83+
} catch (error) {
84+
console.warn(error);
85+
}
86+
87+
return new InputFieldMarkdownRenderChild(container, renderType, declaration as InputFieldDeclaration, this, sourcePath, error);
10588
}
10689

10790
/**
10891
* Helper method to build a declaration from some initial data or a string.
109-
*
110-
* @param {string | InputFieldDeclaration | {}} base 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.
111-
* @param {Record<InputFieldArgumentType, string> | {} | undefined } args (Optional) The arguments, indexed by name.
92+
*
93+
* @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.
11295
* @param {InputFieldType | undefined} inputFieldType (Optional) The input field type if not provided in the base object.
11396
* @param {boolean | undefined} isBound (Optional) If the field should try to be bound to a bindTarget.
114-
* @param {Record<InputFieldArgumentType, string> | {} | undefined} args (Optional) The bind target of the field.
115-
* @param { string | undefined} templateName (Optional) A template to use.
116-
*
97+
* @param {string | undefined} bindTarget (Optional) The bind target of the field.
98+
* @param {string | undefined} templateName (Optional) A template to use.
99+
*
117100
* @returns A constructed InputFieldDeclaration.
118101
*/
119102
buildDeclaration(
120-
base: string | InputFieldDeclaration | {},
121-
args?: Record<InputFieldArgumentType, string> | {} ,
103+
declarationData: string | InputFieldDeclaration | {},
104+
inputFieldArguments?: Record<InputFieldArgumentType, string> | {},
122105
inputFieldType?: InputFieldType,
123106
isBound?: boolean,
124107
bindTarget?: string,
125108
templateName?: string
126-
) : InputFieldDeclaration {
127-
if (typeof base === "string") {
128-
return InputFieldDeclarationParser.parseString(base);
109+
): InputFieldDeclaration {
110+
if (typeof declarationData === 'string') {
111+
return InputFieldDeclarationParser.parseString(declarationData);
129112
} else {
130-
var fullBase = base as InputFieldDeclaration;
131-
fullBase = {
132-
...fullBase,
133-
inputFieldType: inputFieldType ?? fullBase.inputFieldType ?? InputFieldType.INVALID,
134-
isBound: isBound ?? fullBase.isBound ?? false ?? isTruthy(bindTarget),
135-
bindTarget: bindTarget ?? fullBase.bindTarget ?? undefined
136-
}
137-
return InputFieldDeclarationParser.parseDeclaration(fullBase, args, templateName);
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;
117+
118+
return InputFieldDeclarationParser.parseDeclaration(fullBase, inputFieldArguments, templateName);
138119
}
139120
}
140121

src/parsers/InputFieldDeclarationParser.ts

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,12 @@ export enum InputFieldArgumentType {
2929
INVALID = 'invalid',
3030
}
3131

32-
/**
33-
* Declaration of an input field.
34-
*/
3532
export interface InputFieldDeclaration {
36-
/**
37-
* The full declaration string
38-
*/
3933
fullDeclaration: string;
40-
41-
/**
42-
* The type of inplut field plus the optional arguments
43-
*/
4434
declaration: string;
45-
46-
/**
47-
* The type of inplut field
48-
*/
4935
inputFieldType: InputFieldType;
50-
51-
/**
52-
* If it's a bound field or not
53-
*/
5436
isBound: boolean;
55-
56-
/**
57-
* The target field to bind.
58-
*/
5937
bindTarget: string;
60-
61-
/**
62-
* The container.
63-
*/
6438
argumentContainer: InputFieldArgumentContainer;
6539
}
6640

@@ -82,56 +56,55 @@ export class InputFieldDeclarationParser {
8256
static templates: Template[] = [];
8357

8458
static parseDeclaration(
85-
input: InputFieldDeclaration,
86-
args: Record<InputFieldArgumentType, string> | {} | undefined | null = undefined,
59+
fullDeclaration: InputFieldDeclaration,
60+
inputFieldArguments: Record<InputFieldArgumentType, string> | {} | undefined | null = undefined,
8761
templateName: string | undefined | null = undefined
8862
): InputFieldDeclaration {
8963
// field type check
90-
input.inputFieldType = InputFieldDeclarationParser.getInputFieldType(input.inputFieldType);
64+
fullDeclaration.inputFieldType = InputFieldDeclarationParser.getInputFieldType(fullDeclaration.inputFieldType);
9165

9266
// template check:
93-
let useTemplate = isTruthy(templateName) && typeof templateName === "string";
67+
let useTemplate = isTruthy(templateName) && typeof templateName === 'string';
9468
if (useTemplate) {
9569
const template = InputFieldDeclarationParser.templates.filter(x => x.identifier === templateName).first()?.template;
9670
if (template) {
97-
input.bindTarget = input.bindTarget || template.bindTarget;
98-
input.isBound = input.isBound || template.isBound;
99-
input.inputFieldType = input.inputFieldType === InputFieldType.INVALID
100-
? template.inputFieldType
101-
: input.inputFieldType || template.inputFieldType;
102-
input.argumentContainer = template.argumentContainer.mergeByOverride(input.argumentContainer);
71+
fullDeclaration.bindTarget = fullDeclaration.bindTarget || template.bindTarget;
72+
fullDeclaration.isBound = fullDeclaration.isBound || template.isBound;
73+
fullDeclaration.inputFieldType =
74+
fullDeclaration.inputFieldType === InputFieldType.INVALID ? template.inputFieldType : fullDeclaration.inputFieldType || template.inputFieldType;
75+
fullDeclaration.argumentContainer = template.argumentContainer.mergeByOverride(fullDeclaration.argumentContainer);
10376
} else {
10477
throw new MetaBindParsingError(`unknown template name \'${templateName}\'`);
10578
}
10679
}
10780

108-
if (input.inputFieldType === InputFieldType.INVALID) {
81+
if (fullDeclaration.inputFieldType === InputFieldType.INVALID) {
10982
throw new MetaBindParsingError(`unknown input field type`);
11083
}
11184

11285
// arguments check:
113-
input.argumentContainer = new InputFieldArgumentContainer();
114-
if (args) {
115-
for (const inputFieldArgumentIdentifier in Object.keys(args)) {
116-
const inputFieldArgument : AbstractInputFieldArgument = InputFieldArgumentFactory.createInputFieldArgument(inputFieldArgumentIdentifier);
86+
fullDeclaration.argumentContainer = new InputFieldArgumentContainer();
87+
if (inputFieldArguments) {
88+
for (const inputFieldArgumentIdentifier in Object.keys(inputFieldArguments)) {
89+
const inputFieldArgument: AbstractInputFieldArgument = InputFieldArgumentFactory.createInputFieldArgument(inputFieldArgumentIdentifier);
11790

118-
if (!inputFieldArgument.isAllowed(input.inputFieldType)) {
91+
if (!inputFieldArgument.isAllowed(fullDeclaration.inputFieldType)) {
11992
throw new MetaBindParsingError(
12093
`argument \'${inputFieldArgumentIdentifier}\' is only applicable to ${inputFieldArgument.getAllowedInputFieldsAsString()} input fields`
12194
);
12295
}
12396

12497
if (inputFieldArgument.requiresValue) {
125-
inputFieldArgument.parseValue((args as Record<InputFieldArgumentType, string>)[inputFieldArgumentIdentifier as InputFieldArgumentType]);
98+
inputFieldArgument.parseValue((inputFieldArguments as Record<InputFieldArgumentType, string>)[inputFieldArgumentIdentifier as InputFieldArgumentType]);
12699
}
127100

128-
input.argumentContainer.add(inputFieldArgument);
101+
fullDeclaration.argumentContainer.add(inputFieldArgument);
129102
}
130103

131-
input.argumentContainer.validate();
104+
fullDeclaration.argumentContainer.validate();
132105
}
133106

134-
return input;
107+
return fullDeclaration;
135108
}
136109

137110
static parseString(fullDeclaration: string): InputFieldDeclaration {

0 commit comments

Comments
 (0)