codeassembly-agent-logic 0.8.0 → 0.10.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 +85 -0
- package/CHANGELOG.md +24 -0
- package/README.md +47 -18
- package/dist/esm/agent/convert-with-agent.d.ts +3 -3
- package/dist/esm/agent/convert-with-agent.js +2 -1
- package/dist/esm/conversion-session/format-conversion-input.d.ts +9 -0
- package/dist/esm/conversion-session/format-conversion-input.js +11 -1
- package/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/job/create-conversion-job.d.ts +10 -1
- package/dist/esm/job/create-conversion-job.js +6 -2
- package/dist/esm/job/index.d.ts +1 -1
- package/dist/esm/job/index.js +1 -1
- package/dist/esm/react/canvas/CanvasKey.d.ts +38 -0
- package/dist/esm/react/canvas/CanvasKey.js +33 -0
- package/dist/esm/react/hooks/document-types.d.ts +72 -0
- package/dist/esm/react/hooks/document-types.js +3 -0
- package/dist/esm/react/hooks/file-location.d.ts +10 -0
- package/dist/esm/react/hooks/file-location.js +19 -0
- package/dist/esm/react/hooks/label-source.js +2 -1
- package/dist/esm/react/hooks/load-messages.d.ts +2 -0
- package/dist/esm/react/hooks/load-messages.js +3 -0
- package/dist/esm/react/hooks/read-document.d.ts +48 -0
- package/dist/esm/react/hooks/read-document.js +88 -0
- package/dist/esm/react/hooks/stored-base.d.ts +7 -0
- package/dist/esm/react/hooks/stored-base.js +8 -0
- package/dist/esm/react/hooks/stored-upload.d.ts +9 -0
- package/dist/esm/react/hooks/stored-upload.js +16 -0
- package/dist/esm/react/hooks/useDocumentState.d.ts +57 -0
- package/dist/esm/react/hooks/useDocumentState.js +151 -0
- package/dist/esm/react/index.d.ts +18 -1
- package/dist/esm/react/index.js +13 -0
- package/dist/esm/react/inspector/Inspector.d.ts +13 -0
- package/dist/esm/react/inspector/Inspector.js +58 -0
- package/dist/esm/react/messages/FindingList.d.ts +18 -0
- package/dist/esm/react/messages/FindingList.js +15 -0
- package/dist/esm/react/messages/GapList.d.ts +14 -0
- package/dist/esm/react/messages/GapList.js +18 -0
- package/dist/esm/react/messages/Messages.d.ts +29 -0
- package/dist/esm/react/messages/Messages.js +19 -0
- package/dist/esm/react/messages/SectionMessage.d.ts +10 -0
- package/dist/esm/react/messages/SectionMessage.js +8 -0
- package/dist/esm/react/messages/UnconvertedPassages.d.ts +15 -0
- package/dist/esm/react/messages/UnconvertedPassages.js +15 -0
- package/dist/esm/react/messages/abbreviate.d.ts +2 -0
- package/dist/esm/react/messages/abbreviate.js +5 -0
- package/dist/esm/react/messages/icons.d.ts +9 -0
- package/dist/esm/react/messages/icons.js +10 -0
- package/dist/esm/react/slots.d.ts +28 -0
- package/dist/esm/react/slots.js +1 -0
- package/dist/esm/react/source/SourceLine.d.ts +13 -0
- package/dist/esm/react/source/SourceLine.js +49 -0
- package/dist/esm/react/source/Workspace.d.ts +32 -0
- package/dist/esm/react/source/Workspace.js +22 -0
- package/dist/esm/react/source/build-connector.d.ts +39 -0
- package/dist/esm/react/source/build-connector.js +23 -0
- package/dist/esm/react/styles.css +445 -6
- package/dist/esm/schema/agent-logic-file-name.d.ts +4 -0
- package/dist/esm/schema/agent-logic-file-name.js +4 -0
- package/package.json +4 -3
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ComponentType, ReactElement } from 'react';
|
|
2
|
+
import { type ConfigDescription } from '../../layout/describe-config.js';
|
|
3
|
+
import type { AgentLogicConfig, Span } from '../../schema/agent-logic-schema.js';
|
|
4
|
+
import type { MessageCounts } from '../canvas/CanvasKey.js';
|
|
5
|
+
import type { ButtonProps, LinkButtonProps } from '../slots.js';
|
|
6
|
+
export interface MessagesProps {
|
|
7
|
+
config: AgentLogicConfig;
|
|
8
|
+
description: ConfigDescription;
|
|
9
|
+
/** Receives the path of a state or transition named in the panel when it is clicked. */
|
|
10
|
+
onSelect: (path: string) => void;
|
|
11
|
+
/** Receives the passages of a finding or an unconverted passage to mark in the prompt. */
|
|
12
|
+
onShow: (passages: Span[]) => void;
|
|
13
|
+
/** The id that the toggle in the canvas key names as the panel that it controls. */
|
|
14
|
+
id: string;
|
|
15
|
+
isOpen: boolean;
|
|
16
|
+
/** The app's button, which the panel renders for each disclosure and each "Show in prompt". */
|
|
17
|
+
Button: ComponentType<ButtonProps>;
|
|
18
|
+
/** The app's in-page link, which the panel renders, inline in its sentence, for each element that it names. */
|
|
19
|
+
LinkButton: ComponentType<LinkButtonProps>;
|
|
20
|
+
}
|
|
21
|
+
/** Returns the number of each kind of message that a converted document has. */
|
|
22
|
+
export declare function countMessages(config: AgentLogicConfig, description: ConfigDescription): MessageCounts;
|
|
23
|
+
/** Reports whether a document has any message to list. */
|
|
24
|
+
export declare function hasMessages({ gaps, findings, unconverted }: MessageCounts): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* The panel below the canvas of a converted document, of a fixed height and hidden until its toggle opens it: its
|
|
27
|
+
* gaps, the converter's findings, and its unconverted passages.
|
|
28
|
+
*/
|
|
29
|
+
export declare function Messages({ config, description, onSelect, onShow, id, isOpen, Button, LinkButton, }: MessagesProps): ReactElement;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { listGaps } from "../../layout/describe-config.js";
|
|
3
|
+
import { FindingList } from "./FindingList.js";
|
|
4
|
+
import { GapList } from "./GapList.js";
|
|
5
|
+
import { UnconvertedPassages } from "./UnconvertedPassages.js";
|
|
6
|
+
export function countMessages(config, description) {
|
|
7
|
+
return {
|
|
8
|
+
gaps: listGaps(description).length,
|
|
9
|
+
findings: description.findings.length,
|
|
10
|
+
unconverted: config.meta.unconverted.length,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function hasMessages({ gaps, findings, unconverted }) {
|
|
14
|
+
return gaps + findings + unconverted > 0;
|
|
15
|
+
}
|
|
16
|
+
export function Messages({ config, description, onSelect, onShow, id, isOpen, Button, LinkButton, }) {
|
|
17
|
+
const { prompt, unconverted } = config.meta;
|
|
18
|
+
return (_jsxs("section", { id: id, "aria-label": "Messages", hidden: !isOpen, className: "statechart-messages", children: [_jsx(GapList, { description: description, onSelect: onSelect, LinkButton: LinkButton }), _jsx(FindingList, { findings: description.findings, onSelect: onSelect, onShow: onShow, Button: Button, LinkButton: LinkButton }), _jsx(UnconvertedPassages, { prompt: prompt, passages: unconverted, onShow: (passage) => onShow([passage]), Button: Button })] }));
|
|
19
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode } from 'react';
|
|
2
|
+
export interface SectionMessageProps {
|
|
3
|
+
/** `warning` for what needs the reader's attention; `information` for what only informs. */
|
|
4
|
+
appearance: 'information' | 'warning';
|
|
5
|
+
/** The heading; a node rather than text so that it can hold a control, such as a disclosure. */
|
|
6
|
+
title: ReactNode;
|
|
7
|
+
children?: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
/** A tinted section with the icon of its appearance beside a heading and its body. */
|
|
10
|
+
export declare function SectionMessage({ appearance, title, children }: SectionMessageProps): ReactElement;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId } from 'react';
|
|
3
|
+
import { InformationIcon, WarningIcon } from "./icons.js";
|
|
4
|
+
export function SectionMessage({ appearance, title, children }) {
|
|
5
|
+
const titleId = useId();
|
|
6
|
+
const Icon = appearance === 'information' ? InformationIcon : WarningIcon;
|
|
7
|
+
return (_jsxs("section", { "aria-labelledby": titleId, className: `statechart-messages__section statechart-messages__section--${appearance}`, children: [_jsx(Icon, {}), _jsxs("div", { className: "statechart-messages__section-body", children: [_jsx("h2", { id: titleId, className: "statechart-messages__section-title", children: title }), children] })] }));
|
|
8
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ComponentType, type ReactElement } from 'react';
|
|
2
|
+
import type { UnconvertedSpan } from '../../schema/agent-logic-schema.js';
|
|
3
|
+
import type { ButtonProps } from '../slots.js';
|
|
4
|
+
export interface UnconvertedPassagesProps {
|
|
5
|
+
prompt: string;
|
|
6
|
+
passages: UnconvertedSpan[];
|
|
7
|
+
/** Receives the passage whose button was clicked, to show it in the prompt. */
|
|
8
|
+
onShow: (passage: UnconvertedSpan) => void;
|
|
9
|
+
Button: ComponentType<ButtonProps>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The passages of the prompt that the converter could not turn into logic, quoted with the reason for each, under a
|
|
13
|
+
* disclosure that counts them. The list starts collapsed, because the prompt pane marks the same passages.
|
|
14
|
+
*/
|
|
15
|
+
export declare function UnconvertedPassages({ prompt, passages, onShow, Button, }: UnconvertedPassagesProps): ReactElement | undefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId, useState } from 'react';
|
|
3
|
+
import { abbreviate } from "./abbreviate.js";
|
|
4
|
+
import { ChevronIcon } from "./icons.js";
|
|
5
|
+
import { SectionMessage } from "./SectionMessage.js";
|
|
6
|
+
export function UnconvertedPassages({ prompt, passages, onShow, Button, }) {
|
|
7
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
8
|
+
const detailsId = useId();
|
|
9
|
+
if (passages.length === 0)
|
|
10
|
+
return undefined;
|
|
11
|
+
return (_jsx(SectionMessage, { appearance: "warning", title: _jsxs(Button, { "aria-expanded": isOpen, "aria-controls": detailsId, isHeading: true, onClick: () => setIsOpen((wasOpen) => !wasOpen), children: [_jsx(ChevronIcon, { isOpen: isOpen }), "Unconverted passages (", passages.length, ")"] }), children: _jsxs("div", { id: detailsId, hidden: !isOpen, className: "statechart-messages__details", children: [_jsx("p", { children: "Absent from the chart. To convert one, rewrite it as a step, a condition, or a rule that applies in every state, then convert the prompt again." }), _jsx("ul", { className: "statechart-messages__passages", children: passages.map((passage) => {
|
|
12
|
+
const text = prompt.slice(passage.start, passage.end);
|
|
13
|
+
return (_jsxs("li", { children: [_jsx(Button, { "aria-label": `Show in prompt: ${abbreviate(text)}`, onClick: () => onShow(passage), children: "Show in prompt" }), ' ', _jsx("q", { className: "statechart-messages__quote", children: text }), passage.reason === undefined ? undefined : ` ${passage.reason}`] }, `${passage.start}-${passage.end}`));
|
|
14
|
+
}) })] }) }));
|
|
15
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ReactElement } from 'react';
|
|
2
|
+
/** The glyph of a disclosure: a chevron that points right while closed and down while open. */
|
|
3
|
+
export declare function ChevronIcon({ isOpen }: {
|
|
4
|
+
isOpen: boolean;
|
|
5
|
+
}): ReactElement;
|
|
6
|
+
/** The glyph of information: a filled circle holding an `i`. */
|
|
7
|
+
export declare function InformationIcon(): ReactElement;
|
|
8
|
+
/** The glyph of a warning: a filled triangle holding an exclamation mark. */
|
|
9
|
+
export declare function WarningIcon(): ReactElement;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
export function ChevronIcon({ isOpen }) {
|
|
3
|
+
return (_jsx("svg", { viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", strokeWidth: "1.8", "aria-hidden": "true", className: `statechart-chevron${isOpen ? ' statechart-chevron--open' : ''}`, children: _jsx("path", { d: "M6 3.5L10.5 8 6 12.5" }) }));
|
|
4
|
+
}
|
|
5
|
+
export function InformationIcon() {
|
|
6
|
+
return (_jsxs("svg", { viewBox: "0 0 20 20", "aria-hidden": "true", className: "statechart-messages__icon", children: [_jsx("circle", { cx: "10", cy: "10", r: "8", fill: "currentColor" }), _jsx("path", { d: "M10 9v5", className: "statechart-messages__icon-stroke", strokeWidth: "1.8", strokeLinecap: "round" }), _jsx("circle", { cx: "10", cy: "6.25", r: "1.1", className: "statechart-messages__icon-fill" })] }));
|
|
7
|
+
}
|
|
8
|
+
export function WarningIcon() {
|
|
9
|
+
return (_jsxs("svg", { viewBox: "0 0 20 20", "aria-hidden": "true", className: "statechart-messages__icon", children: [_jsx("path", { d: "M8.27 3.2a2 2 0 0 1 3.46 0l6.1 10.55A2 2 0 0 1 16.1 16.75H3.9a2 2 0 0 1-1.73-3L8.27 3.2Z", fill: "currentColor" }), _jsx("path", { d: "M10 7.5v3.75", className: "statechart-messages__icon-stroke", strokeWidth: "1.8", strokeLinecap: "round" }), _jsx("circle", { cx: "10", cy: "13.75", r: "1.1", className: "statechart-messages__icon-fill" })] }));
|
|
10
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* The props of the app's button. The component renders a `<button type="button">` that looks like a button at rest
|
|
4
|
+
* and shows the pointer, hover, and focus states; it forwards each ARIA attribute that it receives to the button.
|
|
5
|
+
*/
|
|
6
|
+
export interface ButtonProps {
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
onClick: () => void;
|
|
9
|
+
'aria-controls'?: string | undefined;
|
|
10
|
+
'aria-describedby'?: string | undefined;
|
|
11
|
+
'aria-expanded'?: boolean | undefined;
|
|
12
|
+
'aria-label'?: string | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Whether the button is the title of a section, as a disclosure that heads its list is, for which the button takes
|
|
15
|
+
* the heading's text size; otherwise it sits in a line of text or a toolbar, at the app's compact size.
|
|
16
|
+
*/
|
|
17
|
+
isHeading?: boolean | undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The props of the app's in-page link. The component renders a `<button type="button">` that looks like a link and
|
|
21
|
+
* shows the pointer, hover, and focus states; `children` is the name of the element that it selects.
|
|
22
|
+
*/
|
|
23
|
+
export interface LinkButtonProps {
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
onClick: () => void;
|
|
26
|
+
/** Whether the link stands inside a sentence, for which the link keeps its underline at rest. */
|
|
27
|
+
isInline?: boolean | undefined;
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ReactElement, type RefObject } from 'react';
|
|
2
|
+
export interface SourceLineProps {
|
|
3
|
+
/** The workspace that contains the canvas and the prompt pane, over which the line is drawn. */
|
|
4
|
+
containerRef: RefObject<HTMLElement | null>;
|
|
5
|
+
selectedPath: string | undefined;
|
|
6
|
+
/** Receives the function that redraws the line, for the canvas to call whenever it pans or zooms. */
|
|
7
|
+
redrawRef: RefObject<(() => void) | undefined>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The line that joins the selected element on the canvas to its passage in the prompt, while both are in view. Its
|
|
11
|
+
* container is positioned, and it holds a `StatechartCanvas` and a `PromptPane`, whose data attributes locate the ends.
|
|
12
|
+
*/
|
|
13
|
+
export declare function SourceLine({ containerRef, selectedPath, redrawRef }: SourceLineProps): ReactElement | undefined;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { buildConnector } from "./build-connector.js";
|
|
4
|
+
export function SourceLine({ containerRef, selectedPath, redrawRef }) {
|
|
5
|
+
const [line, setLine] = useState();
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const container = containerRef.current ?? undefined;
|
|
8
|
+
if (container === undefined)
|
|
9
|
+
return;
|
|
10
|
+
const workspace = container;
|
|
11
|
+
let frame = 0;
|
|
12
|
+
function scheduleDraw() {
|
|
13
|
+
cancelAnimationFrame(frame);
|
|
14
|
+
frame = requestAnimationFrame(() => {
|
|
15
|
+
setLine(selectedPath === undefined ? undefined : measureLine(workspace, selectedPath));
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
scheduleDraw();
|
|
19
|
+
redrawRef.current = scheduleDraw;
|
|
20
|
+
container.addEventListener('scroll', scheduleDraw, { capture: true });
|
|
21
|
+
const resizeObserver = new ResizeObserver(scheduleDraw);
|
|
22
|
+
resizeObserver.observe(container);
|
|
23
|
+
return () => {
|
|
24
|
+
redrawRef.current = undefined;
|
|
25
|
+
cancelAnimationFrame(frame);
|
|
26
|
+
container.removeEventListener('scroll', scheduleDraw, { capture: true });
|
|
27
|
+
resizeObserver.disconnect();
|
|
28
|
+
};
|
|
29
|
+
}, [containerRef, redrawRef, selectedPath]);
|
|
30
|
+
if (line === undefined)
|
|
31
|
+
return undefined;
|
|
32
|
+
return (_jsxs("svg", { "aria-hidden": "true", className: "statechart-source-line", children: [_jsx("path", { d: line.path, fill: "none", strokeWidth: 2.5 }), _jsx("circle", { cx: line.start.x, cy: line.start.y, r: 4, stroke: "none" }), _jsx("circle", { cx: line.end.x, cy: line.end.y, r: 4, stroke: "none" })] }));
|
|
33
|
+
}
|
|
34
|
+
function measureLine(container, path) {
|
|
35
|
+
const canvas = container.querySelector('.statechart-canvas') ?? undefined;
|
|
36
|
+
const element = canvas?.querySelector(`[data-path="${CSS.escape(path)}"]`) ?? undefined;
|
|
37
|
+
const prompt = container.querySelector('[data-prompt-text]') ?? undefined;
|
|
38
|
+
const passage = prompt?.querySelector(`[data-passage-for="${CSS.escape(path)}"]`)?.getClientRects()[0];
|
|
39
|
+
if (canvas === undefined || element === undefined || prompt === undefined || passage === undefined)
|
|
40
|
+
return undefined;
|
|
41
|
+
const origin = container.getBoundingClientRect();
|
|
42
|
+
return buildConnector({
|
|
43
|
+
start: element.getBoundingClientRect(),
|
|
44
|
+
startBounds: canvas.getBoundingClientRect(),
|
|
45
|
+
end: passage,
|
|
46
|
+
endBounds: prompt.getBoundingClientRect(),
|
|
47
|
+
origin: { x: origin.left, y: origin.top },
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type ComponentType, type ReactElement, type ReactNode } from 'react';
|
|
2
|
+
import type { ConfigDescription } from '../../layout/describe-config.js';
|
|
3
|
+
import type { AgentLogicConfig, Span } from '../../schema/agent-logic-schema.js';
|
|
4
|
+
import type { ChartMark } from '../canvas/list-chart-marks.js';
|
|
5
|
+
import { type StatechartCanvasProps } from '../canvas/StatechartCanvas.js';
|
|
6
|
+
import type { ChartLayout } from '../hooks/useChartLayout.js';
|
|
7
|
+
import type { LinkButtonProps } from '../slots.js';
|
|
8
|
+
export interface WorkspaceProps {
|
|
9
|
+
config: AgentLogicConfig;
|
|
10
|
+
description: ConfigDescription;
|
|
11
|
+
/** The chart's layout: its elements, why it failed, or `undefined` while it runs. */
|
|
12
|
+
layout: ChartLayout | undefined;
|
|
13
|
+
selectedPath: string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* The passages that the prompt marks apart from the selection, such as an unconverted passage or a finding's
|
|
16
|
+
* quotes.
|
|
17
|
+
*/
|
|
18
|
+
shownPassages: Span[];
|
|
19
|
+
onSelect: (path: string | undefined) => void;
|
|
20
|
+
renderKey: (marks: ReadonlySet<ChartMark>) => ReactNode;
|
|
21
|
+
/** The messages panel, docked at the canvas's bottom edge. */
|
|
22
|
+
messages?: ReactNode;
|
|
23
|
+
/** The app's in-page link, which the inspector renders for each state and transition that it names. */
|
|
24
|
+
LinkButton: ComponentType<LinkButtonProps>;
|
|
25
|
+
/** The canvas to render, `StatechartCanvas` unless a test replaces it. */
|
|
26
|
+
Canvas?: ComponentType<StatechartCanvasProps> | undefined;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The canvas above the messages panel, beside the source pane, which holds the inspector above the prompt, joined by
|
|
30
|
+
* the source line. It fills the height that its flex parent leaves, and the app sets its outer spacing.
|
|
31
|
+
*/
|
|
32
|
+
export declare function Workspace({ config, description, layout, selectedPath, shownPassages, onSelect, renderKey, messages, LinkButton, Canvas, }: WorkspaceProps): ReactElement;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useMemo, useRef } from 'react';
|
|
3
|
+
import { buildPromptSegments } from "../../source/prompt-segments.js";
|
|
4
|
+
import { StatechartCanvas } from "../canvas/StatechartCanvas.js";
|
|
5
|
+
import { Inspector } from "../inspector/Inspector.js";
|
|
6
|
+
import { PromptPane } from "./PromptPane.js";
|
|
7
|
+
import { SourceLine } from "./SourceLine.js";
|
|
8
|
+
export function Workspace({ config, description, layout, selectedPath, shownPassages, onSelect, renderKey, messages, LinkButton, Canvas = StatechartCanvas, }) {
|
|
9
|
+
const workspaceRef = useRef(null);
|
|
10
|
+
const redrawLineRef = useRef(undefined);
|
|
11
|
+
const segments = useMemo(() => buildPromptSegments(config.meta.prompt, description, config.meta.unconverted, shownPassages), [config, description, shownPassages]);
|
|
12
|
+
const selectedElement = findElement(description, selectedPath);
|
|
13
|
+
const handleViewportChange = useCallback(() => {
|
|
14
|
+
redrawLineRef.current?.();
|
|
15
|
+
}, []);
|
|
16
|
+
return (_jsxs("div", { ref: workspaceRef, className: "statechart-workspace", children: [_jsxs("div", { className: "statechart-workspace__chart", children: [_jsxs("div", { className: "statechart-workspace__canvas", children: [layout !== undefined && 'error' in layout ? (_jsxs("p", { className: "statechart-workspace__error", children: ["The layout failed (", layout.error, ")."] })) : undefined, layout !== undefined && 'elements' in layout ? (_jsx(Canvas, { elements: layout.elements, selectedPath: selectedPath, onSelect: onSelect, onViewportChange: handleViewportChange, renderKey: renderKey }, layout.key)) : undefined] }), messages] }), _jsxs("div", { className: "statechart-workspace__source", children: [_jsx(Inspector, { element: selectedElement, description: description, onSelect: onSelect, LinkButton: LinkButton }), _jsx("div", { className: "statechart-workspace__prompt", children: _jsx(PromptPane, { segments: segments, selected: selectedElement, shownPassages: shownPassages, onSelect: onSelect }) })] }), _jsx(SourceLine, { containerRef: workspaceRef, selectedPath: selectedPath, redrawRef: redrawLineRef })] }));
|
|
17
|
+
}
|
|
18
|
+
function findElement(description, path) {
|
|
19
|
+
if (path === undefined)
|
|
20
|
+
return undefined;
|
|
21
|
+
return [...description.states, ...description.transitions, ...description.ambient].find((element) => element.path === path);
|
|
22
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** The edges of a box on screen, as `getBoundingClientRect` reports them. */
|
|
2
|
+
export interface ScreenRect {
|
|
3
|
+
left: number;
|
|
4
|
+
top: number;
|
|
5
|
+
right: number;
|
|
6
|
+
bottom: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ConnectorInput {
|
|
9
|
+
/** The box at the line's start, in a region at the left. */
|
|
10
|
+
start: ScreenRect;
|
|
11
|
+
/** The visible part of the region that holds `start`. */
|
|
12
|
+
startBounds: ScreenRect;
|
|
13
|
+
/** The first line box at the line's end; the line ends in the gutter of `endBounds` level with it. */
|
|
14
|
+
end: ScreenRect;
|
|
15
|
+
/** The visible part of the scrolling region that holds `end`, at the right of the start's region. */
|
|
16
|
+
endBounds: ScreenRect;
|
|
17
|
+
/** The top-left corner of the overlay in which the line is drawn. */
|
|
18
|
+
origin: {
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface Connector {
|
|
24
|
+
/** An SVG path in the overlay's coordinates. */
|
|
25
|
+
path: string;
|
|
26
|
+
start: {
|
|
27
|
+
x: number;
|
|
28
|
+
y: number;
|
|
29
|
+
};
|
|
30
|
+
end: {
|
|
31
|
+
x: number;
|
|
32
|
+
y: number;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns the curve that joins a box to a line of text in a region at its right, or `undefined` when either is out of
|
|
37
|
+
* view.
|
|
38
|
+
*/
|
|
39
|
+
export declare function buildConnector({ start, startBounds, end, endBounds, origin }: ConnectorInput): Connector | undefined;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function buildConnector({ start, startBounds, end, endBounds, origin }) {
|
|
2
|
+
const startX = Math.min(start.right, startBounds.right);
|
|
3
|
+
const startY = start.top + Math.min((start.bottom - start.top) / 2, MAX_ANCHOR_DROP);
|
|
4
|
+
const endX = endBounds.left + GUTTER_INSET;
|
|
5
|
+
const endY = (end.top + end.bottom) / 2;
|
|
6
|
+
const isStartInView = startX >= startBounds.left && start.left <= startBounds.right && isWithin(startY, startBounds);
|
|
7
|
+
const isEndInView = isWithin(endY, endBounds);
|
|
8
|
+
if (!isStartInView || !isEndInView)
|
|
9
|
+
return undefined;
|
|
10
|
+
const from = { x: startX - origin.x, y: startY - origin.y };
|
|
11
|
+
const to = { x: endX - origin.x, y: endY - origin.y };
|
|
12
|
+
const middleX = (from.x + to.x) / 2;
|
|
13
|
+
return {
|
|
14
|
+
path: `M${from.x},${from.y} C${middleX},${from.y} ${middleX},${to.y} ${to.x},${to.y}`,
|
|
15
|
+
start: from,
|
|
16
|
+
end: to,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const MAX_ANCHOR_DROP = 18;
|
|
20
|
+
const GUTTER_INSET = 8;
|
|
21
|
+
function isWithin(y, rect) {
|
|
22
|
+
return y >= rect.top && y <= rect.bottom;
|
|
23
|
+
}
|