From ae70967f86d144d69cca29884b5cd4c70d9fe2ba Mon Sep 17 00:00:00 2001 From: ZLY Date: Wed, 15 Oct 2025 16:10:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(flow):=20=E6=94=AF=E6=8C=81=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=E8=8A=82=E7=82=B9=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E4=B8=8E=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增对 LOOP_START 和 LOOP_END 组件类型的识别 - 在节点转换过程中构建循环开始和结束节点结构 - 为循环节点设置默认参数和位置信息 - 动态注册 LOOP 类型节点到编辑器中 - 更新节点类型判断逻辑以支持循环组件 - 序列化循环节点关联信息至 customDef 字段- 优化节点数据构造逻辑,确保组件标识正确附加 --- src/utils/convertFlowData.ts | 69 ++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/src/utils/convertFlowData.ts b/src/utils/convertFlowData.ts index 19bff39..4ab6371 100644 --- a/src/utils/convertFlowData.ts +++ b/src/utils/convertFlowData.ts @@ -1,5 +1,6 @@ import { nodeTypeMap, registerNodeType } from '@/components/FlowEditor/node'; import LocalNode from '@/components/FlowEditor/node/localNode/LocalNode'; +import LoopNode from '@/components/FlowEditor/node/loopNode/LoopNode'; /** * 将提供的数据结构转换为适用于 flow editor 的 nodes 和 edges @@ -65,6 +66,9 @@ export const convertFlowData = (flowData: any, useDefault = true) => { else if (nodeConfig.nodeId === 'end') { nodeType = 'end'; } + else if (nodeConfig.component.type === 'LOOP_START' || nodeConfig.component.type === 'LOOP_END') { + nodeType = 'LOOP'; + } else { nodeType = nodeConfig.component.type; } @@ -78,6 +82,67 @@ export const convertFlowData = (flowData: any, useDefault = true) => { console.warn('Failed to parse position for node:', nodeConfig.nodeId); } + // 构建循环节点的默认参数和外壳 + if (nodeType === 'LOOP') { + // 创建循环开始节点 + const loopStartNode = { + id: `LOOP_START-${Date.now()}`, + type: 'LOOP', // 使用本地节点类型 + position: { x: position.x, y: position.y }, + data: { + title: '循环开始', + type: 'LOOP_START', + parameters: { + apiIns: [], + apiOuts: [{ name: 'loopStart', desc: '', dataType: '', defaultValue: '' }], + dataIns: [], + dataOuts: [] + }, + component: {} + } + + }; + + // 创建循环结束节点 + const loopEndNode = { + id: `LOOP_END-${Date.now()}`, + type: 'LOOP', // 使用本地节点类型 + position: { x: position.x + 400, y: position.y }, + data: { + title: '循环结束', + type: 'LOOP_END', + parameters: { + apiIns: [{ name: 'continue', desc: '', dataType: '', defaultValue: '' }], + apiOuts: [{ name: 'break', desc: '', dataType: '', defaultValue: '' }], + dataIns: [{ + 'arrayType': null, + 'dataType': 'INTEGER', + 'defaultValue': 10, + 'desc': '最大循环次数', + 'id': 'maxTime' + }], + dataOuts: [] + }, + component: { + type: 'LOOP_END', + customDef: '', + loopStartNodeId: loopStartNode.id // 这里的参数是为了提供在组件内部处理数据是使用,最后这个字段要序列化后放进customDef + } + } + }; + + loopStartNode.data.component = { + type: 'LOOP_START', + customDef: JSON.stringify({ loopEndNodeId: loopEndNode.id }) + }; + + // 将未定义的节点动态追加进nodeTypes + const nodeMap = Array.from(Object.values(nodeTypeMap).map(key => key)); + if (!nodeMap.includes('LOOP')) { + registerNodeType('LOOP', LoopNode, '循环'); + } + } + // 构造节点数据 const node: any = { id: nodeConfig.nodeId, @@ -111,11 +176,11 @@ export const convertFlowData = (flowData: any, useDefault = true) => { defaultValue: output.defaultValue })) || [] }, - type: nodeType + type: nodeType === 'LOOP' ? nodeConfig.component.type : nodeType } }; - // 如果是机械臂节点,添加组件标识信息 + // 添加组件标识信息 if (nodeConfig.component) { node.data.component = { compIdentifier: nodeConfig.component.compIdentifier,