Skip to content

Commit 00d1f47

Browse files
Merge branch 'master' into master
2 parents 06a41fb + 6e034d5 commit 00d1f47

File tree

19 files changed

+138
-81
lines changed

19 files changed

+138
-81
lines changed

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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/InputFieldMarkdownRenderChild.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { MarkdownRenderChild, TFile } from 'obsidian';
22
import MetaBindPlugin from './main';
3-
import { Logger } from './utils/Logger';
43
import { AbstractInputField } from './inputFields/AbstractInputField';
54
import { InputFieldFactory } from './inputFields/InputFieldFactory';
65
import { InputFieldArgumentType, InputFieldDeclaration, InputFieldDeclarationParser } from './parsers/InputFieldDeclarationParser';
@@ -118,12 +117,14 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
118117
// use this interval to reduce writing operations
119118
async applyValueUpdateQueues(): Promise<void> {
120119
if (this.metadataValueUpdateQueue.length !== 0) {
120+
console.debug(`meta-bind | applying to metadataUpdateQueue to field ${this.uid}`);
121121
await this.applyMetadataValueUpdateQueue();
122122
this.cleanUpUpdateQueues();
123123
return;
124124
}
125125

126126
if (this.inputFieldValueUpdateQueue.length !== 0) {
127+
console.debug(`meta-bind | applying to inputFieldValueUpdateQueue to field ${this.uid}`);
127128
await this.applyInputFieldValueUpdateQueue();
128129
this.cleanUpUpdateQueues();
129130
return;
@@ -163,7 +164,6 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
163164
value = this.inputField.getDefaultValue();
164165
}
165166

166-
Logger.logDebug(`updating input field ${this.uid} to`, value);
167167
this.inputField.setValue(value);
168168
} else {
169169
throw new MetaBindInternalError(`cannot apply inputFieldValueUpdateQueue to inputField ${this.uid}, inputFieldValueUpdateQueue is empty`);
@@ -177,19 +177,23 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
177177

178178
pushToMetadataValueUpdateQueue(value: any): void {
179179
if (this.inputFieldDeclaration?.isBound) {
180+
console.debug(`meta-bind | pushed value ${value} (typeof ${typeof value}) to metadataUpdateQueue on field ${this.uid}`);
180181
this.metadataValueUpdateQueue.push(value);
181182
}
182183
}
183184

184185
pushToInputFieldValueUpdateQueue(value: any): void {
185186
if (!this.inputField?.isEqualValue(value)) {
187+
console.debug(`meta-bind | pushed value ${value} (typeof ${typeof value}) to inputFieldValueUpdateQueue on field ${this.uid}`);
186188
this.inputFieldValueUpdateQueue.push(value);
187189
}
188190
}
189191

190192
getInitialValue(): any | undefined {
191193
if (this.inputFieldDeclaration?.isBound && this.bindTargetMetadataField) {
192-
return traverseObject(this.bindTargetMetadataField, this.metaData) ?? this.inputField?.getDefaultValue();
194+
const value = traverseObject(this.bindTargetMetadataField, this.metaData);
195+
console.debug(`meta-bind | setting initial value to ${value} (typeof ${typeof value}) for input field ${this.uid}`);
196+
return value ?? this.inputField?.getDefaultValue();
193197
}
194198
}
195199

@@ -206,7 +210,7 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
206210
}
207211

208212
async onload(): Promise<void> {
209-
Logger.logDebug('load', this);
213+
console.debug('meta-bind | load inputFieldMarkdownRenderChild', this);
210214

211215
this.metaData = await this.metaData;
212216

@@ -246,13 +250,12 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
246250
}
247251

248252
onunload(): void {
249-
Logger.logDebug('unload', this);
253+
console.debug('meta-bind | unload inputFieldMarkdownRenderChild', this);
250254

251255
this.plugin.unregisterInputFieldMarkdownRenderChild(this);
252256

253257
super.onunload();
254258

255-
//console.log('unload', this);
256259
window.clearInterval(this.limitInterval);
257260
}
258261
}

src/inputFieldArguments/OptionInputFieldArgument.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { AbstractInputFieldArgument } from './AbstractInputFieldArgument';
22
import { InputFieldArgumentType, InputFieldType } from '../parsers/InputFieldDeclarationParser';
3-
import { MetaBindParsingError } from '../utils/Utils';
43

