|
1 | | -import { InlineAPI } from './InlineAPI'; |
2 | 1 | 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'; |
6 | 3 | import { JsViewFieldDeclaration, ViewFieldDeclaration, ViewFieldDeclarationParser } from '../parsers/ViewFieldDeclarationParser'; |
7 | 4 | import { BindTargetParser } from '../parsers/BindTargetParser'; |
8 | 5 | import { ViewFieldMDRC } from '../renderChildren/ViewFieldMDRC'; |
9 | 6 | import { JsViewFieldMDRC } from '../renderChildren/JsViewFieldMDRC'; |
10 | | -import { ErrorCollection } from '../utils/errors/ErrorCollection'; |
11 | 7 | 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'; |
13 | 10 |
|
14 | 11 | export class API { |
15 | 12 | public plugin: MetaBindPlugin; |
16 | | - public inputFieldParser: InputFieldDeclarationParser; |
| 13 | + // public inputFieldParser: InputFieldDeclarationParser; |
17 | 14 | public newInputFieldParser: NewInputFieldDeclarationParser; |
18 | 15 | public viewFieldParser: ViewFieldDeclarationParser; |
19 | 16 | public bindTargetParser: BindTargetParser; |
20 | 17 |
|
21 | 18 | constructor(plugin: MetaBindPlugin) { |
22 | 19 | this.plugin = plugin; |
23 | 20 |
|
24 | | - this.inputFieldParser = new InputFieldDeclarationParser(); |
| 21 | + // this.inputFieldParser = new InputFieldDeclarationParser(); |
25 | 22 | this.newInputFieldParser = new NewInputFieldDeclarationParser(this.plugin); |
26 | 23 | this.viewFieldParser = new ViewFieldDeclarationParser(); |
27 | 24 | this.bindTargetParser = new BindTargetParser(this.plugin); |
28 | 25 | } |
29 | 26 |
|
30 | | - public createInlineAPI(filePath: string, container?: HTMLElement): InlineAPI { |
31 | | - return new InlineAPI(this, filePath, container); |
32 | | - } |
33 | | - |
34 | 27 | public createInputField( |
35 | | - declaration: InputFieldDeclaration, |
36 | | - templateName: string | undefined, |
37 | | - renderChildType: RenderChildType, |
| 28 | + unvalidatedDeclaration: UnvalidatedInputFieldDeclaration, |
| 29 | + renderType: RenderChildType, |
38 | 30 | filePath: string, |
39 | | - container: HTMLElement |
| 31 | + containerEl: HTMLElement, |
| 32 | + component: Component | MarkdownPostProcessorContext |
40 | 33 | ): 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); |
52 | 35 |
|
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); |
57 | 38 |
|
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; |
61 | 40 | } |
62 | 41 |
|
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); |
70 | 50 |
|
71 | | - const errorCollection = new ErrorCollection('InputFieldDeclaration'); |
| 51 | + const inputField = new InputFieldMDRC(containerEl, renderType, declaration, this.plugin, filePath, self.crypto.randomUUID()); |
| 52 | + component.addChild(inputField); |
72 | 53 |
|
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; |
82 | 55 | } |
83 | 56 |
|
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; |
86 | 70 | } |
87 | 71 |
|
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); |
96 | 80 |
|
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); |
99 | 83 |
|
100 | | - return declaration; |
| 84 | + return viewField; |
101 | 85 | } |
102 | 86 | } |
0 commit comments