Skip to content

Commit 6e034d5

Browse files
committed
number input for #27
1 parent 222f0dd commit 6e034d5

20 files changed

+141
-86
lines changed

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

Lines changed: 1 addition & 1 deletion
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/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-meta-bind-plugin",
33
"name": "Meta Bind Plugin",
4-
"version": "0.3.1",
4+
"version": "0.3.2",
55
"minAppVersion": "0.14.0",
66
"description": "This plugin can create input fields inside your notes and bind them to metadata fields.",
77
"author": "Moritz Jung",

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';
@@ -113,12 +112,14 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
113112
// use this interval to reduce writing operations
114113
async applyValueUpdateQueues(): Promise<void> {
115114
if (this.metadataValueUpdateQueue.length !== 0) {
115+
console.debug(`meta-bind | applying to metadataUpdateQueue to field ${this.uid}`);
116116
await this.applyMetadataValueUpdateQueue();
117117
this.cleanUpUpdateQueues();
118118
return;
119119
}
120120

121121
if (this.inputFieldValueUpdateQueue.length !== 0) {
122+
console.debug(`meta-bind | applying to inputFieldValueUpdateQueue to field ${this.uid}`);
122123
await this.applyInputFieldValueUpdateQueue();
123124
this.cleanUpUpdateQueues();
124125
return;
@@ -158,7 +159,6 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
158159
value = this.inputField.getDefaultValue();
159160
}
160161

161-
Logger.logDebug(`updating input field ${this.uid} to`, value);
162162
this.inputField.setValue(value);
163163
} else {
164164
throw new MetaBindInternalError(`cannot apply inputFieldValueUpdateQueue to inputField ${this.uid}, inputFieldValueUpdateQueue is empty`);
@@ -172,19 +172,23 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
172172

173173
pushToMetadataValueUpdateQueue(value: any): void {
174174
if (this.inputFieldDeclaration?.isBound) {
175+
console.debug(`meta-bind | pushed value ${value} (typeof ${typeof value}) to metadataUpdateQueue on field ${this.uid}`);
175176
this.metadataValueUpdateQueue.push(value);
176177
}
177178
}
178179

179180
pushToInputFieldValueUpdateQueue(value: any): void {
180181
if (!this.inputField?.isEqualValue(value)) {
182+
console.debug(`meta-bind | pushed value ${value} (typeof ${typeof value}) to inputFieldValueUpdateQueue on field ${this.uid}`);
181183
this.inputFieldValueUpdateQueue.push(value);
182184
}
183185
}
184186

185187
getInitialValue(): any | undefined {
186188
if (this.inputFieldDeclaration?.isBound && this.bindTargetMetadataField) {
187-
return traverseObject(this.bindTargetMetadataField, this.metaData) ?? this.inputField?.getDefaultValue();
189+
const value = traverseObject(this.bindTargetMetadataField, this.metaData);
190+
console.debug(`meta-bind | setting initial value to ${value} (typeof ${typeof value}) for input field ${this.uid}`);
191+
return value ?? this.inputField?.getDefaultValue();
188192
}
189193
}
190194

@@ -201,7 +205,7 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
201205
}
202206

203207
async onload(): Promise<void> {
204-
Logger.logDebug('load', this);
208+
console.debug('meta-bind | load inputFieldMarkdownRenderChild', this);
205209

206210
this.metaData = await this.metaData;
207211

@@ -241,13 +245,12 @@ export class InputFieldMarkdownRenderChild extends MarkdownRenderChild {
241245
}
242246

243247
onunload(): void {
244-
Logger.logDebug('unload', this);
248+
console.debug('meta-bind | unload inputFieldMarkdownRenderChild', this);
245249

246250
this.plugin.unregisterInputFieldMarkdownRenderChild(this);
247251

248252
super.onunload();
249253

250-
//console.log('unload', this);
251254
window.clearInterval(this.limitInterval);
252255
}
253256
}

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

0 commit comments

Comments
 (0)