198 lines
9.2 KiB
TypeScript
198 lines
9.2 KiB
TypeScript
import { useRef } from "react";
|
|
import { Panel, useReactFlow, useStore } from "@xyflow/react";
|
|
|
|
/**
|
|
* Flow overview for the bottom-left corner of the editor canvas.
|
|
*
|
|
* Deliberately not the ReactFlow `<MiniMap>`: that one can only draw a grey
|
|
* rectangle per node. This renders the real canvas content - the same node
|
|
* cards and the same connectors - inside a scaled-down box, so the overview
|
|
* reads as a zoomed-out copy of what the user sees. The frame marks the visible
|
|
* area and is dragged like a scrollbar thumb to pan the canvas.
|
|
*/
|
|
|
|
const OVERVIEW_WIDTH = 196;
|
|
const OVERVIEW_HEIGHT = 128;
|
|
/** Flow-coordinate breathing room around the outermost nodes. */
|
|
const OVERVIEW_PADDING = 80;
|
|
/** Never blow the miniature up beyond this - a single node would fill the box. */
|
|
const MAX_SCALE = 0.32;
|
|
/** Span multiplier that keeps the frame plus its border inside the box. */
|
|
const FRAME_HEADROOM = 1.08;
|
|
/** Gap between the frame border and the edge of the box, in box pixels. */
|
|
const FRAME_INSET = 2;
|
|
|
|
export type OverviewNode = {
|
|
readonly id: string;
|
|
readonly position: { readonly x: number; readonly y: number };
|
|
readonly width: number;
|
|
readonly height: number;
|
|
readonly parentIds: readonly string[];
|
|
};
|
|
|
|
export type AutomationCanvasOverviewProps = {
|
|
nodes: readonly OverviewNode[];
|
|
/** Node card rendered inside the miniature - the very same one as on the canvas. */
|
|
renderNode: (nodeId: string) => React.ReactNode;
|
|
label: string;
|
|
};
|
|
|
|
export function AutomationCanvasOverview({ nodes, renderNode, label }: AutomationCanvasOverviewProps): React.JSX.Element | null {
|
|
const { setCenter, getZoom } = useReactFlow();
|
|
const boxRef = useRef<HTMLDivElement>(null);
|
|
/** Pointer offset inside the frame while dragging it; null when not dragging. */
|
|
const grab = useRef<{ x: number; y: number } | null>(null);
|
|
const viewport = useStore((state) => ({
|
|
x: -state.transform[0] / state.transform[2],
|
|
y: -state.transform[1] / state.transform[2],
|
|
width: state.width / state.transform[2],
|
|
height: state.height / state.transform[2],
|
|
}));
|
|
|
|
if (nodes.length === 0) return null;
|
|
|
|
// Bounds are anchored on the nodes, never on the viewport position: a frame
|
|
// of reference that moved while panning would make every pointer move land
|
|
// somewhere else than where the pointer actually is. The viewport *size* does
|
|
// widen the span though - a canvas showing more than the flow bounds would
|
|
// otherwise draw a frame wider than the box, with its side borders cut off.
|
|
const nodeMinX = Math.min(...nodes.map((node) => node.position.x)) - OVERVIEW_PADDING;
|
|
const nodeMinY = Math.min(...nodes.map((node) => node.position.y)) - OVERVIEW_PADDING;
|
|
const nodeMaxX = Math.max(...nodes.map((node) => node.position.x + node.width)) + OVERVIEW_PADDING;
|
|
const nodeMaxY = Math.max(...nodes.map((node) => node.position.y + node.height)) + OVERVIEW_PADDING;
|
|
const spanX = Math.max(nodeMaxX - nodeMinX, viewport.width * FRAME_HEADROOM);
|
|
const spanY = Math.max(nodeMaxY - nodeMinY, viewport.height * FRAME_HEADROOM);
|
|
const minX = (nodeMinX + nodeMaxX) / 2 - spanX / 2;
|
|
const minY = (nodeMinY + nodeMaxY) / 2 - spanY / 2;
|
|
const maxX = minX + spanX;
|
|
const maxY = minY + spanY;
|
|
const scale = Math.min(MAX_SCALE, OVERVIEW_WIDTH / spanX, OVERVIEW_HEIGHT / spanY);
|
|
// Centre the miniature in the box - a wide flow leaves room above and below.
|
|
const offsetX = (OVERVIEW_WIDTH - spanX * scale) / 2;
|
|
const offsetY = (OVERVIEW_HEIGHT - spanY * scale) / 2;
|
|
|
|
// The canvas can also be panned directly, past the flow bounds. Keep the
|
|
// frame inside the box in that case: a frame half outside would lose the
|
|
// border on that side and read as a resized frame.
|
|
const frameWidth = Math.min(viewport.width * scale, OVERVIEW_WIDTH - 2 * FRAME_INSET);
|
|
const frameHeight = Math.min(viewport.height * scale, OVERVIEW_HEIGHT - 2 * FRAME_INSET);
|
|
const frame = {
|
|
width: frameWidth,
|
|
height: frameHeight,
|
|
left: clamp(offsetX + (viewport.x - minX) * scale, FRAME_INSET, OVERVIEW_WIDTH - frameWidth - FRAME_INSET),
|
|
top: clamp(offsetY + (viewport.y - minY) * scale, FRAME_INSET, OVERVIEW_HEIGHT - frameHeight - FRAME_INSET),
|
|
};
|
|
|
|
/**
|
|
* Move the frame so its top-left lands under the pointer, minus wherever
|
|
* inside the frame the drag started. Relative like a scrollbar thumb: the
|
|
* canvas never jumps when the drag begins, it only follows the frame.
|
|
*/
|
|
function moveFrame(event: React.PointerEvent<HTMLDivElement>, grabOffset: { x: number; y: number }) {
|
|
const box = boxRef.current?.getBoundingClientRect();
|
|
if (!box) return;
|
|
const left = event.clientX - box.left - grabOffset.x;
|
|
const top = event.clientY - box.top - grabOffset.y;
|
|
setCenter(
|
|
clamp(minX + (left - offsetX) / scale + viewport.width / 2, minX + viewport.width / 2, maxX - viewport.width / 2),
|
|
clamp(minY + (top - offsetY) / scale + viewport.height / 2, minY + viewport.height / 2, maxY - viewport.height / 2),
|
|
{ zoom: getZoom() }
|
|
);
|
|
}
|
|
|
|
function startDrag(event: React.PointerEvent<HTMLDivElement>) {
|
|
const box = boxRef.current?.getBoundingClientRect();
|
|
if (!box) return;
|
|
const pointerX = event.clientX - box.left;
|
|
const pointerY = event.clientY - box.top;
|
|
const insideFrame =
|
|
pointerX >= frame.left && pointerX <= frame.left + frame.width && pointerY >= frame.top && pointerY <= frame.top + frame.height;
|
|
// Grabbing the frame keeps the canvas where it is; clicking next to it
|
|
// centres the view on that spot and drags on from there.
|
|
grab.current = insideFrame
|
|
? { x: pointerX - frame.left, y: pointerY - frame.top }
|
|
: { x: frame.width / 2, y: frame.height / 2 };
|
|
event.currentTarget.setPointerCapture(event.pointerId);
|
|
if (!insideFrame) moveFrame(event, grab.current);
|
|
}
|
|
|
|
return (
|
|
<Panel position="bottom-left" className="!bottom-10 !left-6 !m-0">
|
|
{/* The overview only pans the canvas; every navigation is also reachable
|
|
through the toolbar and the canvas itself, so it stays a plain region. */}
|
|
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/no-static-element-interactions */}
|
|
<div
|
|
ref={boxRef}
|
|
role="img"
|
|
aria-label={label}
|
|
className="relative cursor-grab overflow-hidden rounded-lg border border-solid border-border-default bg-surface-neutral-primary shadow-lg active:cursor-grabbing"
|
|
style={{ width: OVERVIEW_WIDTH, height: OVERVIEW_HEIGHT }}
|
|
onPointerDown={startDrag}
|
|
onPointerMove={(event) => {
|
|
if (grab.current) moveFrame(event, grab.current);
|
|
}}
|
|
onPointerUp={() => {
|
|
grab.current = null;
|
|
}}
|
|
onPointerCancel={() => {
|
|
grab.current = null;
|
|
}}
|
|
data-ui-id="ui.automation.editor.overview"
|
|
>
|
|
<div
|
|
className="pointer-events-none absolute left-0 top-0 origin-top-left"
|
|
style={{
|
|
width: maxX - minX,
|
|
height: maxY - minY,
|
|
transform: `translate(${offsetX}px, ${offsetY}px) scale(${scale}) translate(${-minX}px, ${-minY}px)`,
|
|
}}
|
|
>
|
|
{/* Same coordinate space as the cards below: the wrapper transform
|
|
already maps flow coordinates into the box, and negative
|
|
coordinates stay visible through overflow-visible. */}
|
|
<svg className="absolute left-0 top-0 overflow-visible" width={Math.max(maxX, 1)} height={Math.max(maxY, 1)}>
|
|
{nodes.flatMap((node) =>
|
|
node.parentIds.map((parentId) => {
|
|
const parent = nodes.find((candidate) => candidate.id === parentId);
|
|
if (!parent) return null;
|
|
const fromX = parent.position.x + parent.width / 2;
|
|
const fromY = parent.position.y + parent.height;
|
|
const toX = node.position.x + node.width / 2;
|
|
const toY = node.position.y;
|
|
return (
|
|
<path
|
|
key={`${parentId}-${node.id}`}
|
|
// Right-angled like the smoothstep edges on the canvas, so
|
|
// the miniature keeps the shape of the real connector.
|
|
d={`M ${fromX} ${fromY} V ${(fromY + toY) / 2} H ${toX} V ${toY}`}
|
|
fill="none"
|
|
stroke="var(--color-border-strong)"
|
|
strokeWidth={1.5 / scale}
|
|
/>
|
|
);
|
|
})
|
|
)}
|
|
</svg>
|
|
{nodes.map((node) => (
|
|
<div key={node.id} className="absolute" style={{ left: node.position.x, top: node.position.y, width: node.width }}>
|
|
{renderNode(node.id)}
|
|
</div>
|
|
))}
|
|
</div>
|
|
<div
|
|
className="pointer-events-none absolute rounded-sm border-2 border-solid border-brand-brand-secondary-border-solid-default"
|
|
style={{ backgroundColor: "rgb(0 0 0 / 4%)", left: frame.left, top: frame.top, width: frame.width, height: frame.height }}
|
|
/>
|
|
</div>
|
|
</Panel>
|
|
);
|
|
}
|
|
|
|
function clamp(value: number, lower: number, upper: number): number {
|
|
// A flow smaller than the viewport inverts the range - then the only sane
|
|
// position is the middle of the bounds.
|
|
return lower > upper ? (lower + upper) / 2 : Math.min(Math.max(value, lower), upper);
|
|
}
|
|
|
|
export default AutomationCanvasOverview;
|