Added "Download SVG" option

This commit is contained in:
Przemek Wiech
2019-02-22 23:36:25 +01:00
parent a920147ba8
commit 285294a3c0
5 changed files with 43 additions and 4 deletions

View File

@@ -134,6 +134,12 @@ export class Chart extends React.PureComponent<ChartProps, {}> {
);
}
private getSvgContents() {
const svg = document.getElementById('chart')!.cloneNode(true) as Element;
svg.removeAttribute('transform');
return svg.outerHTML;
}
/** Shows the print dialog to print the currently displayed chart. */
print() {
const printWindow = document.createElement('iframe');
@@ -141,11 +147,8 @@ export class Chart extends React.PureComponent<ChartProps, {}> {
printWindow.style.top = '-1000px';
printWindow.style.left = '-1000px';
printWindow.onload = () => {
const svg = document.getElementById('chart')!.cloneNode(true) as Element;
svg.removeAttribute('transform');
const contents = svg.outerHTML;
printWindow.contentDocument!.open();
printWindow.contentDocument!.write(contents);
printWindow.contentDocument!.write(this.getSvgContents());
printWindow.contentDocument!.close();
// Doesn't work on Firefox without the setTimeout.
setTimeout(() => {
@@ -156,4 +159,14 @@ export class Chart extends React.PureComponent<ChartProps, {}> {
};
document.body.appendChild(printWindow);
}
downloadSvg() {
const hiddenElement = document.createElement('a');
hiddenElement.href = URL.createObjectURL(
new Blob([this.getSvgContents()], {type: 'image/svg+xml'}),
);
hiddenElement.target = '_blank';
hiddenElement.download = 'topola.svg';
hiddenElement.click();
}
}