Save to local storage

1,368 views
Skip to first unread message

yoyoyvr

unread,
Dec 20, 2014, 3:04:54 AM12/20/14
to blo...@googlegroups.com
Here's a handy option for saving and loading your blocks -- use web local storage.
I added Save and Load buttons to my Blockly toolbar, hooked up to these functions.

function saveWorkspace() {
    var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
    var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
    
    localStorage.setItem("blockly.xml", xmlText);
}

function loadWorkspace() {
    var xmlText = localStorage.getItem("blockly.xml");
    if (xmlText) {
        Blockly.mainWorkspace.clear();
        xmlDom = Blockly.Xml.textToDom(xmlText);
        Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xmlDom);
    }
}

This seemed a lot simpler than moving my app over to App Engine in order to use Cloud Storage.

Neil Fraser

unread,
Dec 20, 2014, 3:17:13 AM12/20/14
to blo...@googlegroups.com
Local Storage is great, we use it for Blockly Games.  Some drawbacks though:
* Computer-specific, does not allow for sharing.
* Collides with the European Union's cookie laws (talk to a lawyer).
* Blows up irregularly in Firefox, resulting in needing this try/catch:
  try {
    xml = window.localStorage[name];
  } catch(e) {
    // Firefox sometimes throws a SecurityError when accessing localStorage.
    // Restarting Firefox fixes this, so it looks like a bug.
  }


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

yoyoyvr

unread,
Dec 21, 2014, 4:57:45 PM12/21/14
to blo...@googlegroups.com, ro...@neil.fraser.name
Good to know, thanks Neil. I knew it was computer-specific, and that I hadn't (yet) bothered with error handling, didn't know about the European cookie monster.

Here's a link with more information on the cookie law -- http://www.cookielaw.org/the-cookie-law. An easy solution should be to pop up an alert "Do you wish to save this Blockly graph in your local storage?" and then respect the user's answer. (Probably it's enough to do this once, not every time you save.)

Neil Fraser

unread,
Dec 21, 2014, 9:03:02 PM12/21/14
to blo...@googlegroups.com
On 21 December 2014 at 13:57, yoyoyvr <yggy...@gmail.com> wrote:
Here's a link with more information on the cookie law -- http://www.cookielaw.org/the-cookie-law. An easy solution should be to pop up an alert "Do you wish to save this Blockly graph in your local storage?" and then respect the user's answer. (Probably it's enough to do this once, not every time you save.)

Of course saving the user's preference requires ... a cookie.  Complying with European privacy laws is hilarious.  ;-)

yoyoyvr

unread,
Dec 22, 2014, 11:34:54 PM12/22/14
to blo...@googlegroups.com, ro...@neil.fraser.name
confirm("Do you want me to remember not to ask you this question over and over again?")
Reply all
Reply to author
Forward
0 new messages