diff --git a/src/index.js b/src/index.js index a3b3b7a..10e825b 100644 --- a/src/index.js +++ b/src/index.js @@ -68,14 +68,27 @@ editor.addEventListener("change", () => { }); importButton.onclick = async () => { - const files = await promptUpload("", false); + const files = await promptUpload(".karlkode", false); if (files.length === 0) { return; } if (files.length > 1) { - throw new Error("unreachable: something went wrong ! files.length > 1"); + throw new Error( + `unreachable: something went wrong ! files.length > 1 : files.length = ${files.length}`, + ); } - const item = Vermiparous.de(await fetch(URL.createObjectURL(files[0])).then((x) => x.bytes())); + const items = Vermiparous.de(await fetch(URL.createObjectURL(files[0])).then((x) => x.bytes())); + const sprites = items + .filter((x) => x.tag === "asset") + .map((x) => { + delete x.tag; + return x; + }); + const code = items.find((x) => x.tag === "code"); + delete code.tag; + spriteEditor.importSprites(sprites); + const dec = new TextDecoder(); + editor.setValue(dec.decode(code.content)); }; runButton.onclick = () => { @@ -95,7 +108,26 @@ runButton.onclick = () => { } }; -function downloadFile(content, mime, extension) { +function downloadBinaryFile(content, extension) { + const filename = prompt("Filename?"); + + const blob = new Blob([content]); + const url = URL.createObjectURL(blob); + + const element = document.createElement("a"); + + element.href = url; + element.download = filename.endsWith(extension) ? filename : filename + extension; + element.style.display = "none"; + + document.body.appendChild(element); + + element.click(); + + document.body.removeChild(element); +} + +function downloadTextFile(content, mime, extension) { const filename = prompt("Filename?"); const element = document.createElement("a"); @@ -129,7 +161,7 @@ saveButton.onclick = () => { }; saveJsButton.onclick = () => { - downloadFile(editor.getValue(), "text/javascript", ".js"); + downloadTextFile(editor.getValue(), "text/javascript", ".js"); }; saveHtmlButton.onclick = async () => { @@ -154,6 +186,9 @@ saveHtmlButton.onclick = async () => { height: 100vh; margin: 0; } + + + An iterable object such as an Array, having ArrayBuffers, TypedArrays, DataViews, Blobs, strings, or a mix of any of such elements, that will be put inside the Blob. Strings should be well-formed Unicode, and lone surrogates are sanitized using the same algorithm as String.prototype.toWellFormed().