feat(diagram-engine): layout + XML renderer, verified end to end in draw.io

Completes the tree → coordinates → XML direction, so the model can declare nesting
and never write a coordinate or an mxCell again.

layout.ts — measure bottom-up, place top-down, the same shape as flexbox. A
container sums its children along the flow axis and adds padding, so "child spills
out of its frame" and "siblings overlap" cannot happen by construction rather than
being caught afterwards. Slack from sibling equalisation is shared between children
instead of left as dead margin, capped at one gap so a stretched frame reads as
spaced rather than sparse.

render.ts — writes the mxCells, stamping container=1 and the dai_* markers so
parse.ts can read the structure back. Edges carry no waypoints: draw.io's own router
recomputes the route on every edit, so a user who moves a node never has to re-link
an arrow. Cells the parser could not interpret are re-emitted verbatim, so a
re-layout never deletes a user's annotations.

Phantoms are gone (task #5). The reference project's layout-only wrapper emits no
cell, which makes the round-trip lossy by construction — measured on its own
build_vpc.mjs, a phantom erased a container's "col" direction for good. An
unlabelled frame here emits a real cell with fillColor/strokeColor=none instead:
invisible, but present in the XML and therefore recoverable.

Two bugs the round-trip test caught, both real:

  - An icon's cell was being emitted at its measured slot size, which includes room
    for the label underneath. Parsing read that width back as the glyph size, so the
    icon grew on every round-trip. The cell is now the glyph square and the label
    renders outside it via verticalLabelPosition, as the reference does.
  - An Azure or GCP icon is an embedded base64 image whose style contains no name
    anywhere, so the catalog name was unrecoverable. Added a dai_name marker.

Verified in a real browser (3 Playwright tests, not mocks): engine output renders in
draw.io; dragging a shape into a frame makes draw.io rewrite its parent and the
engine reads the new structure back; re-laying out from that structure PRESERVES the
user's move instead of undoing it, and leaves untouched nodes alone; and the
re-laid-out XML still renders.

That last point is the whole design: there is no second copy of the state, so a
manual edit is an input to the next layout rather than a conflict to reconcile.

304 unit tests + 3 e2e.
This commit is contained in:
dayuan.jiang
2026-08-09 11:56:08 +09:00
parent 8765dfb96c
commit a2f892ca82
8 changed files with 2525 additions and 222 deletions

View File

@@ -36,6 +36,11 @@ export const MARKER = {
cols: "dai_cols",
/** Set by the user to freeze a node's position across re-layouts. */
pin: "dai_pin",
/**
* A catalog icon's name. Needed because an Azure or GCP icon's style is an embedded
* base64 image with no name anywhere in it, so the style alone cannot identify it.
*/
name: "dai_name",
} as const
export type NodeKind = "group" | "grid" | "icon" | "box" | "title"
@@ -54,6 +59,22 @@ export type Direction = "row" | "col" | "grid"
const CONTAINER_TOKENS =
"container=1;pointerEvents=0;collapsible=0;recursiveResize=0;"
/**
* A container that groups children for layout but should not be visible.
*
* The reference project solves this with a "phantom": a wrapper that participates in
* layout and then emits NO cell, reparenting its children onto the nearest visible
* ancestor. That makes the round-trip lossy by construction — the wrapper's direction
* and grouping are simply absent from the XML, so re-deriving the tree cannot recover
* them. Measured on the reference project's own build_vpc.mjs: a phantom erased a
* container's "col" direction, leaving children in a 2-D arrangement that can only be
* read back as a grid.
*
* So we emit a real cell and make it invisible instead. One extra cell per wrapper,
* in exchange for structure that survives being read back.
*/
const INVISIBLE_TOKENS = "fillColor=none;strokeColor=none;"
/** Read a marker's raw value out of a style string. Last occurrence wins, as draw.io does. */
export function readMarker(style: string, key: string): string | null {
// Scan all matches and keep the last, mirroring draw.io's duplicate-key resolution.
@@ -127,10 +148,13 @@ export function stampContainer(
dir: Direction
gap: number
cols?: number
/** Layout-only wrapper: emit a real cell, but draw nothing. */
invisible?: boolean
},
): string {
let s = style.endsWith(";") || style === "" ? style : `${style};`
s += CONTAINER_TOKENS
if (opts.invisible) s += INVISIBLE_TOKENS
s = append(s, MARKER.kind, opts.kind)
s = append(s, MARKER.dir, opts.dir)
s = append(s, MARKER.gap, Math.round(opts.gap))
@@ -139,12 +163,30 @@ export function stampContainer(
return s
}
/** Stamp a leaf (icon or box) with its kind, so the parser need not infer it. */
/**
* 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.
*/
export function isInvisible(style: string): boolean {
if (/grIcon=/.test(style)) return false
const fill = readMarker(style, "fillColor")
const stroke = readMarker(style, "strokeColor")
return fill === "none" && stroke === "none"
}
/**
* Stamp a leaf with its kind, so the parser need not infer it.
*
* For an icon, also record the catalog name: an Azure or GCP icon's style is an embedded
* base64 image with no name in it, so the style alone cannot identify which icon it is.
*/
export function stampLeaf(
style: string,
kind: "icon" | "box" | "title",
opts: { name?: string } = {},
): string {
return append(style, MARKER.kind, kind)
const s = append(style, MARKER.kind, kind)
return opts.name ? append(s, MARKER.name, opts.name) : s
}
/** Strip every `dai_*` marker — for exporting a clean file, or comparing styles. */