diff --git a/src/components/SimpleProcessDesignerV2/src/ProcessNodeTree.vue b/src/components/SimpleProcessDesignerV2/src/ProcessNodeTree.vue index c139468ee..3975c242f 100644 --- a/src/components/SimpleProcessDesignerV2/src/ProcessNodeTree.vue +++ b/src/components/SimpleProcessDesignerV2/src/ProcessNodeTree.vue @@ -144,50 +144,49 @@ const findFromParentNode = (nodeList: SimpleFlowNode[], nodeType: number) => { const nodeCache = new Map() const recursiveFindParentNode = ( - nodeList: SimpleFlowNode[], - findNode: SimpleFlowNode, - nodeType: number + nodeList: SimpleFlowNode[], + findNode: SimpleFlowNode, + nodeType: number ) => { if (!findNode) return - - // 检查缓存 - const cacheKey = `${findNode.id}-${nodeType}` - if (nodeCache.has(cacheKey)) { - const cachedNodes = nodeCache.get(cacheKey) - nodeList.push(...cachedNodes!) - return - } - - const foundNodes: SimpleFlowNode[] = [] - - // 检查当前节点 - if (findNode.type === nodeType) { - foundNodes.push(findNode) - } - - // 检查子节点 - if (findNode.childNode) { - if (findNode.childNode.type === nodeType) { - foundNodes.push(findNode.childNode) - } - recursiveFindParentNode(foundNodes, findNode.childNode, nodeType) - } - - // 检查条件分支节点 - if (findNode.type === NodeType.CONDITION_BRANCH_NODE && findNode.conditionNodes) { - findNode.conditionNodes.forEach(conditionNode => { - if (conditionNode.childNode) { - recursiveFindParentNode(foundNodes, conditionNode.childNode, nodeType) + + // Create a Set to track visited nodes and prevent cycles + const visitedNodes = new Set() + + const findNodesRecursive = (node: SimpleFlowNode) => { + if (!node || visitedNodes.has(node.id)) return + + visitedNodes.add(node.id) + + // Check current node + if (node.type === nodeType) { + if (!nodeList.some(n => n.id === node.id)) { + nodeList.push(node) } - }) + } + + // Check child node + if (node.childNode) { + findNodesRecursive(node.childNode) + } + + // Check condition nodes if present + if (node.type === NodeType.CONDITION_BRANCH_NODE && node.conditionNodes) { + node.conditionNodes.forEach(conditionNode => { + if (conditionNode.childNode) { + findNodesRecursive(conditionNode.childNode) + } + }) + } } - // 存入缓存 - nodeCache.set(cacheKey, foundNodes) - nodeList.push(...foundNodes) + // Start recursive search + findNodesRecursive(findNode) - // 继续向上递归查找 - emits('find:recursiveFindParentNode', nodeList, props.parentNode, nodeType) + // Continue search in parent node + if (props.parentNode) { + emits('find:recursiveFindParentNode', nodeList, props.parentNode, nodeType) + } }