download button (not tested yet)

This commit is contained in:
Max Bradbury 2020-07-20 00:18:06 +01:00
parent dce5d71506
commit 4a0a9d10d3
2 changed files with 21 additions and 2 deletions

View File

@ -30,7 +30,6 @@ html(lang="en-gb")
h2 download
textarea#output
br
//-
button#download download
button.pagination.prev#add add another image
button.pagination.start#reset start again

View File

@ -1,5 +1,17 @@
import init, {load_default_game_data, add_tiles} from './pkg/bitsy_tiles.js';
// stolen from https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server
function download(filename, text) {
let element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function el(id) {
return document.getElementById(id);
}
@ -35,6 +47,7 @@ async function run() {
await init();
const buttonAddImage = el("add");
const buttonDownload = el("download");
const buttonGameDataProceed = el("game-data-next");
const textareaGameDataInput = el("game-data");
const buttonImportGame = el("import");
@ -110,6 +123,13 @@ async function run() {
buttonImportGame.addEventListener("click", addTiles);
buttonImportGame.addEventListener("touchend", addTiles);
function handleDownload() {
download("output.bitsy", textareaGameDataOutput.value);
}
buttonDownload.addEventListener("click", handleDownload);
buttonDownload.addEventListener("touchend", handleDownload);
function addImage() {
textareaGameDataInput.value = textareaGameDataOutput.value;
textareaGameDataOutput.value = "";