Skip to content

Commit 8f79351

Browse files
committed
parser renaming
1 parent 4f7c245 commit 8f79351

38 files changed

+487
-419
lines changed

exampleVault/Buttons/Templater Buttons.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,4 @@ action:
1010
type: "replaceSelf"
1111
replacement: "templates/templater/Templater Template.md"
1212
templater: true
13-
```
14-
15-
```meta-bind-button
16-
label: This is a button
17-
hidden: false
18-
class: ""
19-
tooltip: "test"
20-
id: "test"
21-
style: default
22-
actions:
23-
- type: replaceSelf
24-
replacement: "test"
25-
templater: false
2613
```

exampleVault/Meta Bind API.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,26 @@ const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
3434
3535
const options = ['a', 'b', 'c'];
3636
37-
// first, create an empty declaration
38-
let declaration = mb.inputField.createInputFieldDeclaration();
39-
// set the input field type
40-
declaration = mb.inputField.setType(declaration, 'select');
41-
// bind the input field to 'select'
42-
declaration = mb.inputField.setBindTargetMetadataField(declaration, 'select');
43-
44-
for (const option of options) {
45-
// add all the options
46-
declaration = mb.inputField.addArgument(declaration, {name: 'option', value: [option]});
47-
}
48-
49-
declaration = mb.inputField.addArgument(declaration, {name: 'title', value: ['I was created using JS Engine and the Meta Bind API']});
50-
51-
// create the input field in the container and pass in the component for life cycle management (container and component are globals exposed by js engine)
52-
mb.createInputField(declaration, 'block', context.file.path, container, component);
37+
const arguments = options.map(x => ({
38+
name: 'option',
39+
value: [x],
40+
}));
41+
42+
arguments.push({
43+
name: 'title',
44+
value: ['I was created using JS Engine and the Meta Bind API'],
45+
});
46+
47+
const bindTarget = mb.parseBindTarget('select', context.file.path);
48+
49+
const base = mb.createInputFieldBase(context.file.path, {
50+
renderChildType: 'block',
51+
declaration: {
52+
inputFieldType: 'select',
53+
bindTarget: bindTarget,
54+
arguments: arguments,
55+
},
56+
});
57+
58+
mb.wrapInMDRC(base, container, component);
5359
```

