mirror of
https://github.com/google/blockly.git
synced 2026-01-05 08:00:09 +01:00
Fix cloud storage (issue 100)
This commit is contained in:
@@ -29,13 +29,12 @@ var BlocklyStorage = {};
|
||||
|
||||
/**
|
||||
* Backup code blocks to localStorage.
|
||||
* @param {Blockly.WorkspaceSvg} opt_workspace Workspace.
|
||||
* @param {!Blockly.WorkspaceSvg} workspace Workspace.
|
||||
* @private
|
||||
*/
|
||||
BlocklyStorage.backupBlocks_ = function(opt_workspace) {
|
||||
BlocklyStorage.backupBlocks_ = function(workspace) {
|
||||
if ('localStorage' in window) {
|
||||
var workspace = opt_workspace || Blockly.getMainWorkspace();
|
||||
var xml = Blockly.Xml.workspaceToDom(Blockly.workspace);
|
||||
var xml = Blockly.Xml.workspaceToDom(workspace);
|
||||
// Gets the current URL, not including the hash.
|
||||
var url = window.location.href.split('#')[0];
|
||||
window.localStorage.setItem(url, Blockly.Xml.domToText(xml));
|
||||
@@ -44,9 +43,12 @@ BlocklyStorage.backupBlocks_ = function(opt_workspace) {
|
||||
|
||||
/**
|
||||
* Bind the localStorage backup function to the unload event.
|
||||
* @param {Blockly.WorkspaceSvg} opt_workspace Workspace.
|
||||
*/
|
||||
BlocklyStorage.backupOnUnload = function() {
|
||||
window.addEventListener('unload', BlocklyStorage.backupBlocks_, false);
|
||||
BlocklyStorage.backupOnUnload = function(opt_workspace) {
|
||||
var workspace = opt_workspace || Blockly.getMainWorkspace();
|
||||
window.addEventListener('unload',
|
||||
function() {BlocklyStorage.backupBlocks_(workspace);}, false);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -70,7 +72,7 @@ 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);
|
||||
BlocklyStorage.makeRequest_('/storage', 'xml', data, workspace);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -335,18 +335,6 @@ Code.renderContent = function() {
|
||||
Code.init = function() {
|
||||
Code.initLanguage();
|
||||
|
||||
// Disable the link button if page isn't backed by App Engine storage.
|
||||
var linkButton = document.getElementById('linkButton');
|
||||
if ('BlocklyStorage' in window) {
|
||||
BlocklyStorage['HTTPREQUEST_ERROR'] = MSG['httpRequestError'];
|
||||
BlocklyStorage['LINK_ALERT'] = MSG['linkAlert'];
|
||||
BlocklyStorage['HASH_ERROR'] = MSG['hashError'];
|
||||
BlocklyStorage['XML_ERROR'] = MSG['xmlError'];
|
||||
Code.bindClick(linkButton, BlocklyStorage.link);
|
||||
} else if (linkButton) {
|
||||
linkButton.className = 'disabled';
|
||||
}
|
||||
|
||||
var rtl = Code.isRtl();
|
||||
var container = document.getElementById('content_area');
|
||||
var onresize = function(e) {
|
||||
@@ -390,7 +378,7 @@ Code.init = function() {
|
||||
|
||||
if ('BlocklyStorage' in window) {
|
||||
// Hook a save function onto unload.
|
||||
BlocklyStorage.backupOnUnload();
|
||||
BlocklyStorage.backupOnUnload(Code.workspace);
|
||||
}
|
||||
|
||||
Code.tabClick(Code.selected);
|
||||
@@ -399,6 +387,18 @@ Code.init = function() {
|
||||
Code.bindClick('trashButton',
|
||||
function() {Code.discard(); Code.renderContent();});
|
||||
Code.bindClick('runButton', Code.runJS);
|
||||
// Disable the link button if page isn't backed by App Engine storage.
|
||||
var linkButton = document.getElementById('linkButton');
|
||||
if ('BlocklyStorage' in window) {
|
||||
BlocklyStorage['HTTPREQUEST_ERROR'] = MSG['httpRequestError'];
|
||||
BlocklyStorage['LINK_ALERT'] = MSG['linkAlert'];
|
||||
BlocklyStorage['HASH_ERROR'] = MSG['hashError'];
|
||||
BlocklyStorage['XML_ERROR'] = MSG['xmlError'];
|
||||
Code.bindClick(linkButton,
|
||||
function() {BlocklyStorage.link(Code.workspace);});
|
||||
} else if (linkButton) {
|
||||
linkButton.className = 'disabled';
|
||||
}
|
||||
|
||||
for (var i = 0; i < Code.TABS_.length; i++) {
|
||||
var name = Code.TABS_[i];
|
||||
|
||||
Reference in New Issue
Block a user