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

@@ -325,6 +325,24 @@ export const PageProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
for (const child of node.nodes ?? []) {
childIds.push(walk(child, id));
}
// ColumnLayout uses Craft.js linkedNodes with fixed ids (col-0, col-1, ...).
// The AI emits children as direct `nodes`, but ColumnLayout's render ignores
// them and creates fresh column Elements — the AI's children become orphans
// and any subsequent toNodeTree walk hits an Invariant. Move direct children
// into linkedNodes so they render in the columns the user actually sees.
if (typeName === 'ColumnLayout' && childIds.length > 0) {
const linked: Record<string, string> = {};
childIds.forEach((cid, i) => {
linked[`col-${i}`] = cid;
if (nodes[cid]) (nodes[cid] as any).isCanvas = true; // columns are canvases
});
(nodes[id] as any).nodes = [];
(nodes[id] as any).linkedNodes = linked;
// Reflect the actual column count on the component so its render matches.
const cur = (nodes[id] as any).props || {};
if (!cur.columns || cur.columns !== childIds.length) cur.columns = childIds.length;
(nodes[id] as any).props = cur;
}
return id;
};
const rootId = walk(tree, null);