codeassembly-agent-logic 0.0.0 → 0.2.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 (169) hide show
  1. package/.meta/changelog.json +105 -0
  2. package/CHANGELOG.md +38 -0
  3. package/LICENSE +7 -0
  4. package/README.md +77 -1
  5. package/bin/agent-logic.js +26 -0
  6. package/dist/esm/cli/check-coverage-command.d.ts +3 -0
  7. package/dist/esm/cli/check-coverage-command.js +99 -0
  8. package/dist/esm/cli/convert-command.d.ts +3 -0
  9. package/dist/esm/cli/convert-command.js +116 -0
  10. package/dist/esm/cli/find-package-root.d.ts +7 -0
  11. package/dist/esm/cli/find-package-root.js +31 -0
  12. package/dist/esm/cli/index.d.ts +1 -0
  13. package/dist/esm/cli/index.js +11 -0
  14. package/dist/esm/cli/run.d.ts +8 -0
  15. package/dist/esm/cli/run.js +62 -0
  16. package/dist/esm/contract/built-in-harnesses.d.ts +22 -0
  17. package/dist/esm/contract/built-in-harnesses.js +30 -0
  18. package/dist/esm/contract/conversion-job.d.ts +36 -0
  19. package/dist/esm/contract/conversion-job.js +17 -0
  20. package/dist/esm/contract/index.d.ts +4 -0
  21. package/dist/esm/contract/index.js +3 -0
  22. package/dist/esm/contract/viewer-contract.d.ts +804 -0
  23. package/dist/esm/contract/viewer-contract.js +38 -0
  24. package/dist/esm/convert/convert-prompt.d.ts +21 -0
  25. package/dist/esm/convert/convert-prompt.js +60 -0
  26. package/dist/esm/convert/draft-schema.d.ts +308 -0
  27. package/dist/esm/convert/draft-schema.js +57 -0
  28. package/dist/esm/convert/find-gate-spans.d.ts +6 -0
  29. package/dist/esm/convert/find-gate-spans.js +18 -0
  30. package/dist/esm/convert/format-state-name.d.ts +6 -0
  31. package/dist/esm/convert/format-state-name.js +6 -0
  32. package/dist/esm/convert/normalize-config.d.ts +11 -0
  33. package/dist/esm/convert/normalize-config.js +164 -0
  34. package/dist/esm/convert/resolve-quotes.d.ts +18 -0
  35. package/dist/esm/convert/resolve-quotes.js +169 -0
  36. package/dist/esm/convert/sanitize-name.d.ts +5 -0
  37. package/dist/esm/convert/sanitize-name.js +8 -0
  38. package/dist/esm/coverage-map/check-coverage.d.ts +20 -0
  39. package/dist/esm/coverage-map/check-coverage.js +21 -0
  40. package/dist/esm/coverage-map/coverage-map-schema.d.ts +11 -0
  41. package/dist/esm/coverage-map/coverage-map-schema.js +2 -0
  42. package/dist/esm/coverage-map/find-prompt-blocks.d.ts +15 -0
  43. package/dist/esm/coverage-map/find-prompt-blocks.js +140 -0
  44. package/dist/esm/examples/change-approval.d.ts +2 -0
  45. package/dist/esm/examples/change-approval.js +438 -0
  46. package/dist/esm/examples/examples.d.ts +9 -0
  47. package/dist/esm/examples/examples.js +8 -0
  48. package/dist/esm/examples/password-reset.d.ts +2 -0
  49. package/dist/esm/examples/password-reset.js +235 -0
  50. package/dist/esm/examples/refund-request.d.ts +2 -0
  51. package/dist/esm/examples/refund-request.js +233 -0
  52. package/dist/esm/index.d.ts +14 -0
  53. package/dist/esm/index.js +13 -0
  54. package/dist/esm/layout/build-elk-graph.d.ts +18 -0
  55. package/dist/esm/layout/build-elk-graph.js +96 -0
  56. package/dist/esm/layout/card-limits.d.ts +8 -0
  57. package/dist/esm/layout/card-limits.js +4 -0
  58. package/dist/esm/layout/describe-config.d.ts +118 -0
  59. package/dist/esm/layout/describe-config.js +199 -0
  60. package/dist/esm/layout/layout-config.d.ts +5 -0
  61. package/dist/esm/layout/layout-config.js +73 -0
  62. package/dist/esm/layout/layout-metrics.d.ts +6 -0
  63. package/dist/esm/layout/layout-metrics.js +6 -0
  64. package/dist/esm/layout/pack-preamble.d.ts +20 -0
  65. package/dist/esm/layout/pack-preamble.js +66 -0
  66. package/dist/esm/layout/to-flow-elements.d.ts +46 -0
  67. package/dist/esm/layout/to-flow-elements.js +83 -0
  68. package/dist/esm/react/canvas/AmbientSelectionContext.d.ts +7 -0
  69. package/dist/esm/react/canvas/AmbientSelectionContext.js +5 -0
  70. package/dist/esm/react/canvas/DetailLevelContext.d.ts +3 -0
  71. package/dist/esm/react/canvas/DetailLevelContext.js +2 -0
  72. package/dist/esm/react/canvas/FocusNavigation.d.ts +8 -0
  73. package/dist/esm/react/canvas/FocusNavigation.js +92 -0
  74. package/dist/esm/react/canvas/StateNode.d.ts +5 -0
  75. package/dist/esm/react/canvas/StateNode.js +91 -0
  76. package/dist/esm/react/canvas/StatechartCanvas.d.ts +16 -0
  77. package/dist/esm/react/canvas/StatechartCanvas.js +184 -0
  78. package/dist/esm/react/canvas/TransitionEdge.d.ts +5 -0
  79. package/dist/esm/react/canvas/TransitionEdge.js +22 -0
  80. package/dist/esm/react/canvas/canvas-key-entries.d.ts +14 -0
  81. package/dist/esm/react/canvas/canvas-key-entries.js +51 -0
  82. package/dist/esm/react/canvas/find-element-box.d.ts +8 -0
  83. package/dist/esm/react/canvas/find-element-box.js +25 -0
  84. package/dist/esm/react/canvas/find-view-centre.d.ts +15 -0
  85. package/dist/esm/react/canvas/find-view-centre.js +16 -0
  86. package/dist/esm/react/canvas/list-chart-marks.d.ts +5 -0
  87. package/dist/esm/react/canvas/list-chart-marks.js +37 -0
  88. package/dist/esm/react/canvas/marks.d.ts +19 -0
  89. package/dist/esm/react/canvas/marks.js +24 -0
  90. package/dist/esm/react/canvas/useNewLayout.d.ts +17 -0
  91. package/dist/esm/react/canvas/useNewLayout.js +44 -0
  92. package/dist/esm/react/canvas/zoom-levels.d.ts +28 -0
  93. package/dist/esm/react/canvas/zoom-levels.js +54 -0
  94. package/dist/esm/react/hooks/conversion-client.d.ts +77 -0
  95. package/dist/esm/react/hooks/conversion-client.js +132 -0
  96. package/dist/esm/react/hooks/describe-agent-selection.d.ts +6 -0
  97. package/dist/esm/react/hooks/describe-agent-selection.js +18 -0
  98. package/dist/esm/react/hooks/describe-load-outcome.d.ts +13 -0
  99. package/dist/esm/react/hooks/describe-load-outcome.js +30 -0
  100. package/dist/esm/react/hooks/download-config.d.ts +6 -0
  101. package/dist/esm/react/hooks/download-config.js +11 -0
  102. package/dist/esm/react/hooks/example-location.d.ts +7 -0
  103. package/dist/esm/react/hooks/example-location.js +14 -0
  104. package/dist/esm/react/hooks/label-source.d.ts +21 -0
  105. package/dist/esm/react/hooks/label-source.js +19 -0
  106. package/dist/esm/react/hooks/parse-config-text.d.ts +16 -0
  107. package/dist/esm/react/hooks/parse-config-text.js +38 -0
  108. package/dist/esm/react/hooks/useAgentBridge.d.ts +28 -0
  109. package/dist/esm/react/hooks/useAgentBridge.js +52 -0
  110. package/dist/esm/react/hooks/useAgentSync.d.ts +29 -0
  111. package/dist/esm/react/hooks/useAgentSync.js +75 -0
  112. package/dist/esm/react/hooks/useChartLayout.d.ts +25 -0
  113. package/dist/esm/react/hooks/useChartLayout.js +29 -0
  114. package/dist/esm/react/hooks/useConversion.d.ts +31 -0
  115. package/dist/esm/react/hooks/useConversion.js +177 -0
  116. package/dist/esm/react/hooks/useConvertDraft.d.ts +18 -0
  117. package/dist/esm/react/hooks/useConvertDraft.js +11 -0
  118. package/dist/esm/react/hooks/useDocumentTitle.d.ts +2 -0
  119. package/dist/esm/react/hooks/useDocumentTitle.js +7 -0
  120. package/dist/esm/react/hooks/useEscapeToClear.d.ts +2 -0
  121. package/dist/esm/react/hooks/useEscapeToClear.js +16 -0
  122. package/dist/esm/react/hooks/useHarnessList.d.ts +26 -0
  123. package/dist/esm/react/hooks/useHarnessList.js +24 -0
  124. package/dist/esm/react/hooks/usePromptDirectory.d.ts +27 -0
  125. package/dist/esm/react/hooks/usePromptDirectory.js +34 -0
  126. package/dist/esm/react/hooks/useSelection.d.ts +17 -0
  127. package/dist/esm/react/hooks/useSelection.js +21 -0
  128. package/dist/esm/react/hooks/useShowAmbient.d.ts +7 -0
  129. package/dist/esm/react/hooks/useShowAmbient.js +22 -0
  130. package/dist/esm/react/hooks/useUnknownExample.d.ts +7 -0
  131. package/dist/esm/react/hooks/useUnknownExample.js +26 -0
  132. package/dist/esm/react/index.d.ts +24 -0
  133. package/dist/esm/react/index.js +23 -0
  134. package/dist/esm/react/styles.css +482 -0
  135. package/dist/esm/schema/agent-logic-schema.d.ts +439 -0
  136. package/dist/esm/schema/agent-logic-schema.js +100 -0
  137. package/dist/esm/schema/config-traversal.d.ts +19 -0
  138. package/dist/esm/schema/config-traversal.js +32 -0
  139. package/dist/esm/schema/validate-config.d.ts +17 -0
  140. package/dist/esm/schema/validate-config.js +205 -0
  141. package/dist/esm/schema/write-json-schema.d.ts +4 -0
  142. package/dist/esm/schema/write-json-schema.js +12 -0
  143. package/dist/esm/server/conversion-job-implementation.d.ts +11 -0
  144. package/dist/esm/server/conversion-job-implementation.js +84 -0
  145. package/dist/esm/server/create-prompts-app.d.ts +26 -0
  146. package/dist/esm/server/create-prompts-app.js +41 -0
  147. package/dist/esm/server/create-viewer-server-plugins.d.ts +15 -0
  148. package/dist/esm/server/create-viewer-server-plugins.js +76 -0
  149. package/dist/esm/server/index.d.ts +7 -0
  150. package/dist/esm/server/index.js +6 -0
  151. package/dist/esm/server/prompt-directory-schemas.d.ts +30 -0
  152. package/dist/esm/server/prompt-directory-schemas.js +11 -0
  153. package/dist/esm/server/prompt-directory.d.ts +40 -0
  154. package/dist/esm/server/prompt-directory.js +86 -0
  155. package/dist/esm/server/viewer-contract-implementation.d.ts +12 -0
  156. package/dist/esm/server/viewer-contract-implementation.js +85 -0
  157. package/dist/esm/source/fence.d.ts +16 -0
  158. package/dist/esm/source/fence.js +15 -0
  159. package/dist/esm/source/find-comment-spans.d.ts +6 -0
  160. package/dist/esm/source/find-comment-spans.js +60 -0
  161. package/dist/esm/source/find-occurrences.d.ts +2 -0
  162. package/dist/esm/source/find-occurrences.js +7 -0
  163. package/dist/esm/source/prompt-segments.d.ts +21 -0
  164. package/dist/esm/source/prompt-segments.js +65 -0
  165. package/docs/conversion-agreement.md +37 -0
  166. package/docs/corpus.md +19 -0
  167. package/docs/extraction-rules.md +123 -0
  168. package/package.json +108 -2
  169. package/schema/agent-logic.schema.json +475 -0
