@arkitektio/bsx 6.4.0 → 6.6.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.
Files changed (126) hide show
  1. package/dist/bsx/references.d.ts +2 -0
  2. package/dist/bsx/references.d.ts.map +1 -1
  3. package/dist/bsx/references.js +28 -2
  4. package/dist/bsx/references.js.map +1 -1
  5. package/dist/ports/engine/defaultRegistry.d.ts.map +1 -1
  6. package/dist/ports/engine/defaultRegistry.js +2 -0
  7. package/dist/ports/engine/defaultRegistry.js.map +1 -1
  8. package/dist/ports/forms/ListSearchField.d.ts.map +1 -1
  9. package/dist/ports/forms/ListSearchField.js +4 -2
  10. package/dist/ports/forms/ListSearchField.js.map +1 -1
  11. package/dist/ports/forms/SearchField.d.ts.map +1 -1
  12. package/dist/ports/forms/SearchField.js +4 -2
  13. package/dist/ports/forms/SearchField.js.map +1 -1
  14. package/dist/ports/forms/StructurePickerControl.d.ts +16 -1
  15. package/dist/ports/forms/StructurePickerControl.d.ts.map +1 -1
  16. package/dist/ports/forms/StructurePickerControl.js +26 -3
  17. package/dist/ports/forms/StructurePickerControl.js.map +1 -1
  18. package/dist/ports/host.d.ts +6 -0
  19. package/dist/ports/host.d.ts.map +1 -1
  20. package/dist/ports/host.js.map +1 -1
  21. package/dist/ports/rekuest.d.ts +25 -7
  22. package/dist/ports/rekuest.d.ts.map +1 -1
  23. package/dist/ports/rekuest.js +1 -0
  24. package/dist/ports/rekuest.js.map +1 -1
  25. package/dist/ports/structure/Structure.d.ts.map +1 -1
  26. package/dist/ports/structure/Structure.js +7 -4
  27. package/dist/ports/structure/Structure.js.map +1 -1
  28. package/dist/ports/widgets/custom/ListSearchWidget.d.ts.map +1 -1
  29. package/dist/ports/widgets/custom/ListSearchWidget.js +3 -2
  30. package/dist/ports/widgets/custom/ListSearchWidget.js.map +1 -1
  31. package/dist/ports/widgets/custom/PortBlokWidget.d.ts +30 -0
  32. package/dist/ports/widgets/custom/PortBlokWidget.d.ts.map +1 -0
  33. package/dist/ports/widgets/custom/PortBlokWidget.js +210 -0
  34. package/dist/ports/widgets/custom/PortBlokWidget.js.map +1 -0
  35. package/dist/ports/widgets/custom/SearchWidget.d.ts.map +1 -1
  36. package/dist/ports/widgets/custom/SearchWidget.js +3 -2
  37. package/dist/ports/widgets/custom/SearchWidget.js.map +1 -1
  38. package/dist/ports/widgets/fallbacks/EnumWidget.d.ts.map +1 -1
  39. package/dist/ports/widgets/fallbacks/EnumWidget.js +5 -1
  40. package/dist/ports/widgets/fallbacks/EnumWidget.js.map +1 -1
  41. package/dist/ports.d.ts +2 -0
  42. package/dist/ports.d.ts.map +1 -1
  43. package/dist/ports.js +4 -0
  44. package/dist/ports.js.map +1 -1
  45. package/dist/renderer/BlokRenderer.d.ts +4 -1
  46. package/dist/renderer/BlokRenderer.d.ts.map +1 -1
  47. package/dist/renderer/BlokRenderer.js +5 -1
  48. package/dist/renderer/BlokRenderer.js.map +1 -1
  49. package/dist/renderer/bloks/control.d.ts +5 -0
  50. package/dist/renderer/bloks/control.d.ts.map +1 -1
  51. package/dist/renderer/bloks/control.js +28 -1
  52. package/dist/renderer/bloks/control.js.map +1 -1
  53. package/dist/renderer/bloks/port/host.d.ts +32 -0
  54. package/dist/renderer/bloks/port/host.d.ts.map +1 -0
  55. package/dist/renderer/bloks/port/host.js +14 -0
  56. package/dist/renderer/bloks/port/host.js.map +1 -0
  57. package/dist/renderer/bloks/scene/Model.d.ts.map +1 -1
  58. package/dist/renderer/bloks/scene/Model.js +16 -2
  59. package/dist/renderer/bloks/scene/Model.js.map +1 -1
  60. package/dist/renderer/runtime/context.d.ts +2 -0
  61. package/dist/renderer/runtime/context.d.ts.map +1 -1
  62. package/dist/renderer/runtime/context.js +4 -0
  63. package/dist/renderer/runtime/context.js.map +1 -1
  64. package/dist/renderer/runtime/hooks.d.ts.map +1 -1
  65. package/dist/renderer/runtime/hooks.js +1 -0
  66. package/dist/renderer/runtime/hooks.js.map +1 -1
  67. package/dist/renderer/runtime/resolution.d.ts +3 -0
  68. package/dist/renderer/runtime/resolution.d.ts.map +1 -1
  69. package/dist/renderer/runtime/resolution.js +17 -1
  70. package/dist/renderer/runtime/resolution.js.map +1 -1
  71. package/dist/renderer/runtime/types.d.ts +6 -0
  72. package/dist/renderer/runtime/types.d.ts.map +1 -1
  73. package/dist/renderer/runtime/types.js.map +1 -1
  74. package/dist/spec/bsx_v2.json +1285 -181
  75. package/dist/spec/catalogs/base@4.json +95 -2
  76. package/dist/spec/catalogs/base@4.json.js +95 -2
  77. package/dist/spec/vocabulary.d.ts +5 -1
  78. package/dist/spec/vocabulary.d.ts.map +1 -1
  79. package/dist/spec/vocabulary.js +4 -1
  80. package/dist/spec/vocabulary.js.map +1 -1
  81. package/dist/spec/vocabulary.json +44 -10
  82. package/dist/spec/vocabulary.json.js +44 -10
  83. package/dist/vendor/core/ui/name.d.ts +30 -0
  84. package/dist/vendor/core/ui/name.d.ts.map +1 -0
  85. package/dist/vendor/core/ui/name.js +40 -0
  86. package/dist/vendor/core/ui/name.js.map +1 -0
  87. package/dist/vendor/core/ui/overflow-tooltip.d.ts +20 -0
  88. package/dist/vendor/core/ui/overflow-tooltip.d.ts.map +1 -0
  89. package/dist/vendor/core/ui/overflow-tooltip.js +27 -0
  90. package/dist/vendor/core/ui/overflow-tooltip.js.map +1 -0
  91. package/dist/vendor/core/ui/select.js +3 -3
  92. package/dist/vendor/core/ui/select.js.map +1 -1
  93. package/package.json +1 -1
  94. package/src/bsx/references.test.ts +1 -1
  95. package/src/bsx/references.ts +28 -2
  96. package/src/ports/engine/defaultRegistry.tsx +2 -0
  97. package/src/ports/forms/ListSearchField.tsx +12 -9
  98. package/src/ports/forms/SearchField.tsx +9 -3
  99. package/src/ports/forms/StructurePickerControl.test.tsx +29 -1
  100. package/src/ports/forms/StructurePickerControl.tsx +38 -4
  101. package/src/ports/host.tsx +6 -0
  102. package/src/ports/rekuest.ts +14 -8
  103. package/src/ports/structure/Structure.tsx +14 -5
  104. package/src/ports/widgets/custom/ListSearchWidget.tsx +3 -2
  105. package/src/ports/widgets/custom/PortBlokWidget.test.tsx +88 -0
  106. package/src/ports/widgets/custom/PortBlokWidget.tsx +268 -0
  107. package/src/ports/widgets/custom/SearchWidget.tsx +3 -2
  108. package/src/ports/widgets/fallbacks/EnumWidget.test.tsx +89 -0
  109. package/src/ports/widgets/fallbacks/EnumWidget.tsx +38 -13
  110. package/src/ports.ts +4 -0
  111. package/src/renderer/BlokRenderer.tsx +9 -0
  112. package/src/renderer/bloks/control.tsx +47 -1
  113. package/src/renderer/bloks/port/host.tsx +46 -0
  114. package/src/renderer/bloks/scene/Model.tsx +15 -2
  115. package/src/renderer/runtime/context.tsx +6 -0
  116. package/src/renderer/runtime/hooks.ts +1 -0
  117. package/src/renderer/runtime/resolution.ts +16 -1
  118. package/src/renderer/runtime/types.ts +6 -0
  119. package/src/spec/bsx_v2.json +1285 -181
  120. package/src/spec/catalogs/base@4.json +95 -2
  121. package/src/spec/spec.test.ts +7 -3
  122. package/src/spec/vocabulary.json +44 -10
  123. package/src/spec/vocabulary.ts +9 -2
  124. package/src/vendor/core/ui/name.tsx +69 -0
  125. package/src/vendor/core/ui/overflow-tooltip.tsx +59 -0
  126. package/src/vendor/core/ui/select.tsx +3 -3
