UNPKG

@xrenders/xflow

Version:

一款功能强大、易用灵活的流程编辑器框架,帮助你轻松构建复杂的工作流和流程产品

62 lines 2.09 kB
import { produce } from 'immer'; import dagre from '@dagrejs/dagre'; import { cloneDeep } from 'lodash-es'; export var CUSTOM_NODE = 'custom'; export var CUSTOM_EDGE = 'custom'; export var getLayoutByDagre = function getLayoutByDagre(originNodes, originEdges, rankdir) { var dagreGraph = new dagre.graphlib.Graph(); dagreGraph.setDefaultEdgeLabel(function () { return {}; }); var nodes = cloneDeep(originNodes).filter(function (node) { return !node.parentId && node.type === CUSTOM_NODE; }); var edges = cloneDeep(originEdges).filter(function (edge) { var _a; return !((_a = edge.data) === null || _a === void 0 ? void 0 : _a.isInIteration); }); dagreGraph.setGraph({ ranker: 'network-simplex', rankdir: rankdir, nodesep: 150, ranksep: 150 // 图的各个层次之间的间距 // align: '', // 节点对齐方式,可选:'UL' | 'UR' | 'DL' | 'DR' | undefined }); nodes.forEach(function (node) { dagreGraph.setNode(node.id, { width: node.width || 204, height: node.height || 45 }); }); edges.forEach(function (edge) { dagreGraph.setEdge(edge.source, edge.target); }); dagre.layout(dagreGraph); return dagreGraph; }; export default (function (nodes, edges, rankdir) { var layout = getLayoutByDagre(nodes, edges, rankdir); var rankMap = {}; nodes.forEach(function (node) { if (!node.parentId && node.type === CUSTOM_NODE) { var rank = layout.node(node.id).rank; if (!rankMap[rank]) { rankMap[rank] = node; } else { if (rankMap[rank].position.y > node.position.y) rankMap[rank] = node; } } }); var newNodes = produce(nodes, function (draft) { draft.forEach(function (node) { if (!node.parentId && node.type === CUSTOM_NODE) { var nodeWithPosition = layout.node(node.id); node.position = { x: nodeWithPosition.x - (node.width || 204) / 2, y: nodeWithPosition.y - (node.height || 45) / 2 + (rankMap[nodeWithPosition.rank].height || 45) / 2 }; } }); }); return newNodes; });