@@ -0,0 +1,91 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { Handle, NodeToolbar, Position, useStore } from '@xyflow/react';
3
+ import { useContext, useEffect, useRef, useState } from 'react';
4
+ import { MAX_ACTION_LINES, MAX_AMBIENT_LINES, MAX_CARD_ACTIONS, PREAMBLE_WIDTH } from "../../layout/card-limits.js";
5
+ import { PREAMBLE_COLUMN_GAP } from "../../layout/pack-preamble.js";
6
+ import { AmbientSelectionContext } from "./AmbientSelectionContext.js";
7
+ import { DetailLevelContext } from "./DetailLevelContext.js";
8
+ import { FinalGlyph, InitialMark, NoExitGlyph, UnreachableGlyph, WaitGlyph } from "./marks.js";
9
+ import { placeFlowNames } from "./zoom-levels.js";
10
+ export function StateNode({ data }) {
11
+ const ref = useRef(null);
12
+ const isPointedAt = useIsPointedAt(ref);
13
+ const isBox = useContext(DetailLevelContext) === 'shapes' && !data.isCompound;
14
+ const { shift: nameShift, isShown: isNameShown } = useNamePlacement(data);
15
+ const classNames = [
16
+ 'state-node',
17
+ data.isCompound ? 'state-node--compound' : undefined,
18
+ data.isFinal ? 'state-node--final' : undefined,
19
+ data.inferred ? 'state-node--inferred' : undefined,
20
+ data.waitsForUser ? 'state-node--waiting' : undefined,
21
+ ];
22
+ return (_jsxs("div", { ref: ref, className: classNames.filter(Boolean).join(' '), "data-path": data.path, children: [_jsx(NodeToolbar, { isVisible: (isBox || !isNameShown) && isPointedAt, position: Position.Top, offset: 6, children: _jsx("div", { className: "state-node__tooltip", role: "tooltip", children: data.name }) }), _jsx(Handle, { type: "target", position: Position.Top, isConnectable: false, className: "state-node__handle" }), data.isInitial ? _jsx(InitialMark, { className: "state-node__initial-mark" }) : undefined, data.isCompound ? (_jsx(FlowName, { name: data.name, shift: nameShift, isShown: isNameShown })) : (_jsxs("div", { className: "state-node__title-row", children: [_jsx("div", { className: "state-node__title", children: data.name }), data.waitsForUser || data.isFinal || data.hasNoExit || data.isUnreachable ? (_jsxs("div", { className: "state-node__glyphs", children: [data.waitsForUser ? _jsx(WaitGlyph, {}) : undefined, data.isFinal ? _jsx(FinalGlyph, {}) : undefined, data.hasNoExit ? _jsx(NoExitGlyph, {}) : undefined, data.isUnreachable ? _jsx(UnreachableGlyph, {}) : undefined] })) : undefined] })), _jsx(Preamble, { flowName: data.name, ambient: data.ambient, layout: data.preambleLayout }), data.inferred ? _jsx("div", { className: "state-node__inferred", children: "inferred" }) : undefined, _jsx(ActionList, { heading: "Does", actions: data.entry }), _jsx(ActionList, { heading: "On exit", actions: data.exit }), _jsx(Handle, { type: "source", position: Position.Bottom, isConnectable: false, className: "state-node__handle" })] }));
23
+ }
24
+ function ActionList({ heading, actions }) {
25
+ if (actions.length === 0)
26
+ return undefined;
27
+ return (_jsxs("div", { className: "state-node__actions", children: [_jsx("div", { className: "state-node__actions-heading", children: heading }), _jsx("ul", { children: actions.slice(0, MAX_CARD_ACTIONS).map(({ text, gate }) => (_jsxs("li", { className: "state-node__action", style: { WebkitLineClamp: MAX_ACTION_LINES }, children: [gate ? _jsx(GateWord, {}) : undefined, text] }, text))) }), actions.length > MAX_CARD_ACTIONS ? (_jsx("div", { className: "state-node__more", children: describeHiddenActions(actions.slice(MAX_CARD_ACTIONS)) })) : undefined] }));
28
+ }
29
+ function describeHiddenActions(hidden) {
30
+ const gates = hidden.filter(({ gate }) => gate).length;
31
+ if (gates === 0)
32
+ return `${hidden.length} more`;
33
+ return `${hidden.length} more, including ${gates} ${gates === 1 ? 'gate' : 'gates'}`;
34
+ }
35
+ function findNamePlacement(data, zoom) {
36
+ return data.flowNameSites === undefined ? undefined : placeFlowNames(data.flowNameSites, zoom).get(data.id);
37
+ }
38
+ function FlowName({ name, shift, isShown }) {
39
+ return (_jsx("div", { className: isShown ? 'state-node__flow-name' : 'state-node__flow-name state-node__flow-name--hidden', title: isShown ? name : undefined, style: shift > 0 ? { translate: `${shift}px 0` } : undefined, children: name }));
40
+ }
41
+ function GateWord() {
42
+ return (_jsxs(_Fragment, { children: [_jsx("i", { className: "state-node__gate", children: "gate" }), ' '] }));
43
+ }
44
+ function Preamble({ flowName, ambient, layout, }) {
45
+ const { selectedPath, onSelect } = useContext(AmbientSelectionContext);
46
+ if (ambient === undefined || ambient.length === 0)
47
+ return undefined;
48
+ const style = {
49
+ pointerEvents: 'auto',
50
+ '--preamble-column-width': `${PREAMBLE_WIDTH}px`,
51
+ '--preamble-column-gap': `${PREAMBLE_COLUMN_GAP}px`,
52
+ '--preamble-columns': String(layout?.columns ?? 1),
53
+ ...(layout !== undefined && { '--preamble-column-height': `${layout.columnHeight}px` }),
54
+ };
55
+ return (_jsxs("section", { className: "state-node__preamble", "aria-label": `Ambient instructions of ${flowName}`, style: style, children: [_jsx("div", { className: "state-node__preamble-heading", children: "Ambient" }), _jsx("ul", { children: ambient.map(({ path, text, gate }) => (_jsx("li", { children: _jsxs("button", { type: "button", className: `state-node__ambient nodrag nopan nokey${path === selectedPath ? ' state-node__ambient--selected' : ''}`, "data-path": path, "aria-pressed": path === selectedPath, style: { WebkitLineClamp: MAX_AMBIENT_LINES }, onClick: (event) => {
56
+ event.stopPropagation();
57
+ onSelect(path);
58
+ }, children: [gate ? _jsx(GateWord, {}) : undefined, text] }) }, path))) })] }));
59
+ }
60
+ function useIsPointedAt(ref) {
61
+ const [isPointedAt, setIsPointedAt] = useState(false);
62
+ useEffect(() => {
63
+ const wrapper = ref.current?.closest('.react-flow__node');
64
+ if (wrapper === null || wrapper === undefined)
65
+ return;
66
+ function show() {
67
+ setIsPointedAt(true);
68
+ }
69
+ function hide() {
70
+ setIsPointedAt(false);
71
+ }
72
+ const events = [
73
+ ['pointerenter', show],
74
+ ['pointerleave', hide],
75
+ ['focus', show],
76
+ ['blur', hide],
77
+ ];
78
+ for (const [type, listener] of events)
79
+ wrapper.addEventListener(type, listener);
80
+ return () => {
81
+ for (const [type, listener] of events)
82
+ wrapper.removeEventListener(type, listener);
83
+ };
84
+ }, [ref]);
85
+ return isPointedAt;
86
+ }
87
+ function useNamePlacement(data) {
88
+ const shift = useStore((state) => findNamePlacement(data, state.transform[2])?.shift ?? 0);
89
+ const isShown = useStore((state) => findNamePlacement(data, state.transform[2])?.isShown ?? true);
90
+ return { shift, isShown };
91
+ }
@@ -0,0 +1,16 @@
1
+ import { type ReactElement, type ReactNode } from 'react';
2
+ import type { FlowElements } from '../../layout/to-flow-elements.js';
3
+ import { type ChartMark } from './list-chart-marks.js';
4
+ export interface StatechartCanvasProps {
5
+ elements: FlowElements;
6
+ /** The path of the state or transition to show selected; one selected from outside the canvas is brought into view. */
7
+ selectedPath: string | undefined;
8
+ /** Receives the path of the selected state or transition, or `undefined` when nothing is selected. */
9
+ onSelect: (path: string | undefined) => void;
10
+ /** Called whenever the view pans or zooms. */
11
+ onViewportChange?: (() => void) | undefined;
12
+ /** Renders the key below the chart from the marks that the chart draws; the canvas renders no key without it. */
13
+ renderKey?: ((marks: ReadonlySet<ChartMark>) => ReactNode) | undefined;
14
+ }
15
+ /** An interactive statechart whose states and transitions are selectable by pointer and keyboard. */
16
+ export declare function StatechartCanvas({ elements, renderKey, ...props }: StatechartCanvasProps): ReactElement;
@@ -0,0 +1,184 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { applyEdgeChanges, applyNodeChanges, Background, Controls, getNodesBounds, ReactFlow, ReactFlowProvider, useReactFlow, useStore, useStoreApi, } from '@xyflow/react';
3
+ import { useCallback, useEffect, useMemo, useRef, useState, } from 'react';
4
+ import { AmbientSelectionContext } from "./AmbientSelectionContext.js";
5
+ import { DetailLevelContext } from "./DetailLevelContext.js";
6
+ import { findElementBox } from "./find-element-box.js";
7
+ import { findViewCentre, readView } from "./find-view-centre.js";
8
+ import { FocusNavigation } from "./FocusNavigation.js";
9
+ import { listChartMarks } from "./list-chart-marks.js";
10
+ import { ArrowheadDefs } from "./marks.js";
11
+ import { StateNode } from "./StateNode.js";
12
+ import { TransitionEdge } from "./TransitionEdge.js";
13
+ import { useNewLayout } from "./useNewLayout.js";
14
+ import { findDetailLevel, findFitZoom, FIT_PADDING, READING_ZOOM } from "./zoom-levels.js";
15
+ const nodeTypes = { state: StateNode };
16
+ const edgeTypes = { transition: TransitionEdge };
17
+ const fitViewOptions = {
18
+ padding: FIT_PADDING,
19
+ minZoom: Number.MIN_VALUE,
20
+ maxZoom: READING_ZOOM,
21
+ };
22
+ const MAX_ZOOM = 2;
23
+ const STATE_DESCRIPTION = "Press Enter or Space to select. Press Right arrow to move to the state's outgoing transitions, when it has any.";
24
+ const ariaLabelConfig = {
25
+ 'node.a11yDescription.default': STATE_DESCRIPTION,
26
+ 'node.a11yDescription.keyboardDisabled': STATE_DESCRIPTION,
27
+ 'edge.a11yDescription.default': 'Press Enter or Space to select. Press Up or Down arrow to move between transitions from the same state, or Left arrow to return to that state.',
28
+ };
29
+ const DRAG_THRESHOLD = 3;
30
+ export function StatechartCanvas({ elements, renderKey, ...props }) {
31
+ const marks = useMemo(() => listChartMarks(elements), [elements]);
32
+ return (_jsxs("div", { className: "statechart-canvas", children: [_jsx(ReactFlowProvider, { children: _jsx(StatechartFlow, { elements: elements, ...props }) }), renderKey?.(marks)] }));
33
+ }
34
+ function EdgeDragPan({ onPan }) {
35
+ const store = useStoreApi();
36
+ useEffect(() => {
37
+ const pane = store.getState().domNode;
38
+ if (pane === null)
39
+ return;
40
+ function handlePointerDown(down) {
41
+ if (down.button !== 0 || !(down.target instanceof Element) || down.target.closest('.react-flow__edge') === null) {
42
+ return;
43
+ }
44
+ let last = { x: down.clientX, y: down.clientY };
45
+ let isDragging = false;
46
+ function handlePointerMove(move) {
47
+ if (!isDragging && Math.hypot(move.clientX - down.clientX, move.clientY - down.clientY) < DRAG_THRESHOLD)
48
+ return;
49
+ if (!isDragging)
50
+ onPan();
51
+ isDragging = true;
52
+ void store.getState().panBy({ x: move.clientX - last.x, y: move.clientY - last.y });
53
+ last = { x: move.clientX, y: move.clientY };
54
+ }
55
+ function handlePointerUp() {
56
+ globalThis.removeEventListener('pointermove', handlePointerMove);
57
+ globalThis.removeEventListener('pointerup', handlePointerUp);
58
+ globalThis.removeEventListener('pointercancel', handlePointerUp);
59
+ if (isDragging)
60
+ globalThis.addEventListener('click', stopClick, { capture: true, once: true });
61
+ }
62
+ globalThis.addEventListener('pointermove', handlePointerMove);
63
+ globalThis.addEventListener('pointerup', handlePointerUp);
64
+ globalThis.addEventListener('pointercancel', handlePointerUp);
65
+ }
66
+ pane.addEventListener('pointerdown', handlePointerDown);
67
+ return () => {
68
+ pane.removeEventListener('pointerdown', handlePointerDown);
69
+ };
70
+ }, [onPan, store]);
71
+ return null;
72
+ }
73
+ function SelectionSync({ selectedPath, reportRef, onCentre, }) {
74
+ const store = useStoreApi();
75
+ const { getNodes, getEdges, getInternalNode, getViewport, setCenter } = useReactFlow();
76
+ useEffect(() => {
77
+ const report = reportRef.current;
78
+ reportRef.current = undefined;
79
+ if (report?.path === selectedPath || selectedPath === undefined)
80
+ return;
81
+ const box = findElementBox(selectedPath, getNodes(), getEdges(), getInternalNode);
82
+ if (box === undefined)
83
+ return;
84
+ const { width, height } = store.getState();
85
+ const viewport = getViewport();
86
+ const centre = findViewCentre(box, readView(viewport, width, height));
87
+ if (centre === undefined)
88
+ return;
89
+ onCentre();
90
+ void setCenter(centre.x, centre.y, { zoom: viewport.zoom, duration: 300 });
91
+ }, [getEdges, getInternalNode, getNodes, getViewport, onCentre, reportRef, selectedPath, setCenter, store]);
92
+ return null;
93
+ }
94
+ function StatechartFlow({ elements, selectedPath, onSelect, onViewportChange }) {
95
+ const { fitView, getInternalNode, getViewport, setCenter, zoomTo } = useReactFlow();
96
+ const width = useStore((state) => state.width);
97
+ const height = useStore((state) => state.height);
98
+ const bounds = useMemo(() => getNodesBounds(elements.nodes.filter(({ parentId }) => parentId === undefined)), [elements.nodes]);
99
+ const fitZoom = findFitZoom(bounds, width, height);
100
+ const detailLevel = useStore((state) => findDetailLevel(state.transform[2]));
101
+ const isFitted = useRef(true);
102
+ const markMoved = useCallback(() => {
103
+ isFitted.current = false;
104
+ }, []);
105
+ useEffect(() => {
106
+ if (isFitted.current)
107
+ void fitView(fitViewOptions);
108
+ else if (getViewport().zoom < fitZoom)
109
+ void zoomTo(fitZoom);
110
+ }, [fitView, fitZoom, getViewport, height, width, zoomTo]);
111
+ const [nodes, setNodes] = useState(elements.nodes);
112
+ const [edges, setEdges] = useState(elements.edges);
113
+ const shownNodes = useMemo(() => nodes.map((node) => withSelected(node, node.data.path === selectedPath)), [nodes, selectedPath]);
114
+ const shownEdges = useMemo(() => edges.map((edge) => withSelected(edge, edge.id === selectedPath)), [edges, selectedPath]);
115
+ const reportRef = useRef(undefined);
116
+ useNewLayout({ elements, nodes, setNodes, setEdges, isFitted, selectedPath, fitViewOptions });
117
+ const ambientSelection = useMemo(() => ({
118
+ selectedPath,
119
+ onSelect: (path) => {
120
+ reportRef.current = { path };
121
+ onSelect(path);
122
+ },
123
+ }), [onSelect, selectedPath]);
124
+ const reportSelectChanges = useCallback((changes, findPath) => {
125
+ for (const change of changes) {
126
+ if (change.type !== 'select')
127
+ continue;
128
+ const path = findPath(change.id);
129
+ if (path === undefined)
130
+ continue;
131
+ const currentPath = reportRef.current === undefined ? selectedPath : reportRef.current.path;
132
+ if (!change.selected && path !== currentPath)
133
+ continue;
134
+ const nextPath = change.selected ? path : undefined;
135
+ reportRef.current = { path: nextPath };
136
+ onSelect(nextPath);
137
+ }
138
+ }, [onSelect, selectedPath]);
139
+ const handleNodesChange = useCallback((changes) => {
140
+ reportSelectChanges(changes, (id) => nodes.find((node) => node.id === id)?.data.path);
141
+ const otherChanges = changes.filter(({ type }) => type !== 'select');
142
+ if (otherChanges.length > 0)
143
+ setNodes((current) => applyNodeChanges(otherChanges, current));
144
+ }, [nodes, reportSelectChanges]);
145
+ const handleEdgesChange = useCallback((changes) => {
146
+ reportSelectChanges(changes, (id) => edges.find((edge) => edge.id === id)?.data?.path);
147
+ const otherChanges = changes.filter(({ type }) => type !== 'select');
148
+ if (otherChanges.length > 0)
149
+ setEdges((current) => applyEdgeChanges(otherChanges, current));
150
+ }, [edges, reportSelectChanges]);
151
+ const handleNodeClick = useCallback((_event, node) => {
152
+ if (detailLevel !== 'shapes' || node.data.isCompound)
153
+ return;
154
+ const position = getInternalNode(node.id)?.internals.positionAbsolute ?? node.position;
155
+ const centreX = position.x + (node.width ?? 0) / 2;
156
+ const centreY = position.y + (node.height ?? 0) / 2;
157
+ markMoved();
158
+ void setCenter(centreX, centreY, { zoom: READING_ZOOM, duration: 300 });
159
+ }, [detailLevel, getInternalNode, markMoved, setCenter]);
160
+ return (_jsx(DetailLevelContext.Provider, { value: detailLevel, children: _jsx(AmbientSelectionContext.Provider, { value: ambientSelection, children: _jsxs(ReactFlow, { className: `statechart-canvas__flow--${detailLevel}`, nodes: shownNodes, edges: shownEdges, nodeTypes: nodeTypes, edgeTypes: edgeTypes, onNodesChange: handleNodesChange, onEdgesChange: handleEdgesChange, onNodeClick: handleNodeClick, onMoveStart: (event) => {
161
+ if (event !== null)
162
+ markMoved();
163
+ }, ...(onViewportChange === undefined ? {} : { onMove: onViewportChange }), nodesDraggable: false, nodesConnectable: false, multiSelectionKeyCode: null, fitView: true, fitViewOptions: fitViewOptions, autoPanOnNodeFocus: false, ariaLabelConfig: ariaLabelConfig, minZoom: fitZoom, maxZoom: MAX_ZOOM, colorMode: "system", children: [_jsx(Background, {}), _jsx(ArrowheadDefs, {}), _jsx(Controls, { showInteractive: false, fitViewOptions: fitViewOptions, onFitView: () => {
164
+ isFitted.current = true;
165
+ }, onZoomIn: markMoved, onZoomOut: markMoved }), _jsx(SelectionSync, { selectedPath: selectedPath, reportRef: reportRef, onCentre: markMoved }), _jsx(EdgeDragPan, { onPan: markMoved }), _jsx(FocusNavigation, { onCentre: markMoved }), _jsx(ZoomProperty, {})] }) }) }));
166
+ }
167
+ function ZoomProperty() {
168
+ const store = useStoreApi();
169
+ useEffect(() => {
170
+ function applyZoom({ domNode, transform }) {
171
+ domNode?.style.setProperty('--canvas-zoom', String(transform[2]));
172
+ }
173
+ applyZoom(store.getState());
174
+ return store.subscribe(applyZoom);
175
+ }, [store]);
176
+ return null;
177
+ }
178
+ function stopClick(event) {
179
+ event.stopPropagation();
180
+ event.preventDefault();
181
+ }
182
+ function withSelected(element, isSelected) {
183
+ return element.selected === isSelected ? element : { ...element, selected: isSelected };
184
+ }
@@ -0,0 +1,5 @@
1
+ import { type EdgeProps } from '@xyflow/react';
2
+ import type { ReactElement } from 'react';
3
+ import type { TransitionFlowEdge } from '../../layout/to-flow-elements.js';
4
+ /** A transition on the canvas, drawn along the route that ELK computed, with its trigger, guard, actions, and kind words as its label. */
5
+ export declare function TransitionEdge({ data, sourceX, sourceY, targetX, targetY, }: EdgeProps<TransitionFlowEdge>): ReactElement;
@@ -0,0 +1,22 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { BaseEdge } from '@xyflow/react';
3
+ import { buildTransitionLabel } from "../../layout/describe-config.js";
4
+ export function TransitionEdge({ data, sourceX, sourceY, targetX, targetY, }) {
5
+ const points = data !== undefined && data.points.length >= 2
6
+ ? data.points
7
+ : [
8
+ { x: sourceX, y: sourceY },
9
+ { x: targetX, y: targetY },
10
+ ];
11
+ const path = points.map(({ x, y }, index) => `${index === 0 ? 'M' : 'L'} ${x} ${y}`).join(' ');
12
+ const classNames = [
13
+ 'transition-edge__path',
14
+ data?.isGlobal === true ? 'transition-edge__path--global' : undefined,
15
+ data?.inferred === true ? 'transition-edge__path--inferred' : undefined,
16
+ ];
17
+ return (_jsxs(_Fragment, { children: [_jsx(BaseEdge, { path: path, className: classNames.filter(Boolean).join(' '), interactionWidth: 16 }), data?.labelBox === undefined ? undefined : (_jsx("foreignObject", { x: data.labelBox.x, y: data.labelBox.y, width: data.labelBox.width, height: data.labelBox.height, children: _jsx("div", { className: "transition-edge__label", "data-path": data.path, children: _jsx(TransitionLabelLines, { ...buildTransitionLabel(data) }) }) }))] }));
18
+ }
19
+ const WORD_LINES = new Set(['always', 'gate', 'inferred', 'no alternative', 'otherwise']);
20
+ function TransitionLabelLines({ trigger, details }) {
21
+ return (_jsxs(_Fragment, { children: [trigger === undefined ? undefined : _jsx("div", { className: "transition-edge__trigger", children: trigger }), details.map((line) => (_jsx("div", { className: WORD_LINES.has(line) ? 'transition-edge__word' : undefined, children: line }, line)))] }));
22
+ }
@@ -0,0 +1,14 @@
1
+ import type { ReactElement } from 'react';
2
+ import type { ChartMark } from './list-chart-marks.js';
3
+ /** One entry of a canvas key: a kind of element that the chart marks, drawn as on the chart, with its gloss. */
4
+ export interface CanvasKeyEntry {
5
+ id: ChartMark;
6
+ /** The entry's name; absent when the mark is the word that the chart writes, which the entry does not repeat. */
7
+ name?: string;
8
+ /** What the mark means, for the entry's tooltip or description. */
9
+ gloss: string;
10
+ /** The mark as the chart draws it, styled by the package's stylesheet. */
11
+ mark: ReactElement;
12
+ }
13
+ /** The entries of a canvas key, in the order in which a key lists them; a key shows those whose `id` the chart draws. */
14
+ export declare const CANVAS_KEY_ENTRIES: readonly CanvasKeyEntry[];
@@ -0,0 +1,51 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { FinalGlyph, InitialMark, NoExitGlyph, UnreachableGlyph, WaitGlyph } from "./marks.js";
3
+ export const CANVAS_KEY_ENTRIES = [
4
+ {
5
+ id: 'waits-for-user',
6
+ name: 'Waits for user',
7
+ gloss: 'The agent waits for the user here',
8
+ mark: _jsx(WaitGlyph, { hasTitle: false }),
9
+ },
10
+ { id: 'ends', name: 'Ends', gloss: 'The conversation ends here', mark: _jsx(FinalGlyph, { hasTitle: false }) },
11
+ {
12
+ id: 'starts-here',
13
+ name: 'Starts here',
14
+ gloss: 'Starting state of its flow',
15
+ mark: _jsx(InitialMark, { hasTitle: false }),
16
+ },
17
+ {
18
+ id: 'any-time',
19
+ name: 'Any time',
20
+ gloss: 'Applies throughout the flow',
21
+ mark: _jsx("span", { className: "canvas-key__swatch canvas-key__swatch--global" }),
22
+ },
23
+ {
24
+ id: 'inferred',
25
+ gloss: 'Not stated in the prompt',
26
+ mark: (_jsxs(_Fragment, { children: [_jsx("span", { className: "canvas-key__swatch canvas-key__swatch--inferred" }), _jsx("i", { className: "canvas-key__word", children: "inferred" })] })),
27
+ },
28
+ {
29
+ id: 'gate',
30
+ gloss: 'Marked as a hard gate in the prompt',
31
+ mark: _jsx("i", { className: "canvas-key__word", children: "gate" }),
32
+ },
33
+ {
34
+ id: 'fallback',
35
+ name: 'Fallback',
36
+ gloss: 'Taken when no guarded sibling applies',
37
+ mark: _jsx("i", { className: "canvas-key__word", children: "otherwise" }),
38
+ },
39
+ {
40
+ id: 'no-exit',
41
+ name: 'No exit',
42
+ gloss: 'Not an end, and nothing of its own leads on',
43
+ mark: _jsx(NoExitGlyph, { hasTitle: false }),
44
+ },
45
+ { id: 'unreachable', name: 'Unreachable', gloss: 'Nothing leads to it', mark: _jsx(UnreachableGlyph, { hasTitle: false }) },
46
+ {
47
+ id: 'no-alternative',
48
+ gloss: 'A condition with nothing for when it does not hold',
49
+ mark: _jsx("i", { className: "canvas-key__word", children: "no alternative" }),
50
+ },
51
+ ];
@@ -0,0 +1,8 @@
1
+ import type { useReactFlow } from '@xyflow/react';
2
+ import type { StateFlowNode, TransitionFlowEdge } from '../../layout/to-flow-elements.js';
3
+ import type { CanvasBox } from './find-view-centre.js';
4
+ /**
5
+ * Returns an element's box in canvas coordinates: a state's card, the top of the flow that holds an ambient
6
+ * instruction, or a transition's label or route.
7
+ */
8
+ export declare function findElementBox(path: string, nodes: StateFlowNode[], edges: TransitionFlowEdge[], getInternalNode: ReturnType<typeof useReactFlow<StateFlowNode, TransitionFlowEdge>>['getInternalNode']): CanvasBox | undefined;
@@ -0,0 +1,25 @@
1
+ const AMBIENT_BOX_HEIGHT = 120;
2
+ export function findElementBox(path, nodes, edges, getInternalNode) {
3
+ const node = nodes.find(({ data }) => data.path === path);
4
+ if (node !== undefined) {
5
+ const position = getInternalNode(node.id)?.internals.positionAbsolute ?? node.position;
6
+ return { ...position, width: node.width ?? 0, height: node.height ?? 0 };
7
+ }
8
+ const container = nodes.find(({ data }) => data.ambient?.some((entry) => entry.path === path) === true);
9
+ if (container !== undefined) {
10
+ const position = getInternalNode(container.id)?.internals.positionAbsolute ?? container.position;
11
+ return { ...position, width: container.width ?? 0, height: Math.min(container.height ?? 0, AMBIENT_BOX_HEIGHT) };
12
+ }
13
+ const data = edges.find(({ id }) => id === path)?.data;
14
+ if (data === undefined)
15
+ return undefined;
16
+ if (data.labelBox !== undefined)
17
+ return data.labelBox;
18
+ if (data.points.length === 0)
19
+ return undefined;
20
+ const xs = data.points.map(({ x }) => x);
21
+ const ys = data.points.map(({ y }) => y);
22
+ const left = Math.min(...xs);
23
+ const top = Math.min(...ys);
24
+ return { x: left, y: top, width: Math.max(...xs) - left, height: Math.max(...ys) - top };
25
+ }
@@ -0,0 +1,15 @@
1
+ import type { Viewport } from '@xyflow/react';
2
+ /** A box in canvas coordinates. */
3
+ export interface CanvasBox {
4
+ x: number;
5
+ y: number;
6
+ width: number;
7
+ height: number;
8
+ }
9
+ /** Returns the point to centre the view on to bring a box into it, or `undefined` when the box is already wholly in view. */
10
+ export declare function findViewCentre(box: CanvasBox, view: CanvasBox): {
11
+ x: number;
12
+ y: number;
13
+ } | undefined;
14
+ /** Returns the part of the canvas that a viewport shows on a pane of the given size, in canvas coordinates. */
15
+ export declare function readView({ x, y, zoom }: Viewport, width: number, height: number): CanvasBox;
@@ -0,0 +1,16 @@
1
+ const MARGIN = 16;
2
+ export function findViewCentre(box, view) {
3
+ const isInside = box.x >= view.x &&
4
+ box.y >= view.y &&
5
+ box.x + box.width <= view.x + view.width &&
6
+ box.y + box.height <= view.y + view.height;
7
+ if (isInside)
8
+ return undefined;
9
+ return {
10
+ x: box.width > view.width ? box.x + view.width / 2 - MARGIN : box.x + box.width / 2,
11
+ y: box.height > view.height ? box.y + view.height / 2 - MARGIN : box.y + box.height / 2,
12
+ };
13
+ }
14
+ export function readView({ x, y, zoom }, width, height) {
15
+ return { x: -x / zoom, y: -y / zoom, width: width / zoom, height: height / zoom };
16
+ }
@@ -0,0 +1,5 @@
1
+ import type { FlowElements } from '../../layout/to-flow-elements.js';
2
+ /** A kind of element that the canvas marks and that its key names. */
3
+ export type ChartMark = 'any-time' | 'ends' | 'fallback' | 'gate' | 'inferred' | 'no-alternative' | 'no-exit' | 'starts-here' | 'unreachable' | 'waits-for-user';
4
+ /** Returns the marks that the laid-out chart draws, so that its key names only those. */
5
+ export declare function listChartMarks({ nodes, edges }: FlowElements): Set<ChartMark>;
@@ -0,0 +1,37 @@
1
+ export function listChartMarks({ nodes, edges }) {
2
+ const marks = new Set();
3
+ for (const { data } of nodes) {
4
+ if (data.waitsForUser)
5
+ marks.add('waits-for-user');
6
+ if (data.isFinal)
7
+ marks.add('ends');
8
+ if (data.isInitial)
9
+ marks.add('starts-here');
10
+ if (data.hasNoExit)
11
+ marks.add('no-exit');
12
+ if (data.isUnreachable)
13
+ marks.add('unreachable');
14
+ if (data.inferred)
15
+ marks.add('inferred');
16
+ if (hasGate([...data.entry, ...data.exit]) || data.ambient?.some(({ gate }) => gate) === true)
17
+ marks.add('gate');
18
+ }
19
+ for (const { data } of edges) {
20
+ if (data === undefined)
21
+ continue;
22
+ if (data.isGlobal)
23
+ marks.add('any-time');
24
+ if (data.isFallback)
25
+ marks.add('fallback');
26
+ if (data.lacksAlternative)
27
+ marks.add('no-alternative');
28
+ if (data.inferred)
29
+ marks.add('inferred');
30
+ if (data.gate || hasGate(data.actions))
31
+ marks.add('gate');
32
+ }
33
+ return marks;
34
+ }
35
+ function hasGate(actions) {
36
+ return actions.some(({ gate }) => gate);
37
+ }
@@ -0,0 +1,19 @@
1
+ import type { ReactElement } from 'react';
2
+ export interface GlyphProps {
3
+ /** Whether the glyph names itself in a title; false where a label beside it already names it. */
4
+ hasTitle?: boolean;
5
+ }
6
+ /** The 16px glyph of a state that waits for the user: a speech bubble. */
7
+ export declare function WaitGlyph({ hasTitle }: GlyphProps): ReactElement;
8
+ /** The 16px glyph of a final state: a ring around a dot. */
9
+ export declare function FinalGlyph({ hasTitle }: GlyphProps): ReactElement;
10
+ /** The 16px glyph of a state that is not final and that none of its own transitions leaves: a barred circle. */
11
+ export declare function NoExitGlyph({ hasTitle }: GlyphProps): ReactElement;
12
+ /** The 16px glyph of a state that nothing enters: a dashed ring. */
13
+ export declare function UnreachableGlyph({ hasTitle }: GlyphProps): ReactElement;
14
+ /** The 22×12px mark that points into the state in which a flow starts: a dot and an arrow. */
15
+ export declare function InitialMark({ className, hasTitle }: GlyphProps & {
16
+ className?: string;
17
+ }): ReactElement;
18
+ /** The arrowheads of the canvas's transitions, one per edge colour, which `styles.css` assigns to each edge's path. */
19
+ export declare function ArrowheadDefs(): ReactElement;
@@ -0,0 +1,24 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ export function WaitGlyph({ hasTitle = true }) {
3
+ return (_jsxs("svg", { className: "mark-glyph mark-glyph--wait", viewBox: "0 0 16 16", "aria-hidden": "true", children: [hasTitle ? _jsx("title", { children: "Waits for user" }) : undefined, _jsx("path", { d: "M2 3.5A1.5 1.5 0 0 1 3.5 2h9A1.5 1.5 0 0 1 14 3.5v6A1.5 1.5 0 0 1 12.5 11H7l-3.5 3v-3A1.5 1.5 0 0 1 2 9.5z", fill: "currentColor" }), _jsx("circle", { cx: "5.5", cy: "6.5", r: "1", className: "mark-glyph__cutout" }), _jsx("circle", { cx: "8", cy: "6.5", r: "1", className: "mark-glyph__cutout" }), _jsx("circle", { cx: "10.5", cy: "6.5", r: "1", className: "mark-glyph__cutout" })] }));
4
+ }
5
+ export function FinalGlyph({ hasTitle = true }) {
6
+ return (_jsxs("svg", { className: "mark-glyph mark-glyph--final", viewBox: "0 0 16 16", "aria-hidden": "true", children: [hasTitle ? _jsx("title", { children: "Ends" }) : undefined, _jsx("circle", { cx: "8", cy: "8", r: "6.5", fill: "none", stroke: "currentColor", strokeWidth: "1.5" }), _jsx("circle", { cx: "8", cy: "8", r: "3.5", fill: "currentColor" })] }));
7
+ }
8
+ export function NoExitGlyph({ hasTitle = true }) {
9
+ return (_jsxs("svg", { className: "mark-glyph mark-glyph--no-exit", viewBox: "0 0 16 16", "aria-hidden": "true", children: [hasTitle ? _jsx("title", { children: "No exit" }) : undefined, _jsx("circle", { cx: "8", cy: "8", r: "7", fill: "currentColor" }), _jsx("rect", { x: "3.5", y: "6.75", width: "9", height: "2.5", rx: "0.5", className: "mark-glyph__cutout" })] }));
10
+ }
11
+ export function UnreachableGlyph({ hasTitle = true }) {
12
+ return (_jsxs("svg", { className: "mark-glyph mark-glyph--unreachable", viewBox: "0 0 16 16", "aria-hidden": "true", children: [hasTitle ? _jsx("title", { children: "Unreachable" }) : undefined, _jsx("circle", { cx: "8", cy: "8", r: "6", fill: "none", stroke: "currentColor", strokeWidth: "2.5", strokeDasharray: "3.5 2.78" })] }));
13
+ }
14
+ export function InitialMark({ className, hasTitle = true }) {
15
+ return (_jsxs("svg", { className: ['mark-initial', className].filter(Boolean).join(' '), viewBox: "0 0 22 12", width: "22", height: "12", "aria-hidden": "true", children: [hasTitle ? _jsx("title", { children: "Starts here" }) : undefined, _jsx("circle", { cx: "5", cy: "6", r: "4.5", fill: "currentColor" }), _jsx("path", { d: "M9 6h8", stroke: "currentColor", strokeWidth: "1.5" }), _jsx("path", { d: "M16 2.5L21.5 6L16 9.5z", fill: "currentColor" })] }));
16
+ }
17
+ export function ArrowheadDefs() {
18
+ return (_jsx("svg", { className: "arrowhead-defs", width: "0", height: "0", "aria-hidden": "true", children: _jsx("defs", { children: ARROWHEADS.map(({ id, size }) => (_jsx("marker", { id: id, viewBox: "0 0 10 10", refX: "9", refY: "5", markerWidth: size, markerHeight: size, markerUnits: "userSpaceOnUse", orient: "auto", children: _jsx("path", { d: "M0,0 L10,5 L0,10 z", className: `arrowhead arrowhead--${id}` }) }, id))) }) }));
19
+ }
20
+ const ARROWHEADS = [
21
+ { id: 'arrowhead-global', size: 11 },
22
+ { id: 'arrowhead-ordinary', size: 9 },
23
+ { id: 'arrowhead-selected', size: 11 },
24
+ ];
@@ -0,0 +1,17 @@
1
+ import { type FitViewOptions } from '@xyflow/react';
2
+ import { type RefObject } from 'react';
3
+ import type { FlowElements, StateFlowNode, TransitionFlowEdge } from '../../layout/to-flow-elements.js';
4
+ /**
5
+ * Takes a new layout of the same chart, such as one that shows or hides the preambles, in place of the current one;
6
+ * once React Flow has measured it, fits a fitted view again, or centres the selected element at the current zoom.
7
+ */
8
+ export declare function useNewLayout({ elements, nodes, setNodes, setEdges, isFitted, selectedPath, fitViewOptions, }: {
9
+ elements: FlowElements;
10
+ /** The nodes that the canvas holds, whose change marks that React Flow holds the new layout. */
11
+ nodes: StateFlowNode[];
12
+ setNodes: (nodes: StateFlowNode[]) => void;
13
+ setEdges: (edges: TransitionFlowEdge[]) => void;
14
+ isFitted: RefObject<boolean>;
15
+ selectedPath: string | undefined;
16
+ fitViewOptions: FitViewOptions<StateFlowNode>;
17
+ }): void;
@@ -0,0 +1,44 @@
1
+ import { useNodesInitialized, useReactFlow } from '@xyflow/react';
2
+ import { useEffect, useRef } from 'react';
3
+ import { findElementBox } from "./find-element-box.js";
4
+ export function useNewLayout({ elements, nodes, setNodes, setEdges, isFitted, selectedPath, fitViewOptions, }) {
5
+ const { fitView, getEdges, getInternalNode, getNodes, getViewport, setCenter } = useReactFlow();
6
+ const isMeasured = useNodesInitialized();
7
+ const shownElements = useRef(elements);
8
+ const isRelaidOut = useRef(false);
9
+ useEffect(() => {
10
+ if (shownElements.current === elements)
11
+ return;
12
+ shownElements.current = elements;
13
+ isRelaidOut.current = true;
14
+ setNodes(elements.nodes);
15
+ setEdges(elements.edges);
16
+ }, [elements, setEdges, setNodes]);
17
+ useEffect(() => {
18
+ if (!isRelaidOut.current || !isMeasured)
19
+ return;
20
+ isRelaidOut.current = false;
21
+ if (isFitted.current) {
22
+ void fitView(fitViewOptions);
23
+ return;
24
+ }
25
+ if (selectedPath === undefined)
26
+ return;
27
+ const box = findElementBox(selectedPath, getNodes(), getEdges(), getInternalNode);
28
+ if (box === undefined)
29
+ return;
30
+ void setCenter(box.x + box.width / 2, box.y + box.height / 2, { zoom: getViewport().zoom, duration: 300 });
31
+ }, [
32
+ fitView,
33
+ fitViewOptions,
34
+ getEdges,
35
+ getInternalNode,
36
+ getNodes,
37
+ getViewport,
38
+ isFitted,
39
+ isMeasured,
40
+ nodes,
41
+ selectedPath,
42
+ setCenter,
43
+ ]);
44
+ }