Skip to content

Commit a5af83c

Browse files
committed
fix Plotly.downloadImage when passing figure objects
1 parent 2b66705 commit a5af83c

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/snapshot/download.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ function downloadImage(gd, opts) {
4545
reject(new Error('Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.'));
4646
}
4747

48-
_gd._snapshotInProgress = true;
48+
if(_gd) _gd._snapshotInProgress = true;
4949
var promise = toImage(gd, opts);
5050

5151
var filename = opts.filename || gd.fn || 'newplot';
5252
filename += '.' + opts.format;
5353

5454
promise.then(function(result) {
55-
_gd._snapshotInProgress = false;
55+
if(_gd) _gd._snapshotInProgress = false;
5656
return fileSaver(result, filename);
5757
}).then(function(name) {
5858
resolve(name);
5959
}).catch(function(err) {
60-
_gd._snapshotInProgress = false;
60+
if(_gd) _gd._snapshotInProgress = false;
6161
reject(err);
6262
});
6363
});

test/jasmine/tests/download_test.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ describe('Plotly.downloadImage', function() {
6565
downloadTest('graph', 'svg', done);
6666
}, LONG_TIMEOUT_INTERVAL);
6767

68+
it('should work when passing a figure object', function(done) {
69+
var fig = {
70+
data: [{y: [1, 2, 1]}]
71+
};
72+
Plotly.downloadImage(fig)
73+
.then(function() {
74+
expect(document.createElement).toHaveBeenCalledWith('canvas');
75+
expect(gd._snapshotInProgress)
76+
.toBe(undefined, 'should not attach _snapshotInProgress to figure objects');
77+
})
78+
.catch(failTest)
79+
.then(done);
80+
}, LONG_TIMEOUT_INTERVAL);
81+
6882
it('should produce the right SVG output in IE', function(done) {
6983
// mock up IE behavior
7084
spyOn(Lib, 'isIE').and.callFake(function() { return true; });
@@ -131,15 +145,24 @@ function downloadTest(gd, format, done) {
131145
});
132146
});
133147

134-
Plotly.plot(gd, textchartMock.data, textchartMock.layout).then(function() {
148+
Plotly.plot(gd, textchartMock.data, textchartMock.layout).then(function(_gd) {
135149
// start observing dom
136150
// configuration of the observer:
137151
var config = { childList: true };
138152

139153
// pass in the target node and observer options
140154
observer.observe(target, config);
141155

142-
return Plotly.downloadImage(gd, {format: format, height: 300, width: 300, filename: 'plotly_download'});
156+
var promise = Plotly.downloadImage(gd, {
157+
format: format,
158+
height: 300,
159+
width: 300,
160+
filename: 'plotly_download'
161+
});
162+
163+
expect(_gd._snapshotInProgress).toBe(true, 'should attach _snapshotInProgress to graph divs');
164+
165+
return promise;
143166
})
144167
.then(function(filename) {
145168
// stop observing
@@ -160,7 +183,6 @@ function downloadTest(gd, format, done) {
160183
.then(done);
161184
}
162185

163-
164186
// Only chrome supports webp at the time of writing
165187
function checkWebp(cb) {
166188
var img = new Image();

0 commit comments

Comments
 (0)