@logicflow/extension 1.1.0-alpha.3 → 1.1.0-alpha.4

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.
@@ -0,0 +1,213 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __read = (this && this.__read) || function (o, n) {
14
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
15
+ if (!m) return o;
16
+ var i = m.call(o), r, ar = [], e;
17
+ try {
18
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
+ }
20
+ catch (error) { e = { error: error }; }
21
+ finally {
22
+ try {
23
+ if (r && !r.done && (m = i["return"])) m.call(i);
24
+ }
25
+ finally { if (e) throw e.error; }
26
+ }
27
+ return ar;
28
+ };
29
+ var __spread = (this && this.__spread) || function () {
30
+ for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
31
+ return ar;
32
+ };
33
+ Object.defineProperty(exports, "__esModule", { value: true });
34
+ exports.GroupShrink = void 0;
35
+ var lodash_es_1 = require("lodash-es");
36
+ var GroupShrink = /** @class */ (function () {
37
+ function GroupShrink(_a) {
38
+ var _this = this;
39
+ var lf = _a.lf;
40
+ var _b;
41
+ this.group = null; // group节点
42
+ this.lf = null; // lf 实例
43
+ this.shrinkWidth = 100; // 收缩后节点宽度
44
+ this.shrinkHeight = 60; // 收缩后节点高度
45
+ this.getGraphDataWithGroup = function () {
46
+ var _a = _this.lf.graphModel.modelToGraphData(), nodes = _a.nodes, edges = _a.edges;
47
+ var groupNodes = [];
48
+ var groupEdges = [];
49
+ var shrinkedGroupIds = [];
50
+ nodes.forEach(function (node) {
51
+ if (node.type === 'group' && node.properties.shrinkProperty && node.properties.shrinkProperty) {
52
+ var _a = node.properties.shrinkProperty, shrinked = _a.shrinked, groupNode = _a.groupNode, innerNodes = _a.innerNodes, innerEdges = _a.innerEdges;
53
+ if (shrinked) {
54
+ // 是收缩状态下的分组节点,需要恢复原来的节点和边
55
+ node = groupNode;
56
+ groupNodes = __spread(groupNodes, innerNodes);
57
+ groupEdges = __spread(groupEdges, innerEdges);
58
+ shrinkedGroupIds.push(groupNode.id);
59
+ }
60
+ }
61
+ });
62
+ // 移除与收缩分组节点相连的边
63
+ if (shrinkedGroupIds.length) {
64
+ lodash_es_1.remove(edges, function (edge) {
65
+ var sourceNodeId = edge.sourceNodeId, targetNodeId = edge.targetNodeId;
66
+ return shrinkedGroupIds.indexOf(sourceNodeId) > -1
67
+ || shrinkedGroupIds.indexOf(targetNodeId) > -1;
68
+ });
69
+ }
70
+ return {
71
+ nodes: __spread(nodes, groupNodes),
72
+ edges: __spread(edges, groupEdges),
73
+ };
74
+ };
75
+ this.lf = lf;
76
+ this.lf.extension = __assign(__assign({}, ((_b = this.lf.extension) !== null && _b !== void 0 ? _b : {})), { groupShrink: this });
77
+ this.lf.getGraphData = this.getGraphDataWithGroup;
78
+ }
79
+ /**
80
+ * 收缩
81
+ */
82
+ GroupShrink.prototype.startShrink = function (group) {
83
+ var _this = this;
84
+ if (group.type !== 'group') {
85
+ throw new Error('Only groupNode can be shrinked!');
86
+ }
87
+ this.group = group;
88
+ var nodeModel = this.lf.getNodeModelById(group.id);
89
+ var shrinkConfig = nodeModel.getProperties().shrinkProperty;
90
+ if (shrinkConfig && shrinkConfig.shrinked) {
91
+ throw new Error('GroupNode which is shrinked cannot be shrinked again!');
92
+ }
93
+ var shrinkProperty = {
94
+ shrinked: true,
95
+ groupNode: __assign({}, this.group),
96
+ innerNodes: [],
97
+ innerEdges: [],
98
+ };
99
+ var newEdges = []; // 需要重新生成的边
100
+ var innerNodes = []; // 分组内节点
101
+ var innerEdges = []; // 分组内部边
102
+ var minX = null;
103
+ var minY = null;
104
+ // 处理分组内的节点和边
105
+ if (this.group && this.group.children && this.group.children.length) {
106
+ var edgesInfo = this.getGroupEdges();
107
+ newEdges = edgesInfo.newEdges;
108
+ innerEdges = edgesInfo.innerEdges;
109
+ var nodesInfo = this.getGroupNodes();
110
+ innerNodes = nodesInfo.innerNodes; // 分组内节点信息
111
+ minX = nodesInfo.minX; // 左上角节点的x
112
+ minY = nodesInfo.minY; // 左上角节点的y
113
+ }
114
+ shrinkProperty.innerNodes = innerNodes;
115
+ shrinkProperty.innerEdges = innerEdges;
116
+ this.lf.setProperties(this.group.id, { shrinkProperty: shrinkProperty }); // 分组节点收缩前状态、分组内节点、分组内边存入properties
117
+ // 收缩后的分组节点移动到分组内左上角的节点位置
118
+ nodeModel.x = minX || this.group.x;
119
+ nodeModel.y = minY || this.group.y;
120
+ // 收缩,调整节点大小
121
+ nodeModel.width = this.shrinkWidth;
122
+ nodeModel.height = this.shrinkHeight;
123
+ // 生成与分组节点相连的边
124
+ newEdges.forEach(function (edgeConfig) {
125
+ _this.lf.addEdge(edgeConfig);
126
+ });
127
+ };
128
+ /**
129
+ * 展开
130
+ */
131
+ GroupShrink.prototype.startExpand = function (group) {
132
+ var _this = this;
133
+ this.group = group;
134
+ var nodeModel = this.lf.getNodeModelById(group.id);
135
+ var _a = nodeModel.getProperties().shrinkProperty, shrinkProperty = _a === void 0 ? {} : _a;
136
+ var shrinked = shrinkProperty.shrinked, groupNode = shrinkProperty.groupNode, innerNodes = shrinkProperty.innerNodes, innerEdges = shrinkProperty.innerEdges;
137
+ if (!shrinked) {
138
+ throw new Error('GroupNode which is not shrinked cannot be expanded');
139
+ }
140
+ // 重新渲染分组节点
141
+ this.lf.deleteNode(group.id);
142
+ this.lf.addNode(groupNode);
143
+ // 恢复分组内的节点
144
+ innerNodes.forEach(function (item) {
145
+ _this.lf.addNode(item);
146
+ });
147
+ // 恢复分组内的节点上所有边
148
+ innerEdges.forEach(function (item) {
149
+ _this.lf.addEdge(item);
150
+ });
151
+ // 修改properties
152
+ this.lf.setProperties(group.id, { shrinkProperty: { shrinked: false } });
153
+ };
154
+ /**
155
+ * 获取分组内的节点上的所有边,以及计算新的分组节点需要连接的边,之后删除原来的边
156
+ */
157
+ GroupShrink.prototype.getGroupEdges = function () {
158
+ var _this = this;
159
+ var edges = this.lf.graphModel.modelToGraphData().edges;
160
+ var children = this.group.children;
161
+ var innerEdges = [];
162
+ var newEdges = [];
163
+ edges.forEach(function (item) {
164
+ var startInGroup = children.indexOf(item.sourceNodeId) > -1;
165
+ var endInGroup = children.indexOf(item.targetNodeId) > -1;
166
+ if (startInGroup || endInGroup) {
167
+ innerEdges.push(item);
168
+ if (startInGroup && !endInGroup) {
169
+ // 从分组内向外的边
170
+ newEdges.push({
171
+ sourceNodeId: _this.group.id,
172
+ targetNodeId: item.targetNodeId,
173
+ });
174
+ }
175
+ if (!startInGroup && endInGroup) {
176
+ // 从外部指向分组内的
177
+ newEdges.push({
178
+ sourceNodeId: item.sourceNodeId,
179
+ targetNodeId: _this.group.id,
180
+ });
181
+ }
182
+ _this.lf.deleteEdge(item.id);
183
+ }
184
+ });
185
+ return { innerEdges: innerEdges, newEdges: newEdges };
186
+ };
187
+ /**
188
+ * 获取分组内的节点和左上角节点,获取到有效信息后,删除节点
189
+ */
190
+ GroupShrink.prototype.getGroupNodes = function () {
191
+ var _this = this;
192
+ var innerNodes = [];
193
+ var nodes = this.lf.graphModel.modelToGraphData().nodes;
194
+ var children = this.group.children;
195
+ var minX = null;
196
+ var minY = null;
197
+ // 遍历所有节点,在分组内的, 暂存&删除
198
+ nodes.forEach(function (item) {
199
+ if (children.indexOf(item.id) > -1) {
200
+ innerNodes.push(item);
201
+ if ((!minX || item.x < minX) && (!minY || item.y < minY)) {
202
+ minX = item.x;
203
+ minY = item.y;
204
+ }
205
+ _this.lf.deleteNode(item.id);
206
+ }
207
+ });
208
+ return { innerNodes: innerNodes, minX: minX, minY: minY };
209
+ };
210
+ GroupShrink.pluginName = 'group-shrink';
211
+ return GroupShrink;
212
+ }());
213
+ exports.GroupShrink = GroupShrink;
package/cjs/index.js CHANGED
@@ -29,3 +29,4 @@ __exportStar(require("./tools/flow-path"), exports);
29
29
  __exportStar(require("./tools/auto-layout"), exports);
