From 16106081d68f7e16b4280fa801d6783f5cbb903b Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Thu, 13 Feb 2020 22:49:34 -0500 Subject: [PATCH] Add basic saving/loading --- macrovision.html | 4 +++- macrovision.js | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/macrovision.html b/macrovision.html index 1829010c..0564258e 100644 --- a/macrovision.html +++ b/macrovision.html @@ -30,7 +30,9 @@ - + + + diff --git a/macrovision.js b/macrovision.js index 19b9f913..0a68c7ee 100644 --- a/macrovision.js +++ b/macrovision.js @@ -836,7 +836,15 @@ document.addEventListener("DOMContentLoaded", () => { }); document.querySelector("#menu-export").addEventListener("click", e => { - exportScene(); + copyScene(); + }); + + document.querySelector("#menu-save").addEventListener("click", e => { + saveScene(); + }); + + document.querySelector("#menu-load").addEventListener("click", e => { + loadScene(); }); }); @@ -984,6 +992,26 @@ function setWorldHeight(oldHeight, newHeight) { updateSizes(); } +function loadScene() { + try { + const data = JSON.parse(localStorage.getItem("macrovision-save")); + importScene(data); + } catch (err) { + alert("Something went wrong while loading (maybe you didn't have anything saved. Check the F12 console for the error.") + console.error(err); + } +} + +function saveScene() { + try { + const string = JSON.stringify(exportScene()); + localStorage.setItem("macrovision-save", string); + } catch (err) { + alert("Something went wrong while saving (maybe I don't have localStorage permissions, or exporting failed). Check the F12 console for the error.") + console.error(err); + } +} + function exportScene() { const results = {}; @@ -1006,6 +1034,12 @@ function exportScene() { unit: unit } + return results; +} + +function copyScene() { + const results = exportScene(); + navigator.clipboard.writeText(JSON.stringify(results)) alert("Scene copied to clipboard. Paste text into the page to load the scene.");