Flatten editor.js code

This commit is contained in:
Adam Goldsmith 2017-10-02 04:27:28 -04:00
parent d43804896d
commit afcb1851cc

View File

@ -1,46 +1,53 @@
let deckJSON;
window.addEventListener("load", () => {
let xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => {
let respSVG = xhr.responseXML.children[0];
document.querySelector('#jsonUpload').addEventListener('change', event => {
let files = event.target.files;
let reader = new FileReader();
reader.onload = e => {
deckJSON = JSON.parse(e.target.result);
reader.onload = handleUpload;
reader.readAsText(files[0]);
});
});
function getSVGTemplate(name, callback) {
let xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => {
let respSVG = xhr.responseXML.children[0];
callback(respSVG);
});
xhr.open("GET", "/template/" + name + ".svg");
xhr.send();
}
function handleUpload(event) {
let deckJSON = JSON.parse(event.target.result);
let deck = document.querySelector('#deck');
getSVGTemplate("environment/card", templateSVG => {
deck.style.width = Math.ceil(Math.sqrt(deckJSON.deck.length)) *
parseInt(respSVG.getAttribute("width")) + "pt";
parseInt(templateSVG.getAttribute("width")) + "pt";
deck.style.height = Math.ceil(Math.sqrt(deckJSON.deck.length)) *
parseInt(respSVG.getAttribute("height")) + "pt";
parseInt(templateSVG.getAttribute("height")) + "pt";
deck.innerHTML = "";
deckJSON.deck.forEach((card, index) => {
let cardSVG = respSVG.cloneNode(true);
let cardSVG = templateSVG.cloneNode(true);
deck.appendChild(cardSVG);
for (let prop in card) {
if (prop !== "count") {
wrapSVGText(cardSVG.querySelector('#' + prop),
String(card[prop]));
wrapSVGText(cardSVG.querySelector('#' + prop), String(card[prop]));
}
}
});
});
// POST the generated SVGs to the server
let data = (new XMLSerializer()).serializeToString(deck);
let xhr = new XMLHttpRequest();
xhr.open('POST', "upload");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({body: data, json: deckJSON}));
};
reader.readAsText(files[0]);
});
});
xhr.open("GET", "/template/environment/card.svg");
xhr.send();
});
}
function wrapSVGText(e, string) {
// TODO: bold or italic text