@logicflow/extension 2.0.8 → 2.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -224,6 +224,62 @@ var DynamicGroup = /** @class */ (function () {
224
224
  }
225
225
  }
226
226
  };
227
+ this.onNodeMove = function (_a) {
228
+ var deltaX = _a.deltaX, deltaY = _a.deltaY, data = _a.data;
229
+ var id = data.id, x = data.x, y = data.y, properties = data.properties;
230
+ if (!properties) {
231
+ return;
232
+ }
233
+ var width = properties.width, height = properties.height;
234
+ var groupId = _this.nodeGroupMap.get(id);
235
+ if (!groupId) {
236
+ return;
237
+ }
238
+ var groupModel = _this.lf.getNodeModelById(groupId);
239
+ if (!groupModel || !groupModel.isRestrict || !groupModel.autoResize) {
240
+ return;
241
+ }
242
+ // 当父节点isRestrict=true & autoResize=true
243
+ // 子节点在父节点中移动时,父节点会自动调整大小
244
+ // step1: 计算出当前child的bounds
245
+ var newX = x + deltaX / 2;
246
+ var newY = y + deltaY / 2;
247
+ var minX = newX - width / 2;
248
+ var minY = newY - height / 2;
249
+ var maxX = newX + width / 2;
250
+ var maxY = newY + height / 2;
251
+ // step2:比较当前child.bounds与parent.bounds的差异,比如child.minX<parent.minX,那么parent.minX=child.minX
252
+ var hasChange = false;
253
+ var groupBounds = groupModel.getBounds();
254
+ var newGroupBounds = Object.assign({}, groupBounds);
255
+ if (minX < newGroupBounds.minX) {
256
+ newGroupBounds.minX = minX;
257
+ hasChange = true;
258
+ }
259
+ if (minY < newGroupBounds.minY) {
260
+ newGroupBounds.minY = minY;
261
+ hasChange = true;
262
+ }
263
+ if (maxX > newGroupBounds.maxX) {
264
+ newGroupBounds.maxX = maxX;
265
+ hasChange = true;
266
+ }
267
+ if (maxY > newGroupBounds.maxY) {
268
+ newGroupBounds.maxY = maxY;
269
+ hasChange = true;
270
+ }
271
+ if (!hasChange) {
272
+ return;
273
+ }
274
+ // step3: 根据当前parent.bounds去计算出最新的x、y、width、height
275
+ var newGroupX = newGroupBounds.minX + (newGroupBounds.maxX - newGroupBounds.minX) / 2;
276
+ var newGroupY = newGroupBounds.minY + (newGroupBounds.maxY - newGroupBounds.minY) / 2;
277
+ var newGroupWidth = newGroupBounds.maxX - newGroupBounds.minX;
278
+ var newGroupHeight = newGroupBounds.maxY - newGroupBounds.minY;
279
+ groupModel.moveTo(newGroupX, newGroupY);
280
+ groupModel.width = newGroupWidth;
281
+ groupModel.height = newGroupHeight;
282
+ };
227
283
  this.onGraphRendered = function (_a) {
228
284
  var data = _a.data;
229
285
  console.log('data', data);
@@ -244,24 +300,6 @@ var DynamicGroup = /** @class */ (function () {
244
300
  // 初始化插件,从监听事件开始及设置规则开始
245
301
  this.init();
246
302
  }
247
- /**
248
- * 获取分组内的节点
249
- * @param groupModel
250
- */
251
- DynamicGroup.prototype.getNodesInGroup = function (groupModel) {
252
- var _this = this;
253
- var nodeIds = [];
254
- if (groupModel.isGroup) {
255
- (0, lodash_es_1.forEach)(Array.from(groupModel.children), function (nodeId) {
256
- nodeIds.push(nodeId);
257
- var nodeModel = _this.lf.getNodeModelById(nodeId);
258
- if (nodeModel === null || nodeModel === void 0 ? void 0 : nodeModel.isGroup) {
259
- nodeIds = nodeIds.concat(_this.getNodesInGroup(nodeModel));
260
- }
261
- });
262
- }
263
- return nodeIds;
264
- };
265
303
  /**
266
304
  * 获取节点所属的分组
267
305
  * @param nodeId
@@ -442,6 +480,46 @@ var DynamicGroup = /** @class */ (function () {
442
480
  }
443
481
  return this.lf.graphModel.addEdge(__assign(__assign({}, newEdgeConfig), { sourceNodeId: sourceId, targetNodeId: targetId }));
444
482
  };
483
+ /**
484
+ * 检测group:resize后的bounds是否会小于children的bounds
485
+ * 限制group进行resize时不能小于内部的占地面积
486
+ * @param groupModel
487
+ * @param deltaX
488
+ * @param deltaY
489
+ * @param newWidth
490
+ * @param newHeight
491
+ */
492
+ DynamicGroup.prototype.checkGroupBoundsWithChildren = function (groupModel, deltaX, deltaY, newWidth, newHeight) {
493
+ if (groupModel.children) {
494
+ var children = groupModel.children, x = groupModel.x, y = groupModel.y;
495
+ // 根据deltaX和deltaY计算出当前model的bounds
496
+ var newX = x + deltaX / 2;
497
+ var newY = y + deltaY / 2;
498
+ var groupMinX = newX - newWidth / 2;
499
+ var groupMinY = newY - newHeight / 2;
500
+ var groupMaxX = newX + newWidth / 2;
501
+ var groupMaxY = newY + newHeight / 2;
502
+ var childrenArray = Array.from(children);
503
+ for (var i = 0; i < childrenArray.length; i++) {
504
+ var childId = childrenArray[i];
505
+ var child = this.lf.getNodeModelById(childId);
506
+ if (!child) {
507
+ continue;
508
+ }
509
+ var childBounds = child.getBounds();
510
+ var minX = childBounds.minX, minY = childBounds.minY, maxX = childBounds.maxX, maxY = childBounds.maxY;
511
+ // parent:resize后的bounds不能小于child:bounds,否则阻止其resize
512
+ var canResize = groupMinX <= minX &&
513
+ groupMinY <= minY &&
514
+ groupMaxX >= maxX &&
515
+ groupMaxY >= maxY;
516
+ if (!canResize) {
517
+ return false;
518
+ }
519
+ }
520
+ }
521
+ return true;
522
+ };
445
523
  /**
446
524
  * Group 插件的初始化方法
447
525
  * TODO:1. 待讨论,可能之前插件分类是有意义的 components, material, tools
@@ -463,16 +541,38 @@ var DynamicGroup = /** @class */ (function () {
463
541
  graphModel.addNodeMoveRules(function (model, deltaX, deltaY) {
464
542
  // 判断如果是 group,移动时需要同时移动组内的所有节点
465
543
  if (model.isGroup) {
466
- var nodeIds = _this.getNodesInGroup(model);
467
- graphModel.moveNodes(nodeIds, deltaX, deltaY, true);
544
+ // https://github.com/didi/LogicFlow/issues/1826
545
+ // 这里不应该触发移动子节点的逻辑,这里是判断是否可以移动,而不是触发移动逻辑
546
+ // 而且这里触发移动,会导致resize操作的this.x变动也会触发子item的this.x变动
547
+ // resize时的deltaX跟正常移动的deltaX是不同的
548
+ // const nodeIds = this.getNodesInGroup(model as DynamicGroupNodeModel)
549
+ // graphModel.moveNodes(nodeIds, deltaX, deltaY, true)
468
550
  return true;
469
551
  }
470
552
  var groupId = _this.nodeGroupMap.get(model.id);
471
553
  var groupModel = _this.lf.getNodeModelById(groupId);
472
554
  if (groupModel && groupModel.isRestrict) {
473
- // 如果移动的节点存在与分组中,且这个分组禁止子节点移出去
474
- var groupBounds = groupModel.getBounds();
475
- return (0, utils_1.isAllowMoveTo)(groupBounds, model, deltaX, deltaY);
555
+ if (groupModel.autoResize) {
556
+ // 子节点在父节点中移动时,父节点会自动调整大小
557
+ // 在node:mousemove中进行父节点的调整
558
+ return true;
559
+ }
560
+ else {
561
+ // 如果移动的节点存在于某个分组中,且这个分组禁止子节点移出去
562
+ var groupBounds = groupModel.getBounds();
563
+ return (0, utils_1.isAllowMoveTo)(groupBounds, model, deltaX, deltaY);
564
+ }
565
+ }
566
+ return true;
567
+ });
568
+ // https://github.com/didi/LogicFlow/issues/1442
569
+ // https://github.com/didi/LogicFlow/issues/937
570
+ // 添加分组节点resize规则
571
+ // isRestrict限制模式下,当前model resize时不能小于children的占地面积
572
+ // 并且在isRestrict限制模式下,transformWidthContainer即使设置为true,也无效
573
+ graphModel.addNodeResizeRules(function (model, deltaX, deltaY, width, height) {
574
+ if (model.isGroup && model.isRestrict) {
575
+ return _this.checkGroupBoundsWithChildren(model, deltaX, deltaY, width, height);
476
576
  }
477
577
  return true;
478
578
  });
@@ -481,6 +581,7 @@ var DynamicGroup = /** @class */ (function () {
481
581
  lf.on('node:delete', this.removeNodeFromGroup);
482
582
  lf.on('node:drag,node:dnd-drag', this.setActiveGroup);
483
583
  lf.on('node:click', this.onNodeSelect);
584
+ lf.on('node:mousemove', this.onNodeMove);
484
585
  lf.on('graph:rendered', this.onGraphRendered);
485
586
  lf.on('graph:updated', function (_a) {
486
587
  var data = _a.data;
@@ -538,6 +639,7 @@ var DynamicGroup = /** @class */ (function () {
538
639
  this.lf.off('node:delete', this.removeNodeFromGroup);
539
640
  this.lf.off('node:drag,node:dnd-drag', this.setActiveGroup);
540
641
  this.lf.off('node:click', this.onNodeSelect);
642
+ this.lf.off('node:mousemove', this.onNodeMove);
541
643
  this.lf.off('graph:rendered', this.onGraphRendered);
542
644
  // 还原 lf.addElements 方法?
543
645
  // 移除 graphModel 上重写的 addNodeMoveRules 方法?
@@ -31,6 +31,10 @@ export type IGroupNodeProperties = {
31
31
  */
32
32
  collapsedWidth?: number;
33
33
  collapsedHeight?: number;
34
+ /**
35
+ * 缩放或旋转容器时,是否缩放或旋转组内节点
36
+ */
37
+ transformWithContainer?: boolean;
34
38
  /**
35
39
  * 当前分组元素的 zIndex
36
40
  */
@@ -45,6 +49,7 @@ export declare class DynamicGroupNodeModel extends RectNodeModel<IGroupNodePrope
45
49
  readonly isGroup = true;
46
50
  children: Set<string>;
47
51
  isRestrict: boolean;
52
+ autoResize: boolean;
48
53
  collapsible: boolean;
49
54
  expandWidth: number;
50
55
  expandHeight: number;
@@ -52,7 +57,7 @@ export declare class DynamicGroupNodeModel extends RectNodeModel<IGroupNodePrope
52
57
  collapsedHeight: number;
53
58
  isCollapsed: boolean;
54
59
  groupAddable: boolean;
55
- transformWidthContainer: boolean;
60
+ transformWithContainer: boolean;
56
61
  childrenLastCollapseStateDict: Map<string, boolean>;
57
62
  constructor(data: NodeConfig<IGroupNodeProperties>, graphModel: GraphModel);
58
63
  initNodeData(data: LogicFlow.NodeConfig<IGroupNodeProperties>): void;
@@ -74,6 +74,8 @@ var DynamicGroupNodeModel = /** @class */ (function (_super) {
74
74
  _this.isGroup = true;
75
75
  // 是否限制组内节点的移动范围。默认不限制 TODO: 完善该功能
76
76
  _this.isRestrict = false;
77
+ // isRestrict 模式启用时,如果同时设置 autoResize 为 true,那么子节点在父节点中移动时,父节点会自动调整大小
78
+ _this.autoResize = false;
77
79
  // 分组节点是否可以折叠
78
80
  _this.collapsible = true;
79
81
  // 当前组是否收起状态
@@ -81,7 +83,7 @@ var DynamicGroupNodeModel = /** @class */ (function (_super) {
81
83
  // 当前分组是否在可添加状态 - 实时状态
82
84
  _this.groupAddable = false;
83
85
  // 缩放或旋转容器时,是否缩放或旋转组内节点
84
- _this.transformWidthContainer = true;
86
+ _this.transformWithContainer = false;
85
87
  _this.childrenLastCollapseStateDict = new Map();
86
88
  _this.childrenLastCollapseStateDict = new Map();
87
89
  _this.initNodeData(data);
@@ -91,7 +93,7 @@ var DynamicGroupNodeModel = /** @class */ (function (_super) {
91
93
  DynamicGroupNodeModel.prototype.initNodeData = function (data) {
92
94
  var _a;
93
95
  _super.prototype.initNodeData.call(this, data);
94
- var _b = (_a = data.properties) !== null && _a !== void 0 ? _a : {}, children = _b.children, width = _b.width, height = _b.height, collapsedWidth = _b.collapsedWidth, collapsedHeight = _b.collapsedHeight, collapsible = _b.collapsible, isCollapsed = _b.isCollapsed, zIndex = _b.zIndex, isRestrict = _b.isRestrict, autoResize = _b.autoResize, autoToFront = _b.autoToFront;
96
+ var _b = (_a = data.properties) !== null && _a !== void 0 ? _a : {}, children = _b.children, width = _b.width, height = _b.height, collapsedWidth = _b.collapsedWidth, collapsedHeight = _b.collapsedHeight, collapsible = _b.collapsible, isCollapsed = _b.isCollapsed, zIndex = _b.zIndex, isRestrict = _b.isRestrict, autoResize = _b.autoResize, autoToFront = _b.autoToFront, transformWithContainer = _b.transformWithContainer;
95
97
  this.children = children ? new Set(children) : new Set();
96
98
  this.zIndex = zIndex !== null && zIndex !== void 0 ? zIndex : DEFAULT_BOTTOM_Z_INDEX;
97
99
  this.isCollapsed = isCollapsed !== null && isCollapsed !== void 0 ? isCollapsed : false;
@@ -105,6 +107,7 @@ var DynamicGroupNodeModel = /** @class */ (function (_super) {
105
107
  this.collapsedWidth = collapsedWidth !== null && collapsedWidth !== void 0 ? collapsedWidth : DEFAULT_GROUP_COLLAPSE_WIDTH;
106
108
  this.collapsedHeight = collapsedHeight !== null && collapsedHeight !== void 0 ? collapsedHeight : DEFAULT_GROUP_COLLAPSE_HEIGHT;
107
109
  this.isRestrict = isRestrict !== null && isRestrict !== void 0 ? isRestrict : false;
110
+ this.transformWithContainer = transformWithContainer !== null && transformWithContainer !== void 0 ? transformWithContainer : false;
108
111
  this.autoResize = autoResize !== null && autoResize !== void 0 ? autoResize : false;
109
112
  this.collapsible = collapsible !== null && collapsible !== void 0 ? collapsible : true;
110
113
  this.autoToFront = autoToFront !== null && autoToFront !== void 0 ? autoToFront : false;
@@ -145,8 +148,15 @@ var DynamicGroupNodeModel = /** @class */ (function (_super) {
145
148
  data.isGroup = true;
146
149
  var _a = this, x = _a.x, y = _a.y, collapsedWidth = _a.collapsedWidth, collapsedHeight = _a.collapsedHeight, expandWidth = _a.expandWidth, expandHeight = _a.expandHeight, isCollapsed = _a.isCollapsed;
147
150
  if (isCollapsed) {
151
+ // 如果当前是折叠模式
152
+ // 存入history的时候,将坐标恢复到未折叠前的坐标数据
153
+ // 因为拿出history数据的时候,会触发collapse()进行坐标的折叠计算
148
154
  data.x = x + expandWidth / 2 - collapsedWidth / 2;
149
155
  data.y = y + expandHeight / 2 - collapsedHeight / 2;
156
+ if (data.text) {
157
+ data.text.x = data.text.x + expandWidth / 2 - collapsedWidth / 2;
158
+ data.text.y = data.text.y + expandHeight / 2 - collapsedHeight / 2;
159
+ }
150
160
  }
151
161
  return data;
152
162
  };
@@ -411,7 +421,7 @@ var DynamicGroupNodeModel = /** @class */ (function (_super) {
411
421
  ], DynamicGroupNodeModel.prototype, "groupAddable", void 0);
412
422
  __decorate([
413
423
  core_1.observable
414
- ], DynamicGroupNodeModel.prototype, "transformWidthContainer", void 0);
424
+ ], DynamicGroupNodeModel.prototype, "transformWithContainer", void 0);
415
425
  return DynamicGroupNodeModel;
416
426
  }(core_1.RectNodeModel));
417
427
  exports.DynamicGroupNodeModel = DynamicGroupNodeModel;
@@ -6,6 +6,11 @@ export interface IDynamicGroupNodeProps {
6
6
  }
7
7
  export declare class DynamicGroupNode<P extends IDynamicGroupNodeProps = IDynamicGroupNodeProps> extends RectNode<P> {
8
8
  componentDidMount(): void;
9
+ /**
10
+ * 获取分组内的节点
11
+ * @param groupModel
12
+ */
13
+ getNodesInGroup(groupModel: DynamicGroupNodeModel, graphModel: GraphModel): string[];
9
14
  getResizeControl(): h.JSX.Element | null;
10
15
  getAppendAreaShape(): h.JSX.Element | null;
11
16
  getCollapseIcon(sx: number, sy: number): string;
@@ -29,7 +29,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.DynamicGroupNode = void 0;
30
30
  var core_1 = require("@logicflow/core");
31
31
  var lodash_es_1 = require("lodash-es");
32
- var resize_1 = require("@logicflow/core/es/util/resize");
33
32
  var utils_1 = require("../tools/label/utils");
34
33
  var DynamicGroupNode = /** @class */ (function (_super) {
35
34
  __extends(DynamicGroupNode, _super);
@@ -37,6 +36,7 @@ var DynamicGroupNode = /** @class */ (function (_super) {
37
36
  return _super !== null && _super.apply(this, arguments) || this;
38
37
  }
39
38
  DynamicGroupNode.prototype.componentDidMount = function () {
39
+ var _this = this;
40
40
  _super.prototype.componentDidMount.call(this);
41
41
  var _a = this.props, curGroup = _a.model, graphModel = _a.graphModel;
42
42
  var eventCenter = graphModel.eventCenter;
@@ -44,6 +44,16 @@ var DynamicGroupNode = /** @class */ (function (_super) {
44
44
  // 在 group 旋转时,对组内的所有子节点也进行对应的旋转计算
45
45
  eventCenter.on('node:rotate', function (_a) {
46
46
  var model = _a.model;
47
+ var _b = _this.props.model, transformWithContainer = _b.transformWithContainer, isRestrict = _b.isRestrict;
48
+ if (!transformWithContainer || isRestrict) {
49
+ // isRestrict限制模式下,当前model resize时不能小于占地面积
50
+ // 由于parent:resize=>child:resize计算复杂,需要根据child:resize的判定结果来递归判断parent能否resize
51
+ // 不符合目前 parent:resize成功后emit事件 -> 触发child:resize 的代码交互模式
52
+ // 因此isRestrict限制模式下不支持联动(parent:resize=>child:resize)
53
+ // 由于transformWidthContainer是控制rotate+resize,为保持transformWidthContainer本来的含义
54
+ // parent:resize=>child:resize不支持,那么parent:rotate=>child:rotate也不支持
55
+ return;
56
+ }
47
57
  // DONE: 目前操作是对分组内节点以节点中心旋转节点本身,而按照正常逻辑,应该是以分组中心,旋转节点(跟 Label 旋转操作逻辑一致)
48
58
  if (model.id === curGroup.id) {
49
59
  var center_1 = { x: curGroup.x, y: curGroup.y };
@@ -71,16 +81,30 @@ var DynamicGroupNode = /** @class */ (function (_super) {
71
81
  });
72
82
  // 在 group 缩放时,对组内的所有子节点也进行对应的缩放计算
73
83
  eventCenter.on('node:resize', function (_a) {
74
- var deltaX = _a.deltaX, deltaY = _a.deltaY, index = _a.index, model = _a.model;
75
- // TODO: 目前 Resize 的比例值有问题,导致缩放时,节点会变形,需要修复
84
+ var deltaX = _a.deltaX, deltaY = _a.deltaY, index = _a.index, model = _a.model, preData = _a.preData;
85
+ var _b = _this.props.model, transformWithContainer = _b.transformWithContainer, isRestrict = _b.isRestrict;
86
+ if (!transformWithContainer || isRestrict) {
87
+ // isRestrict限制模式下,当前model resize时不能小于占地面积
88
+ // 由于parent:resize=>child:resize计算复杂,需要根据child:resize的判定结果来递归判断parent能否resize
89
+ // 不符合目前 parent:resize成功后emit事件 -> 触发child:resize 的代码交互模式
90
+ // 因此isRestrict限制模式下不支持联动(parent:resize=>child:resize)
91
+ return;
92
+ }
76
93
  if (model.id === curGroup.id) {
94
+ // node:resize是group已经改变width和height后的回调
95
+ // 因此这里一定得用preData(没resize改变width之前的值),而不是data/model
96
+ var properties = preData.properties;
97
+ var _c = properties || {}, groupWidth_1 = _c.width, groupHeight_1 = _c.height;
77
98
  (0, lodash_es_1.forEach)(Array.from(curGroup.children), function (childId) {
78
99
  var child = graphModel.getNodeModelById(childId);
79
100
  if (child) {
101
+ // 根据比例去控制缩放dx和dy
102
+ var childDx = (child.width / groupWidth_1) * deltaX;
103
+ var childDy = (child.height / groupHeight_1) * deltaY;
80
104
  // child.rotate = model.rotate
81
- (0, resize_1.handleResize)({
82
- deltaX: deltaX,
83
- deltaY: deltaY,
105
+ (0, core_1.handleResize)({
106
+ deltaX: childDx,
107
+ deltaY: childDy,
84
108
  index: index,
85
109
  nodeModel: child,
86
110
  graphModel: graphModel,
@@ -90,6 +114,33 @@ var DynamicGroupNode = /** @class */ (function (_super) {
90
114
  });
91
115
  }
92
116
  });
117
+ // 在 group 移动时,对组内的所有子节点也进行对应的移动计算
118
+ eventCenter.on('node:mousemove', function (_a) {
119
+ var deltaX = _a.deltaX, deltaY = _a.deltaY, data = _a.data;
120
+ if (data.id === curGroup.id) {
121
+ var _b = _this.props, curGroup_1 = _b.model, graphModel_1 = _b.graphModel;
122
+ var nodeIds = _this.getNodesInGroup(curGroup_1, graphModel_1);
123
+ graphModel_1.moveNodes(nodeIds, deltaX, deltaY, true);
124
+ }
125
+ });
126
+ };
127
+ /**
128
+ * 获取分组内的节点
129
+ * @param groupModel
130
+ */
131
+ DynamicGroupNode.prototype.getNodesInGroup = function (groupModel, graphModel) {
132
+ var _this = this;
133
+ var nodeIds = [];
134
+ if (groupModel.isGroup) {
135
+ (0, lodash_es_1.forEach)(Array.from(groupModel.children), function (nodeId) {
136
+ nodeIds.push(nodeId);
137
+ var nodeModel = graphModel.getNodeModelById(nodeId);
138
+ if (nodeModel === null || nodeModel === void 0 ? void 0 : nodeModel.isGroup) {
139
+ nodeIds = nodeIds.concat(_this.getNodesInGroup(nodeModel, graphModel));
140
+ }
141
+ });
142
+ }
143
+ return nodeIds;
93
144
  };
94
145
  DynamicGroupNode.prototype.getResizeControl = function () {
95
146
  var _a = this.props.model, resizable = _a.resizable, isCollapsed = _a.isCollapsed;
@@ -81,7 +81,7 @@ var Label = /** @class */ (function () {
81
81
  }
82
82
  else if (typeof curLabelConfig === 'string' || !curLabelConfig) {
83
83
  // 3. 字符串或者为空的话就是 string 类型,基于 text 的数据合成 LabelConfig 信息(主要复用 text 的 x,y 信息)
84
- var config = __assign(__assign({}, text), { content: curLabelConfig || text.value, draggable: element.BaseType === 'edge' ? edgeTextDraggable : nodeTextDraggable });
84
+ var config = __assign(__assign({}, text), { content: curLabelConfig || (text === null || text === void 0 ? void 0 : text.value), draggable: element.BaseType === 'edge' ? edgeTextDraggable : nodeTextDraggable });
85
85
  formatConfig = config.value ? [config] : [];
86
86
  }
87
87
  // TODO: 针对 Edge,在 edge 更新时 重新计算 Label 的位置
@@ -239,7 +239,7 @@ var Label = /** @class */ (function () {
239
239
  model.setProperty('_label', newLabelConfig);
240
240
  });
241
241
  // 监听元素新增事件,元素label格式化
242
- eventCenter.on('node:add,edge:add', function (_a) {
242
+ eventCenter.on('node:dnd-add,node:add,edge:add', function (_a) {
243
243
  var data = _a.data;
244
244
  var element = graphModel.getElement(data.id);
245
245
  if (element) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logicflow/extension",
3
- "version": "2.0.8",
3
+ "version": "2.0.10",
4
4
  "description": "LogicFlow Extensions",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -20,7 +20,7 @@
20
20
  "author": "Logicflow-Team",
21
21
  "license": "Apache-2.0",
22
22
  "peerDependencies": {
23
- "@logicflow/core": "2.0.4"
23
+ "@logicflow/core": "2.0.6"
24
24
  },
25
25
  "dependencies": {
26
26
  "@antv/hierarchy": "^0.6.11",
@@ -31,7 +31,7 @@
31
31
  "preact": "^10.17.1",
32
32
  "rangy": "^1.3.1",
33
33
  "vanilla-picker": "^2.12.3",
34
- "@logicflow/core": "2.0.4"
34
+ "@logicflow/core": "2.0.6"
35
35
  },
36
36
  "devDependencies": {
37
37
  "less": "^4.1.1",
@@ -50,27 +50,6 @@ export class DynamicGroup {
50
50
  this.init()
51
51
  }
52
52
 
53
- /**
54
- * 获取分组内的节点
55
- * @param groupModel
56
- */
57
- getNodesInGroup(groupModel: DynamicGroupNodeModel): string[] {
58
- let nodeIds: string[] = []
59
- if (groupModel.isGroup) {
60
- forEach(Array.from(groupModel.children), (nodeId: string) => {
61
- nodeIds.push(nodeId)
62
-
63
- const nodeModel = this.lf.getNodeModelById(nodeId)
64
- if (nodeModel?.isGroup) {
65
- nodeIds = nodeIds.concat(
66
- this.getNodesInGroup(nodeModel as DynamicGroupNodeModel),
67
- )
68
- }
69
- })
70
- }
71
- return nodeIds
72
- }
73
-
74
53
  /**
75
54
  * 获取节点所属的分组
76
55
  * @param nodeId
@@ -360,6 +339,72 @@ export class DynamicGroup {
360
339
  }
361
340
  }
362
341
 
342
+ onNodeMove = ({
343
+ deltaX,
344
+ deltaY,
345
+ data,
346
+ }: Omit<CallbackArgs<'node:mousemove'>, 'e' | 'position'>) => {
347
+ const { id, x, y, properties } = data
348
+ if (!properties) {
349
+ return
350
+ }
351
+ const { width, height } = properties
352
+ const groupId = this.nodeGroupMap.get(id)
353
+ if (!groupId) {
354
+ return
355
+ }
356
+ const groupModel = this.lf.getNodeModelById(
357
+ groupId,
358
+ ) as DynamicGroupNodeModel
359
+
360
+ if (!groupModel || !groupModel.isRestrict || !groupModel.autoResize) {
361
+ return
362
+ }
363
+ // 当父节点isRestrict=true & autoResize=true
364
+ // 子节点在父节点中移动时,父节点会自动调整大小
365
+
366
+ // step1: 计算出当前child的bounds
367
+ const newX = x + deltaX / 2
368
+ const newY = y + deltaY / 2
369
+ const minX = newX - width! / 2
370
+ const minY = newY - height! / 2
371
+ const maxX = newX + width! / 2
372
+ const maxY = newY + height! / 2
373
+ // step2:比较当前child.bounds与parent.bounds的差异,比如child.minX<parent.minX,那么parent.minX=child.minX
374
+ let hasChange = false
375
+ const groupBounds = groupModel.getBounds()
376
+ const newGroupBounds = Object.assign({}, groupBounds)
377
+ if (minX < newGroupBounds.minX) {
378
+ newGroupBounds.minX = minX
379
+ hasChange = true
380
+ }
381
+ if (minY < newGroupBounds.minY) {
382
+ newGroupBounds.minY = minY
383
+ hasChange = true
384
+ }
385
+ if (maxX > newGroupBounds.maxX) {
386
+ newGroupBounds.maxX = maxX
387
+ hasChange = true
388
+ }
389
+ if (maxY > newGroupBounds.maxY) {
390
+ newGroupBounds.maxY = maxY
391
+ hasChange = true
392
+ }
393
+ if (!hasChange) {
394
+ return
395
+ }
396
+ // step3: 根据当前parent.bounds去计算出最新的x、y、width、height
397
+ const newGroupX =
398
+ newGroupBounds.minX + (newGroupBounds.maxX - newGroupBounds.minX) / 2
399
+ const newGroupY =
400
+ newGroupBounds.minY + (newGroupBounds.maxY - newGroupBounds.minY) / 2
401
+ const newGroupWidth = newGroupBounds.maxX - newGroupBounds.minX
402
+ const newGroupHeight = newGroupBounds.maxY - newGroupBounds.minY
403
+ groupModel.moveTo(newGroupX, newGroupY)
404
+ groupModel.width = newGroupWidth
405
+ groupModel.height = newGroupHeight
406
+ }
407
+
363
408
  onGraphRendered = ({ data }: CallbackArgs<'graph:rendered'>) => {
364
409
  console.log('data', data)
365
410
  forEach(data.nodes, (node) => {
@@ -472,6 +517,56 @@ export class DynamicGroup {
472
517
  })
473
518
  }
474
519
 
520
+ /**
521
+ * 检测group:resize后的bounds是否会小于children的bounds
522
+ * 限制group进行resize时不能小于内部的占地面积
523
+ * @param groupModel
524
+ * @param deltaX
525
+ * @param deltaY
526
+ * @param newWidth
527
+ * @param newHeight
528
+ */
529
+ checkGroupBoundsWithChildren(
530
+ groupModel: DynamicGroupNodeModel,
531
+ deltaX: number,
532
+ deltaY: number,
533
+ newWidth: number,
534
+ newHeight: number,
535
+ ) {
536
+ if (groupModel.children) {
537
+ const { children, x, y } = groupModel
538
+ // 根据deltaX和deltaY计算出当前model的bounds
539
+ const newX = x + deltaX / 2
540
+ const newY = y + deltaY / 2
541
+ const groupMinX = newX - newWidth / 2
542
+ const groupMinY = newY - newHeight / 2
543
+ const groupMaxX = newX + newWidth / 2
544
+ const groupMaxY = newY + newHeight / 2
545
+
546
+ const childrenArray = Array.from(children)
547
+ for (let i = 0; i < childrenArray.length; i++) {
548
+ const childId = childrenArray[i]
549
+ const child = this.lf.getNodeModelById(childId)
550
+ if (!child) {
551
+ continue
552
+ }
553
+ const childBounds = child.getBounds()
554
+ const { minX, minY, maxX, maxY } = childBounds
555
+ // parent:resize后的bounds不能小于child:bounds,否则阻止其resize
556
+ const canResize =
557
+ groupMinX <= minX &&
558
+ groupMinY <= minY &&
559
+ groupMaxX >= maxX &&
560
+ groupMaxY >= maxY
561
+ if (!canResize) {
562
+ return false
563
+ }
564
+ }
565
+ }
566
+
567
+ return true
568
+ }
569
+
475
570
  /**
476
571
  * Group 插件的初始化方法
477
572
  * TODO:1. 待讨论,可能之前插件分类是有意义的 components, material, tools
@@ -492,8 +587,13 @@ export class DynamicGroup {
492
587
  graphModel.addNodeMoveRules((model, deltaX, deltaY) => {
493
588
  // 判断如果是 group,移动时需要同时移动组内的所有节点
494
589
  if (model.isGroup) {
495
- const nodeIds = this.getNodesInGroup(model as DynamicGroupNodeModel)
496
- graphModel.moveNodes(nodeIds, deltaX, deltaY, true)
590
+ // https://github.com/didi/LogicFlow/issues/1826
591
+ // 这里不应该触发移动子节点的逻辑,这里是判断是否可以移动,而不是触发移动逻辑
592
+ // 而且这里触发移动,会导致resize操作的this.x变动也会触发子item的this.x变动
593
+ // resize时的deltaX跟正常移动的deltaX是不同的
594
+
595
+ // const nodeIds = this.getNodesInGroup(model as DynamicGroupNodeModel)
596
+ // graphModel.moveNodes(nodeIds, deltaX, deltaY, true)
497
597
  return true
498
598
  }
499
599
 
@@ -503,19 +603,45 @@ export class DynamicGroup {
503
603
  ) as DynamicGroupNodeModel
504
604
 
505
605
  if (groupModel && groupModel.isRestrict) {
506
- // 如果移动的节点存在与分组中,且这个分组禁止子节点移出去
507
- const groupBounds = groupModel.getBounds()
508
- return isAllowMoveTo(groupBounds, model, deltaX, deltaY)
606
+ if (groupModel.autoResize) {
607
+ // 子节点在父节点中移动时,父节点会自动调整大小
608
+ // 在node:mousemove中进行父节点的调整
609
+ return true
610
+ } else {
611
+ // 如果移动的节点存在于某个分组中,且这个分组禁止子节点移出去
612
+ const groupBounds = groupModel.getBounds()
613
+ return isAllowMoveTo(groupBounds, model, deltaX, deltaY)
614
+ }
509
615
  }
510
616
 
511
617
  return true
512
618
  })
619
+
620
+ // https://github.com/didi/LogicFlow/issues/1442
621
+ // https://github.com/didi/LogicFlow/issues/937
622
+ // 添加分组节点resize规则
623
+ // isRestrict限制模式下,当前model resize时不能小于children的占地面积
624
+ // 并且在isRestrict限制模式下,transformWidthContainer即使设置为true,也无效
625
+ graphModel.addNodeResizeRules((model, deltaX, deltaY, width, height) => {
626
+ if (model.isGroup && model.isRestrict) {
627
+ return this.checkGroupBoundsWithChildren(
628
+ model as DynamicGroupNodeModel,
629
+ deltaX,
630
+ deltaY,
631
+ width,
632
+ height,
633
+ )
634
+ }
635
+ return true
636
+ })
637
+
513
638
  graphModel.dynamicGroup = this
514
639
 
515
640
  lf.on('node:add,node:drop,node:dnd-add', this.addNodeToGroup)
516
641
  lf.on('node:delete', this.removeNodeFromGroup)
517
642
  lf.on('node:drag,node:dnd-drag', this.setActiveGroup)
518
643
  lf.on('node:click', this.onNodeSelect)
644
+ lf.on('node:mousemove', this.onNodeMove)
519
645
  lf.on('graph:rendered', this.onGraphRendered)
520
646
 
521
647
  lf.on('graph:updated', ({ data }) => console.log('data', data))
@@ -584,6 +710,7 @@ export class DynamicGroup {
584
710
  this.lf.off('node:delete', this.removeNodeFromGroup)
585
711
  this.lf.off('node:drag,node:dnd-drag', this.setActiveGroup)
586
712
  this.lf.off('node:click', this.onNodeSelect)
713
+ this.lf.off('node:mousemove', this.onNodeMove)
587
714
  this.lf.off('graph:rendered', this.onGraphRendered)
588
715
 
589
716
  // 还原 lf.addElements 方法?