/// <summary> /// Bind the localStorage backup function to the unload event. /// </summary> /// <param name="opt_workspace">Workspace.</param> public static void backupOnUnload(WorkspaceSvg opt_workspace = null) { var workspace = opt_workspace ?? Blockly.Core.getMainWorkspace(); Window.AddEventListener("unload", new Action(() => { BlocklyStorage.backupBlocks_(workspace); }), false); }
/// <summary> /// Callback function for AJAX call. /// </summary> private static void handleRequest_() { if (BlocklyStorage.httpRequest_.ReadyState == 4) { if (BlocklyStorage.httpRequest_.Status != 200) { BlocklyStorage.alert(BlocklyStorage.HTTPREQUEST_ERROR + "\n" + "httpRequest_.status: " + BlocklyStorage.httpRequest_.Status); } else { var data = BlocklyStorage.httpRequest_.ResponseText.Trim(); if (BlocklyStorage.httpRequest_.Name == "xml") { Window.Location.Hash = data; BlocklyStorage.alert(BlocklyStorage.LINK_ALERT.Replace("%1", Window.Location.Href)); } else if (BlocklyStorage.httpRequest_.Name == "key") { if (data.Length == 0) { BlocklyStorage.alert(BlocklyStorage.HASH_ERROR.Replace("%1", Window.Location.Hash)); } else { BlocklyStorage.loadXml_(data, (WorkspaceSvg)BlocklyStorage.httpRequest_["workspace"]); } } BlocklyStorage.monitorChanges_((WorkspaceSvg)BlocklyStorage.httpRequest_["workspace"]); } BlocklyStorage.httpRequest_ = null; } }
/// <summary> /// Save blocks to database and return a link containing key to XML. /// </summary> /// <param name="opt_workspace">Workspace.</param> public static void link(WorkspaceSvg opt_workspace = null) { var workspace = opt_workspace ?? Blockly.Core.getMainWorkspace(); var xml = Blockly.Xml.workspaceToDom(workspace); var data = Blockly.Xml.domToText(xml); BlocklyStorage.makeRequest_("/storage", "xml", data, workspace); }
/// <summary> /// Load blocks from XML. /// </summary> /// <param name="xml">Text representation of XML.</param> /// <param name="workspace">Workspace.</param> private static void loadXml_(string xmlStr, WorkspaceSvg workspace) { Element xml; try { xml = Blockly.Xml.textToDom(xmlStr); } catch (Exception) { BlocklyStorage.alert(BlocklyStorage.XML_ERROR + "\nXML: " + xmlStr); return; } // Clear the workspace to avoid merge. workspace.clear(); Blockly.Xml.domToWorkspace(xml, workspace); }
/// <summary> /// Retrieve XML text from database using given key. /// </summary> /// <param name="key">Key to XML, obtained from href.</param> /// <param name="opt_workspace">Workspace.</param> public static void retrieveXml(string key, WorkspaceSvg opt_workspace = null) { var workspace = opt_workspace ?? Blockly.Core.getMainWorkspace(); BlocklyStorage.makeRequest_("/storage", "key", key, workspace); }