54
export class OptionInputFieldArgument extends AbstractInputFieldArgument {
65
identifier: InputFieldArgumentType = InputFieldArgumentType.OPTION;

src/inputFieldArguments/TitleInputFieldArgument.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { AbstractInputFieldArgument } from './AbstractInputFieldArgument';
22
import { InputFieldArgumentType, InputFieldType } from '../parsers/InputFieldDeclarationParser';
3-
import { MetaBindParsingError } from '../utils/Utils';
43

54
export class TitleInputFieldArgument extends AbstractInputFieldArgument {
65
identifier: InputFieldArgumentType = InputFieldArgumentType.TITLE;

src/inputFields/DateInputField.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AbstractInputField } from './AbstractInputField';
22
import { DropdownComponent, moment, TextComponent } from 'obsidian';
33
import { InputFieldMarkdownRenderChild } from '../InputFieldMarkdownRenderChild';
44
import { DateParser } from '../parsers/DateParser';
5-
import { MetaBindInternalError } from '../utils/Utils';
5+
import { MetaBindBindValueError, MetaBindInternalError } from '../utils/Utils';
66

77
export class DateInputField extends AbstractInputField {
88
container: HTMLDivElement | undefined;
@@ -62,7 +62,11 @@ export class DateInputField extends AbstractInputField {
6262
throw new MetaBindInternalError('date input hour component is undefined');
6363
}
6464

65-
this.date = DateParser.parse(value) ?? DateParser.getDefaultDate();
65+
this.date = DateParser.parse(value);
66+
if (!this.date) {
67+
console.warn(new MetaBindBindValueError(`invalid value \'${value}\' at dateInputField ${this.inputFieldMarkdownRenderChild.uid}`));
68+
this.date = DateParser.getDefaultDate();
69+
}
6670

6771
if (!this.date.isValid()) {
6872
this.date = DateParser.getDefaultDate();
@@ -84,6 +88,8 @@ export class DateInputField extends AbstractInputField {
8488
}
8589

8690
public render(container: HTMLDivElement): void {
91+
console.debug(`meta-bind | render dateInputField ${this.inputFieldMarkdownRenderChild.uid}`);
92+
8793
this.date = DateParser.parse(this.inputFieldMarkdownRenderChild.getInitialValue()) ?? DateParser.getDefaultDate();
8894
if (!this.date.isValid()) {
8995
this.date = DateParser.getDefaultDate();

src/inputFields/DatePicker/DatePickerInputField.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { InputFieldMarkdownRenderChild } from '../../InputFieldMarkdownRenderChild';
22
import { AbstractInputField } from '../AbstractInputField';
3-
import { isTruthy, MetaBindInternalError } from '../../utils/Utils';
3+
import { MetaBindInternalError } from '../../utils/Utils';
44
import DatePicker from './DatePicker.svelte';
55
import { moment } from 'obsidian';
66
import { DateParser } from '../../parsers/DateParser';
@@ -58,6 +58,8 @@ export class DatePickerInputField extends AbstractInputField {
5858
}
5959

6060
render(container: HTMLDivElement): void {
61+
console.debug(`meta-bind | render datePickerInputField ${this.inputFieldMarkdownRenderChild.uid}`);
62+
6163
this.container = container;
6264

6365
this.date = DateParser.parse(this.inputFieldMarkdownRenderChild.getInitialValue()) ?? DateParser.getDefaultDate();

src/inputFields/InputFieldFactory.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { MetaBindParsingError } from '../utils/Utils';
1111
import { AbstractInputField } from './AbstractInputField';
1212
import { InputFieldType } from '../parsers/InputFieldDeclarationParser';
1313
import { DatePickerInputField } from './DatePicker/DatePickerInputField';
14+
import { NumberInputField } from './NumberInputField';
1415

1516
export class InputFieldFactory {
1617
static allowCodeBlockMap: Record<string, { codeBlock: boolean; inlineCodeBlock: boolean }> = {
@@ -50,6 +51,10 @@ export class InputFieldFactory {
5051
codeBlock: DatePickerInputField.allowCodeBlock,
5152
inlineCodeBlock: DatePickerInputField.allowInlineCodeBlock,
5253
},
54+
[InputFieldType.NUMBER]: {
55+
codeBlock: NumberInputField.allowCodeBlock,
56+
inlineCodeBlock: NumberInputField.allowInlineCodeBlock,
57+
},
5358
};
5459

5560
static createInputField(
@@ -83,6 +88,9 @@ export class InputFieldFactory {
8388
} else if (inputFieldType === InputFieldType.DATE_PICKER) {
8489
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
8590
return new DatePickerInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
91+
} else if (inputFieldType === InputFieldType.NUMBER) {
92+
InputFieldFactory.checkInputFieldMarkdownRenderChildTypeAllowed(inputFieldType, args.type);
93+
return new NumberInputField(args.inputFieldMarkdownRenderChild, args.onValueChanged);
8694
}
8795

8896
return undefined;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { AbstractInputField } from './AbstractInputField';
2+
import { TextComponent } from 'obsidian';
3+
import { MetaBindBindValueError, MetaBindInternalError, numberToString } from '../utils/Utils';
4+
5+
export class NumberInputField extends AbstractInputField {
6+
numberComponent: TextComponent | undefined;
7+
8+
getValue(): number {
9+
if (!this.numberComponent) {
10+
throw new MetaBindInternalError('number input component is undefined');
11+
}
12+
const value = parseFloat(this.numberComponent.getValue());
13+
return isNaN(value) ? 0 : value;
14+
}
15+
16+
setValue(value: any): void {
17+
if (!this.numberComponent) {
18+
throw new MetaBindInternalError('number input component is undefined');
19+
}
20+
21+
if (value != null && (typeof value == 'number' || typeof value == 'string')) {
22+
this.numberComponent.setValue(numberToString(value));
23+
} else {
24+
console.warn(new MetaBindBindValueError(`invalid value \'${value}\' at numberInputField ${this.inputFieldMarkdownRenderChild.uid}`));
25+
this.numberComponent.setValue(this.getDefaultValue());
26+
}
27+
}
28+
29+
isEqualValue(value: any): boolean {
30+
return this.getValue() == value;
31+
}
32+
33+
getDefaultValue(): any {
34+
return 0;
35+
}
36+
37+
getHtmlElement(): HTMLElement {
38+
if (!this.numberComponent) {
39+
throw new MetaBindInternalError('number input component is undefined');
40+
}
41+
42+
return this.numberComponent.inputEl;
43+
}
44+
45+
render(container: HTMLDivElement): void {
46+
console.debug(`meta-bind | render numberInputField ${this.inputFieldMarkdownRenderChild.uid}`);
47+
48+
const component = new TextComponent(container);
49+
component.inputEl.type = 'number';
50+
component.setValue(numberToString(this.inputFieldMarkdownRenderChild.getInitialValue()));
51+
component.onChange(value => {
52+
const n = parseFloat(value);
53+
this.onValueChange(isNaN(n) ? 0 : n);
54+
});
55+
this.numberComponent = component;
56+
}
57+
}

src/inputFields/SelectInputField.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export class SelectInputField extends AbstractInputField {
5252
}
5353

5454
render(container: HTMLDivElement): void {
55+
console.debug(`meta-bind | render selectInputField ${this.inputFieldMarkdownRenderChild.uid}`);
56+
5557
container.addClass('meta-bind-plugin-select-input-bg');
5658
this.container = container;
5759

src/inputFields/SliderInputField.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { AbstractInputField } from './AbstractInputField';
22
import { SliderComponent } from 'obsidian';
3-
import { Logger } from '../utils/Logger';
43
import { InputFieldMarkdownRenderChild } from '../InputFieldMarkdownRenderChild';
5-
import { MetaBindInternalError } from '../utils/Utils';
4+
import { MetaBindBindValueError, MetaBindInternalError } from '../utils/Utils';
65
import { InputFieldArgumentType } from '../parsers/InputFieldDeclarationParser';
76

87
export class SliderInputField extends AbstractInputField {
@@ -16,7 +15,7 @@ export class SliderInputField extends AbstractInputField {
1615
this.maxValue = inputFieldMarkdownRenderChild.getArgument(InputFieldArgumentType.MAX_VALUE)?.value ?? 100;
1716
}
1817

19-
getValue(): any {
18+
getValue(): number {
2019
if (!this.sliderComponent) {
2120
throw new MetaBindInternalError('slider input component is undefined');
2221
}
@@ -34,7 +33,7 @@ export class SliderInputField extends AbstractInputField {
3433
this.sliderComponent.setValue(value);
3534
}
3635
} else {
37-
Logger.logWarning(`can not set value of slider to \'${value}\'`);
36+
console.warn(new MetaBindBindValueError(`invalid value \'${value}\' at sliderInputField ${this.inputFieldMarkdownRenderChild.uid}`));
3837
this.sliderComponent.setValue(this.getDefaultValue());
3938
}
4039
}
@@ -56,6 +55,8 @@ export class SliderInputField extends AbstractInputField {
5655
}
5756

5857
render(container: HTMLDivElement): void {
58+
console.debug(`meta-bind | render slider ${this.inputFieldMarkdownRenderChild.uid}`);
59+
5960
container.removeClass('meta-bind-plugin-input-wrapper');
6061
container.addClass('meta-bind-plugin-flex-input-wrapper');
6162

0 commit comments

Comments
 (0)