Skip to content

Commit 40a847c

Browse files
committed
add new metadata API method for js engine
1 parent 6f4a5cd commit 40a847c

File tree

9 files changed

+86
-14
lines changed

9 files changed

+86
-14
lines changed

exampleVault/Advanced Examples/Using JS Engine for Complex things.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
text: abcasdas
33
locked: true
4-
from_year: a2323
5-
to_year: 23423dss
4+
from_year: a22
5+
to_year: "23"
66
---
77

88
Locked: `INPUT[toggle:locked]`
@@ -104,4 +104,15 @@ const subscriptionTo = mb.subscribeToMetadata(
104104
);
105105
106106
return reactive;
107+
```
108+
109+
```js-engine
110+
// Grab metabind API and extract metadata fields
111+
const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
112+
const mbFrom = mb.parseBindTarget('from_year', context.file.path);
113+
const mbTo = mb.parseBindTarget('to_year', context.file.path);
114+
115+
return mb.renderOnChanges([mbFrom, mbTo], component, (from, to) => {
116+
return [from, to]
117+
})
107118
```

packages/core/bun.lockb

-1.57 KB
Binary file not shown.

packages/core/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{
22
"name": "meta-bind-core",
33
"main": "src/index.ts",
4-
"dependencies": {
5-
"@lemons_dev/parsinom": "^0.0.12",
6-
"zod": "^3.22.4"
7-
}
4+
"dependencies": {}
85
}

packages/core/src/metadata/MetadataManager.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ export class MetadataManager {
134134
return subscription;
135135
}
136136

137+
/**
138+
* Subscribes a computed value to the metadata manager.
139+
*
140+
* @param uuid
141+
* @param callbackSignal The signal that will hold the computed value.
142+
* @param bindTarget The bind target that the computed value will be written to.
143+
* @param dependencies The dependencies of the computed value.
144+
* @param computeFunction The function that computes the value from the dependencies.
145+
* @param onDelete Called when the metadata manager wants to delete the subscription.
146+
*/
137147
public subscribeComputed(
138148
uuid: string,
139149
callbackSignal: Signal<unknown>,

packages/obsidian/bun.lockb

-704 Bytes
Binary file not shown.

packages/obsidian/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"@codemirror/language": "6.9.2",
66
"@codemirror/state": "6.3.1",
77
"@codemirror/view": "6.22.0",
8-
"obsidian": "latest",
9-
"@lemons_dev/parsinom": "^0.0.12",
10-
"zod": "^3.22.4"
8+
"obsidian": "latest"
119
}
1210
}

packages/obsidian/src/ObsidianAPI.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import { type Component } from 'obsidian';
22
import type MetaBindPlugin from 'packages/obsidian/src/main';
33
import { V_API_createTable } from 'packages/obsidian/src/APIValidators';
4-
import { API } from 'packages/core/src/api/API.js';
4+
import { API, type LifecycleHook } from 'packages/core/src/api/API.js';
55
import { MetaBindTable } from 'packages/core/src/fields/metaBindTable/MetaBindTable';
66
import { type BindTargetDeclaration } from 'packages/core/src/parsers/bindTargetParser/BindTargetDeclaration';
77
import { type UnvalidatedInputFieldDeclaration } from 'packages/core/src/parsers/inputFieldParser/InputFieldDeclaration';
88
import { type UnvalidatedViewFieldDeclaration } from 'packages/core/src/parsers/viewFieldParser/ViewFieldDeclaration';
99
import { getUUID } from 'packages/core/src/utils/Utils';
10-
import { validateArgs } from 'packages/core/src/utils/ZodUtils';
10+
import { validate, validateArgs } from 'packages/core/src/utils/ZodUtils';
1111
import { ErrorLevel, MetaBindInternalError } from 'packages/core/src/utils/errors/MetaBindErrors';
1212
import { MarkdownRenderChildWidget } from 'packages/obsidian/src/cm6/Cm6_Widgets';
1313
import { FieldMDRC } from 'packages/obsidian/src/FieldMDRC';
1414
import { type FieldBase } from 'packages/core/src/fields/FieldBase';
1515
import { type FieldType, isFieldTypeAllowedInline } from 'packages/core/src/config/FieldConfigs';
16+
import { z } from 'zod';
17+
import { V_BindTargetDeclaration } from 'packages/core/src/api/Validators';
18+
import { Signal } from 'packages/core/src/utils/Signal';
19+
import { getJsEnginePluginAPI } from 'packages/obsidian/src/ObsUtils';
20+
import { type ReactiveComponent } from 'jsEngine/api/reactive/ReactiveComponent';
1621

1722
export interface ComponentLike {
1823
addChild(child: Component): void;
@@ -85,4 +90,57 @@ export class ObsidianAPI extends API<MetaBindPlugin> {
8590
cause: `Invalid inline mdrc type "${mdrcType}"`,
8691
});
8792
}
93+
94+
/**
95+
* Creates a JS Engine reactive component that will re-render when the given bind targets change.
96+
*
97+
* This requires JS Engine to be installed and enabled!
98+
*
99+
* @param bindTargets
100+
* @param lifecycleHook
101+
* @param callback
102+
*/
103+
public reactiveMetadata(
104+
bindTargets: BindTargetDeclaration[],
105+
lifecycleHook: LifecycleHook,
106+
callback: (...values: unknown[]) => Promise<unknown>,
107+
): ReactiveComponent {
108+
validate(
109+
z.object({
110+
bindTargets: V_BindTargetDeclaration.array(),
111+
lifecycleHook: this.plugin.internal.getLifecycleHookValidator(),
112+
callback: z.function(),
113+
}),
114+
{
115+
bindTargets: bindTargets,
116+
lifecycleHook: lifecycleHook,
117+
callback: callback,
118+
},
119+
);
120+
121+
const jsEngine = getJsEnginePluginAPI(this.plugin);
122+
123+
const uuid = getUUID();
124+
const signal = new Signal<unknown>(undefined);
125+
126+
const dependencies = bindTargets.map(bindTarget => ({
127+
bindTarget: bindTarget,
128+
callbackSignal: new Signal<unknown>(undefined),
129+
}));
130+
131+
const subscription = this.plugin.metadataManager.subscribeComputed(
132+
uuid,
133+
signal,
134+
undefined,
135+
dependencies,
136+
(values: unknown[]) => reactive.refresh(...values),
137+
() => {},
138+
);
139+
140+
lifecycleHook.register(() => subscription.unsubscribe());
141+
142+
const reactive = jsEngine.reactive(callback, ...dependencies.map(x => x.callbackSignal.get()));
143+
144+
return reactive;
145+
}
88146
}

packages/publish/bun.lockb

-712 Bytes
Binary file not shown.

packages/publish/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
{
22
"name": "meta-bind-publish",
33
"dependencies": {
4-
"obsidian": "latest",
5-
"@lemons_dev/parsinom": "^0.0.12",
6-
"zod": "^3.22.4"
4+
"obsidian": "latest"
75
}
86
}

0 commit comments

Comments
 (0)