Search code examples
blocklygoogle-blockly

What's the event of blockly structure change?


I have a web app that include blockly and I want to be able to save the structure user created on blockly on backend db. I just want to know how to get the current workspace structure so I can post it to server to save it. and then load it again when user login.

Thanks.


Solution

  • From Importing and exporting blocks:

    If your application needs to save and store the user's blocks and restore them at a later visit, use this call for export to XML:

    var xml = Blockly.Xml.workspaceToDom(workspace); var xml_text = Blockly.Xml.domToText(xml);

    This will produce a minimal (but ugly) string containing the XML for the user's blocks. If one wishes to obtain a more readable (but larger) string, use Blockly.Xml.domToPrettyText instead.

    Restoring from an XML string to blocks is just as simple:

    var xml = Blockly.Xml.textToDom(xml_text); Blockly.Xml.domToWorkspace(xml, workspace);