Skip to content

Commit dea5ead

Browse files
committed
small fixes and style changes part 1
1 parent 65c0b6a commit dea5ead

File tree

10 files changed

+163
-168
lines changed

10 files changed

+163
-168
lines changed

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

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

exampleVault/.obsidian/plugins/obsidian-meta-bind-plugin/styles.css

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
:root {
2+
--meta-bind-plugin-border-width: 1px;
3+
--meta-bind-plugin-border-radius: 5px;
4+
--meta-bind-plugin-slider-spacing: 5px;
5+
--meta-bind-plugin-select-element-border-width: 5px;
6+
--meta-bind-plugin-date-input-year-input-width: 80px;
7+
}
8+
19
.meta-bind-plugin-input {
210
background-color: transparent !important;
311
}
@@ -16,8 +24,8 @@
1624

1725
.meta-bind-plugin-slider-input {
1826
align-self: center;
19-
margin-left: 5px !important;
20-
margin-right: 5px !important;
27+
margin-left: var(--meta-bind-plugin-slider-spacing) !important;
28+
margin-right: var(--meta-bind-plugin-slider-spacing) !important;
2129
}
2230

2331
.meta-bind-plugin-error {
@@ -32,8 +40,8 @@
3240
.meta-bind-plugin-select-input-bg {
3341
display: flex;
3442
flex-direction: column;
35-
border: 1px solid var(--background-modifier-border);
36-
border-radius: 4px;
43+
border: var(--meta-bind-plugin-border-width) solid var(--background-modifier-border);
44+
border-radius: var(--meta-bind-plugin-border-radius);
3745
}
3846

3947
.meta-bind-plugin-select-input-wrapper {
@@ -51,15 +59,15 @@
5159
cursor: pointer;
5260
padding: 5px;
5361
margin: 5px 0 5px 0;
54-
border-radius: 5px;
55-
border-left: 5px solid transparent;
62+
border-radius: var(--meta-bind-plugin-border-radius);
63+
border-left: var(--meta-bind-plugin-select-element-border-width) solid transparent;
5664
white-space: pre-wrap;
5765
font-size: 16px;
5866
min-width: 200px;
5967
}
6068

6169
.meta-bind-plugin-select-input-element-selected {
62-
border-left: 5px solid var(--interactive-accent) !important;
70+
border-left: var(--meta-bind-plugin-select-element-border-width) solid var(--interactive-accent) !important;
6371
background: var(--background-secondary);
6472
}
6573

@@ -68,7 +76,12 @@
6876
}
6977

7078
.meta-bind-plugin-date-input-year-input[type="number"] {
71-
width: 80px;
79+
width: var(--meta-bind-plugin-date-input-year-input-width);
80+
height: unset;
81+
align-self: stretch;
82+
}
83+
.meta-bind-plugin-date-input-year-input[type="number"]:focus {
84+
width: calc(var(--meta-bind-plugin-date-input-year-input-width) + var(--meta-bind-plugin-border-width));
7285
height: unset;
7386
align-self: stretch;
7487
}
@@ -78,7 +91,7 @@
7891
}
7992

8093
.meta-bind-plugin-input-element-group .meta-bind-plugin-input-element-group-element:first-child {
81-
border-radius: 0.5em 0 0 0.5em;
94+
border-radius: var(--meta-bind-plugin-border-radius) 0 0 var(--meta-bind-plugin-border-radius);
8295
}
8396

