Skip to content

Commit ee6f865

Browse files
committed
templates
arguments need a rework
1 parent 675d3aa commit ee6f865

File tree

6 files changed

+111
-40
lines changed

6 files changed

+111
-40
lines changed

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

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

exampleVault/other note.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ select: option b
44
multi-select:
55
- option b
66
- option c
7-
date: Friday, September 10th 2004
7+
date: Friday, October 15th 2004
88
time: 19:20
99
---

src/inputFields/DatePicker/Calender.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import {getDateRows, getWeekDays, uuid} from './DatePickerInputSvelteHelpers.js';
33
import {createEventDispatcher} from 'svelte';
44
import {moment} from 'obsidian';
5+
import type { Moment } from 'moment';
56
67
const dispatch = createEventDispatcher();
78
89
// props
9-
export let selectedDate: moment.Moment;
10+
export let selectedDate: Moment;
1011
export let month: number;
1112
export let year: number;
1213

src/inputFields/DatePicker/DatePicker.svelte

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
import Calender from './Calender.svelte';
44
import {getMonthName} from './DatePickerInputSvelteHelpers.js';
55
import {moment} from 'obsidian';
6+
import type { Moment } from 'moment';
67
import Icon from './Icon.svelte';
78
89
const dispatch = createEventDispatcher();
910
10-
// props
11-
export let selectedDate: moment.Moment = moment();
11+
export let selectedDate: Moment = moment();
1212
export let dateFormat: string = 'dddd, MMMM Do YYYY';
13-
export let dateChangeCallback: (date: moment.Moment) => void;
13+
export let dateChangeCallback: (date: Moment) => void;
1414
15-
// state
1615
let date: number;
1716
let month: number;
1817
let year: number;
@@ -26,12 +25,7 @@
2625
year = selectedDate.year();
2726
}
2827
29-
// handlers
30-
function onFocus() {
31-
showDatePicker = true;
32-
}
33-
34-
function next() {
28+
function nextMonth(): void {
3529
if (month === 11) {
3630
month = 0;
3731
year += 1;
@@ -40,7 +34,7 @@
4034
month = month + 1;
4135
}
4236
43-
function prev() {
37+
function prevMonth(): void {
4438
if (month === 0) {
4539
month = 11;
4640
year -= 1;
@@ -49,15 +43,15 @@
4943
month -= 1;
5044
}
5145
52-
function yearChange(value: any) {
46+
function changeYear(value: any): void {
5347
const v = value.target.value;
5448
const vNum = Number.parseInt(v);
5549
if (!Number.isNaN(vNum)) {
5650
year = vNum;
5751
}
5852
}
5953
60-
function onDateChange(d: { detail: moment.Moment }) {
54+
function onDateChange(d: { detail: Moment }): void {
6155
showDatePicker = false;
6256
selectedDate = d.detail;
6357
dateChangeCallback(d.detail);
@@ -142,13 +136,13 @@
142136
<div class="date-picker-close-layer" on:click={() => showDatePicker = false}></div>
143137
<div class="date-picker">
144138
<div class="date-picker-header">
145-
<button class="month-switch-button" on:click={prev}>Prev</button>
139+
<button class="month-switch-button" on:click={prevMonth}>Prev</button>
146140
<div class="date-picker-header-text">
147141
<span class="date-picker-header-text-month">{getMonthName(month)}</span>
148142
<input class="date-picker-header-text-year" type="number" value="{year.toString()}"
149-
on:input="{yearChange}">
143+
on:input="{changeYear}">
150144
</div>
151-
<button class="month-switch-button" on:click={next}>Next</button>
145+
<button class="month-switch-button" on:click={nextMonth}>Next</button>
152146
</div>
153147
<Calender
154148
on:dateChange={onDateChange}

src/parsers/InputFieldDeclarationParser.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ export class InputFieldDeclarationParser {
3939
InputFieldDeclarationParser.curlyBracesPair,
4040
];
4141

42+
static templates: Record<string, InputFieldDeclaration> = {};
4243

43-
static parse(fullDeclaration: string): InputFieldDeclaration {
44-
const inputFieldDeclaration: InputFieldDeclaration = {} as InputFieldDeclaration;
44+
45+
static parse(fullDeclaration: string, templateName?: string): InputFieldDeclaration {
46+
let inputFieldDeclaration: InputFieldDeclaration = {} as InputFieldDeclaration;
4547

4648
// declaration
4749
inputFieldDeclaration.fullDeclaration = fullDeclaration;
@@ -59,9 +61,7 @@ export class InputFieldDeclarationParser {
5961
// input field type
6062
const inputFieldTypeString = ParserUtils.removeInBetween(inputFieldTypeWithArguments, InputFieldDeclarationParser.roundBracesPair);
6163
inputFieldDeclaration.inputFieldType = InputFieldDeclarationParser.getInputFieldType(inputFieldTypeString);
62-
if (inputFieldDeclaration.inputFieldType === InputFieldType.INVALID) {
63-
throw new MetaBindParsingError(`unknown input field type \'${inputFieldTypeString}\'`);
64-
}
64+
6565
// arguments
6666
const inputFieldArgumentsString: string = ParserUtils.getInBetween(inputFieldTypeWithArguments, InputFieldDeclarationParser.roundBracesPair) as string;
6767
// console.log(inputFieldArgumentsString);
@@ -70,11 +70,41 @@ export class InputFieldDeclarationParser {
7070
} else {
7171
inputFieldDeclaration.arguments = [];
7272
}
73-
// console.log(inputFieldDeclaration.arguments);
73+
74+
75+
if (templateName) {
76+
const template = InputFieldDeclarationParser.templates[templateName];
77+
if (template) {
78+
inputFieldDeclaration.bindTarget = inputFieldDeclaration.bindTarget || template.bindTarget;
79+
inputFieldDeclaration.isBound = inputFieldDeclaration.isBound || template.isBound;
80+
inputFieldDeclaration.inputFieldType = inputFieldDeclaration.inputFieldType === InputFieldType.INVALID ? template.inputFieldType : (inputFieldDeclaration.inputFieldType || template.inputFieldType);
81+
inputFieldDeclaration.arguments = inputFieldDeclaration.arguments.concat(template.arguments);
82+
}
83+
}
84+
85+
if (inputFieldDeclaration.inputFieldType === InputFieldType.INVALID) {
86+
throw new MetaBindParsingError(`unknown input field type \'${inputFieldTypeString}\'`);
87+
}
7488

7589
return inputFieldDeclaration;
7690
}
7791

92+
static parseTemplates(templates: string): void {
93+
let templateDeclarations = ParserUtils.split(templates, '\n', InputFieldDeclarationParser.squareBracesPair);
94+
templateDeclarations.map(x => x.trim()).filter(x => x.length > 0);
95+
96+
for (const templateDeclaration of templateDeclarations) {
97+
const templateDeclarationParts: string[] = ParserUtils.split(templateDeclaration, '->', InputFieldDeclarationParser.squareBracesPair);
98+
templateDeclarationParts.map(x => x.trim());
99+
100+
if (templateDeclarationParts.length === 1) {
101+
throw new MetaBindParsingError('Invalid template syntax');
102+
} else if (templateDeclarationParts.length === 2) {
103+
InputFieldDeclarationParser.templates[templateDeclarationParts[0]] = InputFieldDeclarationParser.parse(templateDeclarationParts[1]);
104+
}
105+
}
106+
}
107+
78108
static parseArguments(inputFieldArgumentsString: string, inputFieldType: InputFieldType): InputFieldArgument[] {
79109
// console.log('inputFieldArgumentsString', inputFieldArgumentsString);
80110
let inputFieldArgumentStrings: string[] = ParserUtils.split(inputFieldArgumentsString, ',', InputFieldDeclarationParser.roundBracesPair);

src/settings/Settings.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface MetaBindPluginSettings {
88
syncInterval: number;
99
maxSyncInterval: number;
1010
minSyncInterval: number;
11+
12+
inputTemplates: string;
1113
}
1214

1315
export const DEFAULT_SETTINGS: MetaBindPluginSettings = {
@@ -17,6 +19,8 @@ export const DEFAULT_SETTINGS: MetaBindPluginSettings = {
1719
syncInterval: 200,
1820
minSyncInterval: 50,
1921
maxSyncInterval: 1000,
22+
23+
inputTemplates: '',
2024
};
2125

2226
export class MetaBindSettingTab extends PluginSettingTab {
@@ -76,6 +80,19 @@ export class MetaBindSettingTab extends PluginSettingTab {
7680
});
7781
});
7882

83+
new Setting(containerEl)
84+
.setName('Templates')
85+
.setDesc(`You can specify templates here, and access them using \`TEMPLATE_INPUT[...]\` in your notes.`)
86+
.addTextArea(cb => {
87+
cb.setValue(this.plugin.settings.inputTemplates);
88+
cb.setPlaceholder('template_name -> INPUT[input_type(argument(value)):bind_target]');
89+
cb.onChange(data => {
90+
this.plugin.settings.inputTemplates = data;
91+
this.plugin.saveSettings();
92+
});
93+
});
94+
95+
7996
new Setting(containerEl)
8097
.setName('Dev Mode')
8198
.setDesc('Enable dev mode. Not recommended unless you want to debug this plugin.')

0 commit comments

Comments
 (0)