UNPKG

@rxflow/base

Version:

BaseFlow - 核心 Flow 组件库

131 lines (129 loc) 5.49 kB
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } /** * @author: yanxianliang * @date: 2025-07-03 09:53 * @desc: mindmap 布局 * * Copyright (c) 2025 by yanxianliang, All Rights Reserved. */ import Hierarchy from "@antv/hierarchy"; import { parseHandles } from "../.."; import { getNodeHeight, getNodeWidth } from "../dimension"; export var mindMapLayout = function mindMapLayout() { var layoutConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var config = arguments.length > 1 ? arguments[1] : undefined; var originNodes = config.originNodes, theme = config.theme, nodeTypes = config.nodeTypes; var rootNode = originNodes === null || originNodes === void 0 ? void 0 : originNodes[0]; if (!rootNode) { return { nodes: [], edges: [] }; } var edges = []; var _layoutConfig$hGap = layoutConfig.hGap, hGap = _layoutConfig$hGap === void 0 ? 40 : _layoutConfig$hGap, _layoutConfig$vGap = layoutConfig.vGap, vGap = _layoutConfig$vGap === void 0 ? 20 : _layoutConfig$vGap, markerType = layoutConfig.markerType; var layoutInfo = Hierarchy.mindmap(rootNode, { direction: 'H', getId: function getId(node) { return node.id; }, getWidth: function getWidth(node) { var width = getNodeWidth(node, nodeTypes, theme); // 还不是node实例。 node.width = width; return width; }, getHeight: function getHeight(node) { var height = getNodeHeight(node, nodeTypes, theme); node.height = height; return height; }, getHGap: function getHGap() { return hGap; }, getVGap: function getVGap() { return vGap; }, getSide: function getSide(node) { var data = node.data; return 'side' in data ? data.side : 'right'; }, getChildren: function getChildren(node) { var children = []; if ('collapsed' in node && node.collapsed === false) { children = node.children || []; } var _iterator = _createForOfIteratorHelper(children), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var child = _step.value; // edge ==> 是否显示箭头,需要配置 edges.push({ type: 'smoothstep', id: "".concat(node.id, "_").concat(child.id), source: node.id, target: child.id, markerEnd: markerType }); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return children; } }); // 开始节点x,y 以(0,0)进行修正,根节点需要能够被检测显示在画布区域 var nodes = []; var rootX = layoutInfo.x, rootY = layoutInfo.y; layoutInfo.BFTraverse(function (node) { var _definition$measureHa; var x = node.x, y = node.y, data = node.data; var id = data.id, type = data.type, width = data.width, height = data.height; var nodeWidth = width; var nodeHeight = height; var adjustX = x - rootX; var adjustY = y - rootY; var position = { x: adjustX, y: adjustY }; // 固定宽度和高度的节点,不支持动态适应内容 var graphNode = { id: id, type: type, measured: { width: nodeWidth, height: nodeHeight }, width: nodeWidth, height: nodeHeight, data: data, position: position, zIndex: 10 }; var definition = nodeTypes === null || nodeTypes === void 0 ? void 0 : nodeTypes[type]; var handles = definition === null || definition === void 0 || (_definition$measureHa = definition.measureHandles) === null || _definition$measureHa === void 0 ? void 0 : _definition$measureHa.call(definition, graphNode); graphNode.handles = parseHandles(handles); nodes.push(graphNode); }); return { nodes: nodes, edges: edges }; };