@@ -0,0 +1,89 @@
1
+ // @vitest-environment jsdom
2
+ import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
3
+ import { FormProvider, useForm } from "react-hook-form";
4
+ import { afterEach, describe, expect, it, vi } from "vitest";
5
+
6
+ vi.mock("../../rekuest", () => ({ PortKind: { Enum: "ENUM" } }));
7
+
8
+ import type { InputWidgetProps } from "../../engine/types";
9
+ import { ChoicePortField } from "./EnumWidget";
10
+
11
+ const LONG = "A choice whose label is far too long for the column it sits in";
12
+
13
+ const portWith = (choices: { value: string; label: string; description?: string }[]) =>
14
+ ({ key: "mode", kind: "ENUM", label: "Mode", choices }) as unknown as InputWidgetProps["port"];
15
+
16
+ const Harness = ({ port, value }: { port: InputWidgetProps["port"]; value?: string }) => {
17
+ const form = useForm({ defaultValues: { mode: value } });
18
+ return (
19
+ <FormProvider {...form}>
20
+ <ChoicePortField port={port} widget={null as never} path={["mode"]} />
21
+ </FormProvider>
22
+ );
23
+ };
24
+
25
+ /** jsdom lays nothing out: say every marked text is (or is not) cut off. */
26
+ const cutOff = (yes: boolean) => {
27
+ vi.spyOn(HTMLElement.prototype, "scrollWidth", "get").mockImplementation(function (
28
+ this: HTMLElement,
29
+ ) {
30
+ return yes && this.hasAttribute("data-overflow-text") ? 200 : 0;
31
+ });
32
+ vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockReturnValue(0);
33
+ };
34
+
35
+ const tooltip = () => document.querySelector("[data-slot=tooltip-content]");
36
+
37
+ afterEach(() => {
38
+ cleanup();
39
+ vi.restoreAllMocks();
40
+ });
41
+
42
+ describe("ChoicePortField: segmented", () => {
43
+ const port = portWith([
44
+ { value: "a", label: LONG },
45
+ { value: "b", label: "Short", description: "What short does" },
46
+ ]);
47
+
48
+ it("shows no tooltip for a label that fits", () => {
49
+ cutOff(false);
50
+ render(<Harness port={port} />);
51
+ act(() => void fireEvent.focus(screen.getByRole("radio", { name: LONG })));
52
+ expect(tooltip()).toBeNull();
53
+ });
54
+
55
+ it("shows the whole label when it is cut off", () => {
56
+ cutOff(true);
57
+ render(<Harness port={port} />);
58
+ act(() => void fireEvent.focus(screen.getByRole("radio", { name: LONG })));
59
+ expect(tooltip()?.textContent).toContain(LONG);
60
+ });
61
+
62
+ it("always shows a choice's description, under its label", () => {
63
+ cutOff(false);
64
+ render(<Harness port={port} />);
65
+ act(() => void fireEvent.focus(screen.getByRole("radio", { name: "Short" })));
66
+ expect(tooltip()?.textContent).toContain("Short");
67
+ expect(tooltip()?.textContent).toContain("What short does");
68
+ });
69
+ });
70
+
71
+ describe("ChoicePortField: select", () => {
72
+ const port = portWith(
73
+ Array.from({ length: 6 }, (_, i) => ({ value: `v${i}`, label: i === 2 ? LONG : `Choice ${i}` })),
74
+ );
75
+
76
+ it("shows the chosen label when the trigger cuts it off", () => {
77
+ cutOff(true);
78
+ render(<Harness port={port} value="v2" />);
79
+ act(() => void fireEvent.focus(screen.getByRole("combobox")));
80
+ expect(tooltip()?.textContent).toContain(LONG);
81
+ });
82
+
83
+ it("shows nothing when the chosen label fits", () => {
84
+ cutOff(false);
85
+ render(<Harness port={port} value="v2" />);
86
+ act(() => void fireEvent.focus(screen.getByRole("combobox")));
87
+ expect(tooltip()).toBeNull();
88
+ });
89
+ });
@@ -1,4 +1,5 @@
1
1
  import { SearchField, type SearchOptions } from "../../forms/SearchField";
