Skip to content

Instantly share code, notes, and snippets.

@abarth500
Created January 24, 2019 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abarth500/375950acbe5ae18942bcb50b927355ef to your computer and use it in GitHub Desktop.
Save abarth500/375950acbe5ae18942bcb50b927355ef to your computer and use it in GitHub Desktop.
Blockly workspace.js with XML file loading
Promise.all(
["workspace.xml", "toolbox.xml"].map(async file => {
return fetch(file).then(
(res) => {
return res.text();
}
);
})
).then((xmls) => {
xmls.forEach((xml) => {
var parser = new DOMParser();
var doc = parser.parseFromString(xml, "application/xml");
document.body.appendChild(doc.documentElement);
});
}).then(() => {
/* TODO: Change toolbox XML ID if necessary. Can export toolbox XML from Workspace Factory. */
var toolbox = document.getElementById("toolbox");
var options = {
toolbox: toolbox,
collapse: true,
comments: true,
disable: true,
maxBlocks: Infinity,
trashcan: true,
horizontalLayout: false,
toolboxPosition: 'start',
css: true,
media: 'https://blockly-demo.appspot.com/static/media/',
rtl: false,
scrollbars: true,
sounds: true,
oneBasedIndex: true,
grid: {
spacing: 20,
length: 1,
colour: '#888',
snap: false
},
zoom: {
controls: true,
wheel: true,
startScale: 1,
maxScale: 3,
minScale: 0.3,
scaleSpeed: 1.2
}
};
/* Inject your workspace */
var workspace = Blockly.inject('blocklyDiv', options);
/* Load Workspace Blocks from XML to workspace. Remove all code below if no blocks to load */
/* TODO: Change workspace blocks XML ID if necessary. Can export workspace blocks XML from Workspace Factory. */
var workspaceBlocks = document.getElementById("workspaceBlocks");
/* Load blocks to workspace. */
Blockly.Xml.domToWorkspace(workspaceBlocks, workspace);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment