@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,249 +0,0 @@
1
- /* eslint-disable operator-linebreak */
2
- /* eslint-disable implicit-arrow-linebreak */
3
- /**
4
- * 快照插件,生成视图
5
- */
6
- var Snapshot = /** @class */ (function () {
7
- function Snapshot(_a) {
8
- var _this = this;
9
- var lf = _a.lf;
10
- this.lf = lf;
11
- this.customCssRules = '';
12
- this.useGlobalRules = true;
13
- /* 下载快照 */
14
- lf.getSnapshot = function (fileName, backgroundColor) {
15
- _this.getSnapshot(fileName, backgroundColor);
16
- };
17
- /* 获取Blob对象,用户图片上传 */
18
- lf.getSnapshotBlob = function (backgroundColor) {
19
- return _this.getSnapshotBlob(backgroundColor);
20
- };
21
- /* 获取Base64对象,用户图片上传 */
22
- lf.getSnapshotBase64 = function (backgroundColor) {
23
- return _this.getSnapshotBase64(backgroundColor);
24
- };
25
- }
26
- /* 获取svgRoot对象 */
27
- Snapshot.prototype.getSvgRootElement = function (lf) {
28
- var svgRootElement = lf.container.querySelector('.lf-canvas-overlay');
29
- return svgRootElement;
30
- };
31
- Snapshot.prototype.triggerDownload = function (imgURI) {
32
- var evt = new MouseEvent('click', {
33
- view: window,
34
- bubbles: false,
35
- cancelable: true,
36
- });
37
- var a = document.createElement('a');
38
- a.setAttribute('download', this.fileName);
39
- a.setAttribute('href', imgURI);
40
- a.setAttribute('target', '_blank');
41
- a.dispatchEvent(evt);
42
- };
43
- Snapshot.prototype.removeAnchor = function (element) {
44
- var childNodes = element.childNodes;
45
- var childLength = element.childNodes && element.childNodes.length;
46
- for (var i = 0; i < childLength; i++) {
47
- var child = childNodes[i];
48
- var classList = (child.classList && Array.from(child.classList)) || [];
49
- if (classList.indexOf('lf-anchor') > -1) {
50
- element.removeChild(element.childNodes[i]);
51
- childLength--;
52
- i--;
53
- }
54
- }
55
- };
56
- Snapshot.prototype.removeRotateControl = function (element) {
57
- var childNodes = element.childNodes;
58
- var childLength = element.childNodes && element.childNodes.length;
59
- for (var i = 0; i < childLength; i++) {
60
- var child = childNodes[i];
61
- var classList = (child.classList && Array.from(child.classList)) || [];
62
- if (classList.indexOf('lf-rotate-control') > -1) {
63
- element.removeChild(element.childNodes[i]);
64
- childLength--;
65
- i--;
66
- }
67
- }
68
- };
69
- /* 下载图片 */
70
- Snapshot.prototype.getSnapshot = function (fileName, backgroundColor) {
71
- var _this = this;
72
- this.fileName = fileName || "logic-flow." + Date.now() + ".png";
73
- var svg = this.getSvgRootElement(this.lf);
74
- this.getCanvasData(svg, backgroundColor).then(function (canvas) {
75
- var imgURI = canvas
76
- .toDataURL('image/png')
77
- .replace('image/png', 'image/octet-stream');
78
- _this.triggerDownload(imgURI);
79
- });
80
- };
81
- /* 获取base64对象 */
82
- Snapshot.prototype.getSnapshotBase64 = function (backgroundColor) {
83
- var _this = this;
84
- var svg = this.getSvgRootElement(this.lf);
85
- return new Promise(function (resolve) {
86
- _this.getCanvasData(svg, backgroundColor).then(function (canvas) {
87
- var base64 = canvas.toDataURL('image/png');
88
- // 输出图片数据以及图片宽高
89
- resolve({ data: base64, width: canvas.width, height: canvas.height });
90
- });
91
- });
92
- };
93
- /* 获取Blob对象 */
94
- Snapshot.prototype.getSnapshotBlob = function (backgroundColor) {
95
- var _this = this;
96
- var svg = this.getSvgRootElement(this.lf);
97
- return new Promise(function (resolve) {
98
- _this.getCanvasData(svg, backgroundColor).then(function (canvas) {
99
- canvas.toBlob(function (blob) {
100
- // 输出图片数据以及图片宽高
101
- resolve({ data: blob, width: canvas.width, height: canvas.height });
102
- }, 'image/png');
103
- });
104
- });
105
- };
106
- Snapshot.prototype.getClassRules = function () {
107
- var rules = '';
108
- if (this.useGlobalRules) {
109
- var styleSheets = document.styleSheets;
110
- for (var i = 0; i < styleSheets.length; i++) {
111
- var sheet = styleSheets[i];
112
- for (var j = 0; j < sheet.cssRules.length; j++) {
113
- rules += sheet.cssRules[j].cssText;
114
- }
115
- }
116
- }
117
- if (this.customCssRules) {
118
- rules += this.customCssRules;
119
- }
120
- return rules;
121
- };
122
- // 获取图片生成中中间产物canvas对象,用户转换为其他需要的格式
123
- Snapshot.prototype.getCanvasData = function (svg, backgroundColor) {
124
- var _this = this;
125
- var copy = svg.cloneNode(true);
126
- var graph = copy.lastChild;
127
- var childLength = graph.childNodes && graph.childNodes.length;
128
- if (childLength) {
129
- for (var i = 0; i < childLength; i++) {
130
- var lfLayer = graph.childNodes[i];
131
- // 只保留包含节点和边的基础图层进行下载,其他图层删除
132
- var layerClassList = lfLayer.classList && Array.from(lfLayer.classList);
133
- if (layerClassList && layerClassList.indexOf('lf-base') < 0) {
134
- graph.removeChild(graph.childNodes[i]);
135
- childLength--;
136
- i--;
137
- }
138
- else {
139
- // 删除锚点
140
- var lfBase = graph.childNodes[i];
141
- lfBase &&
142
- lfBase.childNodes.forEach(function (item) {
143
- var element = item;
144
- _this.removeAnchor(element.firstChild);
145
- _this.removeRotateControl(element.firstChild);
146
- });
147
- }
148
- }
149
- }
150
- var dpr = window.devicePixelRatio || 1;
151
- if (dpr < 1) {
152
- // https://github.com/didi/LogicFlow/issues/1222
153
- // canvas.width = bboxWidth * dpr配合ctx.scale(dpr, dpr)是为了解决绘制模糊
154
- // 比如dpr=2,先让canvas.width放大到等同于屏幕的物理像素宽高,然后自适应缩放适配canvas.style.width
155
- // 由于所有元素都缩放了一半,因此需要ctx.scale(dpr, dpr)放大2倍整体绘制的内容
156
- // 当用户缩放浏览器时,window.devicePixelRatio会随着变小
157
- // 当window.devicePixelRatio变小到一定程度,会导致canvas.width<canvas.style.width
158
- // 由于导出图片的svg的大小是canvas.style.width+canvas.style.height
159
- // 因此会导致导出的svg图片无法完整绘制到canvas(因为canvas.width小于svg的宽)
160
- // 从而导致canvas导出图片是缺失的svg
161
- // 而dpr>=1就能保证canvas.width>=canvas.style.width
162
- // 当dpr小于1的时候,我们强制转化为1,并不会产生绘制模糊等问题
163
- dpr = 1;
164
- }
165
- var canvas = document.createElement('canvas');
166
- /*
167
- 为了计算真实宽高需要取图的真实dom
168
- 真实dom存在缩放影响其宽高数值
169
- 在得到真实宽高后除以缩放比例即可得到正常宽高
170
- */
171
- var base = this.lf.graphModel.rootEl.querySelector('.lf-base');
172
- var bbox = base.getBoundingClientRect();
173
- var layout = document
174
- .querySelector('.lf-canvas-overlay')
175
- .getBoundingClientRect();
176
- var offsetX = bbox.x - layout.x;
177
- var offsetY = bbox.y - layout.y;
178
- var graphModel = this.lf.graphModel;
179
- var transformModel = graphModel.transformModel;
180
- var SCALE_X = transformModel.SCALE_X, SCALE_Y = transformModel.SCALE_Y, TRANSLATE_X = transformModel.TRANSLATE_X, TRANSLATE_Y = transformModel.TRANSLATE_Y;
181
- // offset值加10,保证图形不会紧贴着下载图片的左边和上边
182
- copy.lastChild.style.transform = "matrix(1, 0, 0, 1, " + ((-offsetX + TRANSLATE_X) * (1 / SCALE_X) + 10) + ", " + ((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) + 10) + ")";
183
- var bboxWidth = Math.ceil(bbox.width / SCALE_X);
184
- var bboxHeight = Math.ceil(bbox.height / SCALE_Y);
185
- // width,height 值加40,保证图形不会紧贴着下载图片的右边和下边
186
- canvas.style.width = bboxWidth + "px";
187
- canvas.style.height = bboxHeight + "px";
188
- canvas.width = bboxWidth * dpr + 80;
189
- canvas.height = bboxHeight * dpr + 80;
190
- var ctx = canvas.getContext('2d');
191
- ctx.clearRect(0, 0, canvas.width, canvas.height);
192
- ctx.scale(dpr, dpr);
193
- // 如果有背景色,设置流程图导出的背景色
194
- if (backgroundColor) {
195
- ctx.fillStyle = backgroundColor;
196
- ctx.fillRect(0, 0, bboxWidth * dpr + 80, bboxHeight * dpr + 80);
197
- }
198
- else {
199
- ctx.clearRect(0, 0, bboxWidth, bboxHeight);
200
- }
201
- var img = new Image();
202
- var style = document.createElement('style');
203
- style.innerHTML = this.getClassRules();
204
- var foreignObject = document.createElement('foreignObject');
205
- foreignObject.appendChild(style);
206
- copy.appendChild(foreignObject);
207
- return new Promise(function (resolve) {
208
- img.onload = function () {
209
- var isFirefox = navigator.userAgent.indexOf('Firefox') > -1;
210
- try {
211
- if (isFirefox) {
212
- createImageBitmap(img, {
213
- resizeWidth: canvas.width,
214
- resizeHeight: canvas.height,
215
- }).then(function (imageBitmap) {
216
- // 在回调函数中使用 drawImage() 方法绘制图像
217
- ctx.drawImage(imageBitmap, 0, 0);
218
- resolve(canvas);
219
- });
220
- }
221
- else {
222
- ctx.drawImage(img, 0, 0);
223
- resolve(canvas);
224
- }
225
- }
226
- catch (e) {
227
- ctx.drawImage(img, 0, 0);
228
- resolve(canvas);
229
- }
230
- };
231
- /*
232
- 因为svg中存在dom存放在foreignObject元素中
233
- SVG图形转成img对象
234
- todo: 会导致一些清晰度问题这个需要再解决
235
- fixme: XMLSerializer的中的css background url不会下载图片
236
- */
237
- var svg2Img = "data:image/svg+xml;charset=utf-8," + new XMLSerializer().serializeToString(copy);
238
- var imgSrc = svg2Img
239
- .replace(/\n/g, '')
240
- .replace(/\t/g, '')
241
- .replace(/#/g, '%23');
242
- img.src = imgSrc;
243
- });
244
- };
245
- Snapshot.pluginName = 'snapshot';
246
- return Snapshot;
247
- }());
248
- export default Snapshot;
249
- export { Snapshot };