bpmn-js-copy-as-image 0.3.0 → 0.4.0
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.
- package/dist/index.js +3 -117
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { svgToImage } from '@bpmn-io/svg-to-image';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Flatten array, one level deep.
|
|
@@ -139,120 +139,6 @@ function getBBox(elements, stopRecursion) {
|
|
|
139
139
|
};
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
/**
|
|
143
|
-
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
|
|
144
|
-
* under one or more contributor license agreements. See the NOTICE file
|
|
145
|
-
* distributed with this work for additional information regarding copyright
|
|
146
|
-
* ownership.
|
|
147
|
-
*
|
|
148
|
-
* Camunda licenses this file to you under the MIT; you may not use this file
|
|
149
|
-
* except in compliance with the MIT License.
|
|
150
|
-
*/
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
// list of defined encodings
|
|
154
|
-
const ENCODINGS = [
|
|
155
|
-
'image/png',
|
|
156
|
-
'image/jpeg'
|
|
157
|
-
];
|
|
158
|
-
|
|
159
|
-
const OUTPUT_FORMATS = [
|
|
160
|
-
'dataUrl',
|
|
161
|
-
'blob'
|
|
162
|
-
];
|
|
163
|
-
|
|
164
|
-
const INITIAL_SCALE = 3;
|
|
165
|
-
const FINAL_SCALE = 1;
|
|
166
|
-
const SCALE_STEP = 1;
|
|
167
|
-
|
|
168
|
-
const DATA_URL_REGEX = /^data:((?:\w+\/(?:(?!;).)+)?)((?:;[\w\W]*?[^;])*),(.+)$/;
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Generate an image from SVG markup.
|
|
172
|
-
*
|
|
173
|
-
* @param {string} svg
|
|
174
|
-
* @param {{ imageType?: 'png'|'jpeg', outputFormat?: 'dataUrl'|'blob' }} [options]
|
|
175
|
-
*
|
|
176
|
-
* @returns {Promise<string|Blob>}
|
|
177
|
-
*/
|
|
178
|
-
async function generateImageFromSvg(svg, options = {}) {
|
|
179
|
-
const { imageType = 'png', outputFormat = 'dataUrl' } = options;
|
|
180
|
-
const encoding = 'image/' + imageType;
|
|
181
|
-
|
|
182
|
-
if (OUTPUT_FORMATS.indexOf(outputFormat) === -1) {
|
|
183
|
-
throw new Error('<' + outputFormat + '> is not supported output format for converting svg to image');
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (ENCODINGS.indexOf(encoding) === -1) {
|
|
187
|
-
throw new Error('<' + imageType + '> is not supported type for converting svg to image');
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const initialSVG = svg;
|
|
191
|
-
|
|
192
|
-
for (let scale = INITIAL_SCALE; scale >= FINAL_SCALE; scale -= SCALE_STEP) {
|
|
193
|
-
try {
|
|
194
|
-
let canvas = document.createElement('canvas');
|
|
195
|
-
|
|
196
|
-
svg = scaleSvg(initialSVG, scale);
|
|
197
|
-
|
|
198
|
-
const context = canvas.getContext('2d');
|
|
199
|
-
|
|
200
|
-
const canvg = Canvg.fromString(context, svg);
|
|
201
|
-
await canvg.render();
|
|
202
|
-
|
|
203
|
-
// make the background white for every format
|
|
204
|
-
context.globalCompositeOperation = 'destination-over';
|
|
205
|
-
context.fillStyle = 'white';
|
|
206
|
-
|
|
207
|
-
context.fillRect(0, 0, canvas.width, canvas.height);
|
|
208
|
-
|
|
209
|
-
if (outputFormat === 'dataUrl') {
|
|
210
|
-
const dataUrl = canvas.toDataURL(encoding);
|
|
211
|
-
|
|
212
|
-
if (DATA_URL_REGEX.test(dataUrl)) {
|
|
213
|
-
return dataUrl;
|
|
214
|
-
}
|
|
215
|
-
} else {
|
|
216
|
-
const blob = await new Promise(resolve => {
|
|
217
|
-
canvas.toBlob(result => resolve(result), encoding);
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
if (blob) {
|
|
221
|
-
return blob;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
} catch (error) {
|
|
225
|
-
|
|
226
|
-
// If rendering or export fails for this scale, try again with a smaller scale.
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
throw new Error('Error happened generating image. Diagram size is too big.');
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function scaleSvg(svg, scale) {
|
|
235
|
-
return svg
|
|
236
|
-
.replace(/width="([^"]+)"/, function(match, widthStr) {
|
|
237
|
-
const width = parseFloat(widthStr);
|
|
238
|
-
|
|
239
|
-
if (Number.isNaN(width)) {
|
|
240
|
-
return match;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return `width="${width * scale}"`;
|
|
244
|
-
})
|
|
245
|
-
.replace(/height="([^"]+)"/, function(match, heightStr) {
|
|
246
|
-
const height = parseFloat(heightStr);
|
|
247
|
-
|
|
248
|
-
if (Number.isNaN(height)) {
|
|
249
|
-
return match;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return `height="${height * scale}"`;
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
|
|
256
142
|
const PADDING = {
|
|
257
143
|
x: 6,
|
|
258
144
|
y: 6
|
|
@@ -305,7 +191,7 @@ class ElementsRenderer {
|
|
|
305
191
|
*/
|
|
306
192
|
async renderAsPNG(elements) {
|
|
307
193
|
const svg = await this.renderAsSVG(elements);
|
|
308
|
-
return
|
|
194
|
+
return svgToImage(svg, { imageType: 'png', outputFormat: 'blob' });
|
|
309
195
|
}
|
|
310
196
|
|
|
311
197
|
async renderAsSVG(elements) {
|
|
@@ -481,4 +367,4 @@ const CopyAsImageEditorActionsModule = {
|
|
|
481
367
|
copyAsImageEditorActions: [ 'type', CopyAsImageEditorActions ]
|
|
482
368
|
};
|
|
483
369
|
|
|
484
|
-
export { CopyAsImageContextPadModule, CopyAsImageEditorActionsModule, CopyAsImageModule, ElementsRendererModule
|
|
370
|
+
export { CopyAsImageContextPadModule, CopyAsImageEditorActionsModule, CopyAsImageModule, ElementsRendererModule };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bpmn-js-copy-as-image",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A bpmn-js extension which allows to render selected elements as images",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -79,6 +79,7 @@
|
|
|
79
79
|
"webpack": "^5.105.0"
|
|
80
80
|
},
|
|
81
81
|
"dependencies": {
|
|
82
|
+
"@bpmn-io/svg-to-image": "^1.0.0",
|
|
82
83
|
"canvg": "^4.0.3"
|
|
83
84
|
}
|
|
84
85
|
}
|