sitesmith: route ColumnLayout children through linkedNodes (Invariant fix)

ColumnLayout's render uses <Element id="col-0" is={Container} canvas>
which expects the columns to live in linkedNodes, not data.nodes. The
AI nests its column containers as direct children, so they'd land in
data.nodes — Craft.js's render ignores them (the layout draws fresh
empty Elements), but the orphaned children remain in state with
parent: <columnlayout-id>. Any subsequent toNodeTree walk then trips
on this inconsistency and the uncaught Invariant kills the editor.

Normalizer added in two places — treeToState (for scope=site/page
replaces) and buildNodeTree (for scope=section inserts and patch ops):
when we see a ColumnLayout with direct children, move them into
linkedNodes keyed col-0/col-1/col-2..., clear data.nodes, set the
column nodes' isCanvas to true (they hold content), and sync the
"columns" prop to the actual count.
This commit is contained in:
2026-05-24 16:17:25 -07:00
parent 906695379b
commit 6428f93cec
2 changed files with 32 additions and 0 deletions

View File

@@ -86,6 +86,20 @@ function buildNodeTree(query: any, tree: SerializedTreeNode): NodeTree {
const childId = walk(child, id);
craftNodes[id].data.nodes.push(childId);
}
// ColumnLayout uses linkedNodes (col-0, col-1, ...) — not direct children.
if (node.type.resolvedName === 'ColumnLayout' && craftNodes[id].data.nodes.length > 0) {
const linked: Record<string, string> = {};
craftNodes[id].data.nodes.forEach((cid: string, i: number) => {
linked[`col-${i}`] = cid;
if (craftNodes[cid]) craftNodes[cid].data.isCanvas = true;
});
craftNodes[id].data.nodes = [];
craftNodes[id].data.linkedNodes = linked;
const colCount = Object.keys(linked).length;
if (!craftNodes[id].data.props.columns || craftNodes[id].data.props.columns !== colCount) {
craftNodes[id].data.props.columns = colCount;
}
}
return id;
};