8497
.meta-bind-plugin-input-element-group .meta-bind-plugin-input-element-group-element:not(:first-child):not(:last-child):not(:focus) {
@@ -88,18 +101,18 @@
88101

89102
.meta-bind-plugin-input-element-group .meta-bind-plugin-input-element-group-element:not(:first-child):not(:last-child):focus {
90103
border-radius: 0;
91-
margin-left: -1px;
104+
margin-left: calc(var(--meta-bind-plugin-border-width) * -1);
92105
z-index: 10;
93106
}
94107

95108
.meta-bind-plugin-input-element-group .meta-bind-plugin-input-element-group-element:last-child:not(:focus) {
96-
border-radius: 0 0.5em 0.5em 0;
109+
border-radius: 0 var(--meta-bind-plugin-border-radius) var(--meta-bind-plugin-border-radius) 0;
97110
border-left: 0;
98111
}
99112

100113
.meta-bind-plugin-input-element-group .meta-bind-plugin-input-element-group-element:last-child:focus {
101-
border-radius: 0 0.5em 0.5em 0;
102-
margin-left: -1px;
114+
border-radius: 0 var(--meta-bind-plugin-border-radius) var(--meta-bind-plugin-border-radius) 0;
115+
margin-left: calc(var(--meta-bind-plugin-border-width) * -1);
103116
z-index: 10;
104117
}
105118

exampleVault/examples.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ text_area1: Test text area
1010
date1: 06/28/2022
1111
select: option c
1212
multi-select:
13-
- option b
13+
- option a
1414
- option d
1515
time1: 03:11
1616
---
@@ -114,4 +114,9 @@ Lorem ipsum dolor sit amet, `INPUT[date():other note#date]` consectetur adipisci
114114

115115
## Error Messages
116116
- `INPUT[text():meta bind/nonExistantFile#title]`
117-
- `INPUT[select(option(option a),option(option b),option(option c),option(option d)):select]`
117+
- `INPUT[slider(nonExistantArgument)]`
118+
- `INPUT[select(option(option a),option(option b),option(option c),option(option d)):select]`
119+
Code block error
120+
```meta-bind
121+
INPUT[slider(nonExistantArgument)]
122+
```

exampleVault/other note.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
---
22
title: Text in a different note
33
select: option c
4-
multi-select: []
4+
multi-select:
5+
- option b
6+
- option c
57
date: 04/18/2002
68
time: 19:20
79
---

src/InputFieldMarkdownRenderChild.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
3030
constructor(containerEl: HTMLElement, type: InputFieldMarkdownRenderChildType, fullDeclaration: string, plugin: MetaBindPlugin, filePath: string, uid: number) {
3131
super(containerEl);
3232

33-
//console.log(this, 2)
34-
3533
this.error = '';
3634
this.filePath = filePath;
3735
this.uid = uid;
3836
this.plugin = plugin;
3937

4038
this.valueQueue = [];
4139
this.intervalCounter = 0;
42-
this.limitInterval = window.setInterval(() => this.incrementInterval(), this.plugin.settings.syncInterval);
40+
this.limitInterval = window.setInterval(() => this.applyValueQueueToMetadata(), this.plugin.settings.syncInterval);
4341

4442
try {
4543
this.inputFieldDeclaration = InputFieldDeclarationParser.parse(fullDeclaration);
@@ -52,14 +50,12 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
5250
this.inputField = InputFieldFactory.createInputField(this.inputFieldDeclaration.inputFieldType, {
5351
type: type,
5452
inputFieldMarkdownRenderChild: this,
55-
onValueChanged: this.updateMetaData.bind(this),
53+
onValueChanged: this.pushToValueQueue.bind(this),
5654
});
5755
} catch (e) {
5856
this.error = e.message;
5957
Logger.logWarning(e);
6058
}
61-
62-
// console.log(this, 3)
6359
}
6460

6561
parseBindTarget() {
@@ -71,41 +67,41 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
7167

7268
const files = this.plugin.getFilesByName(this.filePath);
7369
if (files.length === 0) {
74-
throw new MetaBindBindTargetError('file not found');
70+
throw new MetaBindBindTargetError('bind target file not found');
7571
} else if (files.length === 1) {
7672
this.bindTargetFile = files[0];
7773
} else {
78-
throw new MetaBindBindTargetError('multiple files found. please specify the file path');
74+
throw new MetaBindBindTargetError('bind target resolves to multiple files; please also specify the file path');
7975
}
8076
} else if (bindTargetParts.length === 2) {
8177
// the bind target is in another file
8278
this.bindTargetMetadataField = bindTargetParts[1];
8379

8480
const files = this.plugin.getFilesByName(bindTargetParts[0]);
8581
if (files.length === 0) {
86-
throw new MetaBindBindTargetError('file not found');
82+
throw new MetaBindBindTargetError('bind target file not found');
8783
} else if (files.length === 1) {
8884
this.bindTargetFile = files[0];
8985
} else {
90-
throw new MetaBindBindTargetError('multiple files found. please specify the file path');
86+
throw new MetaBindBindTargetError('bind target resolves to multiple files; please also specify the file path');
9187
}
9288
} else {
93-
throw new MetaBindBindTargetError('invalid bind target');
89+
throw new MetaBindBindTargetError('bind target may only contain one \'#\' to specify the metadata field');
9490
}
9591

9692

9793
}
9894

