Wired up load and save buttons; general cleanup
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user