From 594da506098b8840c21f64f7878590f43946bb65 Mon Sep 17 00:00:00 2001
From: Quentin Bolsee <quentinbolsee@hotmail.com>
Date: Wed, 11 Oct 2023 12:33:15 -0400
Subject: [PATCH] SVG download

---
 index.html       |  4 ++--
 src/interface.js | 19 +++++++++++++++----
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/index.html b/index.html
index a211edf..81a6fb0 100644
--- a/index.html
+++ b/index.html
@@ -23,7 +23,7 @@
 			<div>
 			<!-- <button class="interface" id="loadButton">Load file(s)...</button></input> -->
 			<button class="interface" id="downloadRenderButton">Download render</button>
-			<button class="interface" id="downloadlayersButton">Download layers</button>
+			<!-- <button class="interface" id="downloadlayersButton">Download layers</button> -->
 			</div>
 		</div>
 		<div class="panel">
@@ -37,7 +37,7 @@
 				</div>
 
 				<div>
-					<input type="checkbox" id="settingsAsSVG" checked=unchecked>
+					<input type="checkbox" id="settingsAsSVG" disabled="true" checked=checked>
 					<label for="settingsAsSVG">Download as SVG</label>
 				</div>
 
diff --git a/src/interface.js b/src/interface.js
index 452fad7..94692ba 100644
--- a/src/interface.js
+++ b/src/interface.js
@@ -217,7 +217,18 @@ function downloadImage(separateLayers=false) {
         return;
     }
 
-    saveSvg.saveSvgAsPng(svgElem, "diagram.png");
+    let svgTxt = new XMLSerializer().serializeToString(svgElem);
+    let url = "data:image/svg+xml;charset=utf-8,"+encodeURIComponent(svgTxt);
+    let downloadLink = document.createElement("a");
+    downloadLink.href = url;
+    downloadLink.download = "render.svg";
+    document.body.appendChild(downloadLink);
+    downloadLink.click();
+    document.body.removeChild(downloadLink);
+    // window.URL.revokeObjectURL(url);
+    // document.getElementById("link").href = url;
+
+    // saveSvg.saveSvgAsPng(svgElem, "diagram.png");
     // if (isNaN(globalSettings.svgDim[0])) {
     //     return;
     // }
@@ -452,9 +463,9 @@ document.getElementById("downloadRenderButton").addEventListener("click", () =>
     downloadImage(false);
 });
 
-document.getElementById("downloadlayersButton").addEventListener("click", () => {
-    downloadImage(true);
-});
+// document.getElementById("downloadlayersButton").addEventListener("click", () => {
+//     downloadImage(true);
+// });
 
 readSettings();
 initListeners();
-- 
GitLab