Skip to content

Commit 62143a2

Browse files
committed
fix unfinished table API #285
1 parent 831c052 commit 62143a2

File tree

5 files changed

+59
-48
lines changed

5 files changed

+59
-48
lines changed

exampleVault/Advanced Examples/PF2e Encounter Calculator.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ Player Level: `INPUT[number:playerLevel]`
3434
```js-engine
3535
const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
3636
37-
const bindTarget = mb.createBindTarget('enemy', context.file.path);
38-
const tableHead = ['Name', 'Level', 'Variant', 'Count'];
39-
const columns = [
40-
mb.inputField.createInputFieldDeclarationFromString('INPUT[text:scope^name]'),
41-
mb.inputField.createInputFieldDeclarationFromString('INPUT[number(class(meta-bind-small-width)):scope^level]'),
42-
mb.inputField.createInputFieldDeclarationFromString('INPUT[inlineSelect(option(-1, weak), option(0, normal), option(1, elite)):scope^variant]'),
43-
mb.inputField.createInputFieldDeclarationFromString('INPUT[number(class(meta-bind-small-width)):scope^count]')
44-
];
37+
const tableOptions = {
38+
bindTarget: mb.createBindTarget('frontmatter', context.file.path, ['enemy']),
39+
tableHead: ['Name', 'Level', 'Variant', 'Count'],
40+
columns: [
41+
'INPUT[text:scope^name]',
42+
'INPUT[number(class(meta-bind-small-width)):scope^level]',
43+
'INPUT[inlineSelect(option(-1, weak), option(0, normal), option(1, elite)):scope^variant]',
44+
'INPUT[number(class(meta-bind-small-width)):scope^count]',
45+
],
46+
};
4547
46-
mb.createTable(container, context.file.path, component, bindTarget, tableHead, columns);
48+
const mountable = mb.createTableMountable(context.file.path, tableOptions);
49+
50+
mb.wrapInMDRC(mountable, container, component);
4751
```
4852

4953
### Encounter Stats

