Commit e6032ac0 authored by Neil Fraser's avatar Neil Fraser

Fix Block Factory save links (issue 95).

parent 5fd42cc8
......@@ -29,11 +29,13 @@ var BlocklyStorage = {};
/**
* Backup code blocks to localStorage.
* @param {Blockly.WorkspaceSvg} opt_workspace Workspace.
* @private
*/
BlocklyStorage.backupBlocks_ = function() {
BlocklyStorage.backupBlocks_ = function(opt_workspace) {
if ('localStorage' in window) {
var xml = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace());
var workspace = opt_workspace || Blockly.getMainWorkspace();
var xml = Blockly.Xml.workspaceToDom(Blockly.workspace);
// Gets the current URL, not including the hash.
var url = window.location.href.split('#')[0];
window.localStorage.setItem(url, Blockly.Xml.domToText(xml));
......@@ -49,20 +51,24 @@ BlocklyStorage.backupOnUnload = function() {
/**
* Restore code blocks from localStorage.
* @param {Blockly.WorkspaceSvg} opt_workspace Workspace.
*/
BlocklyStorage.restoreBlocks = function() {
BlocklyStorage.restoreBlocks = function(opt_workspace) {
var url = window.location.href.split('#')[0];
if ('localStorage' in window && window.localStorage[url]) {
var workspace = opt_workspace || Blockly.getMainWorkspace();
var xml = Blockly.Xml.textToDom(window.localStorage[url]);
Blockly.Xml.domToWorkspace(Blockly.getMainWorkspace(), xml);
Blockly.Xml.domToWorkspace(workspace, xml);
}
};
/**
* Save blocks to database and return a link containing key to XML.
* @param {Blockly.WorkspaceSvg} opt_workspace Workspace.
*/
BlocklyStorage.link = function() {
var xml = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace());
BlocklyStorage.link = function(opt_workspace) {
var workspace = opt_workspace || Blockly.getMainWorkspace();
var xml = Blockly.Xml.workspaceToDom(workspace);
var data = Blockly.Xml.domToText(xml);
BlocklyStorage.makeRequest_('/storage', 'xml', data);
};
......@@ -70,9 +76,11 @@ BlocklyStorage.link = function() {
/**
* Retrieve XML text from database using given key.
* @param {string} key Key to XML, obtained from href.
* @param {Blockly.WorkspaceSvg} opt_workspace Workspace.
*/
BlocklyStorage.retrieveXml = function(key) {
BlocklyStorage.makeRequest_('/storage', 'key', key);
BlocklyStorage.retrieveXml = function(key, opt_workspace) {
var workspace = opt_workspace || Blockly.getMainWorkspace();
BlocklyStorage.makeRequest_('/storage', 'key', key, workspace);
};
/**
......@@ -87,9 +95,10 @@ BlocklyStorage.httpRequest_ = null;
* @param {string} url URL to fetch.
* @param {string} name Name of parameter.
* @param {string} content Content of parameter.
* @param {!Blockly.WorkspaceSvg} workspace Workspace.
* @private
*/
BlocklyStorage.makeRequest_ = function(url, name, content) {
BlocklyStorage.makeRequest_ = function(url, name, content, workspace) {
if (BlocklyStorage.httpRequest_) {
// AJAX call is in-flight.
BlocklyStorage.httpRequest_.abort();
......@@ -102,6 +111,7 @@ BlocklyStorage.makeRequest_ = function(url, name, content) {
BlocklyStorage.httpRequest_.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
BlocklyStorage.httpRequest_.send(name + '=' + encodeURIComponent(content));
BlocklyStorage.httpRequest_.workspace = workspace;
};
/**
......@@ -124,10 +134,10 @@ BlocklyStorage.handleRequest_ = function() {
BlocklyStorage.alert(BlocklyStorage.HASH_ERROR.replace('%1',
window.location.hash));
} else {
BlocklyStorage.loadXml_(data);
BlocklyStorage.loadXml_(data, BlocklyStorage.httpRequest_.workspace);
}
}
BlocklyStorage.monitorChanges_();
BlocklyStorage.monitorChanges_(BlocklyStorage.httpRequest_.workspace);
}
BlocklyStorage.httpRequest_ = null;
}
......@@ -137,10 +147,10 @@ BlocklyStorage.handleRequest_ = function() {
* Start monitoring the workspace. If a change is made that changes the XML,
* clear the key from the URL. Stop monitoring the workspace once such a
* change is detected.
* @param {!Blockly.WorkspaceSvg} workspace Workspace.
* @private
*/
BlocklyStorage.monitorChanges_ = function() {
var workspace = Blockly.getMainWorkspace();
BlocklyStorage.monitorChanges_ = function(workspace) {
var startXmlDom = Blockly.Xml.workspaceToDom(workspace);
var startXmlText = Blockly.Xml.domToText(startXmlDom);
function change() {
......@@ -148,7 +158,7 @@ BlocklyStorage.monitorChanges_ = function() {
var xmlText = Blockly.Xml.domToText(xmlDom);
if (startXmlText != xmlText) {
window.location.hash = '';
Blockly.removeChangeListener(bindData);
workspace.removeChangeListener(bindData);
}
}
var bindData = workspace.addChangeListener(change);
......@@ -157,9 +167,10 @@ BlocklyStorage.monitorChanges_ = function() {
/**
* Load blocks from XML.
* @param {string} xml Text representation of XML.
* @param {!Blockly.WorkspaceSvg} workspace Workspace.
* @private
*/
BlocklyStorage.loadXml_ = function(xml) {
BlocklyStorage.loadXml_ = function(xml, workspace) {
try {
xml = Blockly.Xml.textToDom(xml);
} catch (e) {
......@@ -167,8 +178,8 @@ BlocklyStorage.loadXml_ = function(xml) {
return;
}
// Clear the workspace to avoid merge.
Blockly.getMainWorkspace().clear();
Blockly.Xml.domToWorkspace(Blockly.getMainWorkspace(), xml);
workspace.clear();
Blockly.Xml.domToWorkspace(workspace, xml);
};
/**
......
......@@ -400,7 +400,8 @@ function updatePreview() {
previewWorkspace.dispose();
}
var rtl = newDir == 'rtl';
previewWorkspace = Blockly.inject('preview', {rtl: rtl});
previewWorkspace = Blockly.inject('preview',
{rtl: rtl, media: '../../media/'});
oldDir = newDir;
}
var code = document.getElementById('languagePre').textContent;
......@@ -458,7 +459,8 @@ function init() {
'Perhaps it was created with a different version of Blockly?';
var linkButton = document.getElementById('linkButton');
linkButton.style.display = 'inline-block';
linkButton.addEventListener('click', BlocklyStorage.link);
linkButton.addEventListener('click',
function() {BlocklyStorage.link(mainWorkspace);});
}
document.getElementById('helpButton').addEventListener('click', function() {
......@@ -482,11 +484,13 @@ function init() {
window.addEventListener('resize', onresize);
var toolbox = document.getElementById('toolbox');
mainWorkspace = Blockly.inject('blockly', {toolbox: toolbox});
mainWorkspace = Blockly.inject('blockly',
{toolbox: toolbox, media: '../../media/'});
// Create the root block.
if ('BlocklyStorage' in window && window.location.hash.length > 1) {
BlocklyStorage.retrieveXml(window.location.hash.substring(1));
BlocklyStorage.retrieveXml(window.location.hash.substring(1),
mainWorkspace);
} else {
var rootBlock = Blockly.Block.obtain(mainWorkspace, 'factory_base');
rootBlock.initSvg();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment