稳定 UI 编辑器删除回调引用
使用 useCallback 保持 deleteNode 在输入未变化时的函数引用稳定 减少全局键盘监听器不必要的解绑与重新注册
This commit is contained in:
@@ -175,6 +175,8 @@ export function useUiEditorSession(
|
||||
initialFurthestStepIndex = 0,
|
||||
) {
|
||||
const editor = useUiEditorState(EMPTY_UI_EDITOR_STATE);
|
||||
const editorDeleteNode = editor.deleteNode;
|
||||
const editorUiTrees = editor.state.ui_trees;
|
||||
const replaceEditorState = editor.replaceState;
|
||||
const [isLoading, setIsLoading] = useState(Boolean(resourceId));
|
||||
const [loadError, setLoadError] = useState<string | null>(null);
|
||||
@@ -882,21 +884,24 @@ export function useUiEditorSession(
|
||||
return result;
|
||||
}
|
||||
|
||||
function deleteNode(nodeId: NodeId, treeId = activeImageId) {
|
||||
if (!treeId) return;
|
||||
const tree = editor.state.ui_trees.find(
|
||||
(candidate) => candidate.src_ui_design === treeId,
|
||||
);
|
||||
const location = tree ? findUiNodeLocation(tree.root, nodeId) : null;
|
||||
const deletedNodeIds = location ? collectUiNodeIds(location.node) : null;
|
||||
const result = editor.deleteNode(treeId, nodeId);
|
||||
if (!result.ok) {
|
||||
setStatus('无法删除该节点。');
|
||||
const deleteNode = useCallback(
|
||||
(nodeId: NodeId, treeId = activeImageId) => {
|
||||
if (!treeId) return;
|
||||
const tree = editorUiTrees.find(
|
||||
(candidate) => candidate.src_ui_design === treeId,
|
||||
);
|
||||
const location = tree ? findUiNodeLocation(tree.root, nodeId) : null;
|
||||
const deletedNodeIds = location ? collectUiNodeIds(location.node) : null;
|
||||
const result = editorDeleteNode(treeId, nodeId);
|
||||
if (!result.ok) {
|
||||
setStatus('无法删除该节点。');
|
||||
return result;
|
||||
}
|
||||
if (deletedNodeIds?.has(selectedNodeId ?? '')) setSelectedNodeId(null);
|
||||
return result;
|
||||
}
|
||||
if (deletedNodeIds?.has(selectedNodeId ?? '')) setSelectedNodeId(null);
|
||||
return result;
|
||||
}
|
||||
},
|
||||
[activeImageId, editorDeleteNode, editorUiTrees, selectedNodeId],
|
||||
);
|
||||
|
||||
function selectSprite(id: SpriteAssetId) {
|
||||
setSelectedNodeId(null);
|
||||
|
||||
Reference in New Issue
Block a user