-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialise.js
52 lines (48 loc) · 1.45 KB
/
serialise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function serialise() {
return JSON.stringify(
Object.assign(state,
{
blockly: Blockly.serialization.workspaces.save(workspace)
}
)
)
}
function deserialise(data) {
var data = JSON.parse(data);
try {
Blockly.serialization.workspaces.load(data.blockly || {}, workspace);
} catch (error) {
}
globalThis.state = data;
data.nodes.forEach(node => {
var prim = getPrimitive(node.type);
node.tags = Object.assign(prim.tags, node.tags); //load new tags
});
reloadUI();
}
function fileRead(handler) {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.efb2';
input.onchange = event => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = () => {
handler(reader.result);
};
reader.readAsText(file);
};
input.click();
}
function fileSave(text, fname) {
const blob = new Blob([text], { type: 'text/plain' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = fname || 'mod.efb2';
a.click();
URL.revokeObjectURL(a.href);
}
window.addEventListener("load", ()=>{
document.querySelector("#load").addEventListener("click", ()=>{fileRead(deserialise)});
document.querySelector("#save").addEventListener("click", ()=>{fileSave(serialise())});
});