GroupNode.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. extendStatics(d, b);
  10. function __() { this.constructor = d; }
  11. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  12. };
  13. })();
  14. var __assign = (this && this.__assign) || function () {
  15. __assign = Object.assign || function(t) {
  16. for (var s, i = 1, n = arguments.length; i < n; i++) {
  17. s = arguments[i];
  18. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  19. t[p] = s[p];
  20. }
  21. return t;
  22. };
  23. return __assign.apply(this, arguments);
  24. };
  25. var __read = (this && this.__read) || function (o, n) {
  26. var m = typeof Symbol === "function" && o[Symbol.iterator];
  27. if (!m) return o;
  28. var i = m.call(o), r, ar = [], e;
  29. try {
  30. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  31. }
  32. catch (error) { e = { error: error }; }
  33. finally {
  34. try {
  35. if (r && !r.done && (m = i["return"])) m.call(i);
  36. }
  37. finally { if (e) throw e.error; }
  38. }
  39. return ar;
  40. };
  41. var __spread = (this && this.__spread) || function () {
  42. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  43. return ar;
  44. };
  45. import { h } from '@logicflow/core';
  46. import { RectResize } from '../../NodeResize';
  47. var defaultWidth = 500;
  48. var defaultHeight = 300;
  49. var DEFAULT_BOTTOM_Z_INDEX = -10000;
  50. var GroupNodeModel = /** @class */ (function (_super) {
  51. __extends(GroupNodeModel, _super);
  52. function GroupNodeModel() {
  53. var _this = _super !== null && _super.apply(this, arguments) || this;
  54. _this.isGroup = true;
  55. _this.unfoldedWidth = defaultWidth;
  56. _this.unfoldedHight = defaultHeight;
  57. return _this;
  58. }
  59. GroupNodeModel.prototype.initNodeData = function (data) {
  60. var _this = this;
  61. _super.prototype.initNodeData.call(this, data);
  62. var children = [];
  63. if (Array.isArray(data.children)) {
  64. children = data.children;
  65. }
  66. // 初始化组的子节点
  67. this.children = new Set(children);
  68. this.width = defaultWidth;
  69. this.height = defaultHeight;
  70. this.foldedWidth = 80;
  71. this.foldedHeight = 60;
  72. this.zIndex = DEFAULT_BOTTOM_Z_INDEX;
  73. this.radius = 0;
  74. this.text.editable = false;
  75. this.text.draggable = false;
  76. this.isRestrict = false;
  77. this.resizable = false;
  78. this.autoToFront = false;
  79. this.foldable = false;
  80. if (this.properties.isFolded === undefined) {
  81. this.properties.isFolded = false;
  82. }
  83. this.isFolded = this.properties.isFolded;
  84. // fixme: 虽然默认保存的分组不会收起,但是如果重写保存数据分组了,
  85. // 此处代码会导致多一个history记录
  86. setTimeout(function () {
  87. _this.isFolded && _this.foldGroup(_this.isFolded);
  88. });
  89. // this.foldGroup(this.isFolded);
  90. };
  91. GroupNodeModel.prototype.getResizeOutlineStyle = function () {
  92. var style = _super.prototype.getResizeOutlineStyle.call(this);
  93. style.stroke = 'none';
  94. return style;
  95. };
  96. /**
  97. * 折叠分组
  98. * 1. 折叠分组的宽高
  99. * 2. 处理分组子节点
  100. * 3. 处理连线
  101. */
  102. GroupNodeModel.prototype.foldGroup = function (isFolded) {
  103. var _this = this;
  104. this.setProperty('isFolded', isFolded);
  105. this.isFolded = isFolded;
  106. // step 1
  107. if (isFolded) {
  108. this.x = this.x - this.width / 2 + this.foldedWidth / 2;
  109. this.y = this.y - this.height / 2 + this.foldedHeight / 2;
  110. this.unfoldedWidth = this.width;
  111. this.unfoldedHight = this.height;
  112. this.width = this.foldedWidth;
  113. this.height = this.foldedHeight;
  114. }
  115. else {
  116. this.width = this.unfoldedWidth;
  117. this.height = this.unfoldedHight;
  118. this.x = this.x + this.width / 2 - this.foldedWidth / 2;
  119. this.y = this.y + this.height / 2 - this.foldedHeight / 2;
  120. }
  121. // step 2
  122. var allEdges = this.incoming.edges.concat(this.outgoing.edges);
  123. this.children.forEach(function (elementId) {
  124. var nodeModel = _this.graphModel.getElement(elementId);
  125. // FIX: https://github.com/didi/LogicFlow/issues/1007
  126. if (nodeModel.isGroup && !nodeModel.isFolded) {
  127. nodeModel.foldGroup(isFolded);
  128. }
  129. nodeModel.visible = !isFolded;
  130. allEdges = allEdges.concat(nodeModel.incoming.edges.concat(nodeModel.outgoing.edges));
  131. });
  132. // step 3
  133. this.foldEdge(isFolded, allEdges);
  134. };
  135. GroupNodeModel.prototype.getAnchorStyle = function (anchorInfo) {
  136. var style = _super.prototype.getAnchorStyle.call(this, anchorInfo);
  137. style.stroke = 'transparent';
  138. style.fill = 'transparent';
  139. style.hover.fill = 'transparent';
  140. style.hover.stroke = 'transparent';
  141. return style;
  142. };
  143. /**
  144. * 折叠分组的时候,处理分组自身的连线和分组内部子节点上的连线
  145. * 边的分类:
  146. * - 虚拟边:分组被收起时,表示分组本身与外部节点关系的边。
  147. * - 真实边:分组本身或者分组内部节点与外部节点节点(非收起分组)关系的边。
  148. * 如果一个分组,本身与外部节点有M条连线,且内部N个子节点与外部节点有连线,那么这个分组收起时会生成M+N条连线。
  149. * 折叠分组时:
  150. * - 原有的虚拟边删除;
  151. * - 创建一个虚拟边;
  152. * - 真实边则隐藏;
  153. * 展开分组是:
  154. * - 原有的虚拟边删除;
  155. * - 如果目外部点是收起的分组,则创建虚拟边;
  156. * - 如果外部节点是普通节点,则显示真实边;
  157. */
  158. GroupNodeModel.prototype.foldEdge = function (isFolded, allEdges) {
  159. var _this = this;
  160. allEdges.forEach(function (edgeModel, index) {
  161. var id = edgeModel.id, sourceNodeId = edgeModel.sourceNodeId, targetNodeId = edgeModel.targetNodeId, startPoint = edgeModel.startPoint, endPoint = edgeModel.endPoint, type = edgeModel.type, properties = edgeModel.properties, text = edgeModel.text;
  162. var data = {
  163. id: id + "__" + index,
  164. sourceNodeId: sourceNodeId,
  165. targetNodeId: targetNodeId,
  166. startPoint: startPoint,
  167. endPoint: endPoint,
  168. type: type,
  169. properties: properties,
  170. text: text === null || text === void 0 ? void 0 : text.value,
  171. };
  172. if (edgeModel.virtual) {
  173. _this.graphModel.deleteEdgeById(edgeModel.id);
  174. }
  175. var targetNodeIdGroup = _this.graphModel.group.getNodeGroup(targetNodeId);
  176. // 考虑目标节点本来就是分组的情况
  177. if (!targetNodeIdGroup) {
  178. targetNodeIdGroup = _this.graphModel.getNodeModelById(targetNodeId);
  179. }
  180. var sourceNodeIdGroup = _this.graphModel.group.getNodeGroup(sourceNodeId);
  181. if (!sourceNodeIdGroup) {
  182. sourceNodeIdGroup = _this.graphModel.getNodeModelById(sourceNodeId);
  183. }
  184. // 折叠时,处理未被隐藏的边的逻辑
  185. if (isFolded && edgeModel.visible !== false) {
  186. // 需要确认此分组节点是新连线的起点还是终点
  187. // 创建一个虚拟边,虚拟边相对真实边,起点或者终点从一起分组内部的节点成为了分组,
  188. // 如果需要被隐藏的边的起点在需要折叠的分组中,那么设置虚拟边的开始节点为此分组
  189. if (_this.children.has(sourceNodeId) || _this.id === sourceNodeId) {
  190. data.startPoint = undefined;
  191. data.sourceNodeId = _this.id;
  192. }
  193. else {
  194. data.endPoint = undefined;
  195. data.targetNodeId = _this.id;
  196. }
  197. // 如果边的起点和终点都在分组内部,则不创建新的虚拟边
  198. if (targetNodeIdGroup.id !== _this.id || sourceNodeIdGroup.id !== _this.id) {
  199. _this.createVirtualEdge(data);
  200. }
  201. edgeModel.visible = false;
  202. }
  203. // 展开时,处理被隐藏的边的逻辑
  204. if (!isFolded && edgeModel.visible === false) {
  205. // 展开分组时:判断真实边的起点和终点是否有任一节点在已折叠分组中,如果不是,则显示真实边。如果是,这修改这个边的对应目标节点id来创建虚拟边。
  206. if (targetNodeIdGroup && targetNodeIdGroup.isGroup && targetNodeIdGroup.isFolded) {
  207. data.targetNodeId = targetNodeIdGroup.id;
  208. data.endPoint = undefined;
  209. _this.createVirtualEdge(data);
  210. }
  211. else if (sourceNodeIdGroup && sourceNodeIdGroup.isGroup && sourceNodeIdGroup.isFolded) {
  212. data.sourceNodeId = sourceNodeIdGroup.id;
  213. data.startPoint = undefined;
  214. _this.createVirtualEdge(data);
  215. }
  216. else {
  217. edgeModel.visible = true;
  218. }
  219. }
  220. });
  221. };
  222. GroupNodeModel.prototype.createVirtualEdge = function (edgeData) {
  223. edgeData.pointsList = undefined;
  224. var model = this.graphModel.addEdge(edgeData);
  225. model.virtual = true;
  226. // 强制不保存group连线数据
  227. // model.getData = () => null;
  228. model.text.editable = false;
  229. model.isFoldedEdge = true;
  230. };
  231. GroupNodeModel.prototype.isInRange = function (_a) {
  232. var x1 = _a.x1, y1 = _a.y1, x2 = _a.x2, y2 = _a.y2;
  233. return x1 >= (this.x - this.width / 2)
  234. && x2 <= (this.x + this.width / 2)
  235. && y1 >= (this.y - this.height / 2)
  236. && y2 <= (this.y + this.height / 2);
  237. };
  238. GroupNodeModel.prototype.isAllowMoveTo = function (_a) {
  239. var x1 = _a.x1, y1 = _a.y1, x2 = _a.x2, y2 = _a.y2;
  240. return {
  241. x: x1 >= (this.x - this.width / 2) && x2 <= (this.x + this.width / 2),
  242. y: y1 >= (this.y - this.height / 2) && y2 <= (this.y + this.height / 2),
  243. };
  244. };
  245. GroupNodeModel.prototype.setAllowAppendChild = function (isAllow) {
  246. this.setProperty('groupAddable', isAllow);
  247. };
  248. /**
  249. * 添加分组子节点
  250. * @param id 节点id
  251. */
  252. GroupNodeModel.prototype.addChild = function (id) {
  253. this.children.add(id);
  254. };
  255. /**
  256. * 删除分组子节点
  257. * @param id 节点id
  258. */
  259. GroupNodeModel.prototype.removeChild = function (id) {
  260. this.children.delete(id);
  261. };
  262. GroupNodeModel.prototype.getAddableOutlineStyle = function () {
  263. return {
  264. stroke: '#FEB663',
  265. strokeWidth: 2,
  266. strokeDasharray: '4 4',
  267. fill: 'transparent',
  268. };
  269. };
  270. GroupNodeModel.prototype.getData = function () {
  271. var data = _super.prototype.getData.call(this);
  272. data.children = __spread(this.children);
  273. var properties = data.properties;
  274. delete properties.groupAddable;
  275. delete properties.isFolded;
  276. return data;
  277. };
  278. GroupNodeModel.prototype.getHistoryData = function () {
  279. var data = _super.prototype.getData.call(this);
  280. data.children = __spread(this.children);
  281. data.isGroup = true;
  282. var properties = data.properties;
  283. delete properties.groupAddable;
  284. if (properties.isFolded) { // 如果分组被折叠
  285. data.x = data.x + this.unfoldedWidth / 2 - this.foldedWidth / 2;
  286. data.y = data.y + this.unfoldedHight / 2 - this.foldedHeight / 2;
  287. }
  288. return data;
  289. };
  290. /**
  291. * 是否允许此节点添加到此分组中
  292. */
  293. GroupNodeModel.prototype.isAllowAppendIn = function (nodeData) {
  294. return true;
  295. };
  296. /**
  297. * 当groupA被添加到groupB中时,将groupB及groupB所属的group的zIndex减1
  298. */
  299. GroupNodeModel.prototype.toBack = function () {
  300. this.zIndex--;
  301. };
  302. return GroupNodeModel;
  303. }(RectResize.model));
  304. var GroupNode = /** @class */ (function (_super) {
  305. __extends(GroupNode, _super);
  306. function GroupNode() {
  307. return _super !== null && _super.apply(this, arguments) || this;
  308. }
  309. GroupNode.prototype.getControlGroup = function () {
  310. var _a = this.props.model, resizable = _a.resizable, properties = _a.properties;
  311. return resizable && !properties.isFolded ? _super.prototype.getControlGroup.call(this) : null;
  312. };
  313. GroupNode.prototype.getAddableShape = function () {
  314. var _a = this.props.model, width = _a.width, height = _a.height, x = _a.x, y = _a.y, radius = _a.radius, properties = _a.properties;
  315. if (!properties.groupAddable)
  316. return null;
  317. var strokeWidth = this.props.model.getNodeStyle().strokeWidth;
  318. var style = this.props.model.getAddableOutlineStyle();
  319. var newWidth = width + strokeWidth + 8;
  320. var newHeight = height + strokeWidth + 8;
  321. return h('rect', __assign(__assign({}, style), { width: newWidth, height: newHeight, x: x - newWidth / 2, y: y - newHeight / 2, rx: radius, ry: radius }));
  322. };
  323. GroupNode.prototype.getFoldIcon = function () {
  324. var model = this.props.model;
  325. var foldX = model.x - model.width / 2 + 5;
  326. var foldY = model.y - model.height / 2 + 5;
  327. if (!model.foldable)
  328. return null;
  329. var iconIcon = h('path', {
  330. fill: 'none',
  331. stroke: '#818281',
  332. strokeWidth: 2,
  333. 'pointer-events': 'none',
  334. d: model.properties.isFolded
  335. ? "M " + (foldX + 3) + "," + (foldY + 6) + " " + (foldX + 11) + "," + (foldY + 6) + " M" + (foldX + 7) + "," + (foldY + 2) + " " + (foldX + 7) + "," + (foldY + 10)
  336. : "M " + (foldX + 3) + "," + (foldY + 6) + " " + (foldX + 11) + "," + (foldY + 6) + " ",
  337. });
  338. return h('g', {}, [
  339. h('rect', {
  340. height: 12,
  341. width: 14,
  342. rx: 2,
  343. ry: 2,
  344. strokeWidth: 1,
  345. fill: '#F4F5F6',
  346. stroke: '#CECECE',
  347. cursor: 'pointer',
  348. x: model.x - model.width / 2 + 5,
  349. y: model.y - model.height / 2 + 5,
  350. onClick: function () {
  351. model.foldGroup(!model.properties.isFolded);
  352. },
  353. }),
  354. iconIcon,
  355. ]);
  356. };
  357. GroupNode.prototype.getResizeShape = function () {
  358. return h('g', {}, [
  359. this.getAddableShape(),
  360. _super.prototype.getResizeShape.call(this),
  361. this.getFoldIcon(),
  362. ]);
  363. };
  364. return GroupNode;
  365. }(RectResize.view));
  366. export default {
  367. type: 'group',
  368. view: GroupNode,
  369. model: GroupNodeModel,
  370. };