import { get_root, replace_root } from "./world.mjs"; const get_elem = id => document.getElementById(id); const create_elem = type => document.createElement(type); const save_button = get_elem("save"); const load_button = get_elem("load"); save_button.onclick = _ => { const timestamp = new Date().getTime(); const filename = `boxes_${timestamp}.boxes`; const world = get_root(); const data = btoa(JSON.stringify(world)); save_text(filename, data); }; function save_text (filename, text) { const link = create_elem("a"); const file = new Blob([text], { type: "text/plain" }); link.href = URL.createObjectURL(file); link.download = filename; link.click(); } load_button.onclick = _ => { const input = create_elem("input"); input.type = "file"; input.addEventListener("change", e => { const file = e.target.files[0]; const reader = new FileReader(); reader.onload = e => load_text(e.target.result); reader.onerror = error => alert("couldn't load the file."); reader.readAsText(file); }); input.click(); } function load_text (text) { const data = JSON.parse(atob(text)); replace_root(data); }