From a779d44104c6fdc29e93b9a4e7cf1f4bf243a14a Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Mon, 9 Dec 2019 13:17:00 -0800 Subject: [PATCH] Reduce the vertical spacing for spacers before and after an input row if it contains non-shadow connected blocks. (#3496) --- core/renderers/common/debugger.js | 10 +++++-- core/renderers/zelos/info.js | 46 +++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/core/renderers/common/debugger.js b/core/renderers/common/debugger.js index 294ffc0d0..538c1a4a2 100644 --- a/core/renderers/common/debugger.js +++ b/core/renderers/common/debugger.js @@ -94,14 +94,20 @@ Blockly.blockRendering.Debug.prototype.drawSpacerRow = function(row, cursorY, is return; } + var height = Math.abs(row.height); + var isNegativeSpacing = row.height < 0; + if (isNegativeSpacing) { + cursorY -= height; + } + this.debugElements_.push(Blockly.utils.dom.createSvgElement('rect', { 'class': 'rowSpacerRect blockRenderDebug', 'x': isRtl ? -(row.xPos + row.width) : row.xPos, 'y': cursorY, 'width': row.width, - 'height': row.height, - 'stroke': 'blue', + 'height': height, + 'stroke': isNegativeSpacing ? 'black' : 'blue', 'fill': 'blue', 'fill-opacity': '0.5', 'stroke-width': '1px' diff --git a/core/renderers/zelos/info.js b/core/renderers/zelos/info.js index 27ec71fd2..56a549235 100644 --- a/core/renderers/zelos/info.js +++ b/core/renderers/zelos/info.js @@ -291,12 +291,13 @@ Blockly.zelos.RenderInfo.prototype.finalizeOutputConnection_ = function() { }; /** - * Finalize alignment of elements on the block. In particular, reduce the - * implicit spacing created by the left and right output connection shapes by - * adding setting negative spacing onto the leftmost and rightmost spacers. + * Finalize horizontal alignment of elements on the block. In particular, + * reduce the implicit spacing created by the left and right output connection + * shapes by adding setting negative spacing onto the leftmost and rightmost + * spacers. * @protected */ -Blockly.zelos.RenderInfo.prototype.finalizeAlignment_ = function() { +Blockly.zelos.RenderInfo.prototype.finalizeHorizontalAlignment_ = function() { if (!this.outputConnection) { return; } @@ -379,11 +380,46 @@ Blockly.zelos.RenderInfo.prototype.getNegativeSpacing_ = function(elem) { return 0; }; +/** + * Finalize vertical alignment of rows on a block. In particular, reduce the + * implicit spacing when a non-shadow block is connected to any of an input + * row's inline inputs. + * @protected + */ +Blockly.zelos.RenderInfo.prototype.finalizeVerticalAlignment_ = function() { + if (this.outputConnection) { + return; + } + for (var i = 2; i < this.rows.length - 1; i += 2) { + var prevSpacer = this.rows[i - 1]; + var row = this.rows[i]; + var nextSpacer = this.rows[i + 1]; + + if (Blockly.blockRendering.Types.isInputRow(row)) { + // Determine if the input row has non-shadow connected blocks. + var hasNonShadowConnectedBlocks = false; + for (var j = 0, elem; (elem = row.elements[j]); j++) { + if (Blockly.blockRendering.Types.isInlineInput(elem) && + elem.connectedBlock && !elem.connectedBlock.isShadow()) { + hasNonShadowConnectedBlocks = true; + break; + } + } + if (hasNonShadowConnectedBlocks) { + // Reduce the previous and next spacer's height. + prevSpacer.height -= this.constants_.GRID_UNIT; + nextSpacer.height -= this.constants_.GRID_UNIT; + } + } + } +}; + /** * @override */ Blockly.zelos.RenderInfo.prototype.finalize_ = function() { this.finalizeOutputConnection_(); - this.finalizeAlignment_(); + this.finalizeHorizontalAlignment_(); + this.finalizeVerticalAlignment_(); Blockly.zelos.RenderInfo.superClass_.finalize_.call(this); };