Diagram
@adecore/diagram draws directed graphs that are written rather than drawn. A diagram file lists nodes, groups and edges. The package places the nodes, routes the edges and writes SVG or lines of text. The same file always gives the same picture, so an agent can write a diagram without placing a box.
import { layoutOf, readingOrder, toSvg } from '@adecore/diagram';
import type { DiagramContent, DiagramDirection } from '@adecore/diagram/protocol';
import { Segmented } from '@adecore/ui';
import { ArrowDown, ArrowRight } from 'lucide-react';
import { useState } from 'react';
import { THEME_PALETTE, THEME_PAPER } from '../shared/canvas-theme.ts';
const RELEASE: DiagramContent = {
meta: { title: 'Release', direction: 'right' },
nodes: [
{ id: 'commit', label: 'Commit', shape: 'pill', tone: 'muted' },
{ id: 'build', label: 'Build', sub: 'bun run build' },
{ id: 'test', label: 'Test', sub: 'bun test' },
{ id: 'approved', label: 'Approved?', shape: 'diamond', tone: 'orange' },
{ id: 'publish', label: 'Publish', shape: 'round', tone: 'green' },
{ id: 'registry', label: 'Registry', shape: 'cylinder', tone: 'blue' },
{ id: 'docs', label: 'Docs site', shape: 'round', tone: 'purple' }
],
groups: [{ id: 'ci', label: 'CI', wraps: ['build', 'test'], tone: 'accent' }],
edges: [
{ from: 'commit', to: 'build' },
{ from: 'build', to: 'test' },
{ from: 'test', to: 'approved', label: 'green' },
{ from: 'approved', to: 'publish', label: 'yes' },
{ from: 'approved', to: 'commit', label: 'changes', style: 'dashed', tone: 'red' },
{ from: 'publish', to: 'registry' },
{ from: 'publish', to: 'docs', style: 'dotted' }
]
};
export default function DiagramLayoutDemo() {
const [direction, setDirection] = useState<DiagramDirection>('right');
const content = { ...RELEASE, meta: { ...RELEASE.meta, direction } };
const layout = layoutOf(content);
const svg = toSvg(content, { layout, palette: THEME_PALETTE, paper: THEME_PAPER });
return (
<div className="flex w-full flex-col gap-4">
<Segmented<DiagramDirection>
label="Direction"
value={direction}
onValueChange={setDirection}
options={[
{ id: 'right', label: 'Right', icon: ArrowRight },
{ id: 'down', label: 'Down', icon: ArrowDown }
]}
className="self-start"
/>
<div
className="flex justify-center [&_svg]:h-auto [&_svg]:max-h-[480px] [&_svg]:w-auto [&_svg]:max-w-full"
dangerouslySetInnerHTML={{ __html: svg }}
/>
<ol className="flex flex-col gap-1 font-mono text-code text-text-muted">
{readingOrder(content).map((line) => (
<li key={line}>{line}</li>
))}
</ol>
</div>
);
}The dashed edge from Approved? back to Commit closes a cycle, which the layout ranks without but still draws. Under the diagram are the lines of readingOrder.
What it does
- Validates a diagram file with Zod schemas, and names the first broken reference in a sentence an agent can act on.
- Places nodes in columns along a direction, wraps groups around their members and routes edges in right angles.
- Keeps a node a person dragged where it was put.
- Writes a whole diagram as SVG, or hands out the shapes, text lines and edge paths for a painter of your own.
- Reads the graph back as lines of text, exactly as stored.
What it leaves to the app
The package keeps no state. The app owns storage, the rev check on save, dragging (which writes a node's pos), selection, and the colors and font.
The package takes its palette names, Point, Rect and default colors from @adecore/drawing, so a diagram follows a theme like a drawing does.
Where to next
- Getting started writes a diagram, changes it and exports it.
- Documents describes the file and the rules a schema cannot check.
- Layout explains where nodes and groups go and how edges route.
- Export to SVG covers
toSvgand its options. - A painter of your own covers drawing the layout as your own elements.
- Reading order turns a diagram into lines of text.
- Reference lists the limits and every export.
The package is licensed under FSL-1.1-MIT.