2
+ import { Name } from "../../../vendor/core/ui/name";
2
3
  import {
3
4
  choicePresentation,
4
5
  portDescription,
@@ -24,6 +25,7 @@ import {
24
25
  SelectValue,
25
26
  } from "../../../vendor/core/ui/select";
26
27
  import { ToggleGroup, ToggleGroupItem } from "../../../vendor/core/ui/toggle-group";
28
+ import { OVERFLOW_TEXT, OverflowTooltip } from "../../../vendor/core/ui/overflow-tooltip";
27
29
  import { notEmpty } from "../../../vendor/core/util/utils";
28
30
  import { X } from "lucide-react";
29
31
  import { useCallback, useMemo } from "react";
@@ -31,6 +33,14 @@ import { useFormContext } from "react-hook-form";
31
33
 
32
34
  type Choice = { value: string; label: string; description?: string | null };
33
35
 
36
+ /** What a cut-off choice shows on hover: its whole label, then its description. */
37
+ const ChoiceTip = ({ choice }: { choice: Choice }) => (
38
+ <div className="flex flex-col gap-0.5">
39
+ <span className="font-medium">{choice.label}</span>
40
+ {choice.description && <span className="opacity-80">{choice.description}</span>}
41
+ </div>
42
+ );
43
+
34
44
  /**
35
45
  * A port's `choices`, shown by how many there are: a few sit side by side,
36
46
  * a handful go in a dropdown, a long list is searched. The stored value is
@@ -102,29 +112,44 @@ export const ChoicePortField = (props: InputWidgetProps) => {
102
112
  className="w-full"
103
113
  >
104
114
  {choices.map((choice) => (
105
- <ToggleGroupItem
115
+ <OverflowTooltip
106
116
  key={choice.value}
107
- value={choice.value}
108
- title={choice.description ?? undefined}
109
- className="min-w-0 flex-1 px-2"
117
+ content={<ChoiceTip choice={choice} />}
118
+ always={!!choice.description}
110
119
  >
111
- <span className="truncate">{choice.label}</span>
112
- </ToggleGroupItem>
120
+ <ToggleGroupItem
121
+ value={choice.value}
122
+ aria-label={choice.label}
123
+ className="min-w-0 flex-1 px-2"
124
+ >
125
+ <Name>{choice.label}</Name>
126
+ </ToggleGroupItem>
127
+ </OverflowTooltip>
113
128
  ))}
114
129
  </ToggleGroup>
115
130
  </FormControl>
116
131
  ) : (
117
- <div className="flex items-center gap-1">
132
+ <div className="flex min-w-0 items-center gap-1">
118
133
  <Select value={value} onValueChange={field.onChange}>
119
- <FormControl>
120
- <SelectTrigger className="w-full min-w-0">
121
- <SelectValue placeholder={placeholder} />
122
- </SelectTrigger>
123
- </FormControl>
134
+ <OverflowTooltip
135
+ content={chosen ? <ChoiceTip choice={chosen} /> : placeholder}
136
+ >
137
+ <FormControl>
138
+ <SelectTrigger className="w-full min-w-0">
139
+ <SelectValue {...OVERFLOW_TEXT} placeholder={placeholder} />
140
+ </SelectTrigger>
141
+ </FormControl>
142
+ </OverflowTooltip>
124
143
  <SelectContent>
125
144
  {choices.map((choice) => (
126
145
  <SelectItem key={choice.value} value={choice.value}>
127
- {choice.label}
146
+ {/* Wraps in the list; cut off (with a tooltip) in the trigger. */}
147
+ <span
148
+ {...OVERFLOW_TEXT}
149
+ className="in-data-[slot=select-value]:min-w-0 in-data-[slot=select-value]:truncate"
150
+ >
151
+ {choice.label}
152
+ </span>
128
153
  </SelectItem>
