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