Allow <sep> to be used to add gaps between blocks.

This commit is contained in:
Neil Fraser 2016-02-05 18:41:47 -08:00
parent 3e6e962412
commit 5ca8b36cfc
3 changed files with 26 additions and 6 deletions

View file

@ -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);
}
}

View file

@ -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.
// <sep></sep>
treeOut.add(new Blockly.Toolbox.TreeSeparator());
} else {
// Change the gap between two blocks.
// <sep gap="36"></sep>
// The default gap is 24, can be set larger or smaller.
// Note that a deprecated method is to add a gap to a block.
// <block type="math_arithmetic" gap="8"></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);

View file

@ -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.';
}