Skip to content

Commit 9f6028d

Browse files
committed
implemented #101
1 parent 181d702 commit 9f6028d

File tree

7 files changed

+38
-15
lines changed

7 files changed

+38
-15
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
---
22
select: a
3+
select2: "2"
34
---
45

5-
`INPUT[inlineSelect(option(a), option(b)):select]`
6+
`INPUT[inlineSelect(option(a), option(b), showcase):select]`
67

8+
`INPUT[inlineSelect(option(1, a), option(2, b), showcase):select2]`

exampleVault/Input Fields/Select and Multi Select.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
select: option 2
2+
select: "1"
33
multiSelect:
44
- option 1
55
- option 3
@@ -8,9 +8,9 @@ multiSelect:
88
### Select
99
```meta-bind
1010
INPUT[select(
11-
option(option 1),
12-
option(option 2),
13-
option(option 3),
11+
option(1, option 1),
12+
option(2, option 2),
13+
option(3, option 3),
1414
showcase
1515
):select]
1616
```

src/inputFieldArguments/arguments/OptionInputFieldArgument.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AbstractInputFieldArgument } from '../AbstractInputFieldArgument';
22
import { InputFieldArgumentType, InputFieldType } from '../../parsers/InputFieldDeclarationParser';
3+
import { ErrorLevel, MetaBindArgumentError } from '../../utils/errors/MetaBindErrors';
34

45
export class OptionInputFieldArgument extends AbstractInputFieldArgument {
56
identifier: InputFieldArgumentType = InputFieldArgumentType.OPTION;
@@ -11,10 +12,21 @@ export class OptionInputFieldArgument extends AbstractInputFieldArgument {
1112
InputFieldType.INLINE_SELECT,
1213
];
1314
value: string = '';
15+
name: string = '';
1416
requiresValue: boolean = true;
1517
allowMultiple: boolean = true;
1618

1719
parseValue(valueStr: string): void {
18-
this.value = valueStr;
20+
const valueParts: string[] = valueStr.split(',').map(x => x.trim());
21+
22+
if (valueParts.length === 1) {
23+
this.value = valueParts[0];
24+
this.name = valueParts[0];
25+
} else if (valueParts.length === 2) {
26+
this.value = valueParts[0];
27+
this.name = valueParts[1];
28+
} else {
29+
throw new MetaBindArgumentError(ErrorLevel.WARNING, 'failed to parse option argument value', 'expected there to be either one or two comma seperated values');
30+
}
1931
}
2032
}

src/inputFields/InlineSelectInputField.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import { DropdownComponent } from 'obsidian';
33
import { ErrorLevel, MetaBindInternalError, MetaBindValueError } from '../utils/errors/MetaBindErrors';
44
import { InputFieldMDRC } from '../renderChildren/InputFieldMDRC';
55
import { InputFieldArgumentType } from '../parsers/InputFieldDeclarationParser';
6+
import { OptionInputFieldArgument } from '../inputFieldArguments/arguments/OptionInputFieldArgument';
67

78
export class InlineSelectInputField extends AbstractInputField {
89
static allowBlock: boolean = false;
910
selectComponent: DropdownComponent | undefined;
10-
options: string[];
11+
options: OptionInputFieldArgument[];
1112

1213
constructor(inputFieldMDRC: InputFieldMDRC) {
1314
super(inputFieldMDRC);
1415

15-
this.options = inputFieldMDRC.getArguments(InputFieldArgumentType.OPTION).map(x => x.value) as string[];
16+
this.options = inputFieldMDRC.getArguments(InputFieldArgumentType.OPTION) as OptionInputFieldArgument[];
1617
}
1718

1819
getValue(): string | undefined {
@@ -57,7 +58,7 @@ export class InlineSelectInputField extends AbstractInputField {
5758

5859
const component = new DropdownComponent(container);
5960
for (const option of this.options) {
60-
component.addOption(option, option);
61+
component.addOption(option.value, option.name);
6162
}
6263
component.setValue(this.renderChild.getInitialValue());
6364
component.onChange(this.onValueChange);

src/inputFields/SelectInputField.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { InputFieldArgumentType } from '../parsers/InputFieldDeclarationParser';
55
import { AbstractInputFieldArgument } from '../inputFieldArguments/AbstractInputFieldArgument';
66
import { ErrorLevel, MetaBindInternalError } from '../utils/errors/MetaBindErrors';
77
import { InputFieldMDRC } from '../renderChildren/InputFieldMDRC';
8+
import { OptionInputFieldArgument } from '../inputFieldArguments/arguments/OptionInputFieldArgument';
89

910
export class SelectInputField extends AbstractInputField {
1011
static allowInline: boolean = false;
@@ -59,11 +60,11 @@ export class SelectInputField extends AbstractInputField {
5960
console.debug(`meta-bind | SelectInputField >> render ${this.renderChild.uuid}`);
6061
this.container = container;
6162

62-
const elementArguments: AbstractInputFieldArgument[] = this.renderChild.getArguments(InputFieldArgumentType.OPTION);
63+
const optionArguments: OptionInputFieldArgument[] = this.renderChild.getArguments(InputFieldArgumentType.OPTION) as OptionInputFieldArgument[];
6364

6465
let i = 0;
65-
for (const elementArgument of elementArguments) {
66-
const selectInputFieldElement = new SelectInputFieldElement(elementArgument.value, container, i, this, false);
66+
for (const optionArgument of optionArguments) {
67+
const selectInputFieldElement = new SelectInputFieldElement(optionArgument.value, optionArgument.name, container, i, this, false);
6768

6869
this.elements.push(selectInputFieldElement);
6970

src/inputFields/SelectInputFieldElement.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { SelectInputField } from './SelectInputField';
22

33
export class SelectInputFieldElement {
44
value: string;
5+
name: string;
56
selectInputField: SelectInputField;
67
readonly id: number;
78
element: HTMLDivElement;
@@ -13,8 +14,9 @@ export class SelectInputFieldElement {
1314
private active: boolean;
1415
private highlighted: boolean;
1516

16-
constructor(value: string, parentElement: HTMLElement, id: number, multiSelectInputField: SelectInputField, active: boolean = false) {
17+
constructor(value: string, name: string, parentElement: HTMLElement, id: number, multiSelectInputField: SelectInputField, active: boolean = false) {
1718
this.value = value;
19+
this.name = name;
1820
this.id = id;
1921
this.active = active;
2022
this.highlighted = false;
@@ -91,6 +93,6 @@ export class SelectInputFieldElement {
9193
}
9294

9395
render(): void {
94-
this.element.createEl('div', { text: this.value });
96+
this.element.createEl('div', { text: this.name });
9597
}
9698
}

src/parsers/InputFieldDeclarationParser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,12 @@ export class InputFieldDeclarationParser {
268268
);
269269
continue;
270270
}
271-
inputFieldArgument.parseValue(argument.value);
271+
try {
272+
inputFieldArgument.parseValue(argument.value);
273+
} catch (e) {
274+
errorCollection.add(e);
275+
continue;
276+
}
272277
}
273278

274279
argumentContainer.add(inputFieldArgument);

0 commit comments

Comments
 (0)