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,