WebPlotDigitizer icon indicating copy to clipboard operation
WebPlotDigitizer copied to clipboard

A better exporter

Open KOLANICH opened this issue 6 years ago • 3 comments

From 92086796ec6c8c90ac14a592fce4e35febc018e2 Mon Sep 17 00:00:00 2001
From: KOLANICH
Date: Fri, 9 Feb 2018 12:59:04 +0300
Subject: Made JS export better.

1 There is no need to append the link to body. It works without this.
2 Added MIME type selection.
3 Added revocation of the URI to free resources.
---
 app/javascript/services/download.js | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/app/javascript/services/download.js b/app/javascript/services/download.js
index c91ed28..4e7657b 100644
--- a/app/javascript/services/download.js
+++ b/app/javascript/services/download.js
@@ -33,14 +33,20 @@ wpd.download = (function() {
         }
     }
 
+    const formatMimeMapping = {
+        "json":"application/json",
+        "tsv":"text/tab-separated-values",
+        "csv":"text/comma-separated-values"
+    };
     function textFileLocal(data, filename) {
+        let nameParts=filename.split(".");
+        let ext=nameParts[nameParts.length - 1];
         let $downloadElem = document.createElement('a');
-        $downloadElem.href = URL.createObjectURL(new Blob([data]), {type:"text/plain"});
+        $downloadElem.href = URL.createObjectURL(new Blob([data]), {type:formatMimeMapping[ext.toLowerCase()]});
         $downloadElem.download = stripIllegalCharacters(filename);
         $downloadElem.style.display = "none";
-        document.body.appendChild($downloadElem);
-        $downloadElem.click();
-        document.body.removeChild($downloadElem);
+        $downloadElem.dispatchEvent(new MouseEvent("click"));
+        URL.revokeObjectURL($downloadElem.href);
     }
 
     function textFileServer(data, filename) {
--

KOLANICH avatar Feb 09 '18 10:02 KOLANICH

Thanks, I'll include this too

ankitrohatgi avatar Feb 10 '18 02:02 ankitrohatgi

When are you going to apply it?

Please remove $downloadElem.style.display = "none"; too, I havs forgotten to remove it.

KOLANICH avatar Feb 10 '18 07:02 KOLANICH

Please make pull requests for code changes in the future. I will merge your changes once I have some time to spare (today evening or tomorrow).

ankitrohatgi avatar Feb 10 '18 15:02 ankitrohatgi