@arkitektio/bsx 6.4.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/references.d.ts +2 -0
- package/dist/bsx/references.d.ts.map +1 -1
- package/dist/bsx/references.js +28 -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 +28 -1
- 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 +1285 -181
- package/dist/spec/catalogs/base@4.json +95 -2
- package/dist/spec/catalogs/base@4.json.js +95 -2
- 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 +44 -10
- package/dist/spec/vocabulary.json.js +44 -10
- package/package.json +1 -1
- package/src/bsx/references.test.ts +1 -1
- package/src/bsx/references.ts +28 -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 +47 -1
- 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 +1285 -181
- package/src/spec/catalogs/base@4.json +95 -2
- package/src/spec/spec.test.ts +7 -3
- package/src/spec/vocabulary.json +44 -10
- package/src/spec/vocabulary.ts +9 -2
|
@@ -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]);
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
type BlokSuspenseReporter,
|
|
22
22
|
} from '../runtime';
|
|
23
23
|
import {useBlokRunHost} from './run/host';
|
|
24
|
+
import {useBlokPortParts} from './port/host';
|
|
24
25
|
import {blokPlatform} from '../../platform';
|
|
25
26
|
|
|
26
27
|
/**
|
|
@@ -301,7 +302,7 @@ export const Blok = createBlokComponent(
|
|
|
301
302
|
placement: {notDownstreamOf: [BLOK_COMPONENT]},
|
|
302
303
|
schema: z
|
|
303
304
|
.object({
|
|
304
|
-
kind: z.enum(['agent', 'custom', 'task', 'action', 'assign']).describe('What the blok is: what it reads, and where it may be mounted.'),
|
|
305
|
+
kind: z.enum(['agent', 'custom', 'task', 'action', 'assign', 'port']).describe('What the blok is: what it reads, and where it may be mounted.'),
|
|
305
306
|
context: z
|
|
306
307
|
.enum(['panel', 'scene', 'logo'])
|
|
307
308
|
.describe('What draws it: a panel (a flat rectangle the host lays out), a scene (a 3D view) or a logo (an SVG drawing).'),
|
|
@@ -667,6 +668,46 @@ export const Timeline = createBlokComponent(
|
|
|
667
668
|
() => <>{useBlokRunHost().renderTimeline?.() ?? null}</>,
|
|
668
669
|
);
|
|
669
670
|
|
|
671
|
+
/* -------------------------------------------------------------------------- */
|
|
672
|
+
/* a port blok's parts of the form */
|
|
673
|
+
/* -------------------------------------------------------------------------- */
|
|
674
|
+
|
|
675
|
+
export const PortLabel = createBlokComponent(
|
|
676
|
+
{name: 'label', schema: z.object({}).describe("The port's label, as the form draws it. The form draws its own above a port blok that places none.")},
|
|
677
|
+
() => <>{useBlokPortParts().renderLabel()}</>,
|
|
678
|
+
);
|
|
679
|
+
|
|
680
|
+
export const PortDescription = createBlokComponent(
|
|
681
|
+
{name: 'description', schema: z.object({}).describe("The port's description, as the form draws it.")},
|
|
682
|
+
() => <>{useBlokPortParts().renderDescription()}</>,
|
|
683
|
+
);
|
|
684
|
+
|
|
685
|
+
export const PortMessage = createBlokComponent(
|
|
686
|
+
{
|
|
687
|
+
name: 'message',
|
|
688
|
+
schema: z.object({}).describe("What the form says is wrong with the port's value (`port.error`), as the form draws it; nothing while the value is fine."),
|
|
689
|
+
},
|
|
690
|
+
() => <>{useBlokPortParts().renderMessage()}</>,
|
|
691
|
+
);
|
|
692
|
+
|
|
693
|
+
export const PortControl = createBlokComponent(
|
|
694
|
+
{
|
|
695
|
+
name: 'control',
|
|
696
|
+
schema: z.object({}).describe("The control the form would draw for this port without a blok, bound to the port: place it to draw around the standard control instead of replacing it."),
|
|
697
|
+
},
|
|
698
|
+
() => <>{useBlokPortParts().renderControl()}</>,
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
const fieldSchema = z
|
|
702
|
+
.object({key: z.string().describe('The key of the child port: `key="x"`.')})
|
|
703
|
+
.describe('One child port of the port (a field of a MODEL), as its own widget draws it, bound below the port\'s value.');
|
|
704
|
+
|
|
705
|
+
export const PortField = createBlokComponent({name: 'field', schema: fieldSchema}, ({component, schema}) => {
|
|
706
|
+
const key = useValue(useBlok(component, schema).key);
|
|
707
|
+
const parts = useBlokPortParts();
|
|
708
|
+
return <>{typeof key === 'string' ? parts.renderField(key) : null}</>;
|
|
709
|
+
});
|
|
710
|
+
|
|
670
711
|
export const controlBlokComponents = [
|
|
671
712
|
TextNode,
|
|
672
713
|
Foreach,
|
|
@@ -692,4 +733,9 @@ export const controlBlokComponents = [
|
|
|
692
733
|
Returns,
|
|
693
734
|
Status,
|
|
694
735
|
Timeline,
|
|
736
|
+
PortLabel,
|
|
737
|
+
PortDescription,
|
|
738
|
+
PortMessage,
|
|
739
|
+
PortControl,
|
|
740
|
+
PortField,
|
|
695
741
|
];
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a port blok needs from the form it is a field of.
|
|
5
|
+
*
|
|
6
|
+
* A port blok (`<Blok kind="port">`) is the widget of one port of an action's form. The blok
|
|
7
|
+
* core draws its tree; the form owns the field: its label, its description, what is wrong with
|
|
8
|
+
* its value, the control it would draw without a blok, and the child ports below it. A blok
|
|
9
|
+
* that places one of these parts (`<label />`, `<description />`, `<message />`, `<control />`,
|
|
10
|
+
* `<field key="..." />`) gets it drawn by the form through this context, and the form then
|
|
11
|
+
* draws none of its own for it.
|
|
12
|
+
*
|
|
13
|
+
* Outside a form nothing is drawn: the parts are empty.
|
|
14
|
+
*/
|
|
15
|
+
export type BlokPortParts = {
|
|
16
|
+
/** The port's label, as the form draws it (`<label />`). */
|
|
17
|
+
renderLabel: () => React.ReactNode;
|
|
18
|
+
/** The port's description, as the form draws it (`<description />`). */
|
|
19
|
+
renderDescription: () => React.ReactNode;
|
|
20
|
+
/** What the form says is wrong with the value (`<message />`); nothing while it is fine. */
|
|
21
|
+
renderMessage: () => React.ReactNode;
|
|
22
|
+
/** The control the form would draw for this port without a blok, bound to the port (`<control />`). */
|
|
23
|
+
renderControl: () => React.ReactNode;
|
|
24
|
+
/** One child port of the port, by key, as its own widget draws it (`<field key="x" />`). */
|
|
25
|
+
renderField: (key: string) => React.ReactNode;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const DETACHED_PORT: BlokPortParts = {
|
|
29
|
+
renderLabel: () => null,
|
|
30
|
+
renderDescription: () => null,
|
|
31
|
+
renderMessage: () => null,
|
|
32
|
+
renderControl: () => null,
|
|
33
|
+
renderField: () => null,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const BlokPortPartsContext = React.createContext<BlokPortParts>(DETACHED_PORT);
|
|
37
|
+
|
|
38
|
+
export const BlokPortPartsProvider = BlokPortPartsContext.Provider;
|
|
39
|
+
|
|
40
|
+
export const useBlokPortParts = (): BlokPortParts => React.useContext(BlokPortPartsContext);
|
|
41
|
+
|
|
42
|
+
/** What a port blok does to its port from a callback (`port.set(...)`, `port.clear()`, `port.reset()`, `port.touch()`). */
|
|
43
|
+
export type BlokPortOperation = 'set' | 'clear' | 'reset' | 'touch';
|
|
44
|
+
|
|
45
|
+
/** The parts a port blok may place, as its stored tree names them. */
|
|
46
|
+
export const BLOK_PORT_PARTS = ['label', 'description', 'message', 'control', 'field'] as const;
|
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
BlokRuntimeStore,
|
|
17
17
|
} from './types';
|
|
18
18
|
import {invokeBlokFunction} from '../functions/v2/handler';
|
|
19
|
+
import type {BlokPortOperation} from '../bloks/port/host';
|
|
19
20
|
|
|
20
21
|
/** The blok language's functions (the vocabulary's): what a store calls unless its host says otherwise. */
|
|
21
22
|
const defaultInvokeFunction: BlokInvokeFunctionHandler = invokeBlokFunction;
|
|
@@ -159,6 +160,7 @@ export const createBlokRuntimeStore = (config?: {
|
|
|
159
160
|
travelAt?: string | null;
|
|
160
161
|
answerTask?: (values: Record<string, unknown>) => unknown;
|
|
161
162
|
startRun?: () => unknown;
|
|
163
|
+
writePort?: (operation: BlokPortOperation, value?: unknown) => unknown;
|
|
162
164
|
controlTask?: (operation: BlokTaskControl) => unknown;
|
|
163
165
|
}): BlokRuntimeStore => {
|
|
164
166
|
const initialDataModel = config?.initialDataModel;
|
|
@@ -184,6 +186,7 @@ export const createBlokRuntimeStore = (config?: {
|
|
|
184
186
|
fetchMoreQuery: undefined,
|
|
185
187
|
answerTask: config?.answerTask,
|
|
186
188
|
startRun: config?.startRun,
|
|
189
|
+
writePort: config?.writePort,
|
|
187
190
|
controlTask: config?.controlTask,
|
|
188
191
|
setStatus: (path, status) => {
|
|
189
192
|
const key = canonicalStatusPath(path);
|
|
@@ -224,6 +227,9 @@ export const createBlokRuntimeStore = (config?: {
|
|
|
224
227
|
setStartRun: start => {
|
|
225
228
|
set({startRun: start});
|
|
226
229
|
},
|
|
230
|
+
setWritePort: write => {
|
|
231
|
+
set({writePort: write});
|
|
232
|
+
},
|
|
227
233
|
setControlTask: control => {
|
|
228
234
|
set({controlTask: control});
|
|
229
235
|
},
|
|
@@ -195,6 +195,7 @@ const createLiveResolutionContext = (
|
|
|
195
195
|
fetchMoreQuery: (name, variables) => store.getState().fetchMoreQuery?.(name, variables),
|
|
196
196
|
answerTask: values => store.getState().answerTask?.(values),
|
|
197
197
|
startRun: () => store.getState().startRun?.(),
|
|
198
|
+
writePort: store.getState().writePort ? (operation, value) => store.getState().writePort?.(operation, value) : undefined,
|
|
198
199
|
controlTask: store.getState().controlTask ? operation => store.getState().controlTask?.(operation) : undefined,
|
|
199
200
|
travelling: store.getState().travelAt !== null,
|
|
200
201
|
invokeFunction: (name, args, options) => store.getState().invokeFunction(name, args, options),
|
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
BlokRuntimeDependencies,
|
|
14
14
|
BlokUtilCall,
|
|
15
15
|
} from './types';
|
|
16
|
+
import type {BlokPortOperation} from '../bloks/port/host';
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* How a prop's value is worked out from the data model: paths, literals and
|
|
@@ -392,6 +393,13 @@ const resolvePropValue = (
|
|
|
392
393
|
export const COPY_EFFECT = 'ui.copy';
|
|
393
394
|
export const ANSWER_EFFECT = 'task.answer';
|
|
394
395
|
export const START_EFFECT = 'assign.start';
|
|
396
|
+
/** The effects a port blok writes its port with, by the operation the form runs. */
|
|
397
|
+
export const PORT_EFFECTS: Readonly<Record<string, BlokPortOperation>> = {
|
|
398
|
+
'port.set': 'set',
|
|
399
|
+
'port.clear': 'clear',
|
|
400
|
+
'port.reset': 'reset',
|
|
401
|
+
'port.touch': 'touch',
|
|
402
|
+
};
|
|
395
403
|
/** `task.cancel()` and its kin: what the viewer does to the task the blok is about. */
|
|
396
404
|
const TASK_CONTROL_PREFIX = 'task.';
|
|
397
405
|
export const taskControlOf = (operation: string): BlokTaskControl | null => {
|
|
@@ -523,6 +531,13 @@ const runActionCall = (
|
|
|
523
531
|
// What the host returns (a promise, while the task takes it) keeps the control busy.
|
|
524
532
|
return {ok: true, value: context.controlTask(control)};
|
|
525
533
|
}
|
|
534
|
+
const portOperation = PORT_EFFECTS[operation];
|
|
535
|
+
if (portOperation !== undefined) {
|
|
536
|
+
if (context.travelling) return {ok: true, value: undefined};
|
|
537
|
+
if (!context.writePort) return {ok: false, error: `${operation}: this blok is the widget of no port.`};
|
|
538
|
+
// `port.set(null)` empties the port, as `port.clear()` does.
|
|
539
|
+
return {ok: true, value: context.writePort(portOperation, portOperation === 'set' ? resolvedArguments.value.value : undefined)};
|
|
540
|
+
}
|
|
526
541
|
if (operation === START_EFFECT) {
|
|
527
542
|
if (context.travelling) return {ok: true, value: undefined};
|
|
528
543
|
if (!context.startRun) return {ok: false, error: 'assign.start: nothing here starts a run.'};
|
|
@@ -531,7 +546,7 @@ const runActionCall = (
|
|
|
531
546
|
}
|
|
532
547
|
return {
|
|
533
548
|
ok: false,
|
|
534
|
-
error: `${operation} is not a callback's one thing: a callback holds one task call or one effect (queries.<name>.refetch(), queries.<name>.fetch_more(...), ui.copy, task.answer, assign.start).`,
|
|
549
|
+
error: `${operation} is not a callback's one thing: a callback holds one task call or one effect (queries.<name>.refetch(), queries.<name>.fetch_more(...), ui.copy, task.answer, assign.start, port.set).`,
|
|
535
550
|
};
|
|
536
551
|
};
|
|
537
552
|
|
|
@@ -2,6 +2,7 @@ import type {StoreApi} from 'zustand/vanilla';
|
|
|
2
2
|
import {z} from 'zod';
|
|
3
3
|
import type {BlokSourceStatus} from './status';
|
|
4
4
|
import type {BlokTaskAsk, BlokTaskBinding, BlokTaskControl} from './task';
|
|
5
|
+
import type {BlokPortOperation} from '../bloks/port/host';
|
|
5
6
|
|
|
6
7
|
export type ChildDescriptor = string | {id: string; basePath?: string};
|
|
7
8
|
|
|
@@ -221,6 +222,8 @@ export type BlokResolutionContext = {
|
|
|
221
222
|
answerTask?: (values: Record<string, unknown>) => unknown;
|
|
222
223
|
/** Starts the run an assign blok prepares (`assign.start()`); undefined where the host starts none. */
|
|
223
224
|
startRun?: () => unknown;
|
|
225
|
+
/** Writes the port a port blok is the widget of (`port.set(value)`, `port.clear()`, `port.reset()`, `port.touch()`); undefined outside a form. */
|
|
226
|
+
writePort?: (operation: BlokPortOperation, value?: unknown) => unknown;
|
|
224
227
|
/** Cancels, pauses, resumes, steps, interrupts or reruns `task` (`task.cancel()` and its kin); undefined where the host controls no task. */
|
|
225
228
|
controlTask?: (operation: BlokTaskControl) => unknown;
|
|
226
229
|
/** Set while the blok shows a moment in the past: no command runs. */
|
|
@@ -259,6 +262,8 @@ export type BlokRuntimeContext = {
|
|
|
259
262
|
answerTask: ((values: Record<string, unknown>) => unknown) | undefined;
|
|
260
263
|
/** Starts the run an assign blok prepares; set by a host that can start one. */
|
|
261
264
|
startRun: (() => unknown) | undefined;
|
|
265
|
+
/** Writes the port a port blok is the widget of; set by the form that holds the field. */
|
|
266
|
+
writePort: ((operation: BlokPortOperation, value?: unknown) => unknown) | undefined;
|
|
262
267
|
/** Controls `task`; set by a host that can. */
|
|
263
268
|
controlTask: ((operation: BlokTaskControl) => unknown) | undefined;
|
|
264
269
|
setStatus: (path: string, status: BlokSourceStatus | null) => void;
|
|
@@ -267,6 +272,7 @@ export type BlokRuntimeContext = {
|
|
|
267
272
|
setFetchMoreQuery: (fetchMore: ((name: string, variables: Record<string, unknown>) => void) | undefined) => void;
|
|
268
273
|
setAnswerTask: (answer: ((values: Record<string, unknown>) => unknown) | undefined) => void;
|
|
269
274
|
setStartRun: (start: (() => unknown) | undefined) => void;
|
|
275
|
+
setWritePort: (write: ((operation: BlokPortOperation, value?: unknown) => unknown) | undefined) => void;
|
|
270
276
|
setControlTask: (control: ((operation: BlokTaskControl) => unknown) | undefined) => void;
|
|
271
277
|
agentMappingStateUpdates: BlokAgentMappingStateUpdates;
|
|
272
278
|
invokeFunction: BlokInvokeFunctionHandler;
|