feat(diagram-engine): flowcharts, swimlanes, sequence diagrams and mind maps

Extends the declarative engine past cloud architecture. The tool routing was
divided by icon library — AWS through the engine, everything else hand-written
XML — which is the wrong axis. What matters is the LAYOUT SHAPE.

Measured first: a six-step approval flow declared in its natural order comes out
as one column, because the layout only arranges what nesting tells it to and
never looked at the arrows. That forces the arrow from the decision to its second
branch to jump over the first branch.

graph.ts computes what the layout should have looked at: layer assignment by
longest path, cycle breaking so a loop is drawn without setting the order, and
barycentre sweeping to cut edge crossings. It emits ordinary container
operations, so layout, routing and round-tripping are unchanged — reaching zero
arrows-through-boxes on a 14-node pipeline and zero crossings on a bipartite
graph whose declared order forces three.

Three new container kinds, each because one layout rule cannot serve them all:

  pool     — swimlanes. Lanes are real cells and each step is parented to its
             band, so dragging a step to another role records the change.
  sequence — participants across the top, one lifeline cell per participant so
             head and line stay together on a drag. Messages bypass the router:
             a message's height IS its order.
  radial   — mind maps and org charts. Children are a flat list and the
             hierarchy comes from the links, because a branch is a box and a box
             cannot hold children.

Flowchart box shapes (diamond, stadium, parallelogram, document) so a reader can
tell a branch from a step.

Two bugs the new tests caught: the duplicate-link guard blocked a sequence
diagram from having two messages between the same pair, and the fallback message
numbering was shared across containers, pushing a second diagram's messages off
its own lifelines.

523 unit tests and 17 diagram e2e tests pass. Every kind verified round-trip
stable to a fixed point, and rendered in a real browser — draw.io keeps the
lifeline shape and the lane markers.
This commit is contained in:
dayuan.jiang
2026-08-09 13:47:23 +09:00
parent 1e4c74d464
commit a3814f702d
27 changed files with 4497 additions and 88 deletions

View File