9995
// use this interval to reduce writing operations
100-
async incrementInterval() {
96+
async applyValueQueueToMetadata() {
10197
if (this.valueQueue.length > 0) {
10298
// console.log(this.valueQueue.at(-1))
10399
await this.plugin.updateMetaData(this.bindTargetMetadataField, this.valueQueue.at(-1), this.bindTargetFile);
104100
this.valueQueue = [];
105101
}
106102
}
107103

108-
async updateMetaData(value: any) {
104+
async pushToValueQueue(value: any) {
109105
if (this.inputFieldDeclaration.isBound) {
110106
this.valueQueue.push(value);
111107
}

src/inputFields/InputFieldFactory.ts

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,55 @@ import {InputFieldType} from '../parsers/InputFieldDeclarationParser';
1313

1414

1515
export class InputFieldFactory {
16+
static allowCodeBlockMap: Record<string, { codeBlock: boolean, inlineCodeBlock: boolean }> = {
17+
[InputFieldType.TOGGLE]: { codeBlock: ToggleInputField.allowCodeBlock, inlineCodeBlock: ToggleInputField.allowInlineCodeBlock },
18+
[InputFieldType.SLIDER]: { codeBlock: SliderInputField.allowCodeBlock, inlineCodeBlock: SliderInputField.allowInlineCodeBlock },
19+
[InputFieldType.TEXT]: { codeBlock: TextInputField.allowCodeBlock, inlineCodeBlock: TextInputField.allowInlineCodeBlock },
20+
[InputFieldType.TEXT_AREA]: { codeBlock: TextAreaInputField.allowCodeBlock, inlineCodeBlock: TextAreaInputField.allowInlineCodeBlock },
21+
[InputFieldType.SELECT]: { codeBlock: SelectInputField.allowCodeBlock, inlineCodeBlock: SelectInputField.allowInlineCodeBlock },
22+
[InputFieldType.MULTI_SELECT]: { codeBlock: MultiSelectInputField.allowCodeBlock, inlineCodeBlock: MultiSelectInputField.allowInlineCodeBlock },
23+
[InputFieldType.DATE]: { codeBlock: DateInputField.allowCodeBlock, inlineCodeBlock: DateInputField.allowInlineCodeBlock },
24+
[InputFieldType.TIME]: { codeBlock: TimeInputField.allowCodeBlock, inlineCodeBlock: TimeInputField.allowInlineCodeBlock },
25+
}
26+
1627
static createInputField(inputFieldType: InputFieldType, args: { type: InputFieldMarkdownRenderChildType, inputFieldMarkdownRenderChild: InputFieldMarkdownRenderChild, onValueChanged: (value: any) => void | Promise<void> }): AbstractInputField {
1728
if (inputFieldType === InputFieldType.TOGGLE) {
18-
if (args.type === InputFieldMarkdownRenderChildType.CODE_BLOCK && !ToggleInputField.allowCodeBlock) {
19-
throw new MetaBindParsingError(`can not create ${inputFieldType} as code block`);
20-
}
21-
if (args.type === InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK && !ToggleInputField.allowInlineCodeBlock) {
22-
throw new MetaBindParsingError(`can not create ${inputFieldType} as inline code block`);
23-
}
29+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
2430
return new ToggleInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
2531
} else if (inputFieldType === InputFieldType.SLIDER) {
26-
if (args.type === InputFieldMarkdownRenderChildType.CODE_BLOCK && !SliderInputField.allowCodeBlock) {
27-
throw new MetaBindParsingError(`can not create ${inputFieldType} as code block`);
28-
}
29-
if (args.type === InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK && !SliderInputField.allowInlineCodeBlock) {
30-
throw new MetaBindParsingError(`can not create ${inputFieldType} as inline code block`);
31-
}
32+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
3233
return new SliderInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
3334
} else if (inputFieldType === InputFieldType.TEXT) {
34-
if (args.type === InputFieldMarkdownRenderChildType.CODE_BLOCK && !TextInputField.allowCodeBlock) {
35-
throw new MetaBindParsingError(`can not create ${inputFieldType} as code block`);
36-
}
37-
if (args.type === InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK && !TextInputField.allowInlineCodeBlock) {
38-
throw new MetaBindParsingError(`can not create ${inputFieldType} as inline code block`);
39-
}
35+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
4036
return new TextInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
4137
} else if (inputFieldType === InputFieldType.TEXT_AREA) {
42-
if (args.type === InputFieldMarkdownRenderChildType.CODE_BLOCK && !TextAreaInputField.allowCodeBlock) {
43-
throw new MetaBindParsingError(`can not create ${inputFieldType} as code block`);
44-
}
45-
if (args.type === InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK && !TextAreaInputField.allowInlineCodeBlock) {
46-
throw new MetaBindParsingError(`can not create ${inputFieldType} as inline code block`);
47-
}
38+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
4839
return new TextAreaInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
4940
} else if (inputFieldType === InputFieldType.SELECT) {
50-
if (args.type === InputFieldMarkdownRenderChildType.CODE_BLOCK && !SelectInputField.allowCodeBlock) {
51-
throw new MetaBindParsingError(`can not create ${inputFieldType} as code block`);
52-
}
53-
if (args.type === InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK && !SelectInputField.allowInlineCodeBlock) {
54-
throw new MetaBindParsingError(`can not create ${inputFieldType} as inline code block`);
55-
}
41+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
5642
return new SelectInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
5743
} else if (inputFieldType === InputFieldType.MULTI_SELECT) {
58-
if (args.type === InputFieldMarkdownRenderChildType.CODE_BLOCK && !MultiSelectInputField.allowCodeBlock) {
59-
throw new MetaBindParsingError(`can not create ${inputFieldType} as code block`);
60-
}
61-
if (args.type === InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK && !MultiSelectInputField.allowInlineCodeBlock) {
62-
throw new MetaBindParsingError(`can not create ${inputFieldType} as inline code block`);
63-
}
44+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
6445
return new MultiSelectInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
6546
} else if (inputFieldType === InputFieldType.DATE) {
66-
if (args.type === InputFieldMarkdownRenderChildType.CODE_BLOCK && !DateInputField.allowCodeBlock) {
67-
throw new MetaBindParsingError(`can not create ${inputFieldType} as code block`);
68-
}
69-
if (args.type === InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK && !DateInputField.allowInlineCodeBlock) {
70-
throw new MetaBindParsingError(`can not create ${inputFieldType} as inline code block`);
71-
}
47+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
7248
return new DateInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
7349
} else if (inputFieldType === InputFieldType.TIME) {
74-
if (args.type === InputFieldMarkdownRenderChildType.CODE_BLOCK && !TimeInputField.allowCodeBlock) {
75-
throw new MetaBindParsingError(`can not create ${inputFieldType} as code block`);
76-
}
77-
if (args.type === InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK && !TimeInputField.allowInlineCodeBlock) {
78-
throw new MetaBindParsingError(`can not create ${inputFieldType} as inline code block`);
79-
}
50+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
8051
return new TimeInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
8152
}
8253

8354
return null;
8455
}
8556

57+
static checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType: InputFieldType, type: InputFieldMarkdownRenderChildType) {
58+
const allowCodeBlock: { codeBlock: boolean; inlineCodeBlock: boolean } = InputFieldFactory.allowCodeBlockMap[inputFieldType];
59+
if (type === InputFieldMarkdownRenderChildType.CODE_BLOCK && !allowCodeBlock.codeBlock) {
60+
throw new MetaBindParsingError(`\'${inputFieldType}\' is not allowed as code block`);
61+
}
62+
if (type === InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK && !allowCodeBlock.inlineCodeBlock) {
63+
throw new MetaBindParsingError(`\'${inputFieldType}\' is not allowed as inline code block`);
64+
}
65+
}
66+
8667
}

src/inputFields/MultiSelectInputField.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export class MultiSelectInputField extends SelectInputField {
1515

1616
setValue(value: string[]): void {
1717
if (value.length === 0) {
18+
for (const element of this.elements) {
19+
element.setActive(false, false);
20+
}
1821
return;
1922
}
2023

src/main.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ export default class MetaBindPlugin extends Plugin {
3636

3737
this.registerMarkdownCodeBlockProcessor('meta-bind', (source, el, ctx) => {
3838
const codeBlock = el;
39-
// console.log(JSON.stringify(source));
4039
const text = source.replace(/\n/g, '');
41-
// console.log(text);
4240
const isInputField = text.startsWith('INPUT[') && text.endsWith(']');
4341
// console.log(context.sourcePath);
4442
if (isInputField) {

0 commit comments

Comments
 (0)