Wired up load and save buttons; general cleanup

This commit is contained in:
2026-11-02 00:30:26 -06:00
parent b81851f07e
commit 6545386809
5 changed files with 117 additions and 46 deletions
+30 -3
View File
@@ -5,11 +5,25 @@ const setupScriptRunner = () => {
}
const saveScript = () => {
console.log("Save");
let serializedNodes = [];
for (const elm of script.children) {
serializedNodes.push( elm.serialize() );
}
downloadScript( JSON.stringify(serializedNodes, null, 4) );
}
const loadScript = () => {
console.log("Load");
const loadScript = (file) => {
const reader = new FileReader();
reader.onload = (e) => {
try {
insertLoadedBlocks( JSON.parse(e.target.result) );
} catch (error) {
console.error('Failed to load JSON file: ', error);
}
};
reader.readAsText(file);
}
const runScript = () => {
@@ -18,3 +32,16 @@ const runScript = () => {
elm.action(Painter);
}
}
const downloadScript = (
content, fileName = "block-script.json", contentType = 'text/plain'
) => {
let link = document.createElement("A");
let file = new Blob([content], {type: contentType});
link.href = URL.createObjectURL(file);
link.download = fileName;
link.click();
delete link;
}