Skip to content

Commit 3407179

Browse files
committed
Auto stash before merge of "master" and "origin/master"
1 parent 6189503 commit 3407179

File tree

8 files changed

+305
-586
lines changed

8 files changed

+305
-586
lines changed

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

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

exampleVault/examples.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ rating: 31
55
title: test title test test
66
completed: false
77
toggle1: true
8-
slider1: 37
8+
slider1: 7
99
slider2: 5
1010
text1: Test text
11-
text_area1: Test text area
12-
date1: 06/28/2022
11+
text_area1: Test test
12+
date1: 2022-05-28
1313
select: option c
1414
multi-select:
15-
- option b
16-
- option d
15+
- option a
16+
- option c
1717
time1: 10:17
1818
---
1919

@@ -80,6 +80,15 @@ option(option d)
8080
):multi-select]
8181
```
8282

83+
## In callouts
84+
> quote
85+
> `INPUT[slider(addLabels, minValue(1), maxValue(10)):slider1]`
86+
87+
> [!INFO]
88+
> `INPUT[slider(addLabels, minValue(1), maxValue(10)):slider1]` Slider
89+
90+
> [!INFO]
91+
> `INPUT[slider(addLabels, minValue(1), maxValue(10)):slider1]`
8392
8493
## Linking to a different note
8594
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sit amet porttitor arcu. Quisque scelerisque dolor augue, et posuere nulla bibendum nec. Curabitur sed rhoncus nisl.
@@ -112,7 +121,7 @@ option(option d)
112121
):other note#multi-select]
113122
```
114123

115-
Lorem ipsum dolor sit amet, `INPUT[date():other note#date]` consectetur adipiscing elit. Pellentesque sit amet porttitor arcu. Quisque scelerisque dolor augue, et posuere nulla bibendum nec. Curabitur sed rhoncus nisl. Maecenas nisi justo, viverra vel tempus vel, hendrerit at metus.
124+
Lorem ipsum dolor sit amet, `INPUT[date():other note#date]` consectetur adipiscing elit. Pellentesque sit amet porttitor arcu. Quisque scelerisque dolor augue, et posuere nulla bibendum nec. `INPUT[date():other note#date]` Curabitur sed rhoncus nisl. Maecenas nisi justo, viverra vel tempus vel, hendrerit at metus.
116125

117126
## Error Messages
118127
- `INPUT[text():meta bind/nonExistantFile#title]`

exampleVault/other note.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
title: Text in a different note
3-
select: option c
2+
title: Text in a diffea
3+
select: option b
44
multi-select:
55
- option b
66
- option c
7-
date: 08/08/2002
7+
date: Thursday, June 27th 2030
88
time: 19:20
99
---

src/inputFields/DateInputField.ts

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import {AbstractInputField} from './AbstractInputField';
22
import {DropdownComponent, TextComponent} from 'obsidian';
33
import {InputFieldMarkdownRenderChild} from '../InputFieldMarkdownRenderChild';
4-
import {Date, DateFormat, DateParser} from '../parsers/DateParser';
4+
import {DateParser} from '../parsers/DateParser';
55
import {MetaBindInternalError} from '../utils/Utils';
6+
import {moment} from 'obsidian';
67

