UNPKG

@xrenders/xflow

Version:

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

69 lines (68 loc) 2.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLayoutByDagre = exports.default = exports.CUSTOM_NODE = exports.CUSTOM_EDGE = void 0; var _immer = require("immer"); var _dagre = _interopRequireDefault(require("@dagrejs/dagre")); var _lodashEs = require("lodash-es"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } var CUSTOM_NODE = exports.CUSTOM_NODE = 'custom'; var CUSTOM_EDGE = exports.CUSTOM_EDGE = 'custom'; var getLayoutByDagre = exports.getLayoutByDagre = function getLayoutByDagre(originNodes, originEdges, rankdir) { var dagreGraph = new _dagre.default.graphlib.Graph(); dagreGraph.setDefaultEdgeLabel(function () { return {}; }); var nodes = (0, _lodashEs.cloneDeep)(originNodes).filter(function (node) { return !node.parentId && node.type === CUSTOM_NODE; }); var edges = (0, _lodashEs.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.default.layout(dagreGraph); return dagreGraph; }; var _default = exports.default = function _default(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 = (0, _immer.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; };