30
30
  __exportStar(require("./bpmn-adapter/xml2json"), exports);
31
31
  __exportStar(require("./bpmn-adapter/json2xml"), exports);
32
+ __exportStar(require("./group-shrink"), exports);
@@ -46,11 +46,18 @@ var __spread = (this && this.__spread) || function () {
46
46
  Object.defineProperty(exports, "__esModule", { value: true });
47
47
  var core_1 = require("@logicflow/core");
48
48
  var NodeResize_1 = require("../../NodeResize");
49
+ var defaultWidth = 500;
50
+ var defaultHeight = 300;
49
51
  var GroupNodeModel = /** @class */ (function (_super) {
50
52
  __extends(GroupNodeModel, _super);
51
53
  function GroupNodeModel() {
52
54
  var _this = _super !== null && _super.apply(this, arguments) || this;
53
55
  _this.isGroup = true;
56
+ // isFolded: boolean;
57
+ _this.foldedWidth = 100;
58
+ _this.foldedHeight = 40;
59
+ _this.unfoldedWidth = defaultWidth;
60
+ _this.unfoldedHight = defaultHeight;
54
61
  return _this;
55
62
  }
56
63
  GroupNodeModel.prototype.initNodeData = function (data) {
@@ -61,8 +68,8 @@ var GroupNodeModel = /** @class */ (function (_super) {
61
68
  }
62
69
  // 初始化组的子节点
63
70
  this.children = new Set(children);
64
- this.width = 500;
65
- this.height = 200;
71
+ this.width = defaultWidth;
72
+ this.height = defaultHeight;
66
73
  // todo: 参考bpmn.js, 分组和未加入分组的节点重合时,未加入分组的节点在分组之下。方便标识。
67
74
  this.zIndex = -1;
68
75
  this.radius = 0;
@@ -70,6 +77,86 @@ var GroupNodeModel = /** @class */ (function (_super) {
70
77
  this.text.draggable = false;
71
78
  this.isRestrict = false;
72
79
  this.resizable = false;
80
+ this.autoToFront = false;
81
+ this.isFolded = false;
82
+ this.properties.isFolded = false;
83
+ };
84
+ GroupNodeModel.prototype.foldGroup = function (isFolded) {
85
+ var _this = this;
86
+ this.setProperty('isFolded', isFolded);
87
+ if (isFolded) {
88
+ this.x = this.x - this.width / 2 + this.foldedWidth / 2;
89
+ this.y = this.y - this.height / 2 + this.foldedHeight / 2;
90
+ this.unfoldedWidth = this.width;
91
+ this.unfoldedHight = this.height;
92
+ this.width = this.foldedWidth;
93
+ this.height = this.foldedHeight;
94
+ }
95
+ else {
96
+ this.width = this.unfoldedWidth;
97
+ this.height = this.unfoldedHight;
98
+ this.x = this.x + this.width / 2 - this.foldedWidth / 2;
99
+ this.y = this.y + this.height / 2 - this.foldedHeight / 2;
100
+ }
101
+ // 移动分组上的连线
102
+ var inCommingEdges = this.graphModel.getNodeIncomingEdge(this.id);
103
+ var outgoingEdges = this.graphModel.getNodeOutgoingEdge(this.id);
104
+ inCommingEdges.concat(outgoingEdges).forEach(function (edgeModel) {
105
+ _this.graphModel.deleteEdgeById(edgeModel.id);
106
+ if (!edgeModel.isFoldedEdge) {
107
+ var isCommingEdge = edgeModel.targetNodeId === _this.id;
108
+ var data = edgeModel.getData();
109
+ if (isCommingEdge) {
110
+ data.endPoint = undefined;
111
+ }
112
+ else {
113
+ data.startPoint = undefined;
114
+ }
115
+ data.pointsList = undefined;
116
+ _this.graphModel.addEdge(data);
117
+ }
118
+ });
119
+ this.children.forEach(function (elementId) {
120
+ var nodeModel = _this.graphModel.getElement(elementId);
121
+ nodeModel.visible = !isFolded;
122
+ _this.foldEdge(elementId, isFolded);
123
+ });
124
+ };
125
+ /**
126
+ * 折叠分组的时候,处理分组内部子节点上的连线
127
+ * 1. 为了保证校验规则不被打乱,所以只隐藏子节点上面的连线。
128
+ * 2. 重新创建一个属性一样的边。
129
+ * 3. 这个边拥有virtual=true的属性,表示不支持直接修改此边内容。
130
+ */
131
+ GroupNodeModel.prototype.foldEdge = function (nodeId, isFolded) {
132
+ var _this = this;
133
+ var inCommingEdges = this.graphModel.getNodeIncomingEdge(nodeId);
134
+ var outgoingEdges = this.graphModel.getNodeOutgoingEdge(nodeId);
135
+ inCommingEdges.concat(outgoingEdges).forEach(function (edgeModel, index) {
136
+ var _a;
137
+ edgeModel.visible = !isFolded;
138
+ var isCommingEdge = edgeModel.targetNodeId === nodeId;
139
+ if (isFolded) {
140
+ var data = edgeModel.getData();
141
+ data.id = data.id + "__" + index;
142
+ if (isCommingEdge) {
143
+ data.endPoint = undefined;
144
+ data.targetNodeId = _this.id;
145
+ }
146
+ else {
147
+ data.startPoint = undefined;
148
+ data.sourceNodeId = _this.id;
149
+ }
150
+ data.text = (_a = data.text) === null || _a === void 0 ? void 0 : _a.value;
151
+ data.pointsList = undefined;
152
+ var model = _this.graphModel.addEdge(data);
153
+ model.virtual = true;
154
+ // 强制不保存group连线数据
155
+ model.getData = function () { return null; };
156
+ model.text.editable = false;
157
+ model.isFoldedEdge = true;
158
+ }
159
+ });
73
160
  };
74
161
  GroupNodeModel.prototype.isInRange = function (_a) {
75
162
  var x1 = _a.x1, y1 = _a.y1, x2 = _a.x2, y2 = _a.y2;
@@ -108,6 +195,7 @@ var GroupNodeModel = /** @class */ (function (_super) {
108
195
  data.children = __spread(this.children);
109
196
  var properties = data.properties;
110
197
  delete properties.groupAddable;
198
+ delete properties.isFolded;
111
199
  return data;
112
200
  };
113
201
  return GroupNodeModel;
@@ -117,10 +205,6 @@ var GroupNode = /** @class */ (function (_super) {
117
205
  function GroupNode() {
118
206
  return _super !== null && _super.apply(this, arguments) || this;
119
207
  }
120
- /**
121
- * 重新toFront,阻止其置顶
122
- */
123
- GroupNode.prototype.toFront = function () { };
124
208
  GroupNode.prototype.getControlGroup = function () {
125
209
  return this.props.model.resizable ? _super.prototype.getControlGroup.call(this) : null;
126
210
  };
@@ -134,10 +218,43 @@ var GroupNode = /** @class */ (function (_super) {
134
218
  var newHeight = height + strokeWidth + 8;
135
219
  return core_1.h('rect', __assign(__assign({}, style), { width: newWidth, height: newHeight, x: x - newWidth / 2, y: y - newHeight / 2, rx: radius, ry: radius }));
136
220
  };
221
+ GroupNode.prototype.getFoldIcon = function () {
222
+ var model = this.props.model;
223
+ var foldX = model.x - model.width / 2 + 5;
224
+ var foldY = model.y - model.height / 2 + 5;
225
+ var iconIcon = core_1.h('path', {
226
+ fill: 'none',
227
+ stroke: '#818281',
228
+ strokeWidth: 2,
229
+ 'pointer-events': 'none',
230
+ d: model.properties.isFolded
231
+ ? "M " + (foldX + 3) + "," + (foldY + 6) + " " + (foldX + 11) + "," + (foldY + 6) + " M" + (foldX + 7) + "," + (foldY + 2) + " " + (foldX + 7) + "," + (foldY + 10)
232
+ : "M " + (foldX + 3) + "," + (foldY + 6) + " " + (foldX + 11) + "," + (foldY + 6) + " ",
233
+ });
234
+ return core_1.h('g', {}, [
235
+ core_1.h('rect', {
236
+ height: 12,
237
+ width: 14,
238
+ rx: 2,
239
+ ry: 2,
240
+ strokeWidth: 1,
241
+ fill: '#F4F5F6',
242
+ stroke: '#CECECE',
243
+ cursor: 'pointer',
244
+ x: model.x - model.width / 2 + 5,
245
+ y: model.y - model.height / 2 + 5,
246
+ onClick: function () {
247
+ model.foldGroup(!model.properties.isFolded);
248
+ },
249
+ }),
250
+ iconIcon,
251
+ ]);
252
+ };
137
253
  GroupNode.prototype.getResizeShape = function () {
138
254
  return core_1.h('g', {}, [
139
255
  this.getAddedableShape(),
140
256
  _super.prototype.getResizeShape.call(this),
257
+ this.getFoldIcon(),
141
258
  ]);
142
259
  };
143
260
  return GroupNode;
@@ -9,21 +9,25 @@ var Snapshot = {
9
9
  install: function (lf) {
10
10
  var _this = this;
11
11
  this.lf = lf;
12
- this.offsetX = Number.MAX_SAFE_INTEGER;
13
- this.offsetY = Number.MAX_SAFE_INTEGER;
14
12
  /* 下载快照 */
15
13
  lf.getSnapshot = function (fileName, backgroundColor) {
14
+ _this.offsetX = Number.MAX_SAFE_INTEGER;
15
+ _this.offsetY = Number.MAX_SAFE_INTEGER;
16
16
  _this.fileName = fileName || "logic-flow." + Date.now() + ".png";
17
17
  var svgRootElement = _this.getSvgRootElement(lf);
18
18
  _this.downloadSvg(svgRootElement, _this.fileName, backgroundColor);
19
19
  };
20
20
  /* 获取Blob对象,用户图片上传 */
21
21
  lf.getSnapshotBlob = function (backgroundColor) {
22
+ _this.offsetX = Number.MAX_SAFE_INTEGER;
23
+ _this.offsetY = Number.MAX_SAFE_INTEGER;
22
24
  var svgRootElement = _this.getSvgRootElement(lf);
23
25
  return _this.getBlob(svgRootElement, backgroundColor);
24
26
  };
25
27
  /* 获取Base64对象,用户图片上传 */
26
28
  lf.getSnapshotBase64 = function (backgroundColor) {
29
+ _this.offsetX = Number.MAX_SAFE_INTEGER;
30
+ _this.offsetY = Number.MAX_SAFE_INTEGER;
27
31
  var svgRootElement = _this.getSvgRootElement(lf);
28
32
  return _this.getBase64(svgRootElement, backgroundColor);
29
33
  };
@@ -175,6 +179,7 @@ var Snapshot = {
175
179
  canvas.width = bboxWidth * dpr + 80;
176
180
  canvas.height = bboxHeight * dpr + 80;
177
181
  var ctx = canvas.getContext('2d');
182
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
178
183
  ctx.scale(dpr, dpr);
179
184
  // 如果有背景色,设置流程图导出的背景色
180
185
  if (backgroundColor) {
@@ -0,0 +1,28 @@
1
+ declare class GroupShrink {
2
+ static pluginName: string;
3
+ group: any;
4
+ lf: any;
5
+ shrinkWidth: number;
6
+ shrinkHeight: number;
7
+ constructor({ lf }: {
8
+ lf: any;
9
+ });
10
+ /**
11
+ * 收缩
12
+ */
13
+ startShrink(group: any): void;
14
+ /**
15
+ * 展开
16
+ */
17
+ startExpand(group: any): void;
18
+ /**
19
+ * 获取分组内的节点上的所有边,以及计算新的分组节点需要连接的边,之后删除原来的边
20
+ */
21
+ private getGroupEdges;
22
+ /**
23
+ * 获取分组内的节点和左上角节点,获取到有效信息后,删除节点
24
+ */
25
+ private getGroupNodes;
26
+ private getGraphDataWithGroup;
27
+ }
28
+ export { GroupShrink, };
@@ -0,0 +1,210 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __read = (this && this.__read) || function (o, n) {
13
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
14
+ if (!m) return o;
15
+ var i = m.call(o), r, ar = [], e;
16
+ try {
17
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
18
+ }
19
+ catch (error) { e = { error: error }; }
20
+ finally {
21
+ try {
22
+ if (r && !r.done && (m = i["return"])) m.call(i);
23
+ }
24
+ finally { if (e) throw e.error; }
25
+ }
26
+ return ar;
27
+ };
28
+ var __spread = (this && this.__spread) || function () {
29
+ for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
30
+ return ar;
31
+ };
32
+ import { remove } from 'lodash-es';
33
+ var GroupShrink = /** @class */ (function () {
34
+ function GroupShrink(_a) {
35
+ var _this = this;
36
+ var lf = _a.lf;
37
+ var _b;
38
+ this.group = null; // group节点
39
+ this.lf = null; // lf 实例
40
+ this.shrinkWidth = 100; // 收缩后节点宽度
41
+ this.shrinkHeight = 60; // 收缩后节点高度
42
+ this.getGraphDataWithGroup = function () {
43
+ var _a = _this.lf.graphModel.modelToGraphData(), nodes = _a.nodes, edges = _a.edges;
44
+ var groupNodes = [];
45
+ var groupEdges = [];
46
+ var shrinkedGroupIds = [];
47
+ nodes.forEach(function (node) {
48
+ if (node.type === 'group' && node.properties.shrinkProperty && node.properties.shrinkProperty) {
49
+ var _a = node.properties.shrinkProperty, shrinked = _a.shrinked, groupNode = _a.groupNode, innerNodes = _a.innerNodes, innerEdges = _a.innerEdges;
50
+ if (shrinked) {
51
+ // 是收缩状态下的分组节点,需要恢复原来的节点和边
52
+ node = groupNode;
53
+ groupNodes = __spread(groupNodes, innerNodes);
54
+ groupEdges = __spread(groupEdges, innerEdges);
55
+ shrinkedGroupIds.push(groupNode.id);
56
+ }
57
+ }
58
+ });
59
+ // 移除与收缩分组节点相连的边
60
+ if (shrinkedGroupIds.length) {
61
+ remove(edges, function (edge) {
62
+ var sourceNodeId = edge.sourceNodeId, targetNodeId = edge.targetNodeId;
63
+ return shrinkedGroupIds.indexOf(sourceNodeId) > -1
64
+ || shrinkedGroupIds.indexOf(targetNodeId) > -1;
65
+ });
66
+ }
67
+ return {
68
+ nodes: __spread(nodes, groupNodes),
69
+ edges: __spread(edges, groupEdges),
70
+ };
71
+ };
72
+ this.lf = lf;
73
+ this.lf.extension = __assign(__assign({}, ((_b = this.lf.extension) !== null && _b !== void 0 ? _b : {})), { groupShrink: this });
74
+ this.lf.getGraphData = this.getGraphDataWithGroup;
75
+ }
76
+ /**
77
+ * 收缩
78
+ */
79
+ GroupShrink.prototype.startShrink = function (group) {
80
+ var _this = this;
81
+ if (group.type !== 'group') {
82
+ throw new Error('Only groupNode can be shrinked!');
83
+ }
84
+ this.group = group;
85
+ var nodeModel = this.lf.getNodeModelById(group.id);
86
+ var shrinkConfig = nodeModel.getProperties().shrinkProperty;
87
+ if (shrinkConfig && shrinkConfig.shrinked) {
88
+ throw new Error('GroupNode which is shrinked cannot be shrinked again!');
89
+ }
90
+ var shrinkProperty = {
91
+ shrinked: true,
92
+ groupNode: __assign({}, this.group),
93
+ innerNodes: [],
94
+ innerEdges: [],
95
+ };
96
+ var newEdges = []; // 需要重新生成的边
97
+ var innerNodes = []; // 分组内节点
98
+ var innerEdges = []; // 分组内部边
99
+ var minX = null;
100
+ var minY = null;
101
+ // 处理分组内的节点和边
102
+ if (this.group && this.group.children && this.group.children.length) {
103
+ var edgesInfo = this.getGroupEdges();
104
+ newEdges = edgesInfo.newEdges;
105
+ innerEdges = edgesInfo.innerEdges;
106
+ var nodesInfo = this.getGroupNodes();
107
+ innerNodes = nodesInfo.innerNodes; // 分组内节点信息
108
+ minX = nodesInfo.minX; // 左上角节点的x
109
+ minY = nodesInfo.minY; // 左上角节点的y
110
+ }
111
+ shrinkProperty.innerNodes = innerNodes;
112
+ shrinkProperty.innerEdges = innerEdges;
113
+ this.lf.setProperties(this.group.id, { shrinkProperty: shrinkProperty }); // 分组节点收缩前状态、分组内节点、分组内边存入properties
114
+ // 收缩后的分组节点移动到分组内左上角的节点位置
115
+ nodeModel.x = minX || this.group.x;
116
+ nodeModel.y = minY || this.group.y;
117
+ // 收缩,调整节点大小
118
+ nodeModel.width = this.shrinkWidth;
119
+ nodeModel.height = this.shrinkHeight;
120
+ // 生成与分组节点相连的边
121
+ newEdges.forEach(function (edgeConfig) {
122
+ _this.lf.addEdge(edgeConfig);
123
+ });
124
+ };
125
+ /**
126
+ * 展开
127
+ */
128
+ GroupShrink.prototype.startExpand = function (group) {
129
+ var _this = this;
130
+ this.group = group;
131
+ var nodeModel = this.lf.getNodeModelById(group.id);
132
+ var _a = nodeModel.getProperties().shrinkProperty, shrinkProperty = _a === void 0 ? {} : _a;
133
+ var shrinked = shrinkProperty.shrinked, groupNode = shrinkProperty.groupNode, innerNodes = shrinkProperty.innerNodes, innerEdges = shrinkProperty.innerEdges;
134
+ if (!shrinked) {
135
+ throw new Error('GroupNode which is not shrinked cannot be expanded');
136
+ }
137
+ // 重新渲染分组节点
138
+ this.lf.deleteNode(group.id);
139
+ this.lf.addNode(groupNode);
140
+ // 恢复分组内的节点
141
+ innerNodes.forEach(function (item) {
142
+ _this.lf.addNode(item);
143
+ });
144
+ // 恢复分组内的节点上所有边
145
+ innerEdges.forEach(function (item) {
146
+ _this.lf.addEdge(item);
147
+ });
148
+ // 修改properties
149
+ this.lf.setProperties(group.id, { shrinkProperty: { shrinked: false } });
150
+ };
151
+ /**
152
+ * 获取分组内的节点上的所有边,以及计算新的分组节点需要连接的边,之后删除原来的边
153
+ */
154
+ GroupShrink.prototype.getGroupEdges = function () {
155
+ var _this = this;
156
+ var edges = this.lf.graphModel.modelToGraphData().edges;
157
+ var children = this.group.children;
158
+ var innerEdges = [];
159
+ var newEdges = [];
160
+ edges.forEach(function (item) {
161
+ var startInGroup = children.indexOf(item.sourceNodeId) > -1;
162
+ var endInGroup = children.indexOf(item.targetNodeId) > -1;
163
+ if (startInGroup || endInGroup) {
164
+ innerEdges.push(item);
165
+ if (startInGroup && !endInGroup) {
166
+ // 从分组内向外的边
167
+ newEdges.push({
168
+ sourceNodeId: _this.group.id,
169
+ targetNodeId: item.targetNodeId,
170
+ });
171
+ }
172
+ if (!startInGroup && endInGroup) {
173
+ // 从外部指向分组内的
174
+ newEdges.push({
175
+ sourceNodeId: item.sourceNodeId,
176
+ targetNodeId: _this.group.id,
177
+ });
178
+ }
179
+ _this.lf.deleteEdge(item.id);
180
+ }
181
+ });
182
+ return { innerEdges: innerEdges, newEdges: newEdges };
183
+ };
184
+ /**
185
+ * 获取分组内的节点和左上角节点,获取到有效信息后,删除节点
186
+ */
187
+ GroupShrink.prototype.getGroupNodes = function () {
188
+ var _this = this;
189
+ var innerNodes = [];
190
+ var nodes = this.lf.graphModel.modelToGraphData().nodes;
191
+ var children = this.group.children;
192
+ var minX = null;
193
+ var minY = null;
194
+ // 遍历所有节点,在分组内的, 暂存&删除
195
+ nodes.forEach(function (item) {
196
+ if (children.indexOf(item.id) > -1) {
197
+ innerNodes.push(item);
198
+ if ((!minX || item.x < minX) && (!minY || item.y < minY)) {
199
+ minX = item.x;
200
+ minY = item.y;
201
+ }
202
+ _this.lf.deleteNode(item.id);
203
+ }
204
+ });
205
+ return { innerNodes: innerNodes, minX: minX, minY: minY };
206
+ };
207
+ GroupShrink.pluginName = 'group-shrink';
208
+ return GroupShrink;
209
+ }());
210
+ export { GroupShrink, };
package/es/index.d.ts CHANGED
@@ -16,3 +16,4 @@ export * from './tools/flow-path';
16
16
  export * from './tools/auto-layout';
17
17
  export * from './bpmn-adapter/xml2json';
18
18
  export * from './bpmn-adapter/json2xml';
19
+ export * from './group-shrink';
package/es/index.js CHANGED
@@ -17,3 +17,4 @@ export * from './tools/flow-path';
17
17
  export * from './tools/auto-layout';
18
18
  export * from './bpmn-adapter/xml2json';
19
19
  export * from './bpmn-adapter/json2xml';
20
+ export * from './group-shrink';