From c98810a01c10356cb221ac8b9383695ea22a968a Mon Sep 17 00:00:00 2001 From: Theis Pieter Hollebeek Date: Sat, 11 Oct 2025 20:14:57 +0200 Subject: [PATCH] fix downloadfile + importsprites --- src/index.js | 82 ++++++++++++++++++-------------------------- src/sprite_editor.js | 6 +++- 2 files changed, 39 insertions(+), 49 deletions(-) diff --git a/src/index.js b/src/index.js index 10e825b..4d1bb78 100644 --- a/src/index.js +++ b/src/index.js @@ -86,7 +86,9 @@ importButton.onclick = async () => { }); const code = items.find((x) => x.tag === "code"); delete code.tag; - spriteEditor.importSprites(sprites); + spriteEditor.importSprites( + sprites.map(({ name, mime, content }) => ({ name, mime, bytes: content })), + ); const dec = new TextDecoder(); editor.setValue(dec.decode(code.content)); }; @@ -108,10 +110,10 @@ runButton.onclick = () => { } }; -function downloadBinaryFile(content, extension) { +function downloadFile(content, extension, mime) { const filename = prompt("Filename?"); - const blob = new Blob([content]); + const blob = new Blob([content], { type: mime }); const url = URL.createObjectURL(blob); const element = document.createElement("a"); @@ -127,22 +129,6 @@ function downloadBinaryFile(content, extension) { document.body.removeChild(element); } -function downloadTextFile(content, mime, extension) { - const filename = prompt("Filename?"); - - const element = document.createElement("a"); - - element.href = `data:${mime};charset=utf-8,${encodeURIComponent(content)}`; - element.download = filename.endsWith(extension) ? filename : filename + extension; - element.style.display = "none"; - - document.body.appendChild(element); - - element.click(); - - document.body.removeChild(element); -} - function minifyJs(code) { return code .replace(/[\s\n]+/g, " ") @@ -161,7 +147,7 @@ saveButton.onclick = () => { }; saveJsButton.onclick = () => { - downloadTextFile(editor.getValue(), "text/javascript", ".js"); + downloadFile(editor.getValue(), ".js", "text/javascript"); }; saveHtmlButton.onclick = async () => { @@ -174,43 +160,43 @@ saveHtmlButton.onclick = async () => { const html = ` - - - - Game - - - + - - - - + + + + + `; - downloadTextFile(html, "text/html", ".html"); + downloadTextFile(html, ".html", "text/html"); }; saveKarlkoderButton.onclick = () => { - downloadBinaryFile( + downloadFile( Vermiparous.en( editor.getValue(), spriteEditor.sprites, diff --git a/src/sprite_editor.js b/src/sprite_editor.js index 56e9c59..8036f8e 100644 --- a/src/sprite_editor.js +++ b/src/sprite_editor.js @@ -21,7 +21,11 @@ export class SpriteEditor { } importSprites(sprites) { - this.sprites = sprites; + this.sprites = []; + for (const sprite of sprites) { + console.log(sprite); + this.addSprite({ name: sprite.name, bytes: sprite.bytes, mime: sprite.mime }); + } this.renderList(); }