How can I add a block to the workspace on load?

696 views
Skip to first unread message

Gurmukh Panesar

unread,
Feb 17, 2016, 5:54:41 AM2/17/16
to Blockly
Hi All,

I've been looking through the github repo (workspace.js) and the docs but I can't seem to find out how to I can place a block on page load. 

Is this possible, if so how?

Thanks

André Roberge

unread,
Feb 17, 2016, 7:37:16 AM2/17/16
to Blockly
I've implemented the local storage solution mentioned in the docs.    I save the configuration using this:

    xml = Blockly.Xml.workspaceToDom(workspace);
    xml_text = Blockly.Xml.domToText(xml);
    localStorage.setItem("blockly", xml_text);

and, when loading the page, retrieve it via

    xml_text = localStorage.getItem("blockly");
    if (xml_text) {
        xml = Blockly.Xml.textToDom(xml_text);
        Blockly.Xml.domToWorkspace(.workspace, xml);
    }

You could use a similar approach:  
first, create a "test" version of your workspace,
 with the desired block in it.
Then, use an approach similar to the above to create a string like xml_text above and
copy that string (perhaps to the developer console).

Then hard-code that string in your program as part of the initialization of the workspace.

For example, here's the xml_text currently saved in my localStorage after I inserted a single block in my workspace:

<xml xmlns="http://www.w3.org/1999/xhtml"><block type="_move_" x="88" y="59"></block></xml>

André


 
Thanks

Gurmukh Panesar

unread,
Feb 19, 2016, 6:21:50 AM2/19/16
to Blockly
Great idea, thanks André!
Reply all
Reply to author
Forward
0 new messages