From 2ff248952b306da1d0128f31ca33c5cf23fb5536 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Tue, 7 Apr 2020 15:25:08 -0400 Subject: [PATCH] Add import-from-clipboard button --- macrovision.js | 52 +++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/macrovision.js b/macrovision.js index 1b14d9b1..c211db96 100644 --- a/macrovision.js +++ b/macrovision.js @@ -1207,10 +1207,15 @@ function prepareMenu() { icon: "fas fa-link" }, { - name: "Export", + name: "Export to clipboard", id: "menu-export", icon: "fas fa-share" }, + { + name: "Import from clipboard", + id: "menu-import", + icon: "fas fa-share" + }, { name: "Save", id: "menu-save", @@ -1777,24 +1782,6 @@ document.addEventListener("DOMContentLoaded", () => { }); - document.addEventListener("paste", e => { - try { - const data = JSON.parse(e.clipboardData.getData("text")); - if (data.entities === undefined) { - return; - } - if (data.world === undefined) { - return; - } - - importScene(data); - } catch (err) { - console.error(err); - - // probably wasn't valid data - } - }); - window.addEventListener("resize", handleResize); // TODO: further investigate why the tool initially starts out with wrong @@ -1815,6 +1802,10 @@ document.addEventListener("DOMContentLoaded", () => { copyScene(); }); + document.querySelector("#menu-import").addEventListener("click", e => { + pasteScene(); + }); + document.querySelector("#menu-save").addEventListener("click", e => { saveScene(); }); @@ -2113,11 +2104,28 @@ function linkScene() { 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."); + navigator.clipboard.writeText(JSON.stringify(results)); } +function pasteScene() { + try { + navigator.clipboard.readText().then(text => { + const data = JSON.parse(text); + if (data.entities === undefined) { + return; + } + if (data.world === undefined) { + return; + } + + importScene(data); + }).catch(err => alert(err)); + } catch (err) { + console.error(err); + + // probably wasn't valid data + } +} // TODO - don't just search through every single entity // probably just have a way to do lookups directly