Skip to content

Commit 7c6888a

Browse files
committed
untested new api
1 parent 6562ac5 commit 7c6888a

22 files changed

+1225
-666
lines changed

exampleVault/examples.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
---
2-
slider1: 7
2+
slider1: 8
33
suggest: test
44
toggle1: false
55
Domestic_tasks:
66
- Lunch 🍲
77
Meditate: 100
88
Slept: 00:00
99
select: option c
10-
nested:
1110
---
1211

1312
## In callouts
@@ -62,22 +61,23 @@ option(option d)
6261
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. `INPUT[date_picker():other note#date]` asdasd asdasdasd
6362

6463
## Templates
65-
- unknown tempalate
66-
- `INPUT[][toggle:toggle1]`
67-
- `INPUT[nonExistantTemplate][toggle:toggle1]`
68-
- `INPUT[toggleTemplate][]`
64+
- `INPUT[][toggle:toggle1]` empty template
65+
- `INPUT[nonExistantTemplate][toggle:toggle1]` unknown template
66+
- `INPUT[toggleTemplate][]`
6967

7068
## Error Messages
7169
- `INPUT[text():meta bind/nonExistantFile#title]`
7270
- `INPUT[slider(nonExistantArgument)]`
7371
- `INPUT[slider]`
7472

75-
- `INPUT[inlineSelect(option(option a),option(option b),option(option c),option(option d)):select]`
73+
- `INPUT[inlineSelect(option(option a),option(option b),option(option c),option(option d):select]`
7674

7775

7876
Lorem ipsum dolor sit amet, `INPUT[text():meta bind/nonExistantFile#title]` consectetur adipiscing elit. Pellentesque sit amet porttitor arcu. Quisque scelerisque dolor augue, et posuere nulla bibendum nec. `INPUT[slider(nonExistantArgument)]` Curabitur sed rhoncus nisl. Maecenas nisi justo, viverra vel tempus vel, hendrerit at metus. `INPUT[select(option(option a),option(option b),option(option c),option(option d)):select]` asdasd asdasdasd
7977

