Skip to content

Commit 7b35a20

Browse files
authored
index.html
SNAPSHOT V3.2, V4 unfinished Save and load added
1 parent bf7e59d commit 7b35a20

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

index.html

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@
9595
</head>
9696
<body>
9797
<button id="themeToggle">🎨 Toggle Theme</button>
98-
<h2 style="margin: 10px;">Blockly JavaScript Builder</h2>
98+
<div style="display: flex; justify-content: center; align-items: center; gap: 15px; margin: 10px;">
99+
<h2 style="margin: 0;">Blockly JavaScript Builder</h2>
100+
<button onclick="saveWorkspace()">💾 Save</button>
101+
<input type="file" id="loadFile" style="display: none;" accept=".xml" />
102+
<button onclick="document.getElementById('loadFile').click()">📂 Load</button>
103+
</div>
99104

100105
<div id="main">
101106
<div id="blocklyDiv"></div>
@@ -280,6 +285,35 @@ <h4 style="margin: 0;">📄 JavaScript Code</h4>
280285
document.getElementById('errorMessage').textContent = "";
281286
}
282287
}
288+
289+
function saveWorkspace() {
290+
const xml = Blockly.Xml.workspaceToDom(workspace);
291+
const xmlText = Blockly.Xml.domToPrettyText(xml);
292+
const blob = new Blob([xmlText], { type: 'text/xml' });
293+
const url = URL.createObjectURL(blob);
294+
const a = document.createElement('a');
295+
a.href = url;
296+
a.download = 'blockly_workspace.xml';
297+
a.click();
298+
URL.revokeObjectURL(url);
299+
}
300+
301+
document.getElementById('loadFile').addEventListener('change', function(event) {
302+
const file = event.target.files[0];
303+
if (!file) return;
304+
305+
const reader = new FileReader();
306+
reader.onload = function(e) {
307+
const xmlText = e.target.result;
308+
try {
309+
const xml = Blockly.utils.xml.textToDom(xmlText); // ✅ FIXED
310+
Blockly.Xml.clearWorkspaceAndLoadFromXml(xml, workspace);
311+
} catch (err) {
312+
alert("❌ Failed to load workspace: " + err.message);
313+
}
314+
};
315+
reader.readAsText(file);
316+
});
283317
</script>
284318
</body>
285319
</html>

0 commit comments

Comments
 (0)