129
154
  ))}
130
155
  </SelectContent>
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;
@@ -60,8 +60,21 @@ const Placeholder = ({reason, source}: {reason: string; source?: BlokAssetSource
60
60
  const CLICK_SLOP_PX = 4;
61
61
 
62
62
  const HoverBox = ({node}: {node: THREE.Object3D}) => {
63
- const helper = React.useMemo(() => new THREE.BoxHelper(node, 0x38bdf8), [node]);
64
- useFrame(() => helper.update());
63
+ const helper = React.useMemo(() => {
64
+ const made = new THREE.BoxHelper(node, 0x38bdf8);
65
+ // Its corners are world coordinates already: it must not also take the pose of
66
+ // whatever it is mounted in (a placement in a space), so its own matrix undoes that.
67
+ made.matrixAutoUpdate = false;
68
+ return made;
69
+ }, [node]);
70
+ useFrame(() => {
71
+ helper.update();
72
+ const parent = helper.parent;
73
+ if (!parent) return;
74
+ parent.updateWorldMatrix(true, false);
75
+ helper.matrix.copy(parent.matrixWorld).invert();
76
+ helper.matrixWorldNeedsUpdate = true;
77
+ });
65
78
  React.useEffect(() => () => helper.dispose(), [helper]);
66
79
  return <primitive object={helper} />;
67
80
  };
@@ -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;