78
export class DateInputField extends AbstractInputField {
89
container: HTMLDivElement | undefined;
9-
date: Date;
10+
date: moment.Moment;
1011

1112
months: Record<string, string> = {
12-
'1': 'January',
13-
'2': 'February',
14-
'3': 'March',
15-
'4': 'April',
16-
'5': 'May',
17-
'6': 'June',
18-
'7': 'July',
19-
'8': 'August',
20-
'9': 'September',
21-
'10': 'October',
22-
'11': 'November',
23-
'12': 'December',
13+
'0': 'January',
14+
'1': 'February',
15+
'2': 'March',
16+
'3': 'April',
17+
'4': 'May',
18+
'5': 'June',
19+
'6': 'July',
20+
'7': 'August',
21+
'8': 'September',
22+
'9': 'October',
23+
'10': 'November',
24+
'11': 'December',
2425
};
2526
days: Record<string, string>;
2627

@@ -65,9 +66,9 @@ export class DateInputField extends AbstractInputField {
6566

6667
this.date = DateParser.parse(value) ?? DateParser.getDefaultDate();
6768
// console.log(this.date);
68-
this.monthComponent.setValue(this.date.getMonth().toString());
69-
this.dayComponent.setValue(this.date.getDay().toString());
70-
this.yearComponent.setValue(this.date.getYear().toString());
69+
this.monthComponent.setValue(this.date.month().toString());
70+
this.dayComponent.setValue(this.date.date().toString());
71+
this.yearComponent.setValue(this.date.year().toString());
7172
}
7273

7374
public isEqualValue(value: any): boolean {
@@ -80,40 +81,41 @@ export class DateInputField extends AbstractInputField {
8081

8182
public render(container: HTMLDivElement): void {
8283
this.date = DateParser.parse(this.inputFieldMarkdownRenderChild.getInitialValue()) ?? DateParser.getDefaultDate();
84+
let useUsInputOrder = this.inputFieldMarkdownRenderChild.plugin.settings.useUsDateInputOrder;
8385

8486
container.removeClass('meta-bind-plugin-input-wrapper');
8587
container.addClass('meta-bind-plugin-flex-input-wrapper', 'meta-bind-plugin-input-element-group');
8688

87-
if (DateParser.dateFormat === DateFormat.EU) {
89+
if (!useUsInputOrder) {
8890
this.dayComponent = new DropdownComponent(container);
8991
this.dayComponent.addOptions(this.days);
90-
this.dayComponent.setValue(this.date.getDay().toString());
92+
this.dayComponent.setValue(this.date.date().toString());
9193
this.dayComponent.onChange(this.onDayChange.bind(this));
9294

9395
this.monthComponent = new DropdownComponent(container);
9496
this.monthComponent.addOptions(this.months);
95-
this.monthComponent.setValue(this.date.getMonth().toString());
97+
this.monthComponent.setValue(this.date.month().toString());
9698
this.monthComponent.onChange(this.onMonthChange.bind(this));
9799

98100
this.dayComponent.selectEl.addClass('meta-bind-plugin-input-element-group-element');
99101
this.monthComponent.selectEl.addClass('meta-bind-plugin-input-element-group-element');
100102
} else {
101103
this.monthComponent = new DropdownComponent(container);
102104
this.monthComponent.addOptions(this.months);
103-
this.monthComponent.setValue(this.date.getMonth().toString());
105+
this.monthComponent.setValue(this.date.month().toString());
104106
this.monthComponent.onChange(this.onMonthChange.bind(this));
105107

106108
this.dayComponent = new DropdownComponent(container);
107109
this.dayComponent.addOptions(this.days);
108-
this.dayComponent.setValue(this.date.getDay().toString());
110+
this.dayComponent.setValue(this.date.date().toString());
109111
this.dayComponent.onChange(this.onDayChange.bind(this));
110112

111113
this.dayComponent.selectEl.addClass('meta-bind-plugin-input-element-group-element');
112114
this.monthComponent.selectEl.addClass('meta-bind-plugin-input-element-group-element');
113115
}
114116

115117
this.yearComponent = new TextComponent(container);
116-
this.yearComponent.setValue(this.date.getYear().toString());
118+
this.yearComponent.setValue(this.date.year().toString());
117119
this.yearComponent.onChange(this.onYearChange.bind(this));
118120

119121
this.yearComponent.inputEl.addClass('meta-bind-plugin-date-input-year-input');
@@ -125,19 +127,41 @@ export class DateInputField extends AbstractInputField {
125127
}
126128

127129
private onMonthChange(value: string): void {
128-
// console.log(value);
129-
this.date.setMonthFromString(value);
130+
this.date.month(value);
131+
132+
const clampedDay = this.clampDay(this.date.date());
133+
this.dayComponent?.setValue(clampedDay.toString());
134+
this.date.date(clampedDay);
135+
130136
this.onValueChange(this.getValue());
131137
}
132138

133139
private onDayChange(value: string): void {
134-
this.date.setDayFromString(value);
140+
const day = Number.parseInt(value);
141+
const clampedDay = this.clampDay(day);
142+
if (clampedDay !== day) {
143+
this.dayComponent?.setValue(clampedDay.toString());
144+
}
145+
146+
this.date.date(clampedDay);
135147
this.onValueChange(this.getValue());
136148
}
137149

138150
private onYearChange(value: string): void {
139-
this.date.setYearFromString(value);
151+
const year = Number.parseInt(value);
152+
this.date.year(Number.isNaN(year) ? DateParser.getDefaultYear() : year);
140153
this.onValueChange(this.getValue());
141154
}
142155

156+
private clampDay(day: number): number {
157+
if (Number.isNaN(day)) {
158+
return DateParser.getDefaultDay();
159+
} else if (day < 1) {
160+
return 1;
161+
} else if (day > this.date.daysInMonth()) {
162+
return this.date.daysInMonth();
163+
}
164+
return day;
165+
}
166+
143167
}

src/main.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class MetaBindPlugin extends Plugin {
1818
await this.loadSettings();
1919

2020
Logger.devMode = this.settings.devMode;
21-
DateParser.dateFormat = this.settings.dateFormat;
21+
DateParser.dateFormat = this.settings.preferredDateFormat;
2222

2323
this.activeMarkdownInputFields = [];
2424
this.markDownInputFieldIndex = 0;
@@ -31,7 +31,13 @@ export default class MetaBindPlugin extends Plugin {
3131
const isInputField = text.startsWith('INPUT[') && text.endsWith(']');
3232
// console.log(context.sourcePath);
3333
if (isInputField) {
34-
context.addChild(new InputFieldMarkdownRenderChild(codeBlock, InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK, text, this, context.sourcePath, this.markDownInputFieldIndex));
34+
context.addChild(new InputFieldMarkdownRenderChild(
35+
codeBlock,
36+
InputFieldMarkdownRenderChildType.INLINE_CODE_BLOCK,
37+
text, this,
38+
context.sourcePath,
39+
this.markDownInputFieldIndex
40+
));
3541
this.markDownInputFieldIndex += 1;
3642
}
3743
}
@@ -43,14 +49,20 @@ export default class MetaBindPlugin extends Plugin {
4349
const isInputField = text.startsWith('INPUT[') && text.endsWith(']');
4450
// console.log(context.sourcePath);
4551
if (isInputField) {
46-
ctx.addChild(new InputFieldMarkdownRenderChild(codeBlock, InputFieldMarkdownRenderChildType.CODE_BLOCK, text, this, ctx.sourcePath, this.markDownInputFieldIndex));
52+
ctx.addChild(new InputFieldMarkdownRenderChild(
53+
codeBlock,
54+
InputFieldMarkdownRenderChildType.CODE_BLOCK,
55+
text,
56+
this,
57+
ctx.sourcePath,
58+
this.markDownInputFieldIndex));
4759
this.markDownInputFieldIndex += 1;
4860
}
4961
});
5062

5163
this.registerEvent(this.app.vault.on('modify', async abstractFile => {
5264
if (abstractFile instanceof TFile) {
53-
await this.updateMarkdownInputFieldsOnFileChange(abstractFile as TFile);
65+
await this.updateMarkdownInputFieldsOnFileChange(abstractFile);
5466
}
5567
}));
5668

@@ -97,15 +109,16 @@ export default class MetaBindPlugin extends Plugin {
97109
}
98110

99111
let fileContent: string = await this.app.vault.read(file);
100-
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
101-
fileContent = fileContent.replace(regExp, '');
102112

103-
const metadata: any = await this.getMetaDataForFile(file);
113+
const metadata: any = await this.getMetaDataForFileContent(fileContent);
104114
// console.log(metadata);
105115
if (!metadata) {
106116
return;
107117
}
108118

119+
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
120+
fileContent = fileContent.replace(regExp, '');
121+
109122
metadata[key] = value;
110123
// console.log(metadata);
111124

@@ -118,7 +131,7 @@ export default class MetaBindPlugin extends Plugin {
118131
const fileNameIsPath = isPath(name);
119132
const processedFileName = fileNameIsPath ? removeFileEnding(name) : getFileName(removeFileEnding(name));
120133

121-
const allFiles = this.app.vault.getFiles();
134+
const allFiles = this.app.vault.getMarkdownFiles();
122135
const files: TFile[] = [];
123136
for (const file of allFiles) {
124137
// console.log(removeFileEnding(file.path));
@@ -137,10 +150,18 @@ export default class MetaBindPlugin extends Plugin {
137150
}
138151

139152
async getMetaDataForFile(file: TFile): Promise<any> {
153+
if (!file) {
154+
return undefined;
155+
}
156+
157+
const fileContent: string = await this.app.vault.read(file);
158+
return await this.getMetaDataForFileContent(fileContent);
159+
}
160+
161+
async getMetaDataForFileContent(fileContent: string): Promise<any> {
140162
// Logger.logDebug(`reading metadata for ${file.path}`);
141163
let metadata: any;
142164

143-
const fileContent: string = await this.app.vault.read(file);
144165
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
145166
const frontMatterRegExpResult = regExp.exec(fileContent);
146167
if (!frontMatterRegExpResult) {
@@ -171,7 +192,7 @@ export default class MetaBindPlugin extends Plugin {
171192
}
172193

173194
async saveSettings(): Promise<void> {
174-
DateParser.dateFormat = this.settings.dateFormat;
195+
DateParser.dateFormat = this.settings.preferredDateFormat;
175196
Logger.devMode = this.settings.devMode;
176197
await this.saveData(this.settings);
177198
}

0 commit comments

Comments
 (0)