Skip to content

Commit 70234d1

Browse files
committed
Update chart save button to wait for the promise from the vega getImageURI (co-authored w/Claude, but small). Tested manually
1 parent 0e99155 commit 70234d1

1 file changed

Lines changed: 34 additions & 26 deletions

File tree

src/web/js/repl-ui.js

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -462,32 +462,40 @@
462462
savedOptions = options;
463463
return $.extend({}, options, {chartArea: null});
464464
});
465-
const img = document.createElement('img');
466-
img.src = args.getImageURI();
467-
const temp = document.createElement('canvas');
468-
temp.width = img.width;
469-
temp.height = img.height;
470-
const ctx = temp.getContext('2d');
471-
ctx.drawImage(img, 0, 0);
472-
const image = runtime.getField(imageLib, "internal");
473-
const trimmed = image.trimCanvas(temp);
474-
const download = document.createElement('a');
475-
download.href = trimmed.toDataURL();
476-
download.download = 'chart.png';
477-
// from https://stackoverflow.com/questions/3906142/how-to-save-a-png-from-javascript-variable
478-
function fireEvent(obj, evt){
479-
const fireOnThis = obj;
480-
if(document.createEvent) {
481-
const evObj = document.createEvent('MouseEvents');
482-
evObj.initEvent(evt, true, false);
483-
fireOnThis.dispatchEvent(evObj);
484-
} else if(document.createEventObject) {
485-
const evObj = document.createEventObject();
486-
fireOnThis.fireEvent('on' + evt, evObj);
487-
}
488-
}
489-
fireEvent(download, 'click');
490-
args.draw(_ => savedOptions);
465+
// getImageURI may return a data URL synchronously (Google Charts)
466+
// or a Promise resolving to one (Vega, which renders asynchronously).
467+
Promise.resolve(args.getImageURI()).then((uri) => {
468+
const img = document.createElement('img');
469+
// Must wait for the image to load before reading its dimensions
470+
// and drawing it; for the async (Vega) path it isn't ready yet.
471+
img.onload = () => {
472+
const temp = document.createElement('canvas');
473+
temp.width = img.width;
474+
temp.height = img.height;
475+
const ctx = temp.getContext('2d');
476+
ctx.drawImage(img, 0, 0);
477+
const image = runtime.getField(imageLib, "internal");
478+
const trimmed = image.trimCanvas(temp);
479+
const download = document.createElement('a');
480+
download.href = trimmed.toDataURL();
481+
download.download = 'chart.png';
482+
// from https://stackoverflow.com/questions/3906142/how-to-save-a-png-from-javascript-variable
483+
function fireEvent(obj, evt){
484+
const fireOnThis = obj;
485+
if(document.createEvent) {
486+
const evObj = document.createEvent('MouseEvents');
487+
evObj.initEvent(evt, true, false);
488+
fireOnThis.dispatchEvent(evObj);
489+
} else if(document.createEventObject) {
490+
const evObj = document.createEventObject();
491+
fireOnThis.fireEvent('on' + evt, evObj);
492+
}
493+
}
494+
fireEvent(download, 'click');
495+
args.draw(_ => savedOptions);
496+
};
497+
img.src = uri;
498+
});
491499
});
492500
},
493501
resizeStop: (_, ui) => {

0 commit comments

Comments
 (0)