packages/core/src/api/API.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { type FieldBase } from 'packages/core/src/fields/FieldBase';
55
import { ButtonActionRunner } from 'packages/core/src/fields/button/ButtonActionRunner';
66
import { ButtonBase } from 'packages/core/src/fields/button/ButtonBase';
77
import { ButtonManager } from 'packages/core/src/fields/button/ButtonManager';
8-
import { InlineButtonBase } from 'packages/core/src/fields/button/InlineButtonBase';
8+
import { ButtonGroupBase } from 'packages/core/src/fields/button/ButtonGroupBase';
99
import { InputFieldBase } from 'packages/core/src/fields/inputFields/InputFieldBase';
1010
import { InputFieldFactory } from 'packages/core/src/fields/inputFields/InputFieldFactory';
1111
import { JsViewField } from 'packages/core/src/fields/viewFields/JsViewField';
@@ -32,9 +32,9 @@ import {
3232
import { type ButtonConfig } from 'packages/core/src/config/ButtonConfig';
3333
import {
3434
type ButtonDeclaration,
35+
type ButtonGroupDeclaration,
3536
ButtonParser,
36-
type InlineButtonDeclaration,
37-
type SimpleInlineButtonDeclaration,
37+
type SimpleButtonGroupDeclaration,
3838
} from 'packages/core/src/parsers/ButtonParser';
3939
import { JsViewFieldParser } from 'packages/core/src/parsers/viewFieldParser/JsViewFieldParser';
4040
import { Signal } from 'packages/core/src/utils/Signal';
@@ -61,7 +61,7 @@ export enum FieldType {
6161
INPUT_FIELD = 'INPUT_FIELD',
6262
VIEW_FIELD = 'VIEW_FIELD',
6363
JS_VIEW_FIELD = 'JS_VIEW_FIELD',
64-
INLINE_BUTTON = 'INLINE_BUTTON',
64+
BUTTON_GROUP = 'BUTTON_GROUP',
6565
BUTTON = 'BUTTON',
6666
EMBED = 'EMBED',
6767
EXCLUDED = 'EXCLUDED',
@@ -83,8 +83,10 @@ export interface JsViewFieldOptions {
8383
declaration: SimpleJsViewFieldDeclaration | string;
8484
}
8585

86-
export interface InlineButtonOptions {
87-
declaration: SimpleInlineButtonDeclaration | string;
86+
export interface ButtonGroupOptions {
87+
renderChildType: RenderChildType;
88+
declaration: SimpleButtonGroupDeclaration | string;
89+
position?: NotePosition | undefined;
8890
}
8991

9092
export interface ButtonOptions {
@@ -107,16 +109,16 @@ export interface FieldOptionMap {
107109
[FieldType.INPUT_FIELD]: InputFieldOptions;
108110
[FieldType.VIEW_FIELD]: ViewFieldOptions;
109111
[FieldType.JS_VIEW_FIELD]: JsViewFieldOptions;
110-
[FieldType.INLINE_BUTTON]: InlineButtonOptions;
112+
[FieldType.BUTTON_GROUP]: ButtonGroupOptions;
111113
[FieldType.BUTTON]: ButtonOptions;
112114
[FieldType.EMBED]: EmbedOptions;
113115
[FieldType.EXCLUDED]: undefined;
114116
}
115117

116-
export type InlineFieldType = FieldType.INPUT_FIELD | FieldType.VIEW_FIELD | FieldType.INLINE_BUTTON;
118+
export type InlineFieldType = FieldType.INPUT_FIELD | FieldType.VIEW_FIELD | FieldType.BUTTON_GROUP;
117119

118120
export function isFieldTypeAllowedInline(type: FieldType): type is InlineFieldType {
119-
return type === FieldType.INPUT_FIELD || type === FieldType.VIEW_FIELD || type === FieldType.INLINE_BUTTON;
121+
return type === FieldType.INPUT_FIELD || type === FieldType.VIEW_FIELD || type === FieldType.BUTTON_GROUP;
120122
}
121123

122124
export interface APIFieldOverrides {
@@ -206,8 +208,8 @@ export abstract class API<Plugin extends IPlugin> {
206208
return this.createViewFieldBase(filePath, options as FieldOptionMap[FieldType.VIEW_FIELD]);
207209
} else if (type === FieldType.JS_VIEW_FIELD) {
208210
return this.createJsViewFieldBase(filePath, options as FieldOptionMap[FieldType.JS_VIEW_FIELD]);
209-
} else if (type === FieldType.INLINE_BUTTON) {
210-
return this.createInlineButtonBase(filePath, options as FieldOptionMap[FieldType.INLINE_BUTTON]);
211+
} else if (type === FieldType.BUTTON_GROUP) {
212+
return this.createButtonGroupBase(filePath, options as FieldOptionMap[FieldType.BUTTON_GROUP]);
211213
} else if (type === FieldType.BUTTON) {
212214
return this.createButtonBase(filePath, options as FieldOptionMap[FieldType.BUTTON]);
213215
} else if (type === FieldType.EMBED) {
@@ -230,13 +232,15 @@ export abstract class API<Plugin extends IPlugin> {
230232
* @param filePath
231233
* @param scope
232234
* @param renderChildType
235+
* @param position
233236
* @param honorExcludedSetting
234237
*/
235238
public createInlineFieldFromString(
236239
fieldString: string,
237240
filePath: string,
238241
scope: BindTargetScope | undefined,
239242
renderChildType: RenderChildType = RenderChildType.INLINE,
243+
position?: NotePosition | undefined,
240244
honorExcludedSetting: boolean = true,
241245
): FieldBase {
242246
validate(
@@ -271,6 +275,7 @@ export abstract class API<Plugin extends IPlugin> {
271275
filePath,
272276
scope,
273277
renderChildType,
278+
position,
274279
honorExcludedSetting,
275280
);
276281
}
@@ -284,6 +289,7 @@ export abstract class API<Plugin extends IPlugin> {
284289
* @param declaration
285290
* @param scope
286291
* @param renderChildType
292+
* @param position
287293
* @param honorExcludedSetting
288294
*/
289295
public createInlineFieldOfTypeFromString(
@@ -292,6 +298,7 @@ export abstract class API<Plugin extends IPlugin> {
292298
filePath: string,
293299
scope: BindTargetScope | undefined,
294300
renderChildType: RenderChildType = RenderChildType.INLINE,
301+
position?: NotePosition | undefined,
295302
honorExcludedSetting: boolean = true,
296303
): FieldBase {
297304
validate(
@@ -333,8 +340,12 @@ export abstract class API<Plugin extends IPlugin> {
333340
});
334341
}
335342

336-
if (type === FieldType.INLINE_BUTTON) {
337-
return this.createInlineButtonBase(filePath, { declaration: declaration });
343+
if (type === FieldType.BUTTON_GROUP) {
344+
return this.createButtonGroupBase(filePath, {
345+
renderChildType: renderChildType,
346+
declaration: declaration,
347+
position: position,
348+
});
338349
}
339350

340351
expectType<never>(type);
@@ -426,7 +437,7 @@ export abstract class API<Plugin extends IPlugin> {
426437
return new JsViewField(this.plugin, uuid, filePath, declaration);
427438
}
428439

429-
public createInlineButtonBase(filePath: string, options: InlineButtonOptions): InlineButtonBase {
440+
public createButtonGroupBase(filePath: string, options: ButtonGroupOptions): ButtonGroupBase {
430441
validate(
431442
z.object({
432443
filePath: V_FilePath,
@@ -440,14 +451,14 @@ export abstract class API<Plugin extends IPlugin> {
440451

441452
const uuid = getUUID();
442453

443-
let declaration: InlineButtonDeclaration;
454+
let declaration: ButtonGroupDeclaration;
444455
if (typeof options.declaration === 'string') {
445-
declaration = this.buttonParser.parseInlineString(options.declaration);
456+
declaration = this.buttonParser.fromGroupString(options.declaration);
446457
} else {
447-
declaration = this.buttonParser.validateSimpleInlineDeclaration(options.declaration);
458+
declaration = this.buttonParser.validateGroup(options.declaration);
448459
}
449460

450-
return new InlineButtonBase(this.plugin, uuid, filePath, declaration);
461+
return new ButtonGroupBase(this.plugin, uuid, filePath, declaration, options.renderChildType, options.position);
451462
}
452463

453464
public createButtonBase(filePath: string, options: ButtonOptions): ButtonBase {
@@ -466,9 +477,9 @@ export abstract class API<Plugin extends IPlugin> {
466477

467478
let declaration: ButtonDeclaration;
468479
if (typeof options.declaration === 'string') {
469-
declaration = this.buttonParser.parseButtonString(options.declaration);
480+
declaration = this.buttonParser.fromString(options.declaration);
470481
} else {
471-
declaration = this.buttonParser.validateSimpleButtonConfig(options.declaration);
482+
declaration = this.buttonParser.validate(options.declaration);
472483
}
473484

474485
return new ButtonBase(this.plugin, uuid, filePath, declaration, options.position, options.isPreview);
@@ -523,7 +534,7 @@ export abstract class API<Plugin extends IPlugin> {
523534
return 'INPUT';
524535
} else if (fieldType === FieldType.VIEW_FIELD) {
525536
return 'VIEW';
526-
} else if (fieldType === FieldType.INLINE_BUTTON) {
537+
} else if (fieldType === FieldType.BUTTON_GROUP) {
527538
return 'BUTTON';
528539
}
529540

@@ -678,6 +689,7 @@ export abstract class API<Plugin extends IPlugin> {
678689

679690
this.plugin.metadataManager.write(value, bindTarget);
680691
}
692+
681693
/**
682694
* Reads a property from meta binds metadata cache.
683695
* If the value is not present in the cache, it will check the underlying source. E.g. Obsidians metadata cache.

packages/core/src/api/SyntaxHighlightingAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class SyntaxHighlightingAPI {
3636
return this.highlightInputFieldDeclaration(str, trimWhiteSpace);
3737
} else if (mdrcType === FieldType.VIEW_FIELD) {
3838
return this.highlightViewFieldDeclaration(str, trimWhiteSpace);
39-
} else if (mdrcType === FieldType.INLINE_BUTTON) {
39+
} else if (mdrcType === FieldType.BUTTON_GROUP) {
4040
return this.highlightInlineButtonDeclaration(str, trimWhiteSpace);
4141
}
4242

packages/core/src/api/Validators.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { InputFieldType, RenderChildType } from 'packages/core/src/config/FieldC
33
import { BindTargetScope } from 'packages/core/src/metadata/BindTargetScope';
44
import type { ParsingResultNode } from 'packages/core/src/parsers/nomParsers/GeneralNomParsers';
55
import type {
6-
SimpleFieldArgument,
76
SimpleInputFieldDeclaration,
8-
UnvalidatedFieldArgument,
97
UnvalidatedInputFieldDeclaration,
108
} from 'packages/core/src/parsers/inputFieldParser/InputFieldDeclaration';
119
import {
@@ -15,7 +13,7 @@ import {
1513
type UnvalidatedBindTargetDeclaration,
1614
type UnvalidatedPropAccess,
1715
} from 'packages/core/src/parsers/bindTargetParser/BindTargetDeclaration';
18-
import { PROP_ACCESS_TYPE } from 'packages/core/src/utils/prop/PropAccess';
16+
import { PropAccessType } from 'packages/core/src/utils/prop/PropAccess';
1917
import { ErrorCollection } from 'packages/core/src/utils/errors/ErrorCollection';
2018
import {
2119
type SimpleJsViewFieldBindTargetMapping,
@@ -28,17 +26,21 @@ import { Signal } from 'packages/core/src/utils/Signal';
2826
import { z } from 'zod';
2927
import { type ParsingPosition, type ParsingRange } from '@lemons_dev/parsinom/lib/HelperTypes';
3028
import {
29+
type ButtonGroupOptions,
3130
type ButtonOptions,
3231
type EmbedOptions,
3332
FieldType,
34-
type InlineButtonOptions,
3533
type InputFieldOptions,
3634
type JsViewFieldOptions,
3735
type NotePosition,
3836
type ViewFieldOptions,
3937
} from 'packages/core/src/api/API';
40-
import { type SimpleInlineButtonDeclaration } from 'packages/core/src/parsers/ButtonParser';
38+
import { type SimpleButtonGroupDeclaration } from 'packages/core/src/parsers/ButtonParser';
4139
import { V_ButtonConfig } from 'packages/core/src/config/ButtonConfigValidators';
40+
import {
41+
type SimpleFieldArgument,
42+
type UnvalidatedFieldArgument,
43+
} from 'packages/core/src/parsers/nomParsers/FieldArgumentNomParsers';
4244

4345
export const V_FilePath = schemaForType<string>()(z.string());
4446

@@ -87,7 +89,7 @@ export const V_UnvalidatedFieldArgument = schemaForType<UnvalidatedFieldArgument
8789

8890
export const V_UnvalidatedPropAccess = schemaForType<UnvalidatedPropAccess>()(
8991
z.object({
90-
type: z.nativeEnum(PROP_ACCESS_TYPE),
92+
type: z.nativeEnum(PropAccessType),
9193
prop: V_ParsingResultNode,
9294
}),
9395
);
@@ -141,7 +143,7 @@ export const V_SimpleFieldArgument = schemaForType<SimpleFieldArgument>()(
141143

142144
export const V_SimplePropAccess = schemaForType<SimplePropAccess>()(
143145
z.object({
144-
type: z.nativeEnum(PROP_ACCESS_TYPE),
146+
type: z.nativeEnum(PropAccessType),
145147
prop: z.string(),
146148
}),
147149
);
@@ -188,7 +190,7 @@ export const V_SimpleJsViewFieldDeclaration = schemaForType<SimpleJsViewFieldDec
188190
}),
189191
);
190192

191-
export const V_SimpleInlineButtonDeclaration = schemaForType<SimpleInlineButtonDeclaration>()(
193+
export const V_SimpleInlineButtonDeclaration = schemaForType<SimpleButtonGroupDeclaration>()(
192194
z.object({
193195
referencedButtonIds: z.string().array(),
194196
}),
@@ -216,16 +218,18 @@ export const V_JsViewFieldOptions = schemaForType<JsViewFieldOptions>()(
216218
}),
217219
);
218220

219-
export const V_InlineButtonOptions = schemaForType<InlineButtonOptions>()(
221+
export const V_NotePosition = schemaForType<NotePosition>()(
220222
z.object({
221-
declaration: z.union([z.string(), V_SimpleInlineButtonDeclaration]),
223+
lineStart: z.number(),
224+
lineEnd: z.number(),
222225
}),
223226
);
224227

225-
export const V_NotePosition = schemaForType<NotePosition>()(
228+
export const V_InlineButtonOptions = schemaForType<ButtonGroupOptions>()(
226229
z.object({
227-
lineStart: z.number(),
228-
lineEnd: z.number(),
230+
renderChildType: V_RenderChildType,
231+
declaration: z.union([z.string(), V_SimpleInlineButtonDeclaration]),
232+
position: V_NotePosition.optional(),
229233
}),
230234
);
231235

packages/core/src/config/ButtonConfigValidators.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import {
66
type CommandButtonAction,
77
type CreateNoteButtonAction,
88
type InputButtonAction,
9+
type InsertIntoNoteButtonAction,
910
type JSButtonAction,
10-
type ReplaceInNoteButtonAction,
1111
type OpenButtonAction,
12+
type RegexpReplaceInNoteButtonAction,
13+
type ReplaceInNoteButtonAction,
14+
type ReplaceSelfButtonAction,
1215
type SleepButtonAction,
1316
type TemplaterCreateNoteButtonAction,
1417
type UpdateMetadataButtonAction,
15-
type ReplaceSelfButtonAction,
16-
type RegexpReplaceInNoteButtonAction,
17-
type InsertIntoNoteButtonAction,
1818
} from 'packages/core/src/config/ButtonConfig';
1919
import { oneOf, schemaForType } from 'packages/core/src/utils/ZodUtils';
2020
import { z } from 'zod';

packages/core/src/fields/button/ButtonActionRunner.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import {
77
type CommandButtonAction,
88
type CreateNoteButtonAction,
99
type InputButtonAction,
10+
type InsertIntoNoteButtonAction,
1011
type JSButtonAction,
11-
type ReplaceInNoteButtonAction,
1212
type OpenButtonAction,
13+
type RegexpReplaceInNoteButtonAction,
14+
type ReplaceInNoteButtonAction,
15+
type ReplaceSelfButtonAction,
1316
type SleepButtonAction,
1417
type TemplaterCreateNoteButtonAction,
1518
type UpdateMetadataButtonAction,
16-
type InsertIntoNoteButtonAction,
17-
type RegexpReplaceInNoteButtonAction,
18-
type ReplaceSelfButtonAction,
1919
} from 'packages/core/src/config/ButtonConfig';
2020
import { MDLinkParser } from 'packages/core/src/parsers/MarkdownLinkParser';
2121
import { expectType, openURL } from 'packages/core/src/utils/Utils';

0 commit comments

Comments
 (0)