packages/core/src/api/API.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
type JsViewFieldOptions,
1313
NotePosition,
1414
RenderChildType,
15-
type TableFieldOptions,
15+
type TableOptions,
1616
type ViewFieldOptions,
1717
} from 'packages/core/src/config/FieldConfigs';
1818
import { type FieldMountable } from 'packages/core/src/fields/FieldMountable';
@@ -157,7 +157,7 @@ export abstract class API<Plugin extends IPlugin> {
157157
} else if (type === FieldType.JS_VIEW) {
158158
return this.createJsViewFieldMountable(filePath, options as FieldOptionMap[FieldType.JS_VIEW]);
159159
} else if (type === FieldType.TABLE) {
160-
return this.createTableFieldMountable(filePath, options as FieldOptionMap[FieldType.TABLE]);
160+
return this.createTableMountable(filePath, options as FieldOptionMap[FieldType.TABLE]);
161161
} else if (type === FieldType.BUTTON_GROUP) {
162162
return this.createButtonGroupMountable(filePath, options as FieldOptionMap[FieldType.BUTTON_GROUP]);
163163
} else if (type === FieldType.BUTTON) {
@@ -387,7 +387,7 @@ export abstract class API<Plugin extends IPlugin> {
387387
return new JsViewFieldMountable(this.plugin, uuid, filePath, declaration);
388388
}
389389

390-
public createTableFieldMountable(filePath: string, options: TableFieldOptions): TableMountable {
390+
public createTableMountable(filePath: string, options: TableOptions): TableMountable {
391391
validate(
392392
z.object({
393393
filePath: V_FilePath,

packages/core/src/api/Validators.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
type JsViewFieldOptions,
1010
NotePosition,
1111
RenderChildType,
12-
type TableFieldOptions,
12+
type TableOptions,
1313
type ViewFieldOptions,
1414
} from 'packages/core/src/config/FieldConfigs';
1515
import { BindTargetScope } from 'packages/core/src/metadata/BindTargetScope';
@@ -215,11 +215,11 @@ export const V_JsViewFieldOptions = schemaForType<JsViewFieldOptions>()(
215215
}),
216216
);
217217

218-
export const V_TableFieldOptions = schemaForType<TableFieldOptions>()(
218+
export const V_TableFieldOptions = schemaForType<TableOptions>()(
219219
z.object({
220220
bindTarget: V_BindTargetDeclaration,
221221
tableHead: z.string().array(),
222-
columns: z.array(z.union([V_UnvalidatedInputFieldDeclaration, V_UnvalidatedViewFieldDeclaration])),
222+
columns: z.array(z.union([V_SimpleInputFieldDeclaration, V_SimpleViewFieldDeclaration, z.string()])),
223223
}),
224224
);
225225

packages/core/src/config/FieldConfigs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ export interface JsViewFieldOptions {
611611
declaration: SimpleJsViewFieldDeclaration | string;
612612
}
613613

614-
export interface TableFieldOptions {
614+
export interface TableOptions {
615615
bindTarget: BindTargetDeclaration;
616616
tableHead: string[];
617617
columns: MetaBindColumnDeclaration[];
@@ -655,7 +655,7 @@ export interface FieldOptionMap {
655655
[FieldType.INPUT]: InputFieldOptions;
656656
[FieldType.VIEW]: ViewFieldOptions;
657657
[FieldType.JS_VIEW]: JsViewFieldOptions;
658-
[FieldType.TABLE]: TableFieldOptions;
658+
[FieldType.TABLE]: TableOptions;
659659
[FieldType.BUTTON_GROUP]: ButtonGroupOptions;
660660
[FieldType.BUTTON]: ButtonOptions;
661661
[FieldType.EMBED]: EmbedOptions;

packages/core/src/fields/metaBindTable/TableMountable.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
import MetaBindTableComponent from 'packages/core/src/fields/metaBindTable/MetaBindTableComponent.svelte';
22
import { BindTargetScope } from 'packages/core/src/metadata/BindTargetScope';
3-
import {
4-
type InputFieldDeclaration,
5-
type UnvalidatedInputFieldDeclaration,
6-
} from 'packages/core/src/parsers/inputFieldParser/InputFieldDeclaration';
7-
import {
8-
type UnvalidatedViewFieldDeclaration,
9-
type ViewFieldDeclaration,
10-
} from 'packages/core/src/parsers/viewFieldParser/ViewFieldDeclaration';
3+
import { type SimpleInputFieldDeclaration } from 'packages/core/src/parsers/inputFieldParser/InputFieldDeclaration';
4+
import { type SimpleViewFieldDeclaration } from 'packages/core/src/parsers/viewFieldParser/ViewFieldDeclaration';
115
import { type Listener, Signal } from 'packages/core/src/utils/Signal';
12-
136
import { type IPlugin } from 'packages/core/src/IPlugin';
147
import { RenderChildType } from 'packages/core/src/config/FieldConfigs';
158
import { FieldMountable } from 'packages/core/src/fields/FieldMountable';
16-
import { InputFieldMountable } from 'packages/core/src/fields/inputFields/InputFieldMountable';
17-
import { ViewFieldMountable } from 'packages/core/src/fields/viewFields/ViewFieldMountable';
189
import { type MetadataSubscription } from 'packages/core/src/metadata/MetadataSubscription';
1910
import { type BindTargetDeclaration } from 'packages/core/src/parsers/bindTargetParser/BindTargetDeclaration';
2011
import { type MBExtendedLiteral } from 'packages/core/src/utils/Literal';
21-
import { getUUID, showUnloadedMessage } from 'packages/core/src/utils/Utils';
12+
import { showUnloadedMessage } from 'packages/core/src/utils/Utils';
2213
import { parsePropPath } from 'packages/core/src/utils/prop/PropParser';
2314

24-
export type MetaBindTableCell = InputFieldDeclaration | ViewFieldDeclaration;
15+
// export type MetaBindTableCell =
16+
// | {
17+
// type: FieldType.INPUT;
18+
// declaration: InputFieldDeclaration;
19+
// scope: BindTargetScope;
20+
// }
21+
// | {
22+
// type: FieldType.VIEW;
23+
// declaration: ViewFieldDeclaration;
24+
// scope: BindTargetScope;
25+
// };
26+
27+
export type MetaBindTableCell = FieldMountable;
2528

26-
export type MetaBindColumnDeclaration = UnvalidatedInputFieldDeclaration | UnvalidatedViewFieldDeclaration;
29+
export type MetaBindColumnDeclaration = SimpleInputFieldDeclaration | SimpleViewFieldDeclaration | string;
2730

2831
export interface MetaBindTableRow {
2932
cells: MetaBindTableCell[];
@@ -112,16 +115,29 @@ export class TableMountable extends FieldMountable {
112115
listenToChildren: false,
113116
});
114117

115-
const cells = this.columns.map(x => {
116-
if ('inputFieldType' in x) {
117-
return this.plugin.api.inputFieldParser.validate(x, this.getFilePath(), scope);
118-
} else {
119-
return this.plugin.api.viewFieldParser.validate(
120-
x as UnvalidatedViewFieldDeclaration,
118+
const cells: MetaBindTableCell[] = this.columns.map(x => {
119+
if (typeof x === 'string') {
120+
return this.plugin.api.createInlineFieldFromString(
121+
x,
121122
this.getFilePath(),
122123
scope,
124+
RenderChildType.INLINE,
123125
);
124126
}
127+
128+
if ('inputFieldType' in x) {
129+
return this.plugin.api.createInputFieldMountable(this.getFilePath(), {
130+
declaration: x,
131+
scope: scope,
132+
renderChildType: RenderChildType.INLINE,
133+
});
134+
} else {
135+
return this.plugin.api.createViewFieldMountable(this.getFilePath(), {
136+
declaration: x as SimpleViewFieldDeclaration,
137+
scope: scope,
138+
renderChildType: RenderChildType.INLINE,
139+
});
140+
}
125141
});
126142

127143
tableRows.push({
@@ -145,17 +161,8 @@ export class TableMountable extends FieldMountable {
145161
}
146162

147163
createCell(cell: MetaBindTableCell, element: HTMLElement): () => void {
148-
const uuid = getUUID();
149-
let field: FieldMountable;
150-
151-
if ('inputFieldType' in cell) {
152-
field = new InputFieldMountable(this.plugin, uuid, this.getFilePath(), RenderChildType.INLINE, cell);
153-
} else {
154-
field = new ViewFieldMountable(this.plugin, uuid, this.getFilePath(), RenderChildType.INLINE, cell);
155-
}
156-
157-
field.mount(element);
158-
return () => field.unmount();
164+
cell.mount(element);
165+
return () => cell.unmount();
159166
}
160167

161168
removeColumn(index: number): void {

0 commit comments

Comments
 (0)