Editor: Load existing input JSON if it exists

This commit is contained in:
Adam Goldsmith 2017-10-08 18:21:24 -04:00
parent 2bc6c4b58b
commit d12ab839aa
2 changed files with 20 additions and 4 deletions

View File

@ -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 || "";

View File

@ -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;