@arkitektio/bsx 6.3.0 → 6.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bsx/infer.d.ts.map +1 -1
- package/dist/bsx/infer.js +4 -0
- package/dist/bsx/infer.js.map +1 -1
- package/dist/bsx/language.js +3 -3
- package/dist/bsx/language.js.map +1 -1
- package/dist/bsx/parse.d.ts +1 -0
- package/dist/bsx/parse.d.ts.map +1 -1
- package/dist/bsx/parse.js +8 -1
- package/dist/bsx/parse.js.map +1 -1
- package/dist/bsx/print.d.ts.map +1 -1
- package/dist/bsx/print.js +2 -2
- package/dist/bsx/print.js.map +1 -1
- package/dist/bsx/references.d.ts +4 -0
- package/dist/bsx/references.d.ts.map +1 -1
- package/dist/bsx/references.js +47 -2
- package/dist/bsx/references.js.map +1 -1
- package/dist/ports/engine/defaultRegistry.d.ts.map +1 -1
- package/dist/ports/engine/defaultRegistry.js +2 -0
- package/dist/ports/engine/defaultRegistry.js.map +1 -1
- package/dist/ports/host.d.ts +6 -0
- package/dist/ports/host.d.ts.map +1 -1
- package/dist/ports/host.js.map +1 -1
- package/dist/ports/rekuest.d.ts +14 -1
- package/dist/ports/rekuest.d.ts.map +1 -1
- package/dist/ports/rekuest.js +1 -0
- package/dist/ports/rekuest.js.map +1 -1
- package/dist/ports/widgets/custom/PortBlokWidget.d.ts +30 -0
- package/dist/ports/widgets/custom/PortBlokWidget.d.ts.map +1 -0
- package/dist/ports/widgets/custom/PortBlokWidget.js +210 -0
- package/dist/ports/widgets/custom/PortBlokWidget.js.map +1 -0
- package/dist/ports.d.ts +2 -0
- package/dist/ports.d.ts.map +1 -1
- package/dist/ports.js +4 -0
- package/dist/ports.js.map +1 -1
- package/dist/renderer/BlokRenderer.d.ts +4 -1
- package/dist/renderer/BlokRenderer.d.ts.map +1 -1
- package/dist/renderer/BlokRenderer.js +5 -1
- package/dist/renderer/BlokRenderer.js.map +1 -1
- package/dist/renderer/bloks/control.d.ts +5 -0
- package/dist/renderer/bloks/control.d.ts.map +1 -1
- package/dist/renderer/bloks/control.js +41 -6
- package/dist/renderer/bloks/control.js.map +1 -1
- package/dist/renderer/bloks/port/host.d.ts +32 -0
- package/dist/renderer/bloks/port/host.d.ts.map +1 -0
- package/dist/renderer/bloks/port/host.js +14 -0
- package/dist/renderer/bloks/port/host.js.map +1 -0
- package/dist/renderer/runtime/context.d.ts +2 -0
- package/dist/renderer/runtime/context.d.ts.map +1 -1
- package/dist/renderer/runtime/context.js +4 -0
- package/dist/renderer/runtime/context.js.map +1 -1
- package/dist/renderer/runtime/hooks.d.ts.map +1 -1
- package/dist/renderer/runtime/hooks.js +1 -0
- package/dist/renderer/runtime/hooks.js.map +1 -1
- package/dist/renderer/runtime/resolution.d.ts +3 -0
- package/dist/renderer/runtime/resolution.d.ts.map +1 -1
- package/dist/renderer/runtime/resolution.js +17 -1
- package/dist/renderer/runtime/resolution.js.map +1 -1
- package/dist/renderer/runtime/types.d.ts +6 -0
- package/dist/renderer/runtime/types.d.ts.map +1 -1
- package/dist/renderer/runtime/types.js.map +1 -1
- package/dist/spec/bsx_v2.json +1517 -179
- package/dist/spec/catalogs/base@4.json +103 -3
- package/dist/spec/catalogs/base@4.json.js +103 -3
- package/dist/spec/vocabulary.d.ts +5 -1
- package/dist/spec/vocabulary.d.ts.map +1 -1
- package/dist/spec/vocabulary.js +4 -1
- package/dist/spec/vocabulary.js.map +1 -1
- package/dist/spec/vocabulary.json +50 -15
- package/dist/spec/vocabulary.json.js +50 -15
- package/package.json +1 -1
- package/src/bsx/infer.ts +4 -0
- package/src/bsx/language.ts +3 -3
- package/src/bsx/parse.ts +7 -1
- package/src/bsx/print.ts +2 -1
- package/src/bsx/references.test.ts +1 -1
- package/src/bsx/references.ts +45 -2
- package/src/ports/engine/defaultRegistry.tsx +2 -0
- package/src/ports/host.tsx +6 -0
- package/src/ports/rekuest.ts +7 -1
- package/src/ports/widgets/custom/PortBlokWidget.test.tsx +88 -0
- package/src/ports/widgets/custom/PortBlokWidget.tsx +268 -0
- package/src/ports.ts +4 -0
- package/src/renderer/BlokRenderer.tsx +9 -0
- package/src/renderer/bloks/control.tsx +65 -5
- package/src/renderer/bloks/flow.test.tsx +11 -0
- package/src/renderer/bloks/port/host.tsx +46 -0
- package/src/renderer/runtime/context.tsx +6 -0
- package/src/renderer/runtime/hooks.ts +1 -0
- package/src/renderer/runtime/resolution.ts +16 -1
- package/src/renderer/runtime/types.ts +6 -0
- package/src/spec/bsx_v2.json +1517 -179
- package/src/spec/catalogs/base@4.json +103 -3
- package/src/spec/spec.test.ts +8 -4
- package/src/spec/vocabulary.json +50 -15
- package/src/spec/vocabulary.ts +9 -2
package/src/bsx/references.ts
CHANGED
|
@@ -177,6 +177,8 @@ export type BlokCheckScope = {
|
|
|
177
177
|
assets?: readonly string[] | null;
|
|
178
178
|
/** Preview data, for the logo's foreach limit. */
|
|
179
179
|
demo?: {deps?: unknown; args?: unknown; task?: unknown} | null;
|
|
180
|
+
/** The port a port blok is the widget of, as a match: what `port.value` is typed as. Null (or absent) leaves the value open. */
|
|
181
|
+
port?: BlokPortMatch | null;
|
|
180
182
|
};
|
|
181
183
|
|
|
182
184
|
/** A blok checked on its own, mounted nowhere: what an editor checks from the source alone. */
|
|
@@ -206,6 +208,10 @@ const DEPENDENCY_KEY_PROPS = new Set(['Placed.dep', 'Route.through']);
|
|
|
206
208
|
/** The kinds that have a run's dependencies: an action's own bloks, for a run and for starting one. */
|
|
207
209
|
const RUN_KINDS: readonly string[] = ['action', 'assign'];
|
|
208
210
|
const ASSIGN_KIND = 'assign';
|
|
211
|
+
/** The root of a port blok: the port it is the widget of. */
|
|
212
|
+
const PORT_ROOT = 'port';
|
|
213
|
+
/** The one field of `port` a control writes. */
|
|
214
|
+
const PORT_VALUE_FIELD = 'value';
|
|
209
215
|
const ASK_COMPONENT = 'ask';
|
|
210
216
|
const CHROME_NONE = 'none';
|
|
211
217
|
/** The language's elements whose children do not always show: a host part inside one still claims its slot. */
|
|
@@ -224,6 +230,7 @@ const TOAST_TASK_PROP = 'task';
|
|
|
224
230
|
const CLASS_NAME = 'className';
|
|
225
231
|
const FOREACH = 'foreach';
|
|
226
232
|
const FOREACH_LET = 'let';
|
|
233
|
+
const FOREACH_INDEX = 'index';
|
|
227
234
|
const FOREACH_ITEMS = 'items';
|
|
228
235
|
const FOREACH_KEY = 'key';
|
|
229
236
|
/** An item of these kinds is no scalar: a foreach over it without key is keyed by position (`foreach.position_key`). */
|
|
@@ -327,6 +334,11 @@ export const foreachLet = (node: BlokComponentNode): string | null => {
|
|
|
327
334
|
return node.props?.find(prop => prop.key === FOREACH_LET && prop.declares_value)?.declares_value ?? null;
|
|
328
335
|
};
|
|
329
336
|
|
|
337
|
+
/** The name a `foreach` gives each item's position (`index="i"`), or null when it names none. */
|
|
338
|
+
export const foreachIndex = (node: BlokComponentNode): string | null => {
|
|
339
|
+
if (node.component !== FOREACH) return null;
|
|
340
|
+
return node.props?.find(prop => prop.key === FOREACH_INDEX && prop.declares_value)?.declares_value ?? null;
|
|
341
|
+
};
|
|
330
342
|
|
|
331
343
|
/** A `<local>` in scope: its node and the port it resolved to. */
|
|
332
344
|
type LocalBinding = {local: true; node: string; name: string; port: ResolvedBlokLocal};
|
|
@@ -376,7 +388,7 @@ const DIGITS = /^[0-9]+$/;
|
|
|
376
388
|
* - an agent blok has none: ``agent bloks read only `self` ``.
|
|
377
389
|
*/
|
|
378
390
|
export const dependencyHint = (key: string, kind: BlokKind): string => {
|
|
379
|
-
if (kind === 'action' || kind === 'assign') return `add a dependency parameter \`${key}\` to the action`;
|
|
391
|
+
if (kind === 'action' || kind === 'assign' || kind === 'port') return `add a dependency parameter \`${key}\` to the action`;
|
|
380
392
|
if (kind === 'agent') return 'agent bloks read only `self`';
|
|
381
393
|
return `dependencies=[AgentDependencyInput(key="${key}")]`;
|
|
382
394
|
};
|
|
@@ -677,6 +689,8 @@ class Checker {
|
|
|
677
689
|
const items = node.props?.find(prop => prop.key === FOREACH_ITEMS) ?? null;
|
|
678
690
|
inner.set(declared, this.bindLet(declared, items, {node, key: FOREACH_ITEMS, callback: false, inError}, scope));
|
|
679
691
|
}
|
|
692
|
+
const index = foreachIndex(node);
|
|
693
|
+
if (index !== null) inner.set(index, this.bindIndex(index, {node, key: FOREACH_INDEX, callback: false, inError}, new Map(inner)));
|
|
680
694
|
for (const prop of node.props ?? []) {
|
|
681
695
|
// A foreach reads its items outside the loop, its key inside it.
|
|
682
696
|
const propScope = declared !== null && prop.key === FOREACH_ITEMS ? scope : inner;
|
|
@@ -846,6 +860,11 @@ class Checker {
|
|
|
846
860
|
if (effect !== undefined && vocabulary.functions.get(effect)?.taskOperation && !this.kind.roots.includes('task')) {
|
|
847
861
|
this.notInKind(node.id, effect, 'task', prop.key);
|
|
848
862
|
}
|
|
863
|
+
// An effect that is a root's own (port.set) belongs to the kinds that read that root.
|
|
864
|
+
const needs = effect === undefined ? null : (vocabulary.functions.get(effect)?.needs ?? null);
|
|
865
|
+
if (effect !== undefined && needs !== null && !this.kind.roots.includes(needs)) {
|
|
866
|
+
this.notInKind(node.id, effect, needs, prop.key);
|
|
867
|
+
}
|
|
849
868
|
if (prop.key === BIND_PROP) {
|
|
850
869
|
const path = prop.dynamic_value?.path ?? null;
|
|
851
870
|
if (path === null) {
|
|
@@ -853,7 +872,11 @@ class Checker {
|
|
|
853
872
|
} else {
|
|
854
873
|
const root = path.split('.')[0];
|
|
855
874
|
const binding = scope.get(root);
|
|
856
|
-
if (!
|
|
875
|
+
if (root === PORT_ROOT && !scope.has(root) && this.kind.roots.includes(PORT_ROOT)) {
|
|
876
|
+
// The one thing besides a local a control writes: the value of the port a port blok
|
|
877
|
+
// stands for (and what is below it), never what the form says of it.
|
|
878
|
+
if (path.split('.')[1] !== PORT_VALUE_FIELD) this.add('port.bind', node.id, {path}, prop.key);
|
|
879
|
+
} else if (!isLocalBinding(binding)) this.add('local.unknown', node.id, {path, root}, prop.key);
|
|
857
880
|
else {
|
|
858
881
|
this.boundLocals.add(binding.node);
|
|
859
882
|
if (propSpec) this.checkBindKind(node, propSpec.kind, binding, path);
|
|
@@ -937,6 +960,11 @@ class Checker {
|
|
|
937
960
|
private visitAgentCall(call: BlokAgentCall, where: Where, scope: Scope): void {
|
|
938
961
|
const owner = whereText(where);
|
|
939
962
|
const action = actionKeyFor(call);
|
|
963
|
+
if (!this.kind.calls) {
|
|
964
|
+
const called = call.dependency === 'self' ? `self.${action}(...)` : `deps.${call.dependency}.${action}(...)`;
|
|
965
|
+
this.add('calls.not_in_kind', where.node.id, {path: called, n: article(this.kindName), kind: this.kindName}, where.key);
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
940
968
|
if (call.dependency === 'self') {
|
|
941
969
|
if (!this.kind.roots.includes('self')) {
|
|
942
970
|
this.notInKind(where.node.id, `self.${action}(...)`, 'self', where.key);
|
|
@@ -1048,6 +1076,15 @@ class Checker {
|
|
|
1048
1076
|
if (call.operation === FETCH_MORE_FUNCTION && query.more == null) this.add('query.fetch_more', where.node.id, {path, name}, where.key);
|
|
1049
1077
|
}
|
|
1050
1078
|
|
|
1079
|
+
/** What a foreach's `index` is bound to: the item's 0-based position. `scope` holds the loop variable already. */
|
|
1080
|
+
private bindIndex(name: string, where: Where, scope: Scope): Binding {
|
|
1081
|
+
if (this.vocabulary.reserved.has(name)) this.add('roots.reserved', where.node.id, {name});
|
|
1082
|
+
else if (scope.has(name)) {
|
|
1083
|
+
this.add('roots.shadow', where.node.id, {name, first: bindingWhat(name, scope.get(name), this.into), second: `the index of <foreach> (${where.node.id})`});
|
|
1084
|
+
}
|
|
1085
|
+
return {key: name, kind: 'INT'};
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1051
1088
|
private bindLet(name: string, items: BlokComponentProp | null, where: Where, scope: Scope): Binding {
|
|
1052
1089
|
if (this.vocabulary.reserved.has(name)) this.add('roots.reserved', where.node.id, {name});
|
|
1053
1090
|
else if (scope.has(name)) {
|
|
@@ -1181,6 +1218,12 @@ class Checker {
|
|
|
1181
1218
|
return {key: rest[1], kind: 'BOOL'};
|
|
1182
1219
|
}
|
|
1183
1220
|
return this.descend(recordMatch('assign', vocabulary.records.assign.fields), rest, path, nodeId, propKey);
|
|
1221
|
+
case PORT_ROOT: {
|
|
1222
|
+
// `port.value` is the port's own type where the mount says it; the rest is the record.
|
|
1223
|
+
const value = this.scope.port ?? null;
|
|
1224
|
+
const overrides = value === null ? undefined : {[PORT_VALUE_FIELD]: {...value, at: null, nullable: true}};
|
|
1225
|
+
return this.descend(recordMatch(PORT_ROOT, vocabulary.records.port.fields, overrides), rest, path, nodeId, propKey);
|
|
1226
|
+
}
|
|
1184
1227
|
case 'task':
|
|
1185
1228
|
this.checkReturns(path, rest, nodeId, propKey);
|
|
1186
1229
|
return this.descend(taskRecordMatch('task'), rest, path, nodeId, propKey);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ChoicesWidget } from "../widgets/custom/ChoicesWidget";
|
|
2
2
|
import { CustomWidget } from "../widgets/custom/CustomWidget";
|
|
3
|
+
import { PortBlokWidget } from "../widgets/custom/PortBlokWidget";
|
|
3
4
|
import { ProxyWidget } from "../widgets/custom/ProxyWidget";
|
|
4
5
|
import { SearchWidget } from "../widgets/custom/SearchWidget";
|
|
5
6
|
import { SliderWidget } from "../widgets/custom/SliderWidget";
|
|
@@ -90,6 +91,7 @@ export const createWidgetRegistry = (options: { canPick?: (identifier: string) =
|
|
|
90
91
|
registry.registerInputWidget("ProxyWidget", ProxyWidget);
|
|
91
92
|
registry.registerInputWidget("StringAssignWidget", StringWidget);
|
|
92
93
|
registry.registerInputWidget("CustomAssignWidget", CustomWidget);
|
|
94
|
+
registry.registerInputWidget("BlokAssignWidget", PortBlokWidget);
|
|
93
95
|
|
|
94
96
|
registry.registerReturnWidgetFallback(PortKind.Int, IntReturnWidget);
|
|
95
97
|
registry.registerReturnWidgetFallback(PortKind.Float, FloatReturnWidget);
|
package/src/ports/host.tsx
CHANGED
|
@@ -57,6 +57,12 @@ export type PortHost = {
|
|
|
57
57
|
StructureDisplay: React.ComponentType<StructureDisplayProps>;
|
|
58
58
|
StructureViewer: React.ComponentType<StructureViewerProps>;
|
|
59
59
|
useReportError: () => (error: unknown, title?: string) => void;
|
|
60
|
+
/**
|
|
61
|
+
* Draws a port's own blok (a BLOK widget) with what this app knows beyond the form: the agents
|
|
62
|
+
* a run would get (`self`, `deps.<key>`) and the services its queries ask. Left out, a port
|
|
63
|
+
* blok is drawn with neither: its dependencies read unbound and its queries do not run.
|
|
64
|
+
*/
|
|
65
|
+
PortBlokRenderer?: React.ComponentType<import('./widgets/custom/PortBlokWidget').PortBlokRendererProps>;
|
|
60
66
|
};
|
|
61
67
|
|
|
62
68
|
const notConnected: ServiceQuery = async () => {
|
package/src/ports/rekuest.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
/** The kind of assign widget. */
|
|
7
7
|
export const AssignWidgetKind = {
|
|
8
|
+
Blok: 'BLOK',
|
|
8
9
|
Choice: 'CHOICE',
|
|
9
10
|
Custom: 'CUSTOM',
|
|
10
11
|
Proxy: 'PROXY',
|
|
@@ -229,7 +230,12 @@ export type ArgPortFragment = { __typename: 'ArgPort', key: string, label?: stri
|
|
|
229
230
|
& ValidatorFragment
|
|
230
231
|
)> | null, requires?: Array<{ __typename?: 'Requires', key: string, operator: DescriptorOperator, value?: any | null }> | null };
|
|
231
232
|
|
|
232
|
-
|
|
233
|
+
/** A port blok as the port's widget: BSX (`<Blok kind="port">`) the form draws where the port's control would be. */
|
|
234
|
+
export type BlokAssignWidgetFragment = { __typename: 'BlokAssignWidget', kind: AssignWidgetKind, followValue?: string | null, source: string, vocab: number, fallback?: (
|
|
235
|
+
{ __typename: string, kind: AssignWidgetKind }
|
|
236
|
+
) | null };
|
|
237
|
+
|
|
238
|
+
export type AssignWidgetFragment = BlokAssignWidgetFragment | AssignWidget_ChoiceAssignWidget_Fragment | AssignWidget_CustomAssignWidget_Fragment | AssignWidget_ProxyWidget_Fragment | AssignWidget_SearchAssignWidget_Fragment | AssignWidget_SliderAssignWidget_Fragment | AssignWidget_StateChoiceAssignWidget_Fragment | AssignWidget_StringAssignWidget_Fragment;
|
|
233
239
|
|
|
234
240
|
type AssignWidget_ChoiceAssignWidget_Fragment = (
|
|
235
241
|
{ __typename: 'ChoiceAssignWidget', kind: AssignWidgetKind }
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import {cleanup, fireEvent, render, screen} from '@testing-library/react';
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import {useForm, type FieldValues, type UseFormReturn} from 'react-hook-form';
|
|
5
|
+
import {afterEach, describe, expect, it} from 'vitest';
|
|
6
|
+
|
|
7
|
+
import BlokRenderer from '../../../renderer/BlokRenderer';
|
|
8
|
+
import {testCatalog} from '../../../renderer/bloks/testing';
|
|
9
|
+
import {Form} from '../../../vendor/core/ui/form';
|
|
10
|
+
import {createWidgetRegistry} from '../../engine/defaultRegistry';
|
|
11
|
+
import type {ArgPort} from '../../engine/types';
|
|
12
|
+
import {WidgetRegistryContext, type WidgetRegistryContextType} from '../../engine/WidgetsContext';
|
|
13
|
+
import {configurePortHost} from '../../host';
|
|
14
|
+
import {AssignWidgetKind, PortKind, type BlokAssignWidgetFragment} from '../../rekuest';
|
|
15
|
+
import {argsRead, PortBlokWidget, type PortBlokRendererProps} from './PortBlokWidget';
|
|
16
|
+
import {parseBsx} from '../../../bsx/parse';
|
|
17
|
+
|
|
18
|
+
afterEach(cleanup);
|
|
19
|
+
|
|
20
|
+
const SOURCE = `<Blok kind="port" context="panel">
|
|
21
|
+
<Input bind={port.value} placeholder="exposure" />
|
|
22
|
+
<p>{args.limit}</p>
|
|
23
|
+
<Button onClick={port.set(args.limit)}>Longest</Button>
|
|
24
|
+
<Button onClick={port.reset()}>Back</Button>
|
|
25
|
+
</Blok>`;
|
|
26
|
+
|
|
27
|
+
const port = {__typename: 'ArgPort', key: 'exposure', label: 'Exposure', kind: PortKind.Float, nullable: false, description: 'How long the shutter stays open'} as unknown as ArgPort;
|
|
28
|
+
const widget = (source: string): BlokAssignWidgetFragment => ({__typename: 'BlokAssignWidget', kind: AssignWidgetKind.Blok, source, vocab: 2, fallback: null});
|
|
29
|
+
|
|
30
|
+
/** The renderer an app provides: here with the test catalog, and no agents. */
|
|
31
|
+
const TestRenderer = ({tree, data, writePort, children}: PortBlokRendererProps) => (
|
|
32
|
+
<BlokRenderer uiComponents={[tree]} initialState={data} catalog={testCatalog()} writePort={writePort} chrome="none" sizing="intrinsic">
|
|
33
|
+
{children}
|
|
34
|
+
</BlokRenderer>
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const Harness = ({source, held}: {source: string; held: {form?: UseFormReturn}}) => {
|
|
38
|
+
const form = useForm<FieldValues>({defaultValues: {exposure: 5, limit: 9, gain: 1}});
|
|
39
|
+
held.form = form;
|
|
40
|
+
const registry = React.useMemo(() => createWidgetRegistry() as unknown as WidgetRegistryContextType['registry'], []);
|
|
41
|
+
return (
|
|
42
|
+
<WidgetRegistryContext.Provider value={{registry, setRegistry: () => undefined}}>
|
|
43
|
+
<Form {...form}>
|
|
44
|
+
<PortBlokWidget port={port} widget={widget(source)} path={['exposure']} />
|
|
45
|
+
</Form>
|
|
46
|
+
</WidgetRegistryContext.Provider>
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
describe('a port blok as a field of the form', () => {
|
|
51
|
+
it('reads only the arguments its tree names', () => {
|
|
52
|
+
expect(argsRead(parseBsx(SOURCE).tree)).toEqual(['limit']);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('shows the port and the form, and writes the value of its port and nothing else', () => {
|
|
56
|
+
configurePortHost({PortBlokRenderer: TestRenderer});
|
|
57
|
+
const held: {form?: UseFormReturn} = {};
|
|
58
|
+
render(<Harness source={SOURCE} held={held} />);
|
|
59
|
+
|
|
60
|
+
// The form draws its own label and description around a blok that places none.
|
|
61
|
+
expect(screen.getByText('Exposure')).toBeTruthy();
|
|
62
|
+
expect(screen.getByText('How long the shutter stays open')).toBeTruthy();
|
|
63
|
+
const input = screen.getByPlaceholderText('exposure') as HTMLInputElement;
|
|
64
|
+
expect(input.value).toBe('5');
|
|
65
|
+
expect(screen.getByText('9')).toBeTruthy();
|
|
66
|
+
|
|
67
|
+
// A bound control writes the field, which the form then marks dirty.
|
|
68
|
+
fireEvent.change(input, {target: {value: '7'}});
|
|
69
|
+
expect(held.form?.getValues('exposure')).toBe('7');
|
|
70
|
+
expect(held.form?.getFieldState('exposure').isDirty).toBe(true);
|
|
71
|
+
expect(held.form?.getValues('limit')).toBe(9);
|
|
72
|
+
|
|
73
|
+
// port.set writes it from a callback; port.reset puts back what the form opened with.
|
|
74
|
+
fireEvent.click(screen.getByText('Longest'));
|
|
75
|
+
expect(held.form?.getValues('exposure')).toBe(9);
|
|
76
|
+
fireEvent.click(screen.getByText('Back'));
|
|
77
|
+
expect(held.form?.getValues('exposure')).toBe(5);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('falls back to the port’s own control where the source is no port blok', () => {
|
|
81
|
+
configurePortHost({PortBlokRenderer: TestRenderer});
|
|
82
|
+
const held: {form?: UseFormReturn} = {};
|
|
83
|
+
render(<Harness source={'<Blok kind="agent" context="panel"><Card /></Blok>'} held={held} />);
|
|
84
|
+
|
|
85
|
+
expect(screen.queryByPlaceholderText('exposure')).toBeNull();
|
|
86
|
+
expect(screen.getByText('Exposure')).toBeTruthy();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { parseBsxSafe } from "../../../bsx/parse";
|
|
2
|
+
import BlokRenderer from "../../../renderer/BlokRenderer";
|
|
3
|
+
import { appBlokCatalog } from "../../../renderer/catalog";
|
|
4
|
+
import {
|
|
5
|
+
BlokPortPartsProvider,
|
|
6
|
+
type BlokPortOperation,
|
|
7
|
+
type BlokPortParts,
|
|
8
|
+
} from "../../../renderer/bloks/port/host";
|
|
9
|
+
import { splitPathSegments, useBlokRuntimeStoreApi, type BlokComponentNode } from "../../../renderer/runtime";
|
|
10
|
+
import {
|
|
11
|
+
FormDescription,
|
|
12
|
+
FormField,
|
|
13
|
+
FormItem,
|
|
14
|
+
FormLabel,
|
|
15
|
+
FormMessage,
|
|
16
|
+
} from "../../../vendor/core/ui/form";
|
|
17
|
+
import { portHost } from "../../host";
|
|
18
|
+
import { portDescription, portLabel } from "../../engine/portPresentation";
|
|
19
|
+
import { usePortsRoot } from "../../engine/PortsRootContext";
|
|
20
|
+
import { type InputWidgetProps } from "../../engine/types";
|
|
21
|
+
import { pathToName, recursiveExtract, recursiveSet } from "../../engine/utils";
|
|
22
|
+
import { useWidgetRegistry } from "../../engine/WidgetsContext";
|
|
23
|
+
import { type AssignWidgetFragment, type BlokAssignWidgetFragment } from "../../rekuest";
|
|
24
|
+
import * as React from "react";
|
|
25
|
+
import { useController, useFormContext, useWatch } from "react-hook-form";
|
|
26
|
+
|
|
27
|
+
/** The root a port blok reads its port under, and the field of it a control writes. */
|
|
28
|
+
const PORT_ROOT = "port";
|
|
29
|
+
const PORT_VALUE = "value";
|
|
30
|
+
const ARGS_ROOT = "args";
|
|
31
|
+
|
|
32
|
+
/** What a host that knows a run's agents is handed to draw a port blok with (`PortHost.PortBlokRenderer`). */
|
|
33
|
+
export type PortBlokRendererProps = {
|
|
34
|
+
/** The blok: one `<Blok kind="port">`, as its source parsed. */
|
|
35
|
+
tree: BlokComponentNode;
|
|
36
|
+
/** `port` (the port record) and `args` (the form's other arguments the blok reads). */
|
|
37
|
+
data: Record<string, unknown>;
|
|
38
|
+
/** `port.set(...)`, `port.clear()`, `port.reset()`, `port.touch()`. */
|
|
39
|
+
writePort: (operation: BlokPortOperation, value?: unknown) => unknown;
|
|
40
|
+
/** Hands what the blok wrote to `port.value` over to the form; render it inside the renderer. */
|
|
41
|
+
children: React.ReactNode;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** A port blok drawn with no agents and no service: `deps` are unbound, queries do not run. */
|
|
45
|
+
export const DefaultPortBlokRenderer = ({ tree, data, writePort, children }: PortBlokRendererProps) => (
|
|
46
|
+
<BlokRenderer uiComponents={[tree]} initialState={data} catalog={appBlokCatalog()} writePort={writePort} chrome="none" sizing="intrinsic">
|
|
47
|
+
{children}
|
|
48
|
+
</BlokRenderer>
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
/** The `args.<key>` a tree reads, so the form is watched for exactly those fields. */
|
|
52
|
+
export const argsRead = (tree: BlokComponentNode): string[] => {
|
|
53
|
+
const keys = new Set<string>();
|
|
54
|
+
const read = (path: unknown) => {
|
|
55
|
+
if (typeof path !== "string") return;
|
|
56
|
+
const [root, key] = path.split(".");
|
|
57
|
+
if (root === ARGS_ROOT && key) keys.add(key);
|
|
58
|
+
};
|
|
59
|
+
const walkArgument = (argument: unknown) => {
|
|
60
|
+
if (argument === null || typeof argument !== "object") return;
|
|
61
|
+
const entry = argument as Record<string, unknown>;
|
|
62
|
+
read(entry.value_path);
|
|
63
|
+
for (const call of [entry.util_call, entry.agent_call]) {
|
|
64
|
+
const nested = (call as { arguments?: unknown[] } | null | undefined)?.arguments;
|
|
65
|
+
if (Array.isArray(nested)) nested.forEach(walkArgument);
|
|
66
|
+
}
|
|
67
|
+
for (const list of [entry.value_list, entry.value_dict]) if (Array.isArray(list)) list.forEach(walkArgument);
|
|
68
|
+
};
|
|
69
|
+
const walk = (node: BlokComponentNode) => {
|
|
70
|
+
for (const prop of node.props ?? []) {
|
|
71
|
+
const entry = prop as unknown as Record<string, unknown>;
|
|
72
|
+
read((entry.dynamic_value as { path?: unknown } | null | undefined)?.path);
|
|
73
|
+
for (const call of [entry.util_call, entry.agent_call]) {
|
|
74
|
+
const nested = (call as { arguments?: unknown[] } | null | undefined)?.arguments;
|
|
75
|
+
if (Array.isArray(nested)) nested.forEach(walkArgument);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
(node.children ?? []).forEach(walk);
|
|
79
|
+
};
|
|
80
|
+
walk(tree);
|
|
81
|
+
return [...keys].sort();
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Inside the renderer: what a control wrote to `port.value` is handed to the form, which owns
|
|
86
|
+
* the value, and taken back out of the blok's runtime (the form's own update is what it shows).
|
|
87
|
+
*/
|
|
88
|
+
const PortValueHandover = ({ onValue }: { onValue: (next: unknown) => void }) => {
|
|
89
|
+
const store = useBlokRuntimeStoreApi();
|
|
90
|
+
const onValueRef = React.useRef(onValue);
|
|
91
|
+
React.useEffect(() => {
|
|
92
|
+
onValueRef.current = onValue;
|
|
93
|
+
});
|
|
94
|
+
React.useEffect(() => {
|
|
95
|
+
let handingOver = false;
|
|
96
|
+
return store.subscribe(state => {
|
|
97
|
+
if (handingOver) return;
|
|
98
|
+
const written = Object.keys(state.runtimePathValues).filter(path => {
|
|
99
|
+
const [root, field] = splitPathSegments(path);
|
|
100
|
+
return root === PORT_ROOT && field === PORT_VALUE;
|
|
101
|
+
});
|
|
102
|
+
if (written.length === 0) return;
|
|
103
|
+
const port = (state.dataModel as Record<string, unknown> | undefined)?.[PORT_ROOT] as Record<string, unknown> | undefined;
|
|
104
|
+
const next = port?.[PORT_VALUE];
|
|
105
|
+
handingOver = true;
|
|
106
|
+
try {
|
|
107
|
+
written.forEach(path => store.getState().clearRuntimeValue(path));
|
|
108
|
+
} finally {
|
|
109
|
+
handingOver = false;
|
|
110
|
+
}
|
|
111
|
+
onValueRef.current(next);
|
|
112
|
+
});
|
|
113
|
+
}, [store]);
|
|
114
|
+
return null;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
type PortBlokFieldProps = InputWidgetProps<BlokAssignWidgetFragment> & {
|
|
118
|
+
tree: BlokComponentNode;
|
|
119
|
+
/** The port as the form draws it without its blok: what `<control />` places. */
|
|
120
|
+
control: React.ReactNode;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* A port blok as a field of the form. The form owns everything about the field: its value, whether
|
|
125
|
+
* it was touched or changed, what is wrong with it. The blok is shown that as `port`, writes the
|
|
126
|
+
* value through `bind={port.value}` or `port.set(...)`, and places the form's own label,
|
|
127
|
+
* description and message where it wants them; what it does not place, the form draws around it.
|
|
128
|
+
*/
|
|
129
|
+
const PortBlokField = ({ port, widget, path, options, tree, control }: PortBlokFieldProps) => {
|
|
130
|
+
const form = useFormContext();
|
|
131
|
+
const portsRoot = usePortsRoot();
|
|
132
|
+
const name = pathToName(path);
|
|
133
|
+
const { field, fieldState } = useController({ control: form.control, name });
|
|
134
|
+
|
|
135
|
+
// Only the arguments the blok reads are watched: another field changing redraws nothing here.
|
|
136
|
+
const read = React.useMemo(() => argsRead(tree), [tree]);
|
|
137
|
+
const readKey = read.join("\n");
|
|
138
|
+
const names = React.useMemo(
|
|
139
|
+
() => (readKey ? readKey.split("\n") : []).map(key => pathToName([...portsRoot, key])),
|
|
140
|
+
[readKey, portsRoot],
|
|
141
|
+
);
|
|
142
|
+
const watched = useWatch({ control: form.control, name: names }) as unknown[];
|
|
143
|
+
|
|
144
|
+
const editable = options?.disable !== true && field.disabled !== true;
|
|
145
|
+
const value = recursiveExtract(field.value, port);
|
|
146
|
+
const snapshot = JSON.stringify([value, watched, fieldState.error?.message ?? null, fieldState.isTouched, fieldState.isDirty, editable]);
|
|
147
|
+
const data = React.useMemo(
|
|
148
|
+
() => ({
|
|
149
|
+
[ARGS_ROOT]: Object.fromEntries(read.map((key, index) => [key, watched[index] ?? null])),
|
|
150
|
+
[PORT_ROOT]: {
|
|
151
|
+
value: value ?? null,
|
|
152
|
+
key: port.key,
|
|
153
|
+
label: portLabel(port),
|
|
154
|
+
description: port.description ?? null,
|
|
155
|
+
kind: port.kind,
|
|
156
|
+
identifier: port.identifier ?? null,
|
|
157
|
+
nullable: port.nullable === true,
|
|
158
|
+
default: port.default ?? null,
|
|
159
|
+
choices: port.choices ?? [],
|
|
160
|
+
unit: (port as { referenceUnit?: string | null }).referenceUnit ?? null,
|
|
161
|
+
editable,
|
|
162
|
+
error: fieldState.error?.message ?? null,
|
|
163
|
+
invalid: fieldState.invalid,
|
|
164
|
+
touched: fieldState.isTouched,
|
|
165
|
+
dirty: fieldState.isDirty,
|
|
166
|
+
},
|
|
167
|
+
}),
|
|
168
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
169
|
+
[snapshot, port, readKey],
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const setValue = React.useCallback(
|
|
173
|
+
(next: unknown) => {
|
|
174
|
+
if (!editable) return;
|
|
175
|
+
// A cleared value is "not given", which the form says with undefined.
|
|
176
|
+
field.onChange(next === null || next === undefined ? undefined : recursiveSet(next, port));
|
|
177
|
+
},
|
|
178
|
+
[editable, field, port],
|
|
179
|
+
);
|
|
180
|
+
const writePort = React.useCallback(
|
|
181
|
+
(operation: BlokPortOperation, next?: unknown) => {
|
|
182
|
+
if (operation === "touch") return field.onBlur();
|
|
183
|
+
if (!editable) return undefined;
|
|
184
|
+
if (operation === "reset") return form.resetField(name);
|
|
185
|
+
return setValue(operation === "clear" ? null : next);
|
|
186
|
+
},
|
|
187
|
+
[editable, field, form, name, setValue],
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
// What the blok places of the form's own parts it draws; the rest is drawn around it.
|
|
191
|
+
const placed = React.useMemo(() => {
|
|
192
|
+
const names = new Set<string>();
|
|
193
|
+
const walk = (node: BlokComponentNode) => {
|
|
194
|
+
names.add(node.component);
|
|
195
|
+
(node.children ?? []).forEach(walk);
|
|
196
|
+
};
|
|
197
|
+
walk(tree);
|
|
198
|
+
return names;
|
|
199
|
+
}, [tree]);
|
|
200
|
+
const widgetRegistry = useWidgetRegistry().registry;
|
|
201
|
+
const parts = React.useMemo<BlokPortParts>(
|
|
202
|
+
() => ({
|
|
203
|
+
renderLabel: () => <FormLabel>{portLabel(port)}</FormLabel>,
|
|
204
|
+
renderDescription: () => (port.description ? <FormDescription>{portDescription(port, widget)}</FormDescription> : null),
|
|
205
|
+
renderMessage: () => <FormMessage />,
|
|
206
|
+
renderControl: () => control,
|
|
207
|
+
renderField: (key: string) => {
|
|
208
|
+
const child = (port.children ?? []).find(candidate => candidate?.key === key);
|
|
209
|
+
if (!child) return null;
|
|
210
|
+
const Child = widgetRegistry.getInputWidgetForPort(child as typeof port);
|
|
211
|
+
return <Child port={child as typeof port} widget={(child as typeof port).widget as AssignWidgetFragment} path={[...path, key]} options={options} />;
|
|
212
|
+
},
|
|
213
|
+
}),
|
|
214
|
+
[port, widget, control, widgetRegistry, path, options],
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
const Renderer = portHost().PortBlokRenderer ?? DefaultPortBlokRenderer;
|
|
218
|
+
|
|
219
|
+
return (
|
|
220
|
+
<FormField
|
|
221
|
+
control={form.control}
|
|
222
|
+
name={name}
|
|
223
|
+
render={() => (
|
|
224
|
+
<FormItem className="flex flex-col">
|
|
225
|
+
{!placed.has("label") && <FormLabel>{portLabel(port)}</FormLabel>}
|
|
226
|
+
<BlokPortPartsProvider value={parts}>
|
|
227
|
+
<Renderer tree={tree} data={data} writePort={writePort}>
|
|
228
|
+
<PortValueHandover onValue={setValue} />
|
|
229
|
+
</Renderer>
|
|
230
|
+
</BlokPortPartsProvider>
|
|
231
|
+
{!placed.has("description") && port.description && <FormDescription>{portDescription(port, widget)}</FormDescription>}
|
|
232
|
+
{!placed.has("message") && <FormMessage />}
|
|
233
|
+
</FormItem>
|
|
234
|
+
)}
|
|
235
|
+
/>
|
|
236
|
+
);
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* A BLOK assign widget: the port's own blok, written in BSX (`<Blok kind="port">`), drawn where
|
|
241
|
+
* the port's control would be.
|
|
242
|
+
*
|
|
243
|
+
* Where it cannot be drawn (its source does not parse here, or is no port blok), the port
|
|
244
|
+
* degrades the way the server intends: through the widget's `fallback` when one is given, else
|
|
245
|
+
* through what the port would get without a widget. Either way the port stays editable.
|
|
246
|
+
*/
|
|
247
|
+
export const PortBlokWidget = (props: InputWidgetProps<BlokAssignWidgetFragment>) => {
|
|
248
|
+
const { registry } = useWidgetRegistry();
|
|
249
|
+
const fallback = (props.widget?.fallback ?? null) as AssignWidgetFragment | null;
|
|
250
|
+
|
|
251
|
+
const { port, Widget } = React.useMemo(() => {
|
|
252
|
+
const port = { ...props.port, widget: fallback } as typeof props.port;
|
|
253
|
+
return { port, Widget: registry.getInputWidgetForPort(port) };
|
|
254
|
+
}, [props.port, fallback, registry]);
|
|
255
|
+
const plain = <Widget {...props} port={port} widget={fallback as AssignWidgetFragment} />;
|
|
256
|
+
|
|
257
|
+
const source = props.widget?.source;
|
|
258
|
+
const tree = React.useMemo(() => {
|
|
259
|
+
if (!source) return null;
|
|
260
|
+
const parsed = parseBsxSafe(source);
|
|
261
|
+
if (!parsed.ok) return null;
|
|
262
|
+
const kind = (parsed.tree.props ?? []).find(prop => prop.key === "kind")?.static_value;
|
|
263
|
+
return parsed.tree.component === "Blok" && kind === PORT_ROOT ? parsed.tree : null;
|
|
264
|
+
}, [source]);
|
|
265
|
+
if (tree === null) return plain;
|
|
266
|
+
|
|
267
|
+
return <PortBlokField {...props} tree={tree} control={plain} />;
|
|
268
|
+
};
|
package/src/ports.ts
CHANGED
|
@@ -17,6 +17,10 @@ export * from './ports/dependencies/useDependencyTree';
|
|
|
17
17
|
export * from './ports/engine/useAssignForm';
|
|
18
18
|
export * from './ports/engine/usePortForm';
|
|
19
19
|
export * from './ports/host';
|
|
20
|
+
// A port's own blok (a BLOK widget) as a field of the form, and what an app is handed to draw it
|
|
21
|
+
// with its agents and services (`configurePortHost({PortBlokRenderer})`).
|
|
22
|
+
export {argsRead, DefaultPortBlokRenderer, PortBlokWidget, type PortBlokRendererProps} from './ports/widgets/custom/PortBlokWidget';
|
|
23
|
+
export {BlokPortPartsProvider, useBlokPortParts, type BlokPortOperation, type BlokPortParts} from './renderer/bloks/port/host';
|
|
20
24
|
export * from './ports/rekuest';
|
|
21
25
|
export * from './ports/structure';
|
|
22
26
|
export {ArgsContainer} from './ports/widgets/ArgsContainer';
|
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
type BlokSubscription,
|
|
41
41
|
type BlokViewer,
|
|
42
42
|
} from './runtime';
|
|
43
|
+
import type {BlokPortOperation} from './bloks/port/host';
|
|
43
44
|
|
|
44
45
|
type BlokRendererProps = {
|
|
45
46
|
surfaceId?: string;
|
|
@@ -101,6 +102,8 @@ type BlokRendererProps = {
|
|
|
101
102
|
answerTask?: (values: Record<string, unknown>) => unknown;
|
|
102
103
|
/** Starts the run an assign blok prepares (`assign.start()`). */
|
|
103
104
|
startRun?: () => unknown;
|
|
105
|
+
/** Writes the port a port blok is the widget of (`port.set(...)`, `port.clear()`, `port.reset()`, `port.touch()`). */
|
|
106
|
+
writePort?: (operation: BlokPortOperation, value?: unknown) => unknown;
|
|
104
107
|
/** Cancels, pauses, resumes, steps, interrupts or reruns `task` (`task.cancel()` and its kin). */
|
|
105
108
|
controlTask?: (operation: BlokTaskControl) => unknown;
|
|
106
109
|
/** Where `persist` components keep their state by id; none: they keep nothing. */
|
|
@@ -183,6 +186,7 @@ export default function BlokRenderer({
|
|
|
183
186
|
readExecutor,
|
|
184
187
|
answerTask,
|
|
185
188
|
startRun,
|
|
189
|
+
writePort,
|
|
186
190
|
controlTask,
|
|
187
191
|
componentState = null,
|
|
188
192
|
children,
|
|
@@ -256,6 +260,7 @@ export default function BlokRenderer({
|
|
|
256
260
|
travelAt,
|
|
257
261
|
answerTask,
|
|
258
262
|
startRun,
|
|
263
|
+
writePort,
|
|
259
264
|
controlTask,
|
|
260
265
|
});
|
|
261
266
|
return store;
|
|
@@ -284,6 +289,10 @@ export default function BlokRenderer({
|
|
|
284
289
|
runtimeStore.getState().setStartRun(startRun);
|
|
285
290
|
}, [startRun, runtimeStore]);
|
|
286
291
|
|
|
292
|
+
useEffect(() => {
|
|
293
|
+
runtimeStore.getState().setWritePort(writePort);
|
|
294
|
+
}, [writePort, runtimeStore]);
|
|
295
|
+
|
|
287
296
|
useEffect(() => {
|
|
288
297
|
runtimeStore.getState().setControlTask(controlTask);
|
|
289
298
|
}, [controlTask, runtimeStore]);
|