codeassembly-agent-logic 0.0.0 → 0.1.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/.meta/changelog.json +77 -0
- package/CHANGELOG.md +28 -0
- package/LICENSE +7 -0
- package/README.md +86 -1
- package/bin/agent-logic.js +26 -0
- package/dist/esm/cli/check-coverage-command.d.ts +3 -0
- package/dist/esm/cli/check-coverage-command.js +99 -0
- package/dist/esm/cli/convert-command.d.ts +3 -0
- package/dist/esm/cli/convert-command.js +116 -0
- package/dist/esm/cli/find-package-root.d.ts +7 -0
- package/dist/esm/cli/find-package-root.js +31 -0
- package/dist/esm/cli/index.d.ts +1 -0
- package/dist/esm/cli/index.js +11 -0
- package/dist/esm/cli/run.d.ts +8 -0
- package/dist/esm/cli/run.js +62 -0
- package/dist/esm/contract/built-in-harnesses.d.ts +22 -0
- package/dist/esm/contract/built-in-harnesses.js +30 -0
- package/dist/esm/contract/conversion-job.d.ts +36 -0
- package/dist/esm/contract/conversion-job.js +17 -0
- package/dist/esm/contract/index.d.ts +4 -0
- package/dist/esm/contract/index.js +3 -0
- package/dist/esm/contract/viewer-contract.d.ts +804 -0
- package/dist/esm/contract/viewer-contract.js +38 -0
- package/dist/esm/convert/convert-prompt.d.ts +21 -0
- package/dist/esm/convert/convert-prompt.js +60 -0
- package/dist/esm/convert/draft-schema.d.ts +308 -0
- package/dist/esm/convert/draft-schema.js +57 -0
- package/dist/esm/convert/find-gate-spans.d.ts +6 -0
- package/dist/esm/convert/find-gate-spans.js +18 -0
- package/dist/esm/convert/format-state-name.d.ts +6 -0
- package/dist/esm/convert/format-state-name.js +6 -0
- package/dist/esm/convert/normalize-config.d.ts +11 -0
- package/dist/esm/convert/normalize-config.js +164 -0
- package/dist/esm/convert/resolve-quotes.d.ts +18 -0
- package/dist/esm/convert/resolve-quotes.js +169 -0
- package/dist/esm/convert/sanitize-name.d.ts +5 -0
- package/dist/esm/convert/sanitize-name.js +8 -0
- package/dist/esm/coverage-map/check-coverage.d.ts +20 -0
- package/dist/esm/coverage-map/check-coverage.js +21 -0
- package/dist/esm/coverage-map/coverage-map-schema.d.ts +11 -0
- package/dist/esm/coverage-map/coverage-map-schema.js +2 -0
- package/dist/esm/coverage-map/find-prompt-blocks.d.ts +15 -0
- package/dist/esm/coverage-map/find-prompt-blocks.js +140 -0
- package/dist/esm/examples/change-approval.d.ts +2 -0
- package/dist/esm/examples/change-approval.js +438 -0
- package/dist/esm/examples/examples.d.ts +9 -0
- package/dist/esm/examples/examples.js +8 -0
- package/dist/esm/examples/password-reset.d.ts +2 -0
- package/dist/esm/examples/password-reset.js +235 -0
- package/dist/esm/examples/refund-request.d.ts +2 -0
- package/dist/esm/examples/refund-request.js +233 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/layout/build-elk-graph.d.ts +18 -0
- package/dist/esm/layout/build-elk-graph.js +96 -0
- package/dist/esm/layout/card-limits.d.ts +8 -0
- package/dist/esm/layout/card-limits.js +4 -0
- package/dist/esm/layout/describe-config.d.ts +118 -0
- package/dist/esm/layout/describe-config.js +199 -0
- package/dist/esm/layout/layout-config.d.ts +5 -0
- package/dist/esm/layout/layout-config.js +73 -0
- package/dist/esm/layout/layout-metrics.d.ts +6 -0
- package/dist/esm/layout/layout-metrics.js +6 -0
- package/dist/esm/layout/pack-preamble.d.ts +20 -0
- package/dist/esm/layout/pack-preamble.js +66 -0
- package/dist/esm/layout/to-flow-elements.d.ts +46 -0
- package/dist/esm/layout/to-flow-elements.js +83 -0
- package/dist/esm/react/canvas/AmbientSelectionContext.d.ts +7 -0
- package/dist/esm/react/canvas/AmbientSelectionContext.js +5 -0
- package/dist/esm/react/canvas/DetailLevelContext.d.ts +3 -0
- package/dist/esm/react/canvas/DetailLevelContext.js +2 -0
- package/dist/esm/react/canvas/FocusNavigation.d.ts +8 -0
- package/dist/esm/react/canvas/FocusNavigation.js +92 -0
- package/dist/esm/react/canvas/StateNode.d.ts +5 -0
- package/dist/esm/react/canvas/StateNode.js +91 -0
- package/dist/esm/react/canvas/StatechartCanvas.d.ts +16 -0
- package/dist/esm/react/canvas/StatechartCanvas.js +184 -0
- package/dist/esm/react/canvas/TransitionEdge.d.ts +5 -0
- package/dist/esm/react/canvas/TransitionEdge.js +22 -0
- package/dist/esm/react/canvas/canvas-key-entries.d.ts +14 -0
- package/dist/esm/react/canvas/canvas-key-entries.js +51 -0
- package/dist/esm/react/canvas/find-element-box.d.ts +8 -0
- package/dist/esm/react/canvas/find-element-box.js +25 -0
- package/dist/esm/react/canvas/find-view-centre.d.ts +15 -0
- package/dist/esm/react/canvas/find-view-centre.js +16 -0
- package/dist/esm/react/canvas/list-chart-marks.d.ts +5 -0
- package/dist/esm/react/canvas/list-chart-marks.js +37 -0
- package/dist/esm/react/canvas/marks.d.ts +19 -0
- package/dist/esm/react/canvas/marks.js +24 -0
- package/dist/esm/react/canvas/useNewLayout.d.ts +17 -0
- package/dist/esm/react/canvas/useNewLayout.js +44 -0
- package/dist/esm/react/canvas/zoom-levels.d.ts +28 -0
- package/dist/esm/react/canvas/zoom-levels.js +54 -0
- package/dist/esm/react/hooks/conversion-client.d.ts +77 -0
- package/dist/esm/react/hooks/conversion-client.js +132 -0
- package/dist/esm/react/hooks/describe-agent-selection.d.ts +6 -0
- package/dist/esm/react/hooks/describe-agent-selection.js +18 -0
- package/dist/esm/react/hooks/describe-load-outcome.d.ts +13 -0
- package/dist/esm/react/hooks/describe-load-outcome.js +30 -0
- package/dist/esm/react/hooks/download-config.d.ts +6 -0
- package/dist/esm/react/hooks/download-config.js +11 -0
- package/dist/esm/react/hooks/example-location.d.ts +7 -0
- package/dist/esm/react/hooks/example-location.js +14 -0
- package/dist/esm/react/hooks/label-source.d.ts +21 -0
- package/dist/esm/react/hooks/label-source.js +19 -0
- package/dist/esm/react/hooks/parse-config-text.d.ts +16 -0
- package/dist/esm/react/hooks/parse-config-text.js +38 -0
- package/dist/esm/react/hooks/useAgentBridge.d.ts +28 -0
- package/dist/esm/react/hooks/useAgentBridge.js +52 -0
- package/dist/esm/react/hooks/useAgentSync.d.ts +29 -0
- package/dist/esm/react/hooks/useAgentSync.js +75 -0
- package/dist/esm/react/hooks/useChartLayout.d.ts +25 -0
- package/dist/esm/react/hooks/useChartLayout.js +29 -0
- package/dist/esm/react/hooks/useConversion.d.ts +31 -0
- package/dist/esm/react/hooks/useConversion.js +177 -0
- package/dist/esm/react/hooks/useConvertDraft.d.ts +18 -0
- package/dist/esm/react/hooks/useConvertDraft.js +11 -0
- package/dist/esm/react/hooks/useDocumentTitle.d.ts +2 -0
- package/dist/esm/react/hooks/useDocumentTitle.js +7 -0
- package/dist/esm/react/hooks/useEscapeToClear.d.ts +2 -0
- package/dist/esm/react/hooks/useEscapeToClear.js +16 -0
- package/dist/esm/react/hooks/useHarnessList.d.ts +26 -0
- package/dist/esm/react/hooks/useHarnessList.js +24 -0
- package/dist/esm/react/hooks/usePromptDirectory.d.ts +27 -0
- package/dist/esm/react/hooks/usePromptDirectory.js +34 -0
- package/dist/esm/react/hooks/useSelection.d.ts +17 -0
- package/dist/esm/react/hooks/useSelection.js +21 -0
- package/dist/esm/react/hooks/useShowAmbient.d.ts +7 -0
- package/dist/esm/react/hooks/useShowAmbient.js +22 -0
- package/dist/esm/react/hooks/useUnknownExample.d.ts +7 -0
- package/dist/esm/react/hooks/useUnknownExample.js +26 -0
- package/dist/esm/react/index.d.ts +24 -0
- package/dist/esm/react/index.js +23 -0
- package/dist/esm/react/styles.css +482 -0
- package/dist/esm/schema/agent-logic-schema.d.ts +439 -0
- package/dist/esm/schema/agent-logic-schema.js +100 -0
- package/dist/esm/schema/config-traversal.d.ts +19 -0
- package/dist/esm/schema/config-traversal.js +32 -0
- package/dist/esm/schema/validate-config.d.ts +17 -0
- package/dist/esm/schema/validate-config.js +205 -0
- package/dist/esm/schema/write-json-schema.d.ts +4 -0
- package/dist/esm/schema/write-json-schema.js +12 -0
- package/dist/esm/server/conversion-job-implementation.d.ts +11 -0
- package/dist/esm/server/conversion-job-implementation.js +84 -0
- package/dist/esm/server/create-prompts-app.d.ts +26 -0
- package/dist/esm/server/create-prompts-app.js +41 -0
- package/dist/esm/server/create-viewer-server-plugins.d.ts +15 -0
- package/dist/esm/server/create-viewer-server-plugins.js +76 -0
- package/dist/esm/server/index.d.ts +7 -0
- package/dist/esm/server/index.js +6 -0
- package/dist/esm/server/prompt-directory-schemas.d.ts +30 -0
- package/dist/esm/server/prompt-directory-schemas.js +11 -0
- package/dist/esm/server/prompt-directory.d.ts +40 -0
- package/dist/esm/server/prompt-directory.js +86 -0
- package/dist/esm/server/viewer-contract-implementation.d.ts +12 -0
- package/dist/esm/server/viewer-contract-implementation.js +85 -0
- package/dist/esm/source/fence.d.ts +16 -0
- package/dist/esm/source/fence.js +15 -0
- package/dist/esm/source/find-comment-spans.d.ts +6 -0
- package/dist/esm/source/find-comment-spans.js +60 -0
- package/dist/esm/source/find-occurrences.d.ts +2 -0
- package/dist/esm/source/find-occurrences.js +7 -0
- package/dist/esm/source/prompt-segments.d.ts +21 -0
- package/dist/esm/source/prompt-segments.js +65 -0
- package/docs/conversion-agreement.md +37 -0
- package/docs/extraction-rules.md +123 -0
- package/package.json +106 -2
- package/schema/agent-logic.schema.json +475 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { listStates, listTransitions } from "../schema/config-traversal.js";
|
|
2
|
+
import { formatPath } from "../schema/validate-config.js";
|
|
3
|
+
const ALWAYS_TRIGGER = 'always';
|
|
4
|
+
export function describeConfig(config) {
|
|
5
|
+
const states = [];
|
|
6
|
+
const transitions = [];
|
|
7
|
+
const ambient = [];
|
|
8
|
+
const findings = [];
|
|
9
|
+
const enteredIds = listEnteredIds(config);
|
|
10
|
+
const parentIdByPath = new Map();
|
|
11
|
+
const initialPaths = new Set();
|
|
12
|
+
const triggersByPath = new Map();
|
|
13
|
+
const namesById = new Map(listStates(config).map(({ state }) => [state.id, state.meta.name ?? state.id]));
|
|
14
|
+
function nameState(id) {
|
|
15
|
+
return namesById.get(id) ?? id;
|
|
16
|
+
}
|
|
17
|
+
for (const { path, state } of listStates(config)) {
|
|
18
|
+
const formattedPath = formatPath(path);
|
|
19
|
+
const isCompound = state.states !== undefined;
|
|
20
|
+
const isFinal = 'type' in state && state.type === 'final';
|
|
21
|
+
const stateTransitions = listTransitions(state);
|
|
22
|
+
const enclosingTriggers = triggersByPath.get(formatPath(path.slice(0, -2))) ?? new Set();
|
|
23
|
+
triggersByPath.set(formattedPath, new Set([...enclosingTriggers, ...stateTransitions.map(({ event }) => event ?? ALWAYS_TRIGGER)]));
|
|
24
|
+
const childKeys = Object.keys(state.states ?? {});
|
|
25
|
+
for (const key of childKeys) {
|
|
26
|
+
parentIdByPath.set(formatPath([...path, 'states', key]), state.id);
|
|
27
|
+
}
|
|
28
|
+
if (state.initial !== undefined)
|
|
29
|
+
initialPaths.add(formatPath([...path, 'states', state.initial]));
|
|
30
|
+
const parentId = parentIdByPath.get(formattedPath);
|
|
31
|
+
const stateDescription = {
|
|
32
|
+
kind: 'state',
|
|
33
|
+
id: state.id,
|
|
34
|
+
name: nameState(state.id),
|
|
35
|
+
path: formattedPath,
|
|
36
|
+
parentId,
|
|
37
|
+
isCompound,
|
|
38
|
+
isFinal,
|
|
39
|
+
isInitial: initialPaths.has(formattedPath),
|
|
40
|
+
waitsForUser: !isCompound && stateTransitions.some(({ event }) => event?.startsWith('user.') === true),
|
|
41
|
+
hasNoExit: !isCompound && !isFinal && stateTransitions.every(({ transition }) => staysIn(state.id, transition)),
|
|
42
|
+
isUnreachable: isOutermostUnentered(state.id, parentId, enteredIds),
|
|
43
|
+
entry: describeActions(state.entry),
|
|
44
|
+
exit: describeActions(state.exit),
|
|
45
|
+
inferred: 'inferred' in state.meta && state.meta.inferred === true,
|
|
46
|
+
source: state.meta.source,
|
|
47
|
+
};
|
|
48
|
+
states.push(stateDescription);
|
|
49
|
+
findings.push(...describeFindings(state.meta.findings, path, parentId === undefined ? undefined : stateDescription));
|
|
50
|
+
ambient.push(...describeAmbient(path, state, nameState(state.id), config.meta.prompt));
|
|
51
|
+
for (const { path: transitionPath, event, transition } of stateTransitions) {
|
|
52
|
+
const fullPath = [...path, ...transitionPath];
|
|
53
|
+
const transitionDescription = {
|
|
54
|
+
kind: 'transition',
|
|
55
|
+
path: formatPath(fullPath),
|
|
56
|
+
sourceId: state.id,
|
|
57
|
+
sourceName: nameState(state.id),
|
|
58
|
+
targetId: transition.target?.slice(1) ?? state.id,
|
|
59
|
+
targetName: nameState(transition.target?.slice(1) ?? state.id),
|
|
60
|
+
event,
|
|
61
|
+
guard: transition.guard?.params.text,
|
|
62
|
+
actions: describeActions(transition.actions),
|
|
63
|
+
isGlobal: isCompound,
|
|
64
|
+
isFallback: transition.guard === undefined &&
|
|
65
|
+
stateTransitions.some((sibling) => sibling.event === event && sibling.transition.guard !== undefined),
|
|
66
|
+
lacksAlternative: lacksAlternative(transition, event, stateTransitions, enclosingTriggers),
|
|
67
|
+
inferred: transition.meta.inferred === true,
|
|
68
|
+
gate: transition.meta.gate === true,
|
|
69
|
+
source: transition.meta.source,
|
|
70
|
+
};
|
|
71
|
+
transitions.push(transitionDescription);
|
|
72
|
+
findings.push(...describeFindings(transition.meta.findings, fullPath, transitionDescription));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return { states, transitions, ambient, findings };
|
|
76
|
+
}
|
|
77
|
+
export function buildTransitionLabel(transition) {
|
|
78
|
+
const { event, guard, isFallback, isGlobal, lacksAlternative } = transition;
|
|
79
|
+
const trigger = event === undefined ? (isGlobal ? 'Any time' : undefined) : isGlobal ? `Any time: ${event}` : event;
|
|
80
|
+
const guardLine = guard === undefined ? (isFallback ? 'otherwise' : event === undefined ? 'always' : undefined) : `If ${guard}`;
|
|
81
|
+
return {
|
|
82
|
+
trigger,
|
|
83
|
+
details: [
|
|
84
|
+
...(guardLine === undefined ? [] : [guardLine]),
|
|
85
|
+
...(lacksAlternative ? ['no alternative'] : []),
|
|
86
|
+
...transition.actions.map(({ text, gate }) => `Do: ${text}${gate ? ' (gate)' : ''}`),
|
|
87
|
+
...(transition.inferred ? ['inferred'] : []),
|
|
88
|
+
...(transition.gate ? ['gate'] : []),
|
|
89
|
+
],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export function listGaps({ states, transitions }) {
|
|
93
|
+
return states.flatMap((state) => [
|
|
94
|
+
...(state.hasNoExit || state.isUnreachable ? [state] : []),
|
|
95
|
+
...transitions.filter(({ sourceId, lacksAlternative }) => sourceId === state.id && lacksAlternative),
|
|
96
|
+
]);
|
|
97
|
+
}
|
|
98
|
+
export function listStateKinds(state) {
|
|
99
|
+
return [
|
|
100
|
+
...(state.isInitial ? ['Starts its flow'] : []),
|
|
101
|
+
...(state.waitsForUser ? ['Waits for user'] : []),
|
|
102
|
+
...(state.entry.length > 0 || state.exit.length > 0 ? ['Performs actions'] : []),
|
|
103
|
+
...(state.isFinal ? ['Ends'] : []),
|
|
104
|
+
...(state.inferred ? ['Inferred'] : []),
|
|
105
|
+
...([...state.entry, ...state.exit].some(({ gate }) => gate) ? ['Has a gate'] : []),
|
|
106
|
+
...(state.hasNoExit ? ['No exit'] : []),
|
|
107
|
+
...(state.isUnreachable ? ['Unreachable'] : []),
|
|
108
|
+
];
|
|
109
|
+
}
|
|
110
|
+
export function nameElement(element) {
|
|
111
|
+
if (element.kind === 'state')
|
|
112
|
+
return element.name;
|
|
113
|
+
return `${element.sourceName}, ${buildTransitionLabel(element).trigger ?? 'always'}`;
|
|
114
|
+
}
|
|
115
|
+
export function listTransitionKinds(transition) {
|
|
116
|
+
return [
|
|
117
|
+
...(transition.isGlobal ? ['Any time'] : []),
|
|
118
|
+
...(transition.isFallback ? ['Fallback'] : []),
|
|
119
|
+
...(transition.inferred ? ['Inferred'] : []),
|
|
120
|
+
...(transition.gate ? ['Gate'] : []),
|
|
121
|
+
...(transition.lacksAlternative ? ['No alternative'] : []),
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
function describeActions(actions) {
|
|
125
|
+
return (actions ?? []).map(({ params, meta }) => ({
|
|
126
|
+
text: params.text,
|
|
127
|
+
gate: meta?.gate === true,
|
|
128
|
+
source: meta?.source,
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
function describeAmbient(path, state, containerName, prompt) {
|
|
132
|
+
return (state.meta.ambient ?? []).map((span, index) => ({
|
|
133
|
+
kind: 'ambient',
|
|
134
|
+
path: formatPath([...path, 'meta', 'ambient', index]),
|
|
135
|
+
containerId: state.id,
|
|
136
|
+
containerName,
|
|
137
|
+
text: prompt.slice(span.start, span.end),
|
|
138
|
+
gate: span.gate === true,
|
|
139
|
+
source: { start: span.start, end: span.end },
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
function describeFindings(findings, path, element) {
|
|
143
|
+
return (findings ?? []).map(({ kind, sources, reason }, index) => ({
|
|
144
|
+
kind,
|
|
145
|
+
path: formatPath([...path, 'meta', 'findings', index]),
|
|
146
|
+
reason,
|
|
147
|
+
sources,
|
|
148
|
+
element,
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
function isOutermostUnentered(id, parentId, enteredIds) {
|
|
152
|
+
return !enteredIds.has(id) && (parentId === undefined || enteredIds.has(parentId));
|
|
153
|
+
}
|
|
154
|
+
function lacksAlternative(transition, event, stateTransitions, enclosingTriggers) {
|
|
155
|
+
return (transition.guard !== undefined &&
|
|
156
|
+
stateTransitions.every((sibling) => sibling.event !== event || sibling.transition === transition) &&
|
|
157
|
+
!enclosingTriggers.has(event ?? ALWAYS_TRIGGER));
|
|
158
|
+
}
|
|
159
|
+
function listEnteredIds(config) {
|
|
160
|
+
const entries = listStates(config);
|
|
161
|
+
const stateById = new Map(entries.map(({ state }) => [state.id, state]));
|
|
162
|
+
const parentIdById = new Map();
|
|
163
|
+
for (const { state } of entries) {
|
|
164
|
+
const children = Object.values(state.states ?? {});
|
|
165
|
+
for (const child of children)
|
|
166
|
+
parentIdById.set(child.id, state.id);
|
|
167
|
+
}
|
|
168
|
+
const entered = new Set();
|
|
169
|
+
const pending = [];
|
|
170
|
+
enter(config.id);
|
|
171
|
+
for (let id = pending.pop(); id !== undefined; id = pending.pop()) {
|
|
172
|
+
const state = stateById.get(id);
|
|
173
|
+
if (state === undefined)
|
|
174
|
+
continue;
|
|
175
|
+
for (const { transition } of listTransitions(state)) {
|
|
176
|
+
if (transition.target !== undefined)
|
|
177
|
+
enter(transition.target.slice(1));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return entered;
|
|
181
|
+
function enter(id) {
|
|
182
|
+
for (let ancestor = parentIdById.get(id); ancestor !== undefined; ancestor = parentIdById.get(ancestor)) {
|
|
183
|
+
mark(ancestor);
|
|
184
|
+
}
|
|
185
|
+
for (let state = stateById.get(id); state !== undefined;) {
|
|
186
|
+
mark(state.id);
|
|
187
|
+
state = state.initial === undefined ? undefined : state.states?.[state.initial];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function mark(id) {
|
|
191
|
+
if (entered.has(id))
|
|
192
|
+
return;
|
|
193
|
+
entered.add(id);
|
|
194
|
+
pending.push(id);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function staysIn(stateId, transition) {
|
|
198
|
+
return transition.target === undefined || transition.target === `#${stateId}`;
|
|
199
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AgentLogicConfig } from '../schema/agent-logic-schema.js';
|
|
2
|
+
import { type ElkGraphOptions } from './build-elk-graph.js';
|
|
3
|
+
import { type FlowElements } from './to-flow-elements.js';
|
|
4
|
+
/** Lays out a valid config as positioned React Flow nodes and edges. */
|
|
5
|
+
export declare function layoutConfig(config: AgentLogicConfig, options?: Pick<ElkGraphOptions, 'showAmbient'>): Promise<FlowElements>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import elkModule from 'elkjs/lib/elk.bundled.js';
|
|
2
|
+
import { buildElkGraph, HIDDEN_AMBIENT, PREAMBLE_FLOW_WIDTH } from "./build-elk-graph.js";
|
|
3
|
+
import { describeConfig } from "./describe-config.js";
|
|
4
|
+
import { countPreambleColumns } from "./pack-preamble.js";
|
|
5
|
+
import { toFlowElements } from "./to-flow-elements.js";
|
|
6
|
+
const elk = new elkModule.default();
|
|
7
|
+
export async function layoutConfig(config, options = HIDDEN_AMBIENT) {
|
|
8
|
+
const description = describeConfig(config);
|
|
9
|
+
const extraWidths = new Map();
|
|
10
|
+
const columnLimits = new Map();
|
|
11
|
+
const graphOptions = { ...options, extraWidths, preambleColumnLimits: columnLimits };
|
|
12
|
+
let layout = await elk.layout(buildElkGraph(description, graphOptions));
|
|
13
|
+
for (;;) {
|
|
14
|
+
const widths = collectWidths(layout);
|
|
15
|
+
const deficits = findDeficits(description, widths, options);
|
|
16
|
+
for (const [id, deficit] of deficits)
|
|
17
|
+
extraWidths.set(id, (extraWidths.get(id) ?? 0) + deficit);
|
|
18
|
+
if (deficits.size === 0 && !updateColumnLimits(description, widths, options, columnLimits))
|
|
19
|
+
break;
|
|
20
|
+
layout = await elk.layout(buildElkGraph(description, graphOptions));
|
|
21
|
+
}
|
|
22
|
+
return toFlowElements(description, layout, { ...options, preambleColumnLimits: columnLimits });
|
|
23
|
+
}
|
|
24
|
+
function collectWidths(layout, widths = new Map()) {
|
|
25
|
+
widths.set(layout.id, layout.width ?? 0);
|
|
26
|
+
const children = layout.children ?? [];
|
|
27
|
+
for (const child of children)
|
|
28
|
+
collectWidths(child, widths);
|
|
29
|
+
return widths;
|
|
30
|
+
}
|
|
31
|
+
function findDeficits(description, widths, { showAmbient }) {
|
|
32
|
+
const deficits = new Map();
|
|
33
|
+
if (!showAmbient)
|
|
34
|
+
return deficits;
|
|
35
|
+
const narrowIds = new Set(listPreambleFlowIds(description).filter((id) => (widths.get(id) ?? 0) < PREAMBLE_FLOW_WIDTH));
|
|
36
|
+
for (const state of description.states) {
|
|
37
|
+
if (!narrowIds.has(state.id))
|
|
38
|
+
continue;
|
|
39
|
+
const hasNarrowDescendant = description.states.some(({ id }) => narrowIds.has(id) && isDescendant(description, id, state.id));
|
|
40
|
+
if (!hasNarrowDescendant)
|
|
41
|
+
deficits.set(state.id, PREAMBLE_FLOW_WIDTH - (widths.get(state.id) ?? 0));
|
|
42
|
+
}
|
|
43
|
+
return deficits;
|
|
44
|
+
}
|
|
45
|
+
function updateColumnLimits(description, widths, { showAmbient }, columnLimits) {
|
|
46
|
+
if (!showAmbient)
|
|
47
|
+
return false;
|
|
48
|
+
let hasChanged = false;
|
|
49
|
+
for (const id of listPreambleFlowIds(description)) {
|
|
50
|
+
const fitting = countPreambleColumns(widths.get(id) ?? 0);
|
|
51
|
+
const current = columnLimits.get(id);
|
|
52
|
+
const next = current === undefined ? fitting : Math.min(current, fitting);
|
|
53
|
+
columnLimits.set(id, next);
|
|
54
|
+
if (next !== (current ?? 1))
|
|
55
|
+
hasChanged = true;
|
|
56
|
+
}
|
|
57
|
+
return hasChanged;
|
|
58
|
+
}
|
|
59
|
+
function isDescendant(description, id, ancestorId) {
|
|
60
|
+
let parentId = description.states.find((state) => state.id === id)?.parentId;
|
|
61
|
+
while (parentId !== undefined) {
|
|
62
|
+
if (parentId === ancestorId)
|
|
63
|
+
return true;
|
|
64
|
+
const currentId = parentId;
|
|
65
|
+
parentId = description.states.find((state) => state.id === currentId)?.parentId;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
function listPreambleFlowIds(description) {
|
|
70
|
+
return description.states
|
|
71
|
+
.filter(({ id, isCompound }) => isCompound && description.ambient.some(({ containerId }) => containerId === id))
|
|
72
|
+
.map(({ id }) => id);
|
|
73
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The left padding of a flow: room for the initial mark, which sits outside a state's left edge. */
|
|
2
|
+
export declare const COMPOUND_PADDING_LEFT = 28;
|
|
3
|
+
/** The right padding of a flow, before any width that `layoutConfig` adds to reach a preamble's width. */
|
|
4
|
+
export declare const COMPOUND_PADDING_RIGHT = 20;
|
|
5
|
+
/** Counts the lines that text takes when wrapped at a width, from an average character width. */
|
|
6
|
+
export declare function countWrappedLines(lines: string[], width: number, characterWidth: number): number;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const COMPOUND_PADDING_LEFT = 28;
|
|
2
|
+
export const COMPOUND_PADDING_RIGHT = 20;
|
|
3
|
+
export function countWrappedLines(lines, width, characterWidth) {
|
|
4
|
+
const charactersPerLine = Math.floor(width / characterWidth);
|
|
5
|
+
return lines.reduce((count, line) => count + Math.max(1, Math.ceil(line.length / charactersPerLine)), 0);
|
|
6
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AmbientDescription } from './describe-config.js';
|
|
2
|
+
/** The gap between two columns of a preamble. */
|
|
3
|
+
export declare const PREAMBLE_COLUMN_GAP = 32;
|
|
4
|
+
/** The columns of a preamble, filled down each column and then across, and the height of the tallest. */
|
|
5
|
+
export interface PreambleColumns {
|
|
6
|
+
columns: number;
|
|
7
|
+
columnHeight: number;
|
|
8
|
+
}
|
|
9
|
+
/** The parts of an ambient instruction that its height depends on. */
|
|
10
|
+
type AmbientEntry = Pick<AmbientDescription, 'text' | 'gate'>;
|
|
11
|
+
/** Counts the columns of the preamble's measure, and the gaps between them, that fit inside a flow of a width; at least one. */
|
|
12
|
+
export declare function countPreambleColumns(flowWidth: number): number;
|
|
13
|
+
/** Estimates the height of a flow's preamble in at most a number of columns: a heading, then the columns; zero without any entries. */
|
|
14
|
+
export declare function estimatePreambleHeight(ambient: AmbientEntry[], columnLimit: number): number;
|
|
15
|
+
/**
|
|
16
|
+
* Finds the lowest column height at which the entries, filled down each column and then across without splitting
|
|
17
|
+
* one, take at most a number of columns, and counts the columns that they then take.
|
|
18
|
+
*/
|
|
19
|
+
export declare function packPreamble(ambient: AmbientEntry[], columnLimit: number): PreambleColumns;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { MAX_AMBIENT_LINES, PREAMBLE_WIDTH } from "./card-limits.js";
|
|
2
|
+
import { COMPOUND_PADDING_LEFT, COMPOUND_PADDING_RIGHT, countWrappedLines } from "./layout-metrics.js";
|
|
3
|
+
const PROMPT_CHARACTER_WIDTH = 7.2;
|
|
4
|
+
const PREAMBLE_LINE_HEIGHT = 20;
|
|
5
|
+
const PREAMBLE_HEADING_HEIGHT = 16;
|
|
6
|
+
const PREAMBLE_ENTRY_GAP = 4;
|
|
7
|
+
const PREAMBLE_GAP_BELOW = 8;
|
|
8
|
+
export const PREAMBLE_COLUMN_GAP = 32;
|
|
9
|
+
export function countPreambleColumns(flowWidth) {
|
|
10
|
+
const innerWidth = flowWidth - COMPOUND_PADDING_LEFT - COMPOUND_PADDING_RIGHT;
|
|
11
|
+
return Math.max(1, Math.floor((innerWidth + PREAMBLE_COLUMN_GAP) / (PREAMBLE_WIDTH + PREAMBLE_COLUMN_GAP)));
|
|
12
|
+
}
|
|
13
|
+
export function estimatePreambleHeight(ambient, columnLimit) {
|
|
14
|
+
if (ambient.length === 0)
|
|
15
|
+
return 0;
|
|
16
|
+
return PREAMBLE_HEADING_HEIGHT + packPreamble(ambient, columnLimit).columnHeight + PREAMBLE_GAP_BELOW;
|
|
17
|
+
}
|
|
18
|
+
export function packPreamble(ambient, columnLimit) {
|
|
19
|
+
const heights = ambient.map(estimateEntryHeight);
|
|
20
|
+
if (heights.length === 0)
|
|
21
|
+
return { columns: 0, columnHeight: 0 };
|
|
22
|
+
const candidates = [...new Set(listRunHeights(heights))].toSorted((a, b) => a - b);
|
|
23
|
+
let low = 0;
|
|
24
|
+
let high = candidates.length - 1;
|
|
25
|
+
while (low < high) {
|
|
26
|
+
const middle = Math.floor((low + high) / 2);
|
|
27
|
+
if (countFilledColumns(heights, candidates[middle] ?? 0) <= columnLimit)
|
|
28
|
+
high = middle;
|
|
29
|
+
else
|
|
30
|
+
low = middle + 1;
|
|
31
|
+
}
|
|
32
|
+
const columnHeight = candidates[low] ?? 0;
|
|
33
|
+
return { columns: countFilledColumns(heights, columnHeight), columnHeight };
|
|
34
|
+
}
|
|
35
|
+
function countFilledColumns(heights, columnHeight) {
|
|
36
|
+
let columns = 1;
|
|
37
|
+
let used;
|
|
38
|
+
for (const height of heights) {
|
|
39
|
+
if (used === undefined)
|
|
40
|
+
used = height;
|
|
41
|
+
else if (used + PREAMBLE_ENTRY_GAP + height <= columnHeight)
|
|
42
|
+
used += PREAMBLE_ENTRY_GAP + height;
|
|
43
|
+
else {
|
|
44
|
+
columns += 1;
|
|
45
|
+
used = height;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return columns;
|
|
49
|
+
}
|
|
50
|
+
function estimateEntryHeight({ text, gate }) {
|
|
51
|
+
const lines = countWrappedLines([gate ? `gate ${text}` : text], PREAMBLE_WIDTH, PROMPT_CHARACTER_WIDTH);
|
|
52
|
+
return Math.min(MAX_AMBIENT_LINES, lines) * PREAMBLE_LINE_HEIGHT;
|
|
53
|
+
}
|
|
54
|
+
function listRunHeights(heights) {
|
|
55
|
+
const tallest = Math.max(...heights);
|
|
56
|
+
const runHeights = [];
|
|
57
|
+
for (let start = 0; start < heights.length; start += 1) {
|
|
58
|
+
let height = -PREAMBLE_ENTRY_GAP;
|
|
59
|
+
for (let end = start; end < heights.length; end += 1) {
|
|
60
|
+
height += PREAMBLE_ENTRY_GAP + (heights[end] ?? 0);
|
|
61
|
+
if (height >= tallest)
|
|
62
|
+
runHeights.push(height);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return runHeights;
|
|
66
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Edge, Node } from '@xyflow/react';
|
|
2
|
+
import type { ElkNode, ElkPoint } from 'elkjs/lib/elk-api.js';
|
|
3
|
+
import { type ElkGraphOptions } from './build-elk-graph.js';
|
|
4
|
+
import { type AmbientDescription, type ConfigDescription, type StateDescription, type TransitionDescription } from './describe-config.js';
|
|
5
|
+
import { type PreambleColumns } from './pack-preamble.js';
|
|
6
|
+
export type StateFlowNode = Node<StateNodeData, 'state'>;
|
|
7
|
+
export type StateNodeData = Plain<StateDescription> & {
|
|
8
|
+
/** Every flow of the chart, parents first, which a flow places its name among; absent for a leaf. */
|
|
9
|
+
flowNameSites?: FlowNameSite[];
|
|
10
|
+
/** The flow's ambient instructions, drawn as its preamble; absent for a leaf and while preambles are hidden. */
|
|
11
|
+
ambient?: Plain<AmbientDescription>[];
|
|
12
|
+
/** The columns that the layout reserved for the flow's preamble; absent when the flow draws none. */
|
|
13
|
+
preambleLayout?: Plain<PreambleColumns>;
|
|
14
|
+
};
|
|
15
|
+
/** A flow's id, the name that it shows, and the top-left corner and width of the flow in canvas coordinates. */
|
|
16
|
+
export interface FlowNameSite {
|
|
17
|
+
id: string;
|
|
18
|
+
name: string;
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
width: number;
|
|
22
|
+
}
|
|
23
|
+
export type TransitionEdgeData = Plain<TransitionDescription> & {
|
|
24
|
+
/** The route in canvas coordinates, from the source's border to the target's. */
|
|
25
|
+
points: ElkPoint[];
|
|
26
|
+
/** The label's box in canvas coordinates. */
|
|
27
|
+
labelBox: {
|
|
28
|
+
x: number;
|
|
29
|
+
y: number;
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
} | undefined;
|
|
33
|
+
};
|
|
34
|
+
export type TransitionFlowEdge = Edge<TransitionEdgeData, 'transition'>;
|
|
35
|
+
/** Restates an interface as an object type, which, unlike an interface, satisfies React Flow's `Record<string, unknown>` data constraint. */
|
|
36
|
+
type Plain<T> = {
|
|
37
|
+
[K in keyof T]: T[K];
|
|
38
|
+
};
|
|
39
|
+
export interface FlowElements {
|
|
40
|
+
/** Nodes with each parent before its children, as React Flow requires. */
|
|
41
|
+
nodes: StateFlowNode[];
|
|
42
|
+
edges: TransitionFlowEdge[];
|
|
43
|
+
}
|
|
44
|
+
/** Combines a described config with its ELK layout into React Flow nodes and edges. */
|
|
45
|
+
export declare function toFlowElements(description: ConfigDescription, layout: ElkNode, { showAmbient, preambleColumnLimits }?: Pick<ElkGraphOptions, 'showAmbient' | 'preambleColumnLimits'>): FlowElements;
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { HIDDEN_AMBIENT } from "./build-elk-graph.js";
|
|
2
|
+
import { buildTransitionLabel, listStateKinds, } from "./describe-config.js";
|
|
3
|
+
import { packPreamble } from "./pack-preamble.js";
|
|
4
|
+
export function toFlowElements(description, layout, { showAmbient, preambleColumnLimits } = HIDDEN_AMBIENT) {
|
|
5
|
+
const elkNodes = new Map();
|
|
6
|
+
const elkEdges = new Map();
|
|
7
|
+
collectLayout(layout, elkNodes, elkEdges);
|
|
8
|
+
const flowNameSites = listFlowNameSites(description, elkNodes);
|
|
9
|
+
const nodes = description.states.map((state) => {
|
|
10
|
+
const elkNode = requireLayout(elkNodes.get(state.id), state.id);
|
|
11
|
+
const ambient = state.isCompound && showAmbient
|
|
12
|
+
? description.ambient.filter(({ containerId }) => containerId === state.id)
|
|
13
|
+
: undefined;
|
|
14
|
+
return {
|
|
15
|
+
id: state.id,
|
|
16
|
+
type: 'state',
|
|
17
|
+
position: { x: elkNode.x ?? 0, y: elkNode.y ?? 0 },
|
|
18
|
+
width: elkNode.width ?? 0,
|
|
19
|
+
height: elkNode.height ?? 0,
|
|
20
|
+
...(state.parentId !== undefined && { parentId: state.parentId }),
|
|
21
|
+
...(state.isCompound && { style: { pointerEvents: 'none' } }),
|
|
22
|
+
ariaLabel: [`State ${state.name}`, ...listStateKinds(state)].join(', '),
|
|
23
|
+
data: {
|
|
24
|
+
...state,
|
|
25
|
+
...(state.isCompound && { flowNameSites }),
|
|
26
|
+
...(ambient !== undefined && { ambient }),
|
|
27
|
+
...(ambient !== undefined &&
|
|
28
|
+
ambient.length > 0 && { preambleLayout: packPreamble(ambient, preambleColumnLimits?.get(state.id) ?? 1) }),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
const edges = description.transitions.map((transition) => {
|
|
33
|
+
const elkEdge = requireLayout(elkEdges.get(transition.path), transition.path);
|
|
34
|
+
const [section] = elkEdge.sections ?? [];
|
|
35
|
+
const [label] = elkEdge.labels ?? [];
|
|
36
|
+
const { trigger, details } = buildTransitionLabel(transition);
|
|
37
|
+
return {
|
|
38
|
+
id: transition.path,
|
|
39
|
+
type: 'transition',
|
|
40
|
+
source: transition.sourceId,
|
|
41
|
+
target: transition.targetId,
|
|
42
|
+
ariaLabel: `Transition from ${transition.sourceName} to ${transition.targetName}: ${[...(trigger === undefined ? [] : [trigger]), ...details].join('; ')}`,
|
|
43
|
+
domAttributes: { tabIndex: -1 },
|
|
44
|
+
data: {
|
|
45
|
+
...transition,
|
|
46
|
+
points: section === undefined ? [] : [section.startPoint, ...(section.bendPoints ?? []), section.endPoint],
|
|
47
|
+
labelBox: label === undefined
|
|
48
|
+
? undefined
|
|
49
|
+
: { x: label.x ?? 0, y: label.y ?? 0, width: label.width ?? 0, height: label.height ?? 0 },
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
return { nodes, edges };
|
|
54
|
+
}
|
|
55
|
+
function collectLayout(node, nodes, edges) {
|
|
56
|
+
nodes.set(node.id, node);
|
|
57
|
+
const ownEdges = node.edges ?? [];
|
|
58
|
+
for (const edge of ownEdges) {
|
|
59
|
+
edges.set(edge.id, edge);
|
|
60
|
+
}
|
|
61
|
+
const children = node.children ?? [];
|
|
62
|
+
for (const child of children) {
|
|
63
|
+
collectLayout(child, nodes, edges);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function listFlowNameSites(description, elkNodes) {
|
|
67
|
+
const origins = new Map();
|
|
68
|
+
const sites = [];
|
|
69
|
+
for (const state of description.states) {
|
|
70
|
+
const elkNode = requireLayout(elkNodes.get(state.id), state.id);
|
|
71
|
+
const parent = state.parentId === undefined ? { x: 0, y: 0 } : requireLayout(origins.get(state.parentId), state.parentId);
|
|
72
|
+
const origin = { x: parent.x + (elkNode.x ?? 0), y: parent.y + (elkNode.y ?? 0) };
|
|
73
|
+
origins.set(state.id, origin);
|
|
74
|
+
if (state.isCompound)
|
|
75
|
+
sites.push({ id: state.id, name: state.name, ...origin, width: elkNode.width ?? 0 });
|
|
76
|
+
}
|
|
77
|
+
return sites;
|
|
78
|
+
}
|
|
79
|
+
function requireLayout(value, id) {
|
|
80
|
+
if (value === undefined)
|
|
81
|
+
throw new Error(`The layout lacks the element "${id}".`);
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** What a flow's preamble needs to mark its selected entry and to select one: the canvas's selection and its setter. */
|
|
2
|
+
export interface AmbientSelection {
|
|
3
|
+
selectedPath: string | undefined;
|
|
4
|
+
onSelect: (path: string) => void;
|
|
5
|
+
}
|
|
6
|
+
/** The canvas's selection, for the ambient instructions that React Flow's node selection does not reach. */
|
|
7
|
+
export declare const AmbientSelectionContext: import("react").Context<AmbientSelection>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Moves keyboard focus between a state and its outgoing transitions with the arrow keys, hands Tab on a transition to
|
|
3
|
+
* its source state, so that the Tab sequence of states continues from there, and brings an element that the keyboard
|
|
4
|
+
* focuses into view, reporting that move to `onCentre`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function FocusNavigation({ onCentre }: {
|
|
7
|
+
onCentre: () => void;
|
|
8
|
+
}): null;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { useReactFlow, useStoreApi } from '@xyflow/react';
|
|
2
|
+
import { useEffect } from 'react';
|
|
3
|
+
import { findElementBox } from "./find-element-box.js";
|
|
4
|
+
import { findViewCentre, readView } from "./find-view-centre.js";
|
|
5
|
+
export function FocusNavigation({ onCentre }) {
|
|
6
|
+
const store = useStoreApi();
|
|
7
|
+
const { getEdges, getInternalNode, getNodes, getViewport, setCenter } = useReactFlow();
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
const pane = store.getState().domNode;
|
|
10
|
+
if (pane === null)
|
|
11
|
+
return;
|
|
12
|
+
function handleKeyDown(event) {
|
|
13
|
+
const root = event.currentTarget;
|
|
14
|
+
if (!(root instanceof HTMLElement))
|
|
15
|
+
return;
|
|
16
|
+
if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey)
|
|
17
|
+
return;
|
|
18
|
+
if (!(event.target instanceof Element))
|
|
19
|
+
return;
|
|
20
|
+
const element = event.target.closest('.react-flow__node, .react-flow__edge');
|
|
21
|
+
if (!(element instanceof HTMLElement || element instanceof SVGElement))
|
|
22
|
+
return;
|
|
23
|
+
const id = element.dataset['id'];
|
|
24
|
+
if (id === undefined)
|
|
25
|
+
return;
|
|
26
|
+
const edges = getEdges();
|
|
27
|
+
if (element.classList.contains('react-flow__node')) {
|
|
28
|
+
const first = edges.find(({ source }) => source === id);
|
|
29
|
+
if (event.key !== 'ArrowRight' || first === undefined)
|
|
30
|
+
return;
|
|
31
|
+
event.preventDefault();
|
|
32
|
+
focusElement(root, 'edge', first.id);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const edge = edges.find((candidate) => candidate.id === id);
|
|
36
|
+
if (edge === undefined)
|
|
37
|
+
return;
|
|
38
|
+
const siblings = edges.filter(({ source }) => source === edge.source);
|
|
39
|
+
const index = siblings.indexOf(edge);
|
|
40
|
+
switch (event.key) {
|
|
41
|
+
case 'ArrowDown':
|
|
42
|
+
case 'ArrowUp': {
|
|
43
|
+
event.preventDefault();
|
|
44
|
+
const next = siblings[event.key === 'ArrowDown' ? index + 1 : index - 1];
|
|
45
|
+
if (next !== undefined)
|
|
46
|
+
focusElement(root, 'edge', next.id);
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
case 'ArrowLeft':
|
|
50
|
+
event.preventDefault();
|
|
51
|
+
focusElement(root, 'node', edge.source);
|
|
52
|
+
break;
|
|
53
|
+
case 'Tab':
|
|
54
|
+
focusElement(root, 'node', edge.source);
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function handleFocusIn(event) {
|
|
59
|
+
if (!(event.target instanceof HTMLElement || event.target instanceof SVGElement))
|
|
60
|
+
return;
|
|
61
|
+
const { target } = event;
|
|
62
|
+
const id = target.dataset['id'];
|
|
63
|
+
if (id === undefined || !target.matches(':focus-visible'))
|
|
64
|
+
return;
|
|
65
|
+
const nodes = getNodes();
|
|
66
|
+
const path = target.classList.contains('react-flow__node') ? nodes.find((node) => node.id === id)?.data.path : id;
|
|
67
|
+
if (path === undefined)
|
|
68
|
+
return;
|
|
69
|
+
const box = findElementBox(path, nodes, getEdges(), getInternalNode);
|
|
70
|
+
if (box === undefined)
|
|
71
|
+
return;
|
|
72
|
+
const { width, height } = store.getState();
|
|
73
|
+
const viewport = getViewport();
|
|
74
|
+
const centre = findViewCentre(box, readView(viewport, width, height));
|
|
75
|
+
if (centre === undefined)
|
|
76
|
+
return;
|
|
77
|
+
onCentre();
|
|
78
|
+
void setCenter(centre.x, centre.y, { zoom: viewport.zoom });
|
|
79
|
+
}
|
|
80
|
+
pane.addEventListener('keydown', handleKeyDown);
|
|
81
|
+
pane.addEventListener('focusin', handleFocusIn);
|
|
82
|
+
return () => {
|
|
83
|
+
pane.removeEventListener('keydown', handleKeyDown);
|
|
84
|
+
pane.removeEventListener('focusin', handleFocusIn);
|
|
85
|
+
};
|
|
86
|
+
}, [getEdges, getInternalNode, getNodes, getViewport, onCentre, setCenter, store]);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
function focusElement(pane, kind, id) {
|
|
90
|
+
const elements = pane.querySelectorAll(`.react-flow__${kind}`);
|
|
91
|
+
[...elements].find((element) => element.dataset['id'] === id)?.focus();
|
|
92
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type NodeProps } from '@xyflow/react';
|
|
2
|
+
import { type ReactElement } from 'react';
|
|
3
|
+
import type { StateFlowNode } from '../../layout/to-flow-elements.js';
|
|
4
|
+
/** A state on the canvas: a container named above its top edge for a compound state, a titled card for any other, each with its kind's marks. */
|
|
5
|
+
export declare function StateNode({ data }: NodeProps<StateFlowNode>): ReactElement;
|