Skip to main content

balance_node

Function balance_node 

Source
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 untyped extra catch-all, where the frontend stores it) suppresses the single-child-branch hoist.
  • columnDissolve (same extra catch-all) suppresses the direction-flip alternation, so a dissolved column’s own children stay stacked.
  • The single-leaf collapse copies the child’s data + id only; the node keeps its own size / flex_direction / extra — exactly as TS does.