8078
Code block error
8179
```meta-bind
8280
INPUT[slider(nonExistantArgument)]
83-
```
81+
```
82+
83+
`INPUT[inlineSelect(option(a), option(b), option(c)]`

exampleVault/other note.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
tags: test
33
title: test test
44
select: option b
5-
date: Thursday, July 20th 2023
5+
date: Wednesday, August 16th 2023
66
time: 19:20
77
multi-select:
88
- option a

src/api/API.ts

Lines changed: 48 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,86 @@
1-
import { InlineAPI } from './InlineAPI';
21
import { InputFieldMDRC, RenderChildType } from '../renderChildren/InputFieldMDRC';
3-
import { InputFieldArgumentType, InputFieldDeclaration, InputFieldDeclarationParser, InputFieldType } from '../parsers/InputFieldDeclarationParser';
4-
import { ErrorLevel, MetaBindBindTargetError, MetaBindParsingError } from '../utils/errors/MetaBindErrors';
5-
import { isTruthy } from '../utils/Utils';
2+
import { InputFieldDeclaration } from '../parsers/InputFieldDeclarationParser';
63
import { JsViewFieldDeclaration, ViewFieldDeclaration, ViewFieldDeclarationParser } from '../parsers/ViewFieldDeclarationParser';
74
import { BindTargetParser } from '../parsers/BindTargetParser';
85
import { ViewFieldMDRC } from '../renderChildren/ViewFieldMDRC';
96
import { JsViewFieldMDRC } from '../renderChildren/JsViewFieldMDRC';
10-
import { ErrorCollection } from '../utils/errors/ErrorCollection';
117
import MetaBindPlugin from '../main';
12-
import { DeclarationParser, NewInputFieldDeclarationParser } from '../parsers/newInputFieldParser/InputFieldParser';
8+
import { NewInputFieldDeclarationParser, UnvalidatedInputFieldDeclaration } from '../parsers/newInputFieldParser/InputFieldParser';
9+
import { Component, MarkdownPostProcessorContext } from 'obsidian';
1310

1411
export class API {
1512
public plugin: MetaBindPlugin;
16-
public inputFieldParser: InputFieldDeclarationParser;
13+
// public inputFieldParser: InputFieldDeclarationParser;
1714
public newInputFieldParser: NewInputFieldDeclarationParser;
1815
public viewFieldParser: ViewFieldDeclarationParser;
1916
public bindTargetParser: BindTargetParser;
2017

2118
constructor(plugin: MetaBindPlugin) {
2219
this.plugin = plugin;
2320

24-
this.inputFieldParser = new InputFieldDeclarationParser();
21+
// this.inputFieldParser = new InputFieldDeclarationParser();
2522
this.newInputFieldParser = new NewInputFieldDeclarationParser(this.plugin);
2623
this.viewFieldParser = new ViewFieldDeclarationParser();
2724
this.bindTargetParser = new BindTargetParser(this.plugin);
2825
}
2926

30-
public createInlineAPI(filePath: string, container?: HTMLElement): InlineAPI {
31-
return new InlineAPI(this, filePath, container);
32-
}
33-
3427
public createInputField(
35-
declaration: InputFieldDeclaration,
36-
templateName: string | undefined,
37-
renderChildType: RenderChildType,
28+
unvalidatedDeclaration: UnvalidatedInputFieldDeclaration,
29+
renderType: RenderChildType,
3830
filePath: string,
39-
container: HTMLElement
31+
containerEl: HTMLElement,
32+
component: Component | MarkdownPostProcessorContext
4033
): InputFieldMDRC {
41-
if (!Object.values(RenderChildType).contains(renderChildType)) {
42-
throw new MetaBindParsingError(ErrorLevel.CRITICAL, 'failed to create input field', `unknown render child type '${renderChildType}'`);
43-
}
44-
declaration = this.inputFieldParser.parseDeclaration(declaration, undefined, templateName);
45-
return new InputFieldMDRC(container, renderChildType, declaration, this.plugin, filePath, self.crypto.randomUUID());
46-
}
47-
48-
public createInputFieldFromString(fullDeclaration: string, renderType: RenderChildType, filePath: string, container: HTMLElement): InputFieldMDRC {
49-
const declaration: InputFieldDeclaration = this.newInputFieldParser.parseString(fullDeclaration, filePath);
50-
return new InputFieldMDRC(container, renderType, declaration, this.plugin, filePath, self.crypto.randomUUID());
51-
}
34+
const declaration = this.newInputFieldParser.validateDeclaration(unvalidatedDeclaration);
5235

53-
public createViewFieldFromString(fullDeclaration: string, renderType: RenderChildType, filePath: string, container: HTMLElement): ViewFieldMDRC {
54-
const declaration: ViewFieldDeclaration = this.viewFieldParser.parseString(fullDeclaration);
55-
return new ViewFieldMDRC(container, renderType, declaration, this.plugin, filePath, self.crypto.randomUUID());
56-
}
36+
const inputField = new InputFieldMDRC(containerEl, renderType, declaration, this.plugin, filePath, self.crypto.randomUUID());
37+
component.addChild(inputField);
5738

58-
public createJsViewFieldFromString(fullDeclaration: string, renderType: RenderChildType, filePath: string, container: HTMLElement): JsViewFieldMDRC {
59-
const declaration: JsViewFieldDeclaration = this.viewFieldParser.parseJsString(fullDeclaration);
60-
return new JsViewFieldMDRC(container, renderType, declaration, this.plugin, filePath, self.crypto.randomUUID());
39+
return inputField;
6140
}
6241

63-
public createInputFieldDeclaration(
64-
inputFieldType: InputFieldType,
65-
inputFieldArguments?: { type: InputFieldArgumentType; value: string }[]
66-
): InputFieldDeclaration {
67-
if (this.inputFieldParser.getInputFieldType(inputFieldType) === InputFieldType.INVALID) {
68-
throw new MetaBindParsingError(ErrorLevel.CRITICAL, 'failed to create input field declaration', `input field type '${inputFieldType}' is invalid`);
69-
}
42+
public createInputFieldFromString(
43+
fullDeclaration: string,
44+
renderType: RenderChildType,
45+
filePath: string,
46+
containerEl: HTMLElement,
47+
component: Component | MarkdownPostProcessorContext
48+
): InputFieldMDRC {
49+
const declaration: InputFieldDeclaration = this.newInputFieldParser.parseString(fullDeclaration);
7050

71-
const errorCollection = new ErrorCollection('InputFieldDeclaration');
51+
const inputField = new InputFieldMDRC(containerEl, renderType, declaration, this.plugin, filePath, self.crypto.randomUUID());
52+
component.addChild(inputField);
7253

73-
return {
74-
declaration: undefined,
75-
fullDeclaration: undefined,
76-
inputFieldType: inputFieldType,
77-
argumentContainer: this.inputFieldParser.parseArguments(inputFieldType, inputFieldArguments, errorCollection),
78-
isBound: false,
79-
bindTarget: '',
80-
errorCollection: errorCollection,
81-
} as InputFieldDeclaration;
54+
return inputField;
8255
}
8356

84-
public createInputFieldDeclarationFromString(fullDeclaration: string): InputFieldDeclaration {
85-
return this.inputFieldParser.parseString(fullDeclaration);
57+
public createViewFieldFromString(
58+
fullDeclaration: string,
59+
renderType: RenderChildType,
60+
filePath: string,
61+
container: HTMLElement,
62+
component: Component | MarkdownPostProcessorContext
63+
): ViewFieldMDRC {
64+
const declaration: ViewFieldDeclaration = this.viewFieldParser.parseString(fullDeclaration);
65+
66+
const viewField = new ViewFieldMDRC(container, renderType, declaration, this.plugin, filePath, self.crypto.randomUUID());
67+
component.addChild(viewField);
68+
69+
return viewField;
8670
}
8771

88-
public bindInputFieldDeclaration(declaration: InputFieldDeclaration, bindTargetField: string, bindTargetFile?: string): InputFieldDeclaration {
89-
if (bindTargetFile && !bindTargetField) {
90-
throw new MetaBindBindTargetError(
91-
ErrorLevel.ERROR,
92-
'failed to bind input field declaration',
93-
'if a bind target file is specified, a bind target field must also be specified'
94-
);
95-
}
72+
public createJsViewFieldFromString(
73+
fullDeclaration: string,
74+
renderType: RenderChildType,
75+
filePath: string,
76+
containerEl: HTMLElement,
77+
component: Component | MarkdownPostProcessorContext
78+
): JsViewFieldMDRC {
79+
const declaration: JsViewFieldDeclaration = this.viewFieldParser.parseJsString(fullDeclaration);
9680

97-
declaration.isBound = isTruthy(bindTargetField);
98-
declaration.bindTarget = bindTargetFile ? bindTargetFile + '#' + bindTargetField : bindTargetField;
81+
const viewField = new JsViewFieldMDRC(containerEl, renderType, declaration, this.plugin, filePath, self.crypto.randomUUID());
82+
component.addChild(viewField);
9983

100-
return declaration;
84+
return viewField;
10185
}
10286
}

src/api/InlineAPI.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/api/InputFieldAPI.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { InputFieldArgumentType, InputFieldType } from '../parsers/InputFieldDeclarationParser';
2+
import { UnvalidatedInputFieldDeclaration } from '../parsers/newInputFieldParser/InputFieldParser';
3+
import { ErrorCollection } from '../utils/errors/ErrorCollection';
4+
import { API } from './API';
5+
import { useSyncExternalStore } from 'preact/compat';
6+
7+
export class InputFieldAPI {
8+
private readonly api: API;
9+
10+
constructor(api: API) {
11+
this.api = api;
12+
}
13+
14+
public createInputFieldDeclaration(
15+
inputFieldType?: InputFieldType,
16+
inputFieldArguments?: { name: InputFieldArgumentType; value?: string }[]
17+
): UnvalidatedInputFieldDeclaration {
18+
const errorCollection = new ErrorCollection('input field declaration');
19+
20+
const mappedArguments = (inputFieldArguments ?? []).map(x => ({
21+
name: { result: x.name },
22+
value: x.value !== undefined ? { result: x.value } : undefined,
23+
}));
24+
25+
return {
26+
fullDeclaration: '',
27+
inputFieldType: inputFieldType ? { result: inputFieldType } : undefined,
28+
bindTargetFile: undefined,
29+
bindTargetPath: undefined,
30+
arguments: mappedArguments,
31+
errorCollection: errorCollection,
32+
} as UnvalidatedInputFieldDeclaration;
33+
}
34+
35+
public createInputFieldDeclarationFromString(fullDeclaration: string): UnvalidatedInputFieldDeclaration {
36+
return this.api.newInputFieldParser.parseStringWithoutValidation(fullDeclaration);
37+
}
38+
39+
public setType(unvalidatedDeclaration: UnvalidatedInputFieldDeclaration, inputFieldType: InputFieldType): UnvalidatedInputFieldDeclaration {
40+
unvalidatedDeclaration.inputFieldType = { result: inputFieldType };
41+
42+
return unvalidatedDeclaration;
43+
}
44+
45+
public setArguments(
46+
unvalidatedDeclaration: UnvalidatedInputFieldDeclaration,
47+
inputFieldArguments: { name: InputFieldArgumentType; value?: string | undefined }[]
48+
): UnvalidatedInputFieldDeclaration {
49+
unvalidatedDeclaration.arguments = inputFieldArguments.map(x => ({
50+
name: { result: x.name },
51+
value: x.value !== undefined ? { result: x.value } : undefined,
52+
}));
53+
54+
return unvalidatedDeclaration;
55+
}
56+
57+
public addArgument(
58+
unvalidatedDeclaration: UnvalidatedInputFieldDeclaration,
59+
inputFieldArguments: { name: InputFieldArgumentType; value?: string | undefined }[] | { name: InputFieldArgumentType; value?: string | undefined }
60+
): UnvalidatedInputFieldDeclaration {
61+
if (Array.isArray(inputFieldArguments)) {
62+
unvalidatedDeclaration.arguments = unvalidatedDeclaration.arguments.concat(
63+
inputFieldArguments.map(x => ({
64+
name: { result: x.name },
65+
value: x.value !== undefined ? { result: x.value } : undefined,
66+
}))
67+
);
68+
} else {
69+
unvalidatedDeclaration.arguments.push({
70+
name: { result: inputFieldArguments.name },
71+
value: inputFieldArguments.value !== undefined ? { result: inputFieldArguments.value } : undefined,
72+
});
73+
}
74+
75+
return unvalidatedDeclaration;
76+
}
77+
78+
public setBindTargetFile(unvalidatedDeclaration: UnvalidatedInputFieldDeclaration, bindTargetFile: string): UnvalidatedInputFieldDeclaration {
79+
unvalidatedDeclaration.bindTargetFile = { result: bindTargetFile };
80+
81+
return unvalidatedDeclaration;
82+
}
83+
84+
public setBindTargetMetadataField(
85+
unvalidatedDeclaration: UnvalidatedInputFieldDeclaration,
86+
bindTargetMetadataField: string
87+
): UnvalidatedInputFieldDeclaration {
88+
unvalidatedDeclaration.bindTargetPath = { result: bindTargetMetadataField };
89+
90+
return unvalidatedDeclaration;
91+
}
92+
}

src/cm6/Cm6_Widgets.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ export abstract class MarkdownRenderChildWidget<T extends AbstractMDRC> extends
2424
return other.content === this.content;
2525
}
2626

27-
abstract createRenderChild(container: HTMLElement): T;
27+
abstract createRenderChild(container: HTMLElement, component: Component): T;
2828

2929
public toDOM(view: EditorView): HTMLElement {
3030
const div = document.createElement('span');
3131
div.addClass('cm-inline-code');
3232

33-
this.renderChild = this.createRenderChild(div);
34-
this.renderChild.load();
35-
36-
this.parentComponent.addChild(this.renderChild);
33+
this.renderChild = this.createRenderChild(div, this.parentComponent);
3734

3835
return div;
3936
}
@@ -45,14 +42,14 @@ export abstract class MarkdownRenderChildWidget<T extends AbstractMDRC> extends
4542
}
4643

4744
export class ViewFieldWidget extends MarkdownRenderChildWidget<ViewFieldMDRC> {
48-
public createRenderChild(container: HTMLElement): ViewFieldMDRC {
49-
return this.plugin.api.createViewFieldFromString(this.content, RenderChildType.INLINE, this.filePath, container);
45+
public createRenderChild(container: HTMLElement, component: Component): ViewFieldMDRC {
46+
return this.plugin.api.createViewFieldFromString(this.content, RenderChildType.INLINE, this.filePath, container, component);
5047
}
5148
}
5249

5350
export class InputFieldWidget extends MarkdownRenderChildWidget<InputFieldMDRC> {
54-
public createRenderChild(container: HTMLElement): InputFieldMDRC {
55-
return this.plugin.api.createInputFieldFromString(this.content, RenderChildType.INLINE, this.filePath, container);
51+
public createRenderChild(container: HTMLElement, component: Component): InputFieldMDRC {
52+
return this.plugin.api.createInputFieldFromString(this.content, RenderChildType.INLINE, this.filePath, container, component);
5653
}
5754
}
5855

0 commit comments

Comments
 (0)