fix(builder): regenerate node ids on duplicate/paste to prevent state corruption

Craft.js duplicate (ContextMenu + keyboard shortcut) and paste were reusing
the original node's toNodeTree() output verbatim, so addNodeTree() inserted
duplicate node ids into the editor tree. Added regenerateTreeIds() which
deep-clones a NodeTree and remaps rootNodeId, node map keys, node.id,
internal node.data.parent, node.data.nodes, and node.data.linkedNodes via
Craft.js's own getRandomId(). Also fixed pasteNode to insert as a sibling
of the right-clicked node (using its parent) instead of using a leaf node
as the new parent, which previously threw.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-12 12:18:44 -07:00
parent 4c001e1af4
commit 97123c4c58
4 changed files with 219 additions and 10 deletions
+2 -1
View File
@@ -1,6 +1,7 @@
import { useEffect } from 'react';
import { useEditor } from '@craftjs/core';
import { findDeletableTarget } from '../utils/craft-helpers';
import { regenerateTreeIds } from '../utils/craft-tree';
function isInputFocused(): boolean {
const el = document.activeElement;
@@ -73,7 +74,7 @@ export function useKeyboardShortcuts() {
const node = query.node(nodeId).get();
const parentId = node?.data?.parent;
if (parentId) {
const tree = query.node(nodeId).toNodeTree();
const tree = regenerateTreeIds(query.node(nodeId).toNodeTree());
actions.addNodeTree(tree, parentId);
}
}