@logicflow/extension 1.2.19 → 1.2.20

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.
@@ -1,215 +0,0 @@
1
- "use strict";
2
- /**
3
- * 路径插件,此插件支持获取绘制的图中所有的路径。
4
- * 需要指定开始节点类型。
5
- */
6
- var __read = (this && this.__read) || function (o, n) {
7
- var m = typeof Symbol === "function" && o[Symbol.iterator];
8
- if (!m) return o;
9
- var i = m.call(o), r, ar = [], e;
10
- try {
11
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
12
- }
13
- catch (error) { e = { error: error }; }
14
- finally {
15
- try {
16
- if (r && !r.done && (m = i["return"])) m.call(i);
17
- }
18
- finally { if (e) throw e.error; }
19
- }
20
- return ar;
21
- };
22
- var __spread = (this && this.__spread) || function () {
23
- for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
24
- return ar;
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.FlowPath = void 0;
28
- var getBpmnId_1 = require("../../bpmn/getBpmnId");
29
- var FlowPath = /** @class */ (function () {
30
- function FlowPath(_a) {
31
- var _this = this;
32
- var lf = _a.lf;
33
- this.lf = lf;
34
- this.pathes = [];
35
- // 给lf添加方法
36
- lf.getPathes = function () {
37
- if (!_this.startNodeType) {
38
- throw new Error('需要预先指定开始节点类型');
39
- }
40
- return _this.getPathes();
41
- };
42
- lf.setRawPathes = function (pathes) {
43
- _this.setPathes(pathes);
44
- };
45
- lf.getRawPathes = function () { return _this.pathes; };
46
- lf.setStartNodeType = function (type) {
47
- _this.startNodeType = type;
48
- };
49
- }
50
- FlowPath.prototype.setPathes = function (pathes) {
51
- this.pathes = pathes.map(function (_a) {
52
- var routeId = _a.routeId, name = _a.name, elements = _a.elements, type = _a.type;
53
- return ({
54
- routeId: routeId,
55
- name: name,
56
- elements: elements,
57
- type: type,
58
- similarElement: null,
59
- weight: 0,
60
- });
61
- });
62
- };
63
- FlowPath.prototype.getPathes = function () {
64
- var _this = this;
65
- var graphData = this.lf.getGraphRawData();
66
- var nodesMap = new Map();
67
- var startNodeIds = [];
68
- graphData.nodes.forEach(function (node) {
69
- nodesMap.set(node.id, {
70
- id: node.id,
71
- data: node,
72
- nextNodes: [],
73
- });
74
- if (node.type === _this.startNodeType) {
75
- startNodeIds.push(node.id);
76
- }
77
- });
78
- graphData.edges.forEach(function (edge) {
79
- var node = nodesMap.get(edge.sourceNodeId);
80
- node.nextNodes.push(edge.targetNodeId);
81
- });
82
- var pathElements = [];
83
- startNodeIds.forEach(function (id) {
84
- pathElements = pathElements.concat(_this.findPathElements(nodesMap.get(id), nodesMap, []));
85
- });
86
- return this.getNewPathes(pathElements);
87
- };
88
- FlowPath.prototype.findPathElements = function (node, nodesMap, elements) {
89
- if (elements === void 0) { elements = []; }
90
- var newPathes = __spread(elements);
91
- newPathes.push(node.id);
92
- if (node.nextNodes.length === 0) {
93
- return [newPathes];
94
- }
95
- var subPath = [];
96
- for (var i = 0; i < node.nextNodes.length; i++) {
97
- var n = nodesMap.get(node.nextNodes[i]);
98
- var p = void 0;
99
- // 循环路径
100
- var idx = newPathes.indexOf(n.id);
101
- if (idx !== -1) {
102
- p = [__spread(newPathes.slice(idx), [n.id])];
103
- }
104
- else {
105
- p = this.findPathElements(n, nodesMap, __spread(newPathes));
106
- }
107
- subPath = subPath.concat(p);
108
- }
109
- return subPath;
110
- };
111
- /**
112
- * 设置路径id
113
- * 如果存在原始路径Id, 则需要比较新路径是否在原始路径中存在相似路径
114
- * 如果有,则尽量使用原始路径。
115
- * 相似路径
116
- * 1. 如果所有的节点都相同,则必定为同一路径。(包括顺序不同)
117
- * 2. 如果新路径比原来路径少了或者多了部分节点,则记录为相似路径。基于不同的差异,标记不同的权重。
118
- * 3. 基于新路径在旧路径占用权限,设置新路径Id.
119
- * 4. 如果某一条旧路径被多条新路径标记为相同的权重,则这两条新路径都使用新Id.
120
- */
121
- FlowPath.prototype.getNewPathes = function (pathElements) {
122
- var _this = this;
123
- var pathes = [];
124
- // 由于循环路径不包括开始,所以存在重复的情况,此处去重。
125
- var LoopSet = new Set();
126
- pathElements.forEach(function (elements) {
127
- var routeId = _this.getNewId('path');
128
- var name = _this.getNewId('路径');
129
- var isLoop = _this.isLoopPath(elements);
130
- var elementStr = elements.join(',');
131
- if (!LoopSet.has(elementStr)) {
132
- LoopSet.add(elementStr);
133
- pathes.push({
134
- routeId: routeId,
135
- name: name,
136
- elements: elements,
137
- type: isLoop,
138
- weight: 0,
139
- similarElement: '',
140
- });
141
- }
142
- });
143
- var oldPathes = JSON.parse(JSON.stringify(this.pathes));
144
- // 1) 找到所有路径最相似的路径, 给旧路径标记其最接近的路径
145
- pathes.forEach(function (newPath) {
146
- for (var i = 0; i < oldPathes.length; i++) {
147
- var oldPath = oldPathes[i];
148
- var weight = _this.similar2Path(__spread(newPath.elements), __spread(oldPath.elements));
149
- if (weight > newPath.weight && oldPath.weight <= weight) {
150
- newPath.weight = weight;
151
- newPath.similarElement = oldPath;
152
- if (weight === oldPath.weight && oldPath.similarElement) {
153
- // 特殊处理,如果两个路径都与同一条旧路径有相似的权重,则这两个路径的相似路径都置空
154
- oldPath.similarElement.similarElement = null;
155
- oldPath.similarElement.weight = 0;
156
- oldPath.similarElement = null;
157
- oldPath.weight = 0;
158
- }
159
- else {
160
- oldPath.similarElement = newPath;
161
- oldPath.weight = weight;
162
- }
163
- }
164
- }
165
- });
166
- // 2) 再次遍历所有路径,如果该路径的相似路径对应的相似路径是自己,那么就设置该路径id和name为其相似路径。
167
- pathes.forEach(function (newPath) {
168
- if (newPath.similarElement && newPath.similarElement.similarElement === newPath) {
169
- newPath.routeId = newPath.similarElement.routeId;
170
- newPath.name = newPath.similarElement.name;
171
- }
172
- delete newPath.similarElement;
173
- delete newPath.weight;
174
- });
175
- this.setPathes(pathes);
176
- return pathes;
177
- };
178
- FlowPath.prototype.similar2Path = function (x, y) {
179
- var z = 0;
180
- var s = x.length + y.length;
181
- x.sort();
182
- y.sort();
183
- var a = x.shift();
184
- var b = y.shift();
185
- while (a !== undefined && b !== undefined) {
186
- if (a === b) {
187
- z++;
188
- a = x.shift();
189
- b = y.shift();
190
- }
191
- else if (a < b) {
192
- a = x.shift();
193
- }
194
- else if (a > b) {
195
- b = y.shift();
196
- }
197
- }
198
- return (z / s) * 200;
199
- };
200
- FlowPath.prototype.getNewId = function (prefix) {
201
- return prefix + "_" + getBpmnId_1.getBpmnId();
202
- };
203
- /**
204
- * 判断是否为循环路径
205
- * 由于前面进行了特殊处理,将循环部分单独提出来作为路径
206
- * 所有循环路径必定开始节点等于结束节点。
207
- */
208
- FlowPath.prototype.isLoopPath = function (elements) {
209
- var length = elements.length;
210
- return elements.indexOf(elements[length - 1]) !== length - 1 ? 1 : 0;
211
- };
212
- FlowPath.pluginName = 'flowPath';
213
- return FlowPath;
214
- }());
215
- exports.FlowPath = FlowPath;
@@ -1,252 +0,0 @@
1
- "use strict";
2
- /* eslint-disable operator-linebreak */
3
- /* eslint-disable implicit-arrow-linebreak */
4
- /**
5
- * 快照插件,生成视图
6
- */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.Snapshot = void 0;
9
- var Snapshot = /** @class */ (function () {
10
- function Snapshot(_a) {
11
- var _this = this;
12
- var lf = _a.lf;
13
- this.lf = lf;
14
- this.customCssRules = '';
15
- this.useGlobalRules = true;
16
- /* 下载快照 */
17
- lf.getSnapshot = function (fileName, backgroundColor) {
18
- _this.getSnapshot(fileName, backgroundColor);
19
- };
20
- /* 获取Blob对象,用户图片上传 */
21
- lf.getSnapshotBlob = function (backgroundColor) {
22
- return _this.getSnapshotBlob(backgroundColor);
23
- };
24
- /* 获取Base64对象,用户图片上传 */
25
- lf.getSnapshotBase64 = function (backgroundColor) {
26
- return _this.getSnapshotBase64(backgroundColor);
27
- };
28
- }
29
- /* 获取svgRoot对象 */
30
- Snapshot.prototype.getSvgRootElement = function (lf) {
31
- var svgRootElement = lf.container.querySelector('.lf-canvas-overlay');
32
- return svgRootElement;
33
- };
34
- Snapshot.prototype.triggerDownload = function (imgURI) {
35
- var evt = new MouseEvent('click', {
36
- view: window,
37
- bubbles: false,
38
- cancelable: true,
39
- });
40
- var a = document.createElement('a');
41
- a.setAttribute('download', this.fileName);
42
- a.setAttribute('href', imgURI);
43
- a.setAttribute('target', '_blank');
44
- a.dispatchEvent(evt);
45
- };
46
- Snapshot.prototype.removeAnchor = function (element) {
47
- var childNodes = element.childNodes;
48
- var childLength = element.childNodes && element.childNodes.length;
49
- for (var i = 0; i < childLength; i++) {
50
- var child = childNodes[i];
51
- var classList = (child.classList && Array.from(child.classList)) || [];
52
- if (classList.indexOf('lf-anchor') > -1) {
53
- element.removeChild(element.childNodes[i]);
54
- childLength--;
55
- i--;
56
- }
57
- }
58
- };
59
- Snapshot.prototype.removeRotateControl = function (element) {
60
- var childNodes = element.childNodes;
61
- var childLength = element.childNodes && element.childNodes.length;
62
- for (var i = 0; i < childLength; i++) {
63
- var child = childNodes[i];
64
- var classList = (child.classList && Array.from(child.classList)) || [];
65
- if (classList.indexOf('lf-rotate-control') > -1) {
66
- element.removeChild(element.childNodes[i]);
67
- childLength--;
68
- i--;
69
- }
70
- }
71
- };
72
- /* 下载图片 */
73
- Snapshot.prototype.getSnapshot = function (fileName, backgroundColor) {
74
- var _this = this;
75
- this.fileName = fileName || "logic-flow." + Date.now() + ".png";
76
- var svg = this.getSvgRootElement(this.lf);
77
- this.getCanvasData(svg, backgroundColor).then(function (canvas) {
78
- var imgURI = canvas
79
- .toDataURL('image/png')
80
- .replace('image/png', 'image/octet-stream');
81
- _this.triggerDownload(imgURI);
82
- });
83
- };
84
- /* 获取base64对象 */
85
- Snapshot.prototype.getSnapshotBase64 = function (backgroundColor) {
86
- var _this = this;
87
- var svg = this.getSvgRootElement(this.lf);
88
- return new Promise(function (resolve) {
89
- _this.getCanvasData(svg, backgroundColor).then(function (canvas) {
90
- var base64 = canvas.toDataURL('image/png');
91
- // 输出图片数据以及图片宽高
92
- resolve({ data: base64, width: canvas.width, height: canvas.height });
93
- });
94
- });
95
- };
96
- /* 获取Blob对象 */
97
- Snapshot.prototype.getSnapshotBlob = function (backgroundColor) {
98
- var _this = this;
99
- var svg = this.getSvgRootElement(this.lf);
100
- return new Promise(function (resolve) {
101
- _this.getCanvasData(svg, backgroundColor).then(function (canvas) {
102
- canvas.toBlob(function (blob) {
103
- // 输出图片数据以及图片宽高
104
- resolve({ data: blob, width: canvas.width, height: canvas.height });
105
- }, 'image/png');
106
- });
107
- });
108
- };
109
- Snapshot.prototype.getClassRules = function () {
110
- var rules = '';
111
- if (this.useGlobalRules) {
112
- var styleSheets = document.styleSheets;
113
- for (var i = 0; i < styleSheets.length; i++) {
114
- var sheet = styleSheets[i];
115
- for (var j = 0; j < sheet.cssRules.length; j++) {
116
- rules += sheet.cssRules[j].cssText;
117
- }
118
- }
119
- }
120
- if (this.customCssRules) {
121
- rules += this.customCssRules;
122
- }
123
- return rules;
124
- };
125
- // 获取图片生成中中间产物canvas对象,用户转换为其他需要的格式
126
- Snapshot.prototype.getCanvasData = function (svg, backgroundColor) {
127
- var _this = this;
128
- var copy = svg.cloneNode(true);
129
- var graph = copy.lastChild;
130
- var childLength = graph.childNodes && graph.childNodes.length;
131
- if (childLength) {
132
- for (var i = 0; i < childLength; i++) {
133
- var lfLayer = graph.childNodes[i];
134
- // 只保留包含节点和边的基础图层进行下载,其他图层删除
135
- var layerClassList = lfLayer.classList && Array.from(lfLayer.classList);
136
- if (layerClassList && layerClassList.indexOf('lf-base') < 0) {
137
- graph.removeChild(graph.childNodes[i]);
138
- childLength--;
139
- i--;
140
- }
141
- else {
142
- // 删除锚点
143
- var lfBase = graph.childNodes[i];
144
- lfBase &&
145
- lfBase.childNodes.forEach(function (item) {
146
- var element = item;
147
- _this.removeAnchor(element.firstChild);
148
- _this.removeRotateControl(element.firstChild);
149
- });
150
- }
151
- }
152
- }
153
- var dpr = window.devicePixelRatio || 1;
154
- if (dpr < 1) {
155
- // https://github.com/didi/LogicFlow/issues/1222
156
- // canvas.width = bboxWidth * dpr配合ctx.scale(dpr, dpr)是为了解决绘制模糊
157
- // 比如dpr=2,先让canvas.width放大到等同于屏幕的物理像素宽高,然后自适应缩放适配canvas.style.width
158
- // 由于所有元素都缩放了一半,因此需要ctx.scale(dpr, dpr)放大2倍整体绘制的内容
159
- // 当用户缩放浏览器时,window.devicePixelRatio会随着变小
160
- // 当window.devicePixelRatio变小到一定程度,会导致canvas.width<canvas.style.width
161
- // 由于导出图片的svg的大小是canvas.style.width+canvas.style.height
162
- // 因此会导致导出的svg图片无法完整绘制到canvas(因为canvas.width小于svg的宽)
163
- // 从而导致canvas导出图片是缺失的svg
164
- // 而dpr>=1就能保证canvas.width>=canvas.style.width
165
- // 当dpr小于1的时候,我们强制转化为1,并不会产生绘制模糊等问题
166
- dpr = 1;
167
- }
168
- var canvas = document.createElement('canvas');
169
- /*
170
- 为了计算真实宽高需要取图的真实dom
171
- 真实dom存在缩放影响其宽高数值
172
- 在得到真实宽高后除以缩放比例即可得到正常宽高
173
- */
174
- var base = this.lf.graphModel.rootEl.querySelector('.lf-base');
175
- var bbox = base.getBoundingClientRect();
176
- var layout = document
177
- .querySelector('.lf-canvas-overlay')
178
- .getBoundingClientRect();
179
- var offsetX = bbox.x - layout.x;
180
- var offsetY = bbox.y - layout.y;
181
- var graphModel = this.lf.graphModel;
182
- var transformModel = graphModel.transformModel;
183
- var SCALE_X = transformModel.SCALE_X, SCALE_Y = transformModel.SCALE_Y, TRANSLATE_X = transformModel.TRANSLATE_X, TRANSLATE_Y = transformModel.TRANSLATE_Y;
184
- // offset值加10,保证图形不会紧贴着下载图片的左边和上边
185
- copy.lastChild.style.transform = "matrix(1, 0, 0, 1, " + ((-offsetX + TRANSLATE_X) * (1 / SCALE_X) + 10) + ", " + ((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) + 10) + ")";
186
- var bboxWidth = Math.ceil(bbox.width / SCALE_X);
187
- var bboxHeight = Math.ceil(bbox.height / SCALE_Y);
188
- // width,height 值加40,保证图形不会紧贴着下载图片的右边和下边
189
- canvas.style.width = bboxWidth + "px";
190
- canvas.style.height = bboxHeight + "px";
191
- canvas.width = bboxWidth * dpr + 80;
192
- canvas.height = bboxHeight * dpr + 80;
193
- var ctx = canvas.getContext('2d');
194
- ctx.clearRect(0, 0, canvas.width, canvas.height);
195
- ctx.scale(dpr, dpr);
196
- // 如果有背景色,设置流程图导出的背景色
197
- if (backgroundColor) {
198
- ctx.fillStyle = backgroundColor;
199
- ctx.fillRect(0, 0, bboxWidth * dpr + 80, bboxHeight * dpr + 80);
200
- }
201
- else {
202
- ctx.clearRect(0, 0, bboxWidth, bboxHeight);
203
- }
204
- var img = new Image();
205
- var style = document.createElement('style');
206
- style.innerHTML = this.getClassRules();
207
- var foreignObject = document.createElement('foreignObject');
208
- foreignObject.appendChild(style);
209
- copy.appendChild(foreignObject);
210
- return new Promise(function (resolve) {
211
- img.onload = function () {
212
- var isFirefox = navigator.userAgent.indexOf('Firefox') > -1;
213
- try {
214
- if (isFirefox) {
215
- createImageBitmap(img, {
216
- resizeWidth: canvas.width,
217
- resizeHeight: canvas.height,
218
- }).then(function (imageBitmap) {
219
- // 在回调函数中使用 drawImage() 方法绘制图像
220
- ctx.drawImage(imageBitmap, 0, 0);
221
- resolve(canvas);
222
- });
223
- }
224
- else {
225
- ctx.drawImage(img, 0, 0);
226
- resolve(canvas);
227
- }
228
- }
229
- catch (e) {
230
- ctx.drawImage(img, 0, 0);
231
- resolve(canvas);
232
- }
233
- };
234
- /*
235
- 因为svg中存在dom存放在foreignObject元素中
236
- SVG图形转成img对象
237
- todo: 会导致一些清晰度问题这个需要再解决
238
- fixme: XMLSerializer的中的css background url不会下载图片
239
- */
240
- var svg2Img = "data:image/svg+xml;charset=utf-8," + new XMLSerializer().serializeToString(copy);
241
- var imgSrc = svg2Img
242
- .replace(/\n/g, '')
243
- .replace(/\t/g, '')
244
- .replace(/#/g, '%23');
245
- img.src = imgSrc;
246
- });
247
- };
248
- Snapshot.pluginName = 'snapshot';
249
- return Snapshot;
250
- }());
251
- exports.Snapshot = Snapshot;
252
- exports.default = Snapshot;
@@ -1,40 +0,0 @@
1
- /**
2
- * 自动布局插件
3
- * 依赖flowPath插件
4
- * 未完善
5
- */
6
- import LogicFlow from '@logicflow/core';
7
- declare class AutoLayout {
8
- lf: LogicFlow;
9
- levelHeight: any[];
10
- newNodeMap: Map<string, any>;
11
- trunk: any[];
12
- static pluginName: string;
13
- constructor({ lf }: {
14
- lf: any;
15
- });
16
- layout(data: any, path: any): void;
17
- private setNodePosition;
18
- /**
19
- * 1. 处理边上的文本
20
- * 2. 主干节点之间直接的边
21
- * 3. 一个节点被多个连接作为目标节点,合理分配锚点位置。
22
- */
23
- private getEdgeDataPoints;
24
- /**
25
- * 获取边的连接节点相对位置。
26
- * source一定在target左边。
27
- * 1. 如果source和target在同一x, y坐标内容。
28
- * 2. 如果source在target左上方。
29
- * 3. 如果source在target左下方。
30
- */
31
- private getRelativePosition;
32
- /**
33
- * 获取边节点图形的宽高。
34
- */
35
- private getShape;
36
- private formatData;
37
- addLevelHeight(level: any, height?: number, isNegative?: boolean): void;
38
- getLevelHeight(level: any, isNegative?: boolean): any;
39
- }
40
- export { AutoLayout };