feat(utils): 增加reactFlow数据转换为原始数据的方法

master
钟良源 4 months ago
parent c9cf8a07e8
commit 059a04cfd1

@ -104,4 +104,130 @@ export const convertFlowData = (flowData: any) => {
}
return { nodes, edges };
};
};
/**
* 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;
};

Loading…
Cancel
Save