From 5ca8b36cfc2bed55044da0b7bf4b67dcbdb5cb3c Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Fri, 5 Feb 2016 18:41:47 -0800 Subject: [PATCH] Allow to be used to add gaps between blocks. --- core/flyout.js | 2 +- core/toolbox.js | 25 +++++++++++++++++++++++-- core/workspace_svg.js | 5 ++--- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/core/flyout.js b/core/flyout.js index 0d4d4c200..82def585e 100644 --- a/core/flyout.js +++ b/core/flyout.js @@ -399,7 +399,7 @@ Blockly.Flyout.prototype.show = function(xmlList) { } blocks.push(block); var gap = parseInt(xml.getAttribute('gap'), 10); - gaps.push(gap || margin * 3); + gaps.push(isNaN(gap) ? margin * 3 : gap); } } diff --git a/core/toolbox.js b/core/toolbox.js index 1e67dd382..6fb6c9cf8 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -191,6 +191,7 @@ Blockly.Toolbox.prototype.populate_ = function(newTree) { rootOut.blocks = []; var hasColours = false; function syncTrees(treeIn, treeOut) { + var lastElement = null; for (var i = 0, childIn; childIn = treeIn.childNodes[i]; i++) { if (!childIn.tagName) { // Skip over text. @@ -227,13 +228,33 @@ Blockly.Toolbox.prototype.populate_ = function(newTree) { } else { childOut.setExpanded(false); } + lastElement = childIn; break; case 'SEP': - treeOut.add(new Blockly.Toolbox.TreeSeparator()); + if (lastElement) { + if (lastElement.tagName.toUpperCase() == 'CATEGORY') { + // Separator between two categories. + // + treeOut.add(new Blockly.Toolbox.TreeSeparator()); + } else { + // Change the gap between two blocks. + // + // The default gap is 24, can be set larger or smaller. + // Note that a deprecated method is to add a gap to a block. + // + var newGap = parseFloat(childIn.getAttribute('gap')); + if (!isNaN(newGap)) { + var oldGap = parseFloat(lastElement.getAttribute('gap')); + var gap = isNaN(oldGap) ? newGap : oldGap + newGap; + lastElement.setAttribute('gap', gap); + } + } + } break; case 'BLOCK': case 'SHADOW': treeOut.blocks.push(childIn); + lastElement = childIn; break; } } @@ -298,7 +319,7 @@ Blockly.Toolbox.prototype.getClientRect = function() { if (this.workspace_.RTL) { var width = toolboxRect.left + toolboxRect.width + BIG_NUM; return new goog.math.Rect(toolboxRect.left, -BIG_NUM, width, BIG_NUM * 2); - } + } // LTR var width = BIG_NUM + toolboxRect.width + toolboxRect.left; return new goog.math.Rect(-BIG_NUM, -BIG_NUM, width, BIG_NUM * 2); diff --git a/core/workspace_svg.js b/core/workspace_svg.js index b2f18d8d7..bc66c39aa 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -881,13 +881,12 @@ Blockly.WorkspaceSvg.prototype.updateToolbox = function(tree) { if (this.options.languageTree) { throw 'Can\'t nullify an existing toolbox.'; } - // No change (null to null). - return; + return; // No change (null to null). } if (!this.options.languageTree) { throw 'Existing toolbox is null. Can\'t create new toolbox.'; } - if (this.options.hasCategories) { + if (tree.getElementsByTagName('category').length) { if (!this.toolbox_) { throw 'Existing toolbox has no categories. Can\'t change mode.'; }