From 285294a3c0866efcd8b835f021d8a4111dfab60c Mon Sep 17 00:00:00 2001 From: Przemek Wiech Date: Fri, 22 Feb 2019 23:36:25 +0100 Subject: [PATCH] Added "Download SVG" option --- src/chart.tsx | 21 +++++++++++++++++---- src/chart_view.tsx | 6 ++++++ src/index.tsx | 1 + src/top_bar.tsx | 17 +++++++++++++++++ src/translations/pl.json | 2 ++ 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/chart.tsx b/src/chart.tsx index e478c17..9e26624 100644 --- a/src/chart.tsx +++ b/src/chart.tsx @@ -134,6 +134,12 @@ export class Chart extends React.PureComponent { ); } + 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 { 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 { }; 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(); + } } diff --git a/src/chart_view.tsx b/src/chart_view.tsx index 7d85960..08c0465 100644 --- a/src/chart_view.tsx +++ b/src/chart_view.tsx @@ -255,4 +255,10 @@ export class ChartView extends React.Component { this.chartRef.print(); } } + + downloadSvg() { + if (this.chartRef) { + this.chartRef.downloadSvg(); + } + } } diff --git a/src/index.tsx b/src/index.tsx index c9d31d0..be68376 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -47,6 +47,7 @@ if (browser && browser.name === 'ie') { chartViewRef && chartViewRef.print()} + onDownloadSvg={() => chartViewRef && chartViewRef.downloadSvg()} /> )} /> diff --git a/src/top_bar.tsx b/src/top_bar.tsx index 2d619cf..39d5309 100644 --- a/src/top_bar.tsx +++ b/src/top_bar.tsx @@ -12,6 +12,7 @@ import { Modal, Input, Form, + Dropdown, } from 'semantic-ui-react'; /** Menus and dialogs state. */ @@ -22,6 +23,7 @@ interface State { interface Props { onPrint: () => void; + onDownloadSvg: () => void; } export class TopBar extends React.Component< @@ -175,6 +177,21 @@ export class TopBar extends React.Component< + + + + + } + className="item" + > + + this.props.onDownloadSvg()}> + + + +