Skip to content

Commit 28dd213

Browse files
committed
fix #576
1 parent ca62bdd commit 28dd213

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

packages/core/src/api/API.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,12 @@ export abstract class API<Components extends MB_Comps> {
648648
/**
649649
* Sets a property in meta binds metadata cache.
650650
*
651+
* @example
652+
* // Assumes you use the JS Engine plugin to run this.
653+
* const mb = engine.getPlugin("obsidian-meta-bind-plugin").api;
654+
* const bindTarget = mb.parseBindTarget("property", context.file.path);
655+
* mb.setMetadata(bindTarget, "some value");
656+
*
651657
* @param bindTarget
652658
* @param value
653659
*/
@@ -668,6 +674,12 @@ export abstract class API<Components extends MB_Comps> {
668674
* Reads a property from meta binds metadata cache.
669675
* If the value is not present in the cache, it will check the underlying source. E.g. Obsidians metadata cache.
670676
*
677+
* @example
678+
* // Assumes you use the JS Engine plugin to run this.
679+
* const mb = engine.getPlugin("obsidian-meta-bind-plugin").api;
680+
* const bindTarget = mb.parseBindTarget("property", context.file.path);
681+
* const value = mb.getMetadata(bindTarget);
682+
*
671683
* @param bindTarget
672684
*/
673685
public getMetadata(bindTarget: BindTargetDeclaration): unknown {
@@ -686,6 +698,14 @@ export abstract class API<Components extends MB_Comps> {
686698
/**
687699
* Updates a property in meta binds metadata cache.
688700
*
701+
* @example
702+
* // Assumes you use the JS Engine plugin to run this.
703+
* const mb = engine.getPlugin("obsidian-meta-bind-plugin").api;
704+
* const bindTarget = mb.parseBindTarget("property", context.file.path);
705+
* mb.updateMetadata(bindTarget, (value) => {
706+
* return value + 1;
707+
* });
708+
*
689709
* @param bindTarget
690710
* @param updateFn a function that takes the current value and returns the new value
691711
*/

packages/core/src/parsers/bindTargetParser/BindTargetParser.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ export class BindTargetParser {
4141
return {
4242
storageType: toResultNode(declaration.storageType),
4343
storagePath: toResultNode(declaration.storagePath),
44-
storageProp: declaration.storageProp.path.map(x => ({
45-
type: x.type,
46-
prop: toResultNode(x.prop),
47-
})),
44+
storageProp: declaration.storageProp.path.map(x => x.toUnvalidatedPropAccess()),
4845
listenToChildren: declaration.listenToChildren,
4946
};
5047
}

packages/core/src/parsers/inputFieldParser/InputFieldParser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ export class InputFieldParser implements ITemplateSupplier<UnvalidatedInputField
7373
* @param simpleDeclaration
7474
*/
7575
public fromSimpleDeclaration(simpleDeclaration: SimpleInputFieldDeclaration): UnvalidatedInputFieldDeclaration {
76-
const errorCollection = new ErrorCollection('InputField');
77-
7876
return {
7977
declarationString: undefined,
8078
inputFieldType: toResultNode(simpleDeclaration.inputFieldType),
@@ -83,7 +81,7 @@ export class InputFieldParser implements ITemplateSupplier<UnvalidatedInputField
8381
name: toResultNode(x.name),
8482
value: x.value.map(y => toResultNode(y)),
8583
})),
86-
errorCollection: errorCollection,
84+
errorCollection: new ErrorCollection('InputField'),
8785
};
8886
}
8987

packages/core/src/utils/prop/PropAccess.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { UnvalidatedPropAccess } from 'packages/core/src/parsers/bindTargetParser/BindTargetDeclaration';
2+
13
export enum PropAccessType {
24
OBJECT = 'object',
35
ARRAY = 'array',
@@ -87,4 +89,18 @@ export class PropAccess {
8789
obj[this.index] = undefined;
8890
}
8991
}
92+
93+
toUnvalidatedPropAccess(): UnvalidatedPropAccess {
94+
if (this.type === PropAccessType.OBJECT) {
95+
return {
96+
type: this.type,
97+
prop: { value: this.prop },
98+
};
99+
} else {
100+
return {
101+
type: this.type,
102+
prop: { value: this.index.toString() },
103+
};
104+
}
105+
}
90106
}

0 commit comments

Comments
 (0)