Skip to content

Commit a39dc2a

Browse files
committed
♻️ a few snapshot helpers
1 parent fb73e52 commit a39dc2a

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

src/plot_api/to_image.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ var attrs = {
7474
}
7575
};
7676

77-
var IMAGE_URL_PREFIX = /^data:image\/\w+;base64,/;
78-
7977
/** Plotly.toImage
8078
*
8179
* @param {object | string | HTML div} gd
@@ -179,7 +177,7 @@ function toImage(gd, opts) {
179177
if(imageDataOnly) {
180178
return resolve(svg);
181179
} else {
182-
return resolve('data:image/svg+xml,' + encodeURIComponent(svg));
180+
return resolve(helpers.encodeSVG(svg));
183181
}
184182
}
185183

@@ -206,7 +204,7 @@ function toImage(gd, opts) {
206204

207205
function urlToImageData(url) {
208206
if(imageDataOnly) {
209-
return url.replace(IMAGE_URL_PREFIX, '');
207+
return url.replace(helpers.IMAGE_URL_PREFIX, '');
210208
} else {
211209
return url;
212210
}

src/snapshot/download.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
'use strict';
1010

11-
var toImage = require('../plot_api/to_image');
1211
var Lib = require('../lib');
12+
13+
var toImage = require('../plot_api/to_image');
14+
1315
var fileSaver = require('./filesaver');
16+
var helpers = require('./helpers');
1417

1518
/** Plotly.downloadImage
1619
*
@@ -41,7 +44,7 @@ function downloadImage(gd, opts) {
4144
// does not allow toDataURL
4245
// svg format will work though
4346
if(Lib.isIE() && opts.format !== 'svg') {
44-
reject(new Error('Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.'));
47+
reject(new Error(helpers.MSG_IE_BAD_FORMAT));
4548
}
4649

4750
if(_gd) _gd._snapshotInProgress = true;

src/snapshot/helpers.js

+8
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ exports.getRedrawFunc = function(gd) {
3131
}
3232
};
3333
};
34+
35+
exports.encodeSVG = function(svg) {
36+
return 'data:image/svg+xml,' + encodeURIComponent(svg);
37+
};
38+
39+
exports.IMAGE_URL_PREFIX = /^data:image\/\w+;base64,/;
40+
41+
exports.MSG_IE_BAD_FORMAT = 'Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.';

src/snapshot/svgtoimg.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
var Lib = require('../lib');
1212
var EventEmitter = require('events').EventEmitter;
1313

14+
var helpers = require('./helpers');
15+
1416
function svgToImg(opts) {
1517
var ev = opts.emitter || new EventEmitter();
1618

@@ -21,7 +23,7 @@ function svgToImg(opts) {
2123

2224
// IE only support svg
2325
if(Lib.isIE() && format !== 'svg') {
24-
var ieSvgError = new Error('Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.');
26+
var ieSvgError = new Error(helpers.MSG_IE_BAD_FORMAT);
2527
reject(ieSvgError);
2628
// eventually remove the ev
2729
// in favor of promises
@@ -45,7 +47,7 @@ function svgToImg(opts) {
4547
// for Safari support, eliminate createObjectURL
4648
// this decision could cause problems if content
4749
// is not restricted to svg
48-
var url = 'data:image/svg+xml,' + encodeURIComponent(svg);
50+
var url = helpers.encodeSVG(svg);
4951

5052
canvas.width = w1;
5153
canvas.height = h1;

0 commit comments

Comments
 (0)