diff --git a/src/utils/convertFlowData.ts b/src/utils/convertFlowData.ts index afe9d93..c464db2 100644 --- a/src/utils/convertFlowData.ts +++ b/src/utils/convertFlowData.ts @@ -104,4 +104,130 @@ export const convertFlowData = (flowData: any) => { } return { nodes, edges }; -}; \ No newline at end of file +}; + +/** + * 将 flow editor 的 nodes 和 edges 数据结构转换回原始数据结构 + * @param nodes - flow editor 的节点数组 + * @param edges - flow editor 的连线数组 + * @returns 原始数据结构 + */ +export const revertFlowData = (nodes: any[], edges: any[]) => { + // 初始化返回的数据结构 + const flowData: any = { + id: 'main', + nodeConfigs: [], + lineConfigs: [] + }; + + // 转换节点数据 + if (nodes && nodes.length > 0) { + flowData.nodeConfigs = nodes.map(node => { + // 确定 nodeId 和 nodeName + const nodeId = node.id; + const nodeName = node.data?.title || nodeId; + + // 确定节点类型 + let nodeType = node.type; + // 特殊处理 start 和 end 节点 + if (nodeId === 'start') { + nodeType = 'start'; + } + else if (nodeId === 'end') { + nodeType = 'end'; + } + + // 构造 x6 数据(位置信息) + const x6 = JSON.stringify({ + position: node.position + }); + + // 构造 nodeConfig 对象 + const nodeConfig: any = { + nodeId, + nodeName, + x6 + }; + + // 处理 component 信息 + if (node.data?.component) { + nodeConfig.component = { + type: nodeType, + compIdentifier: node.data.component.compIdentifier, + compInstanceIdentifier: node.data.component.compInstanceIdentifier + }; + } + else if (nodeType !== 'start' && nodeType !== 'end') { + // 对于非 start/end 节点,添加基本的 component 信息 + nodeConfig.component = { + type: nodeType + }; + } + + // 处理参数信息 + const parameters = node.data?.parameters || {}; + + // 处理 dataIns(输入数据) + if (parameters.dataIns && parameters.dataIns.length > 0) { + nodeConfig.dataIns = parameters.dataIns.map((input: any) => ({ + id: input.name, + desc: input.desc, + dataType: input.dataType, + defaultValue: input.defaultValue + })); + } + + // 处理 dataOuts(输出数据) + if (parameters.dataOuts && parameters.dataOuts.length > 0) { + nodeConfig.dataOuts = parameters.dataOuts.map((output: any) => ({ + id: output.name, + desc: output.desc, + dataType: output.dataType, + defaultValue: output.defaultValue + })); + } + + return nodeConfig; + }); + } + +// 转换连线数据 + if (edges && edges.length > 0) { + flowData.lineConfigs = edges.map((edge, index) => { + // 查找源节点和目标节点以确定连线类型 + const sourceNode = nodes.find(node => node.id === edge.source); + const targetNode = nodes.find(node => node.id === edge.target); + + let lineType = 'DATA'; // 默认为DATA类型 + + // 判断是否为CONVERT类型的连线 + if (targetNode && ['JSONCONVERT', 'JSON2STR', 'STR2JSON'].includes(targetNode.type)) { + lineType = 'CONVERT'; + } + // 判断是否为API类型的连线 + else if (edge.sourceHandle && (edge.sourceHandle === 'apiOuts' || + sourceNode?.data?.parameters?.apiOuts?.some((out: any) => out.name === edge.sourceHandle))) { + lineType = 'API'; + } + else if (edge.targetHandle && (edge.targetHandle === 'apiIns' || + targetNode?.data?.parameters?.apiIns?.some((inp: any) => inp.name === edge.targetHandle))) { + lineType = 'API'; + } + + return { + id: edge.id || `edge_${index}`, // 如果没有 id,则生成一个 + lineType, // 添加lineType属性 + prev: { + nodeId: edge.source, + endpointId: edge.sourceHandle || 'done' // 默认使用 'done' + }, + next: { + nodeId: edge.target, + endpointId: edge.targetHandle || 'start' // 默认使用 'start' + } + }; + }); + } + + return flowData; +};