2025-09-04 16:41:54 +08:00
|
|
|
|
export const getInput = (nodeId, isStrict = true) => {
|
|
|
|
|
|
const data = lf.getGraphData();
|
|
|
|
|
|
let flag = false
|
|
|
|
|
|
if (isStrict) {
|
|
|
|
|
|
data.edges.forEach(item => {
|
|
|
|
|
|
if (item.targetNodeId === nodeId) {
|
|
|
|
|
|
flag = true
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
flag = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
|
|
const arr = []
|
2025-09-24 08:59:23 +08:00
|
|
|
|
const filterNodes = ['sleep', 'stopLoop', 'subEnd', 'subStart', 'branch', 'end']
|
2025-09-04 16:41:54 +08:00
|
|
|
|
data.nodes.forEach(item => {
|
|
|
|
|
|
if (item.id === nodeId) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 08:59:23 +08:00
|
|
|
|
if (filterNodes.includes(item.type)) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const obj = {
|
|
|
|
|
|
value: item.id,
|
|
|
|
|
|
label: item.properties.name,
|
|
|
|
|
|
children: [{
|
|
|
|
|
|
value: 'input',
|
|
|
|
|
|
label: '输入',
|
2025-09-19 14:16:28 +08:00
|
|
|
|
type: 'input',
|
2025-09-04 16:41:54 +08:00
|
|
|
|
children: []
|
2025-09-24 08:59:23 +08:00
|
|
|
|
}, {
|
|
|
|
|
|
value: 'output',
|
|
|
|
|
|
label: '输出',
|
|
|
|
|
|
type: 'output',
|
|
|
|
|
|
children: []
|
|
|
|
|
|
}]
|
2025-09-04 16:41:54 +08:00
|
|
|
|
}
|
2025-09-24 08:59:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (item.properties.inputParams) {
|
|
|
|
|
|
transformTree(item.properties.inputParams, obj.children[0].children, 'input')
|
2025-09-04 16:41:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (item.properties.nodeParams) {
|
2025-09-24 08:59:23 +08:00
|
|
|
|
transformTree(item.properties.nodeParams, obj.children[0].children, 'input')
|
2025-09-04 16:41:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (item.properties.outputParams) {
|
2025-09-24 08:59:23 +08:00
|
|
|
|
transformTree(item.properties.outputParams, obj.children[1].children, 'output')
|
2025-09-04 16:41:54 +08:00
|
|
|
|
}
|
2025-09-24 08:59:23 +08:00
|
|
|
|
arr.push(obj)
|
2025-09-04 16:41:54 +08:00
|
|
|
|
})
|
|
|
|
|
|
return arr
|
|
|
|
|
|
}
|
|
|
|
|
|
return flag
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-19 14:16:28 +08:00
|
|
|
|
const transformTree = (arr, parent = [], type) => {
|
2025-09-04 16:41:54 +08:00
|
|
|
|
arr.forEach(item => {
|
|
|
|
|
|
const currentNode = {
|
|
|
|
|
|
value: item.name,
|
|
|
|
|
|
label: item.name,
|
2025-09-19 14:16:28 +08:00
|
|
|
|
type,
|
2025-09-04 16:41:54 +08:00
|
|
|
|
children: []
|
|
|
|
|
|
};
|
|
|
|
|
|
parent.push(currentNode);
|
|
|
|
|
|
|
|
|
|
|
|
if (item.children?.length > 0) {
|
|
|
|
|
|
transformTree(item.children, currentNode.children);
|
|
|
|
|
|
} else {
|
2025-09-19 14:16:28 +08:00
|
|
|
|
delete currentNode.children
|
2025-09-04 16:41:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
return parent;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const getStartNodeFormData = () => {
|
|
|
|
|
|
const flowData = lf.getGraphData();
|
|
|
|
|
|
const startNode = flowData.nodes.find(item => item.type === 'start')
|
|
|
|
|
|
const data = addValueProperty(startNode.properties?.inputParams || [])
|
|
|
|
|
|
return data
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-25 16:35:06 +08:00
|
|
|
|
export const addValueProperty = (data, schemeId = '') => {
|
2025-09-04 16:41:54 +08:00
|
|
|
|
// 处理单个对象或对象数组
|
|
|
|
|
|
const processItem = (item, index, path) => {
|
|
|
|
|
|
// 根据类型设置默认值
|
2025-09-25 16:35:06 +08:00
|
|
|
|
item.value = item.name === 'schemeId' ? schemeId : '';
|
2025-09-04 16:41:54 +08:00
|
|
|
|
item.propPath = `${path}.${index}.`
|
|
|
|
|
|
if (item.type.includes('array') && !item.type.includes('object')) {
|
|
|
|
|
|
const type = item.type.match(/<([^>]+)>/)[1]
|
|
|
|
|
|
item.children = [{ name: 'Array Item', type, value: '', propPath: item.propPath + '.0'}]
|
|
|
|
|
|
}
|
|
|
|
|
|
// 递归处理子项
|
|
|
|
|
|
if (item.children && item.children.length > 0) {
|
|
|
|
|
|
item.children = item.children.map((cItem, cIndex) => {
|
|
|
|
|
|
return processItem(cItem, cIndex , item.propPath + 'children')
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 如果传入的是数组,处理每个元素;否则处理单个对象
|
|
|
|
|
|
return Array.isArray(data) ? data.map((item, index) => processItem(item, index, '')) : processItem(data, 0, '')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const recursiveFilter = (data, id, type = ['loop', 'customGroup'], result = []) => {
|
|
|
|
|
|
data.forEach((item) => {
|
|
|
|
|
|
if (type.includes(item.type) && item.children && item.children.includes(id)) {
|
|
|
|
|
|
result.push(item)
|
|
|
|
|
|
recursiveFilter(data, item.id, result)
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const formatTableData = (arr, data = {}) => {
|
|
|
|
|
|
arr.forEach(item => {
|
|
|
|
|
|
data[item.name] = item.value
|
|
|
|
|
|
if (item?.children && item.children.length > 0) {
|
|
|
|
|
|
data[item.name] = {}
|
|
|
|
|
|
formatTableData(item.children, data[item.name])
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
return data
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const addNewEdge = (nodeId) => {
|
2025-11-17 14:54:32 +08:00
|
|
|
|
const { edges } = lf.getGraphData();
|
|
|
|
|
|
const ttt = edges.slice(); // 浅拷贝(比 JSON 深拷贝高效 10x+)
|
|
|
|
|
|
|
|
|
|
|
|
const edgesToDelete = ttt.filter(_edge =>
|
|
|
|
|
|
_edge.sourceNodeId === nodeId || _edge.targetNodeId === nodeId
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
edgesToDelete.forEach(edge => lf.deleteEdge(edge.id));
|
|
|
|
|
|
|
|
|
|
|
|
if (window.addNewEdgeTimer) {
|
|
|
|
|
|
clearTimeout(window.addNewEdgeTimer); // 清理上一次未执行的定时器
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
window.addNewEdgeTimer = setTimeout(() => {
|
|
|
|
|
|
edgesToDelete.forEach(item => {
|
|
|
|
|
|
lf.addEdge({
|
|
|
|
|
|
type: "bezier",
|
|
|
|
|
|
sourceNodeId: item.sourceNodeId,
|
|
|
|
|
|
targetNodeId: item.targetNodeId,
|
|
|
|
|
|
sourceAnchorId: item.sourceAnchorId,
|
|
|
|
|
|
targetAnchorId: item.targetAnchorId
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
|
|
|
|
|
delete window.addNewEdgeTimer;
|
|
|
|
|
|
}, 50)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const newEdge = (nodeId) => {
|
2025-09-04 16:41:54 +08:00
|
|
|
|
const { edges } = lf.getGraphData();
|
|
|
|
|
|
const arr = JSON.parse(JSON.stringify(edges))
|
|
|
|
|
|
arr.forEach(_edge => {
|
|
|
|
|
|
const { sourceNodeId, targetNodeId, sourceAnchorId, targetAnchorId } = _edge
|
|
|
|
|
|
if (sourceNodeId === nodeId || targetNodeId === nodeId) {
|
|
|
|
|
|
lf.deleteEdge(_edge.id);
|
|
|
|
|
|
lf.addEdge({
|
|
|
|
|
|
type: "bezier",
|
|
|
|
|
|
sourceNodeId: sourceNodeId,
|
|
|
|
|
|
targetNodeId: targetNodeId,
|
|
|
|
|
|
sourceAnchorId: sourceAnchorId,
|
|
|
|
|
|
targetAnchorId: targetAnchorId
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-17 15:24:45 +08:00
|
|
|
|
export const initNodeZoom = (modelId, nodeZoom, className, init=false) => {
|
|
|
|
|
|
const nodes = document.getElementsByClassName(modelId)
|
|
|
|
|
|
if (nodes.length > 0) {
|
|
|
|
|
|
const node = nodes[0]
|
|
|
|
|
|
const parent = node.closest(className);
|
|
|
|
|
|
if (nodeZoom) {
|
|
|
|
|
|
parent.style.width = '680px'
|
|
|
|
|
|
} else {
|
|
|
|
|
|
parent.style.width = '300px'
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!init) {
|
|
|
|
|
|
addNewEdge(modelId)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 16:41:54 +08:00
|
|
|
|
export const getInputNumber = (nodeId) => {
|
|
|
|
|
|
const data = lf.getGraphData();
|
|
|
|
|
|
let flag = false
|
|
|
|
|
|
data.edges.forEach(item => {
|
|
|
|
|
|
if (item.targetNodeId === nodeId) {
|
|
|
|
|
|
flag = true
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (flag) {
|
|
|
|
|
|
const arr = []
|
|
|
|
|
|
data.nodes.forEach(item => {
|
|
|
|
|
|
if (item.properties.inputParams) {
|
|
|
|
|
|
item.properties.inputParams.forEach(item => {
|
|
|
|
|
|
if (item.type === 'number') {
|
|
|
|
|
|
arr.push(item)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
return arr
|
|
|
|
|
|
}
|
|
|
|
|
|
return flag
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const removeSwitchEdge = (nodeId, sourceAnchorId) => {
|
|
|
|
|
|
const { edges } = lf.getGraphData();
|
|
|
|
|
|
const edge = edges.find(item => {
|
|
|
|
|
|
return item.sourceNodeId === nodeId && item.sourceAnchorId === sourceAnchorId
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if (edge) {
|
|
|
|
|
|
lf.deleteEdge(edge.id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export const convertToTree = (data) => {
|
|
|
|
|
|
// 创建 id 到节点的映射
|
|
|
|
|
|
const nodeMap = new Map();
|
|
|
|
|
|
data.forEach(node => {
|
|
|
|
|
|
nodeMap.set(node.id, { ...node, name: node.properties.name || '' }); // 浅拷贝节点
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 标记非根节点(被其他节点引用的节点)
|
|
|
|
|
|
const nonRootIds = new Set();
|
|
|
|
|
|
data.forEach(node => {
|
|
|
|
|
|
if (node.children) {
|
|
|
|
|
|
node.children.forEach(childId => {
|
|
|
|
|
|
nonRootIds.add(childId);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 构建树形结构
|
|
|
|
|
|
const result = [];
|
|
|
|
|
|
data.forEach(node => {
|
|
|
|
|
|
// 只处理根节点(未被引用的节点)
|
|
|
|
|
|
if (!nonRootIds.has(node.id)) {
|
|
|
|
|
|
const rootNode = nodeMap.get(node.id);
|
|
|
|
|
|
// 递归处理子节点
|
|
|
|
|
|
const processNode = (currentNode) => {
|
|
|
|
|
|
if (currentNode.children) {
|
|
|
|
|
|
currentNode.children = currentNode.children.map(childId => {
|
|
|
|
|
|
const childNode = nodeMap.get(childId);
|
|
|
|
|
|
processNode(childNode); // 递归处理子节点
|
|
|
|
|
|
return childNode;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
return currentNode;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
result.push(processNode(rootNode));
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
2025-08-21 14:11:58 +08:00
|
|
|
|
}
|