From 323ac54b471ecf1abb7e6c881e86f57e157586a6 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Fri, 29 Sep 2017 22:30:22 +0200 Subject: Download generated image files automatically (instead of having them open in a new tab) --- src/components/app/map/MapStageComponent.js | 8 ++++---- src/components/app/sidebars/elements/LoadChartComponent.js | 11 +++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'src/components') diff --git a/src/components/app/map/MapStageComponent.js b/src/components/app/map/MapStageComponent.js index cd6e5e1b..c5b31e0f 100644 --- a/src/components/app/map/MapStageComponent.js +++ b/src/components/app/map/MapStageComponent.js @@ -30,10 +30,10 @@ class MapStageComponent extends React.Component { window.addEventListener("wheel", this.updateScale); window["exportCanvasToImage"] = () => { - const canvasData = this.stage.getStage().toDataURL(); - const newWindow = window.open(""); - newWindow.document.write("Canvas Image Export"); - newWindow.document.title = "OpenDC Canvas Export"; + const download = document.createElement("a"); + download.href = this.stage.getStage().toDataURL(); + download.download = "opendc-canvas-export-" + Date.now() + ".png"; + download.click(); } } diff --git a/src/components/app/sidebars/elements/LoadChartComponent.js b/src/components/app/sidebars/elements/LoadChartComponent.js index 93664ab6..13c700db 100644 --- a/src/components/app/sidebars/elements/LoadChartComponent.js +++ b/src/components/app/sidebars/elements/LoadChartComponent.js @@ -1,15 +1,18 @@ import React from "react"; import ReactDOM from "react-dom/server"; +import SvgSaver from "svgsaver"; import {VictoryAxis, VictoryChart, VictoryLine, VictoryScatter} from "victory"; import {convertSecondsToFormattedTime} from "../../../../util/date-time"; const LoadChartComponent = ({data, currentTick}) => { const onExport = () => { - const newWindow = window.open(""); - newWindow.document.write(ReactDOM.renderToString( + const div = document.createElement("div"); + div.innerHTML = ReactDOM.renderToString( - )); - newWindow.document.title = "OpenDC Chart Export"; + ); + div.firstChild.style = "font-family: Roboto, Arial, sans-serif; font-size: 10pt;"; + const svgSaver = new SvgSaver(); + svgSaver.asSvg(div.firstChild); }; return ( -- cgit v1.2.3