Merge pull request #1178 from picklesrus/develop-clear-and-load-batch

Add an xml utility to clear the workspace and then load xml into the …
This commit is contained in:
Paul Kaplan 2017-10-25 10:31:28 -04:00 committed by GitHub
commit 54bf2d6b62
2 changed files with 44 additions and 1 deletions

View file

@ -150,6 +150,14 @@ Blockly.WorkspaceSvg.prototype.isMutator = false;
*/
Blockly.WorkspaceSvg.prototype.resizesEnabled_ = true;
/**
* Whether this workspace has toolbox/flyout refreshes enabled.
* Disable during batch operations for a performance improvement.
* @type {boolean}
* @private
*/
Blockly.WorkspaceSvg.prototype.toolboxRefreshEnabled_ = true;
/**
* Current horizontal scrolling offset in pixel units.
* @type {number}
@ -1004,7 +1012,10 @@ Blockly.WorkspaceSvg.prototype.paste = function(xmlBlock) {
* @private
*/
Blockly.WorkspaceSvg.prototype.refreshToolboxSelection_ = function() {
if (this.toolbox_ && this.toolbox_.flyout_ && !this.currentGesture_) {
// Updating the toolbox can be expensive. Don't do it when when it is
// disabled.
if (this.toolbox_ && this.toolbox_.flyout_ && !this.currentGesture_ &&
this.toolboxRefreshEnabled_) {
this.toolbox_.refreshSelection();
}
};
@ -1866,6 +1877,22 @@ Blockly.WorkspaceSvg.prototype.setResizesEnabled = function(enabled) {
}
};
/**
* Update whether this workspace has toolbox refreshes enabled.
* If enabled, the toolbox will refresh when appropriate.
* If disabled, workspace will not refresh until re-enabled.
* Use to avoid refreshing during a batch operation, for performance.
* @param {boolean} enabled Whether refreshes should be enabled.
*/
Blockly.WorkspaceSvg.prototype.setToolboxRefreshEnabled = function(enabled) {
var reenabled = (!this.toolboxRefreshEnabled_ && enabled);
this.toolboxRefreshEnabled_ = enabled;
if (reenabled) {
// Newly enabled. Trigger a refresh.
this.refreshToolboxSelection_();
}
};
/**
* Dispose of all blocks in workspace, with an optimization to prevent resizes.

View file

@ -301,6 +301,22 @@ Blockly.Xml.textToDom = function(text) {
return dom.firstChild;
};
/**
* Clear the given workspace then decode an XML DOM and
* create blocks on the workspace.
* @param {!Element} xml XML DOM.
* @param {!Blockly.Workspace} workspace The workspace.
* @return {Array.<string>} An array containing new block ids.
*/
Blockly.Xml.clearWorkspaceAndLoadFromXml = function(xml, workspace) {
workspace.setResizesEnabled(false);
workspace.setToolboxRefreshEnabled(false);
workspace.clear();
var blockIds = Blockly.Xml.domToWorkspace(xml, workspace);
workspace.setResizesEnabled(true);
workspace.setToolboxRefreshEnabled(true);
return blockIds;
};
/**
* Decode an XML DOM and create blocks on the workspace.
* @param {!Element} xml XML DOM.