pub fn balance_node(node: &mut LayoutNode) -> Result<(), LayoutError>Expand description
Port of balanceNode in frontend/layout/lib/layoutNode.ts. Recursively
normalizes the tree: validates each node, alternates child flex direction,
hoists redundant single-child-branch chains, drops empty branches, and
collapses a branch with a single leaf child into that leaf.
Returns Err(InvalidNode) where the TS oracle throws “Invalid node”, so
the reducer can surface an Event::Error rather than panic.
Faithfulness notes (matched to the TS oracle, deliberately NOT “improved”):
- The direction flip mutates only the iterated child; hoisted
grandchildren are returned unflipped (a later pass alternates them) —
matches the TS
flatMap. _slipAnchor(read from the untypedextracatch-all, where the frontend stores it) suppresses the single-child-branch hoist.columnDissolve(sameextracatch-all) suppresses the direction-flip alternation, so a dissolved column’s own children stay stacked.- The single-leaf collapse copies the child’s
data+idonly; the node keeps its ownsize/flex_direction/extra— exactly as TS does.