From 0c55ba745b454de2be1190529d7190b94a94a77c Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Sun, 8 Oct 2017 18:21:24 -0400 Subject: [PATCH] Editor: Load existing input JSON if it exists --- js/editor.js | 21 +++++++++++++++++---- server.js | 3 +++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/js/editor.js b/js/editor.js index 260dacc..9c810f1 100644 --- a/js/editor.js +++ b/js/editor.js @@ -5,11 +5,26 @@ let deckName = window.location.pathname.split('/')[2]; document.title = "Editor|" + deckName; window.addEventListener("load", () => { + // load input json + let xhr = new XMLHttpRequest(); + xhr.addEventListener("load", () => { + // if deck already has input json, load it + if (xhr.status === 200) { + deckJSON = JSON.parse(xhr.responseText); + makeSVGs(deckJSON); + } + }); + xhr.open("GET", "deck.input.json"); + xhr.send(); + // deck JSON uploader document.querySelector('#jsonUpload').addEventListener('change', event => { let files = event.target.files; let reader = new FileReader(); - reader.onload = handleUpload; + reader.onload = event => { + deckJSON = JSON.parse(event.target.result); + makeSVGs(deckJSON); + }; reader.readAsText(files[0]); }); @@ -57,9 +72,7 @@ function getSVGTemplate(name, callback) { xhr.send(); } -function handleUpload(event) { - deckJSON = JSON.parse(event.target.result); - +function makeSVGs(deckJSON) { document.querySelector('#deckName').value = deckJSON.name || ""; document.querySelector('#deckType').value = deckJSON.type || ""; diff --git a/server.js b/server.js index 232716a..f9312fc 100644 --- a/server.js +++ b/server.js @@ -82,6 +82,9 @@ const server = http.createServer((req, res) => { case 'deck.json': sendFileJSON(res, deckName); break; + case 'deck.input.json': + sendFile(res, deckName + ".input.json", 'application/json'); + break; case 'upload': handleUpload(res, req, deckName); break;