Skip to content

Commit 5e04a70

Browse files
committed
refactor towards publish plugin
1 parent a229ba6 commit 5e04a70

File tree

67 files changed

+1101
-774
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1101
-774
lines changed

exampleVault/Examples.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
slider1: 9
2+
slider1: 8
33
suggest: test
44
toggle1: false
55
Domestic_tasks:
@@ -13,7 +13,7 @@ inlineSelect: 0
1313
nested:
1414
object: test
1515
number1: 2
16-
number2: 18
16+
number2: 16
1717
---
1818

1919
## Fields Work Everywhere

src/api/API.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import { ViewFieldFactory } from '../fields/viewFields/ViewFieldFactory';
2626
import { getUUID } from '../utils/Utils';
2727
import { parsePropPath } from '../utils/prop/PropParser';
28-
import { RenderChildType } from '../config/FieldConfigs';
28+
import { type RenderChildType } from '../config/FieldConfigs';
2929
import { type ButtonActionRunner } from '../fields/button/ButtonActionRunner';
3030
import { ButtonManager } from '../fields/button/ButtonManager';
3131
import { type BindTargetDeclaration, BindTargetStorageType } from '../parsers/bindTargetParser/BindTargetDeclaration';
@@ -118,7 +118,7 @@ export class API implements IAPI {
118118

119119
const declaration = this.inputFieldParser.validateDeclaration(unvalidatedDeclaration, filePath, scope);
120120

121-
const inputField = new InputFieldMDRC(containerEl, renderType, declaration, this.plugin, filePath, getUUID());
121+
const inputField = new InputFieldMDRC(this.plugin, filePath, containerEl, renderType, declaration);
122122
component.addChild(inputField);
123123

124124
return inputField;
@@ -158,7 +158,7 @@ export class API implements IAPI {
158158

159159
const declaration: InputFieldDeclaration = this.inputFieldParser.parseString(fullDeclaration, filePath, scope);
160160

161-
const inputField = new InputFieldMDRC(containerEl, renderType, declaration, this.plugin, filePath, getUUID());
161+
const inputField = new InputFieldMDRC(this.plugin, filePath, containerEl, renderType, declaration);
162162
component.addChild(inputField);
163163

164164
return inputField;
@@ -198,7 +198,7 @@ export class API implements IAPI {
198198

199199
const declaration: ViewFieldDeclaration = this.viewFieldParser.parseString(fullDeclaration, filePath, scope);
200200

201-
const viewField = new ViewFieldMDRC(containerEl, renderType, declaration, this.plugin, filePath, getUUID());
201+
const viewField = new ViewFieldMDRC(this.plugin, filePath, containerEl, renderType, declaration);
202202
component.addChild(viewField);
203203

204204
return viewField;
@@ -235,7 +235,7 @@ export class API implements IAPI {
235235

236236
const declaration: JsViewFieldDeclaration = this.viewFieldParser.parseJsString(fullDeclaration, filePath);
237237

238-
const viewField = new JsViewFieldMDRC(containerEl, renderType, declaration, this.plugin, filePath, getUUID());
238+
const viewField = new JsViewFieldMDRC(this.plugin, filePath, containerEl, renderType, declaration);
239239
component.addChild(viewField);
240240

241241
return viewField;
@@ -251,7 +251,7 @@ export class API implements IAPI {
251251
public createExcludedField(containerEl: HTMLElement, filePath: string, component: ComponentLike): ExcludedMDRC {
252252
validateArgs(V_API_createExcludedField, [containerEl, filePath, component]);
253253

254-
const excludedField = new ExcludedMDRC(containerEl, RenderChildType.INLINE, this.plugin, filePath, getUUID());
254+
const excludedField = new ExcludedMDRC(this.plugin, filePath, containerEl);
255255
component.addChild(excludedField);
256256

257257
return excludedField;
@@ -318,16 +318,7 @@ export class API implements IAPI {
318318
): MetaBindTable {
319319
validateArgs(V_API_createTable, [containerEl, filePath, component, bindTarget, tableHead, columns]);
320320

321-
const table = new MetaBindTable(
322-
containerEl,
323-
RenderChildType.INLINE,
324-
this.plugin,
325-
filePath,
326-
getUUID(),
327-
bindTarget,
328-
tableHead,
329-
columns,
330-
);
321+
const table = new MetaBindTable(this.plugin, filePath, containerEl, bindTarget, tableHead, columns);
331322
component.addChild(table);
332323

333324
return table;
@@ -351,7 +342,7 @@ export class API implements IAPI {
351342
return this.createExcludedField(containerEl, filePath, component);
352343
}
353344

354-
const button = new ButtonMDRC(containerEl, fullDeclaration, this.plugin, filePath, getUUID(), false);
345+
const button = new ButtonMDRC(this.plugin, filePath, containerEl, fullDeclaration, false);
355346
component.addChild(button);
356347

357348
return button;
@@ -369,7 +360,7 @@ export class API implements IAPI {
369360
return this.createExcludedField(containerEl, filePath, component);
370361
}
371362

372-
const button = new InlineButtonMDRC(containerEl, fullDeclaration, this.plugin, filePath, getUUID());
363+
const button = new InlineButtonMDRC(this.plugin, filePath, containerEl, fullDeclaration);
373364
component.addChild(button);
374365

375366
return button;

src/api/IAPI.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { type InputFieldFactory } from '../fields/inputFields/InputFieldFactory'
77
import { type ButtonActionRunner } from '../fields/button/ButtonActionRunner';
88
import { type ButtonManager } from '../fields/button/ButtonManager';
99
import { type SyntaxHighlightingAPI } from './SyntaxHighlightingAPI';
10+
import { type ViewFieldFactory } from '../fields/viewFields/ViewFieldFactory';
1011

1112
export interface IAPI {
1213
readonly plugin: IPlugin;
@@ -29,6 +30,7 @@ export interface IAPI {
2930
readonly bindTargetParser: BindTargetParser;
3031

3132
readonly inputFieldFactory: InputFieldFactory;
33+
readonly viewFieldFactory: ViewFieldFactory;
3234

3335
readonly buttonActionRunner: ButtonActionRunner;
3436
readonly buttonManager: ButtonManager;

src/api/internalApi/IInternalAPI.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import { type SuggesterLikeIFP, type SuggesterOption } from '../../fields/inputF
22
import { type MBLiteral } from '../../utils/Literal';
33
import { type ImageSuggesterIPF } from '../../fields/inputFields/fields/ImageSuggester/ImageSuggesterIPF';
44
import { type DatePickerIPF } from '../../fields/inputFields/fields/DatePicker/DatePickerIPF';
5+
import type { ErrorCollection } from '../../utils/errors/ErrorCollection';
6+
import { type IJsRenderer } from '../../fields/viewFields/jsRenderer/IJsRenderer';
7+
8+
export interface ErrorIndicatorProps {
9+
errorCollection: ErrorCollection;
10+
code?: string | undefined;
11+
text?: string | undefined;
12+
errorText?: string | undefined;
13+
warningText?: string | undefined;
14+
}
515

616
export interface IInternalAPI {
717
openTextPromptModal(
@@ -26,13 +36,19 @@ export interface IInternalAPI {
2636

2737
executeCommandById(id: string): boolean;
2838

39+
isJsEngineAvailable(): boolean;
40+
2941
jsEngineRunFile(filePath: string, callingFilePath: string, container?: HTMLElement): Promise<() => void>;
3042

3143
jsEngineRunCode(code: string, callingFilePath: string, container?: HTMLElement): Promise<() => void>;
3244

45+
createJsRenderer(container: HTMLElement, filePath: string, code: string): IJsRenderer;
46+
3347
openFile(filePath: string, callingFilePath: string, newTab: boolean): void;
3448

3549
getFilePathByName(name: string): string | undefined;
3650

3751
showNotice(message: string): void;
52+
53+
createErrorIndicator(element: HTMLElement, props: ErrorIndicatorProps): void;
3854
}

src/api/internalApi/ObsidianAPIAdapter.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type IInternalAPI } from './IInternalAPI';
1+
import { type ErrorIndicatorProps, type IInternalAPI } from './IInternalAPI';
22
import type MetaBindPlugin from '../../main';
33
import { type App, Component, MarkdownRenderer, Notice, TFile } from 'obsidian';
44
import { type DatePickerIPF } from '../../fields/inputFields/fields/DatePicker/DatePickerIPF';
@@ -10,6 +10,9 @@ import { openSuggesterModalForInputField } from '../../fields/inputFields/fields
1010
import { openImageSuggesterModalForInputField } from '../../fields/inputFields/fields/ImageSuggester/ImageSuggesterModalHelper';
1111
import { DatePickerInputModal } from '../../fields/inputFields/fields/DatePicker/DatePickerInputModal';
1212
import { getJsEnginePluginAPI } from '../../utils/ObsUtils';
13+
import ErrorIndicatorComponent from '../../utils/errors/ErrorIndicatorComponent.svelte';
14+
import { type IJsRenderer } from '../../fields/viewFields/jsRenderer/IJsRenderer';
15+
import { ObsidianJsRenderer } from '../../fields/viewFields/jsRenderer/ObsidianJsRenderer';
1316

1417
export class ObsidianAPIAdapter implements IInternalAPI {
1518
readonly plugin: MetaBindPlugin;
@@ -56,6 +59,15 @@ export class ObsidianAPIAdapter implements IInternalAPI {
5659
return this.app.commands.executeCommandById(id);
5760
}
5861

62+
public isJsEngineAvailable(): boolean {
63+
try {
64+
getJsEnginePluginAPI(this.plugin);
65+
return true;
66+
} catch (e) {
67+
return false;
68+
}
69+
}
70+
5971
public async jsEngineRunFile(
6072
filePath: string,
6173
callingFilePath: string,
@@ -107,6 +119,10 @@ export class ObsidianAPIAdapter implements IInternalAPI {
107119
return () => component.unload();
108120
}
109121

122+
public createJsRenderer(container: HTMLElement, filePath: string, code: string): IJsRenderer {
123+
return new ObsidianJsRenderer(this.plugin, container, filePath, code);
124+
}
125+
110126
public openFile(filePath: string, callingFilePath: string, newTab: boolean): void {
111127
void this.app.workspace.openLinkText(filePath, callingFilePath, newTab);
112128
}
@@ -118,4 +134,14 @@ export class ObsidianAPIAdapter implements IInternalAPI {
118134
public showNotice(message: string): void {
119135
new Notice(message);
120136
}
137+
138+
public createErrorIndicator(element: HTMLElement, props: ErrorIndicatorProps): void {
139+
new ErrorIndicatorComponent({
140+
target: element,
141+
props: {
142+
app: this.plugin.app,
143+
props: props,
144+
},
145+
});
146+
}
121147
}

src/api/internalApi/PublishAPIAdapter.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { type IInternalAPI } from './IInternalAPI';
1+
import { type ErrorIndicatorProps, type IInternalAPI } from './IInternalAPI';
22
import { type MetaBindPublishPlugin } from '../../publish/Publish';
33
import { type DatePickerIPF } from '../../fields/inputFields/fields/DatePicker/DatePickerIPF';
44
import { type ImageSuggesterIPF } from '../../fields/inputFields/fields/ImageSuggester/ImageSuggesterIPF';
55
import { type SuggesterLikeIFP, type SuggesterOption } from '../../fields/inputFields/fields/Suggester/SuggesterHelper';
66
import { type MBLiteral } from '../../utils/Literal';
7+
import { type IJsRenderer } from '../../fields/viewFields/jsRenderer/IJsRenderer';
78

89
// TODO: implement
910
export class PublishAPIAdapter implements IInternalAPI {
@@ -39,6 +40,10 @@ export class PublishAPIAdapter implements IInternalAPI {
3940
throw new Error('not implemented');
4041
}
4142

43+
public isJsEngineAvailable(): boolean {
44+
return false;
45+
}
46+
4247
public jsEngineRunFile(_filePath: string, _callingFilePath: string, _container?: HTMLElement): Promise<() => void> {
4348
return Promise.reject(new Error('not implemented'));
4449
}
@@ -47,6 +52,10 @@ export class PublishAPIAdapter implements IInternalAPI {
4752
return Promise.reject(new Error('not implemented'));
4853
}
4954

55+
public createJsRenderer(_container: HTMLElement, _filePath: string, _code: string): IJsRenderer {
56+
throw new Error('not implemented');
57+
}
58+
5059
public openFile(_filePath: string, _callingFilePath: string, _newTab: boolean): void {
5160
throw new Error('not implemented');
5261
}
@@ -56,4 +65,6 @@ export class PublishAPIAdapter implements IInternalAPI {
5665
}
5766

5867
public showNotice(_: string): void {}
68+
69+
public createErrorIndicator(_: HTMLElement, _props: ErrorIndicatorProps): void {}
5970
}

src/fields/IFieldBase.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { IPlugin } from '../IPlugin';
2+
3+
export interface IFieldBase {
4+
readonly plugin: IPlugin;
5+
6+
getUuid(): string;
7+
8+
getFilePath(): string;
9+
10+
mount(): void;
11+
12+
destroy(): void;
13+
}

src/fields/button/ButtonBase.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { type IFieldBase } from '../IFieldBase';
2+
import { type IPlugin } from '../../IPlugin';
3+
import { ErrorCollection } from '../../utils/errors/ErrorCollection';
4+
import { showUnloadedMessage } from '../../utils/Utils';
5+
import { ButtonField } from './ButtonField';
6+
import { parseYaml } from 'obsidian';
7+
8+
export type IButtonBase = IFieldBase;
9+
10+
export class ButtonBase implements IButtonBase {
11+
readonly plugin: IPlugin;
12+
filePath: string;
13+
uuid: string;
14+
errorCollection: ErrorCollection;
15+
containerEl: HTMLElement;
16+
17+
declarationString: string;
18+
buttonField: ButtonField | undefined;
19+
isPreview: boolean;
20+
21+
constructor(
22+
plugin: IPlugin,
23+
uuid: string,
24+
filePath: string,
25+
containerEl: HTMLElement,
26+
declaration: string,
27+
isPreview: boolean,
28+
) {
29+
this.plugin = plugin;
30+
this.filePath = filePath;
31+
this.containerEl = containerEl;
32+
this.declarationString = declaration;
33+
this.isPreview = isPreview;
34+
35+
this.uuid = uuid;
36+
this.errorCollection = new ErrorCollection(this.uuid);
37+
}
38+
39+
getUuid(): string {
40+
return this.uuid;
41+
}
42+
43+
getFilePath(): string {
44+
return this.filePath;
45+
}
46+
47+
mount(): void {
48+
console.debug('meta-bind | ButtonBase >> mount');
49+
50+
this.containerEl.className = '';
51+
52+
const yamlContent = parseYaml(this.declarationString) as unknown;
53+
54+
this.buttonField = new ButtonField(this.plugin, yamlContent, this.filePath, false, this.isPreview);
55+
56+
try {
57+
this.buttonField.mount(this.containerEl);
58+
} catch (e) {
59+
this.errorCollection.add(e);
60+
61+
this.plugin.internal.createErrorIndicator(this.containerEl, {
62+
errorCollection: this.errorCollection,
63+
errorText:
64+
'Errors caused the creation of the field to fail. Sometimes one error only occurs because of another.',
65+
warningText:
66+
'Warnings will not cause the creation of a field to fail, but they indicate that a part of the declaration was invalid or uses deprecated functionality.',
67+
code: this.declarationString,
68+
});
69+
}
70+
}
71+
72+
destroy(): void {
73+
console.debug('meta-bind | ButtonBase >> destroy');
74+
75+
this.buttonField?.unmount();
76+
77+
showUnloadedMessage(this.containerEl, 'button');
78+
}
79+
}

0 commit comments

Comments
 (0)