@@ -41,9 +41,43 @@ export const MARKER = {
* base64 image with no name anywhere in it, so the style alone cannot identify it.
*/
name: "dai_name",
/**
* Which (lane, column) cell of a swimlane pool a node occupies, as "lane,col".
*
* Position alone cannot recover this once the user drags a node: the cell it lands in
* is a guess, whereas the marker records which lane the model assigned it to. It is
* also the only way an empty cell stays empty — geometry can only tell us where things
* ARE, never that a role deliberately does nothing at a given step.
*/
cell: "dai_cell",
/** A pool's lane names, tab-separated (a tab cannot appear in a draw.io style value). */
lanes: "dai_lanes",
/** A pool's milestone labels, tab-separated. */
phases: "dai_phases",
/** A pool's orientation: "h" or "v". */
orient: "dai_orient",
/** Vertical distance between consecutive messages in a sequence diagram. */
step: "dai_step",
/** How a radial container fans its branches out: "radial" or "down". */
spread: "dai_spread",
/**
* Marks a cell as chrome the engine draws and owns: a pool's lane bands, its label
* columns, its milestone strip. The parser must not read these back as nodes — they are
* re-derived from the pool's own parameters on every layout — and the edge router must
* not treat them as obstacles, since a sequence flow crossing lanes is the norm.
*/
lane: "dai_lane",
} as const
export type NodeKind = "group" | "grid" | "icon" | "box" | "title"
export type NodeKind =
| "group"
| "grid"
| "pool"
| "sequence"
| "radial"
| "icon"
| "box"
| "title"
export type Direction = "row" | "col" | "grid"
/**
@@ -88,17 +122,59 @@ export function readMarker(style: string, key: string): string | null {
return last
}
const KINDS: readonly NodeKind[] = [
"group",
"grid",
"pool",
"sequence",
"radial",
"icon",
"box",
"title",
]
export function readKind(style: string): NodeKind | null {
const v = readMarker(style, MARKER.kind)
if (
v === "group" ||
v === "grid" ||
v === "icon" ||
v === "box" ||
v === "title"
)
return v
return null
return KINDS.includes(v as NodeKind) ? (v as NodeKind) : null
}
/**
* The (lane, column) cell a node occupies in a swimlane pool, or null.
*
* Both must be non-negative integers: a malformed value is safer read as "no cell
* declared" (which puts the node in lane 0 column 0) than as a negative index, which would
* place it outside the pool's frame.
*/
export function readCell(style: string): { lane: number; col: number } | null {
const v = readMarker(style, MARKER.cell)
if (!v) return null
const m = v.match(/^(\d+),(\d+)$/)
return m ? { lane: Number(m[1]), col: Number(m[2]) } : null
}
/**
* A tab-separated marker list, as written by `joinList`.
*
* A tab cannot appear in a draw.io style value — the editor writes styles as a single
* semicolon-separated line — so it is safe as a separator inside one value, where a comma
* would collide with the label text it has to carry.
*/
export function readList(style: string, key: string): string[] | null {
const v = readMarker(style, key)
if (v === null) return null
if (v === "") return []
return v.split("\t").map(decodeURIComponent)
}
/** Encode a list of labels into one marker value. */
export function joinList(items: string[]): string {
// Percent-encoding keeps a label containing ";" or "=" from breaking the style string.
return items.map((s) => encodeURIComponent(s)).join("\t")
}
/** Is this cell pool chrome the engine draws and owns, rather than a node? */
export function isLaneChrome(style: string): boolean {
return readMarker(style, MARKER.lane) !== null
}
export function readDir(style: string): Direction | null {
@@ -163,6 +239,93 @@ export function stampContainer(
return s
}
/**
* Stamp a swimlane pool: its lane names, milestone labels and orientation.
*
* Unlike a group, a pool is NOT stamped as a draw.io container. Its lane bands are separate
* cells sitting inside it, and they are what a shape should reparent into when the user
* drags it — that is how "the user moved this step to a different role" gets recorded. If
* the pool itself claimed the drop, every node would come back in lane 0.
*/
export function stampPool(
style: string,
opts: {
lanes: string[]
phases: string[]
orientation: "horizontal" | "vertical"
gap: number
},
): string {
let s = append(style, MARKER.kind, "pool")
s = append(s, MARKER.lanes, joinList(opts.lanes))
s = append(s, MARKER.phases, joinList(opts.phases))
s = append(s, MARKER.orient, opts.orientation === "vertical" ? "v" : "h")
return append(s, MARKER.gap, Math.round(opts.gap))
}
/** Stamp a sequence container: participant spacing and message spacing. */
export function stampSequence(
style: string,
opts: { gap: number; step: number },
): string {
const s = append(style, MARKER.kind, "sequence")
return append(
append(s, MARKER.gap, Math.round(opts.gap)),
MARKER.step,
Math.round(opts.step),
)
}
/** Stamp a radial container: how it fans branches out, and the ring spacing. */
export function stampRadial(
style: string,
opts: { spread: "radial" | "down"; gap: number },
): string {
const s = append(style, MARKER.kind, "radial")
return append(
append(s, MARKER.spread, opts.spread),
MARKER.gap,
Math.round(opts.gap),
)
}
/**
* Stamp one of a pool's lane bands.
*
* A band IS a draw.io container, so dragging a step onto another role's band reparents it
* there and the marker on the band tells the parser which lane that is. The lane index is
* the band's identity, not its position, so the assignment survives the pool being
* re-measured to a different size.
*/
export function stampLane(style: string, lane: number): string {
let s = style.endsWith(";") || style === "" ? style : `${style};`
s += CONTAINER_TOKENS
return append(s, MARKER.lane, Math.max(0, Math.round(lane)))
}
/**
* Stamp a pool's own decoration — a lane-name column or a milestone strip.
*
* `dai_lane=-1` marks it as chrome without claiming a lane: it is a label, and a shape
* dropped on it belongs to no role. It stays a draw.io container only so clicks fall
* through to whatever is behind it.
*/
export function stampPoolDecoration(style: string): string {
return append(style, MARKER.lane, -1)
}
/** Record which pool cell a node occupies. */
export function stampCell(
style: string,
cell: { lane: number; col: number },
): string {
return append(
style,
MARKER.cell,
`${Math.max(0, Math.round(cell.lane))},${Math.max(0, Math.round(cell.col))}`,
)
}
/**
* Is this an invisible layout wrapper? Both colours set to `none` and no group
* stencil — a visible frame always has a stroke or a stencil.