Skip to content

Commit 960169c

Browse files
committed
Argument rework
1 parent ee6f865 commit 960169c

19 files changed

+1443
-1021
lines changed

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

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

exampleVault/examples.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ rating: 31
33
title: test title test test
44
completed: false
55
toggle1: true
6-
slider1: 7
6+
slider1: 63
77
slider2: 5
88
text1: Test text
99
text_area1: Test test
1010
date1: 2022-05-28
11-
select: option c
11+
select: option b
1212
multi-select:
1313
- option a
1414
- option c
@@ -127,6 +127,7 @@ asdasd
127127
- `INPUT[text():meta bind/nonExistantFile#title]`
128128
- `INPUT[slider(nonExistantArgument)]`
129129
- `INPUT[select(option(option a),option(option b),option(option c),option(option d)):select]`
130+
130131
Code block error
131132
```meta-bind
132133
INPUT[slider(nonExistantArgument)]

exampleVault/other note.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ select: option b
44
multi-select:
55
- option b
66
- option c
7-
date: Friday, October 15th 2004
7+
date: 2029-05-18
88
time: 19:20
99
---

src/InputFieldMarkdownRenderChild.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ import MetaBindPlugin from './main';
33
import {Logger} from './utils/Logger';
44
import {AbstractInputField} from './inputFields/AbstractInputField';
55
import {InputFieldFactory} from './inputFields/InputFieldFactory';
6-
import {InputFieldArgument, InputFieldDeclaration, InputFieldDeclarationParser} from './parsers/InputFieldDeclarationParser';
6+
import {
7+
InputFieldArgumentType,
8+
InputFieldDeclaration,
9+
InputFieldDeclarationParser
10+
} from './parsers/InputFieldDeclarationParser';
711
import {MetaBindBindTargetError, MetaBindInternalError} from './utils/Utils';
12+
import {AbstractInputFieldArgument} from "./inputFieldArguments/AbstractInputFieldArgument";
13+
import {ClassInputFieldArgument} from "./inputFieldArguments/ClassInputFieldArgument";
814

915
export enum InputFieldMarkdownRenderChildType {
1016
INLINE_CODE_BLOCK,
@@ -146,15 +152,15 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
146152
}
147153
}
148154

149-
getArguments(name: string): InputFieldArgument[] {
155+
getArguments(name: InputFieldArgumentType): AbstractInputFieldArgument[] {
150156
if (!this.inputFieldDeclaration) {
151157
throw new MetaBindInternalError('inputFieldDeclaration is undefined, can not retrieve arguments');
152158
}
153159

154-
return this.inputFieldDeclaration.arguments.filter(x => x.name === name);
160+
return this.inputFieldDeclaration.argumentContainer.arguments.filter(x => x.identifier === name);
155161
}
156162

157-
getArgument(name: string): InputFieldArgument | undefined {
163+
getArgument(name: InputFieldArgumentType): AbstractInputFieldArgument | undefined {
158164
return this.getArguments(name).at(0);
159165
}
160166

@@ -189,9 +195,9 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
189195

190196
this.inputField.render(container);
191197

192-
const classArgument = this.getArguments('class');
193-
if (classArgument) {
194-
this.inputField.getHtmlElement().addClasses(classArgument.map(x => x.value));
198+
const classArguments: ClassInputFieldArgument[] = this.getArguments(InputFieldArgumentType.CLASS);
199+
if (classArguments) {
200+
this.inputField.getHtmlElement().addClasses(classArguments.map(x => x.value).flat());
195201
}
196202

197203

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {InputFieldArgumentType, InputFieldType} from "../parsers/InputFieldDeclarationParser";
2+
3+
export abstract class AbstractInputFieldArgument {
4+
identifier: InputFieldArgumentType = InputFieldArgumentType.INVALID;
5+
allowedInputFields: InputFieldType[] = [];
6+
value: any;
7+
requiresValue: boolean = false;
8+
allowMultiple: boolean = false;
9+
10+
abstract parseValue(valueStr: string): void;
11+
12+
isAllowed(inputFieldType: InputFieldType): boolean {
13+
if (this.allowedInputFields.length === 0) {
14+
return true;
15+
}
16+
17+
return this.allowedInputFields.contains(inputFieldType);
18+
}
19+
20+
getAllowedInputFieldsAsString() {
21+
return this.allowedInputFields.length === 0 ? 'all' : this.allowedInputFields.join(', ');
22+
}
23+
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {AbstractInputFieldArgument} from "./AbstractInputFieldArgument";
2+
import {InputFieldArgumentType, InputFieldType} from "../parsers/InputFieldDeclarationParser";
3+
4+
export class AddLabelsInputFieldArgument extends AbstractInputFieldArgument {
5+
identifier: InputFieldArgumentType = InputFieldArgumentType.ADD_LABELS;
6+
allowedInputFields: InputFieldType[] = [
7+
InputFieldType.SLIDER,
8+
];
9+
value: boolean = true;
10+
requiresValue: boolean = false;
11+
allowMultiple: boolean = false;
12+
13+
parseValue(valueStr: string): void {
14+
this.value = (valueStr.toLowerCase() === 'true');
15+
}
16+
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {AbstractInputFieldArgument} from "./AbstractInputFieldArgument";
2+
import {InputFieldArgumentType, InputFieldType} from "../parsers/InputFieldDeclarationParser";
3+
4+
export class ClassInputFieldArgument extends AbstractInputFieldArgument {
5+
identifier: InputFieldArgumentType = InputFieldArgumentType.CLASS;
6+
allowedInputFields: InputFieldType[] = [];
7+
value: string[] = [];
8+
requiresValue: boolean = true;
9+
allowMultiple: boolean = true;
10+
11+
parseValue(valueStr: string): void {
12+
this.value = valueStr.split(' ');
13+
}
14+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {AbstractInputFieldArgument} from "./AbstractInputFieldArgument";
2+
import {InputFieldArgumentType} from "../parsers/InputFieldDeclarationParser";
3+
import {MetaBindParsingError} from "../utils/Utils";
4+
5+
export class InputFieldArgumentContainer {
6+
arguments: AbstractInputFieldArgument[] = [];
7+
8+
add(argument: AbstractInputFieldArgument): void {
9+
this.arguments.push(argument);
10+
}
11+
12+
validate(): void {
13+
let map: Record<string, number> = {};
14+
for (const inputFieldArgumentType of Object.values(InputFieldArgumentType)) {
15+
map[inputFieldArgumentType] = 0;
16+
}
17+
18+
for (const argument of this.arguments) {
19+
map[argument.identifier] += 1;
20+
if (map[argument.identifier] > 1 && !argument.allowMultiple) {
21+
throw new MetaBindParsingError(`argument \'${argument.identifier}\' does not allow duplicates`)
22+
}
23+
}
24+
}
25+
26+
/**
27+
* Merges two InputFieldArgumentContainers by overriding.
28+
* The arguments form the other container take priority.
29+
*
30+
* @param other
31+
*/
32+
mergeByOverride(other: InputFieldArgumentContainer): InputFieldArgumentContainer {
33+
for (const argument of other.arguments) {
34+
if (!argument.allowMultiple) {
35+
this.arguments = this.arguments.filter(x => x.identifier !== argument.identifier);
36+
}
37+
this.arguments.push(argument);
38+
}
39+
40+
// should not be necessary but it is better to check
41+
this.validate();
42+
43+
return this;
44+
}
45+
46+
/**
47+
* Merges two InputFieldArgumentContainers.
48+
* If there is an argument that does not allow duplicates in both containers this will throw an error.
49+
*
50+
* @param other
51+
*/
52+
mergeByThrow(other: InputFieldArgumentContainer): InputFieldArgumentContainer {
53+
for (const argument of other.arguments) {
54+
if (!argument.allowMultiple) {
55+
if (this.arguments.filter(x => x.identifier === argument.identifier).length > 0) {
56+
throw new MetaBindParsingError('can not merge InputFieldArgumentContainers, since arguments overlap');
57+
}
58+
}
59+
this.arguments.push(argument);
60+
}
61+
62+
// should not be necessary but it is better to check
63+
this.validate();
64+
65+
return this;
66+
}
67+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {ClassInputFieldArgument} from "./ClassInputFieldArgument";
2+
import {MetaBindParsingError} from "../utils/Utils";
3+
import {InputFieldArgumentType} from "../parsers/InputFieldDeclarationParser";
4+
import {AddLabelsInputFieldArgument} from "./AddLabelsInputFieldArgument";
5+
import {MinValueInputFieldArgument} from "./MinValueInputFieldArgument";
6+
import {MaxValueInputFieldArgument} from "./MaxValueInputFieldArgument";
7+
import {OptionInputFieldArgument} from "./OptionInputFieldArgument";
8+
import {TitleInputFieldArgument} from "./TitleInputFieldArgument";
9+
10+
11+
export class InputFieldArgumentFactory {
12+
static createInputFieldArgument(argumentIdentifier: string) {
13+
if (argumentIdentifier === InputFieldArgumentType.CLASS) {
14+
return new ClassInputFieldArgument();
15+
} else if (argumentIdentifier === InputFieldArgumentType.ADD_LABELS) {
16+
return new AddLabelsInputFieldArgument();
17+
} else if (argumentIdentifier === InputFieldArgumentType.MIN_VALUE) {
18+
return new MinValueInputFieldArgument();
19+
} else if (argumentIdentifier === InputFieldArgumentType.MAX_VALUE) {
20+
return new MaxValueInputFieldArgument();
21+
} else if (argumentIdentifier === InputFieldArgumentType.OPTION) {
22+
return new OptionInputFieldArgument();
23+
} else if (argumentIdentifier === InputFieldArgumentType.TITLE) {
24+
return new TitleInputFieldArgument();
25+
} else {
26+
throw new MetaBindParsingError(`unknown argument \'${argumentIdentifier}\'`);
27+
}
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {AbstractInputFieldArgument} from "./AbstractInputFieldArgument";
2+
import {InputFieldArgumentType, InputFieldType} from "../parsers/InputFieldDeclarationParser";
3+
import {MetaBindParsingError} from "../utils/Utils";
4+
5+
export class MaxValueInputFieldArgument extends AbstractInputFieldArgument {
6+
identifier: InputFieldArgumentType = InputFieldArgumentType.MAX_VALUE;
7+
allowedInputFields: InputFieldType[] = [
8+
InputFieldType.SLIDER,
9+
];
10+
value: number = 100;
11+
requiresValue: boolean = true;
12+
allowMultiple: boolean = false;
13+
14+
parseValue(valueStr: string): void {
15+
this.value = Number.parseInt(valueStr);
16+
if (Number.isNaN(this.value)) {
17+
throw new MetaBindParsingError('value of argument \'maxValue\' must be of type number');
18+
}
19+
}
20+
21+
}

0 commit comments

Comments
 (0)