diff --git a/src/composables/useThinkFlow.ts b/src/composables/useThinkFlow.ts index 57ec998..c31abea 100644 --- a/src/composables/useThinkFlow.ts +++ b/src/composables/useThinkFlow.ts @@ -103,6 +103,8 @@ export function useThinkFlow({ t, locale }: { t: Translate; locale: Ref nodes: flowNodes, edges: flowEdges, updateNode, + removeNodes, + removeEdges, fitView, onNodeDragStart, onNodeDragStop @@ -176,6 +178,17 @@ export function useThinkFlow({ t, locale }: { t: Translate; locale: Ref return ids } + /** + * 删除指定节点的所有后代节点 + * 用于在重新扩展某个节点时,清空其原有的子树 + */ + const removeDescendants = (nodeId: string) => { + const descendantIds = getDescendantIds(nodeId) + if (descendantIds.size > 0) { + removeNodes(Array.from(descendantIds)) + } + } + /** * 当前激活路径(节点集合 + 边集合) * - 向上:从激活节点回溯到根 @@ -663,6 +676,11 @@ export function useThinkFlow({ t, locale }: { t: Translate; locale: Ref const data = await response.json() const result = JSON.parse(data.choices[0].message.content) + // 如果是重新扩展已有节点,先清空其现有的所有后代节点 + if (parentNode && currentParentId) { + removeDescendants(currentParentId) + } + const parentNodeObj = flowNodes.value.find(n => n.id === currentParentId) const startX = parentNodeObj ? parentNodeObj.position.x : 50 const startY = parentNodeObj ? parentNodeObj.position.y : 300