From 2b335dcb6b5bd67ee1a12338c2ce80d231772ad3 Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Wed, 11 Dec 2019 13:42:10 -0800 Subject: [PATCH] Make alignment common renderer behaviour (#3498) * Make alignment common --- core/renderers/common/info.js | 26 ++++++++++++++++++++------ core/renderers/geras/info.js | 28 ---------------------------- core/renderers/thrasos/info.js | 28 ---------------------------- core/renderers/zelos/info.js | 17 ----------------- tests/blocks/test_blocks.js | 6 +++--- 5 files changed, 23 insertions(+), 82 deletions(-) diff --git a/core/renderers/common/info.js b/core/renderers/common/info.js index 4203d9c92..bf7fd3c1a 100644 --- a/core/renderers/common/info.js +++ b/core/renderers/common/info.js @@ -560,14 +560,28 @@ Blockly.blockRendering.RenderInfo.prototype.alignRowElements_ = function() { */ Blockly.blockRendering.RenderInfo.prototype.addAlignmentPadding_ = function(row, missingSpace) { + var firstSpacer = row.getFirstSpacer(); var lastSpacer = row.getLastSpacer(); - if (lastSpacer) { - lastSpacer.width += missingSpace; - row.width += missingSpace; - if (row.hasExternalInput || row.hasStatement) { - row.widthWithConnectedBlocks += missingSpace; - } + if (row.hasExternalInput || row.hasStatement) { + row.widthWithConnectedBlocks += missingSpace; } + + // Decide where the extra padding goes. + if (row.align == Blockly.ALIGN_LEFT) { + // Add padding to the end of the row. + lastSpacer.width += missingSpace; + } else if (row.align == Blockly.ALIGN_CENTRE) { + // Split the padding between the beginning and end of the row. + firstSpacer.width += missingSpace / 2; + lastSpacer.width += missingSpace / 2; + } else if (row.align == Blockly.ALIGN_RIGHT) { + // Add padding at the beginning of the row. + firstSpacer.width += missingSpace; + } else { + // Default to left-aligning. + lastSpacer.width += missingSpace; + } + row.width += missingSpace; }; /** diff --git a/core/renderers/geras/info.js b/core/renderers/geras/info.js index cb929766a..f0b2dcb25 100644 --- a/core/renderers/geras/info.js +++ b/core/renderers/geras/info.js @@ -307,34 +307,6 @@ Blockly.geras.RenderInfo.prototype.getInRowSpacing_ = function(prev, next) { return this.constants_.MEDIUM_PADDING; }; -/** - * @override - */ -Blockly.geras.RenderInfo.prototype.addAlignmentPadding_ = function(row, missingSpace) { - var firstSpacer = row.getFirstSpacer(); - var lastSpacer = row.getLastSpacer(); - if (row.hasExternalInput || row.hasStatement) { - row.widthWithConnectedBlocks += missingSpace; - } - - // Decide where the extra padding goes. - if (row.align == Blockly.ALIGN_LEFT) { - // Add padding to the end of the row. - lastSpacer.width += missingSpace; - } else if (row.align == Blockly.ALIGN_CENTRE) { - // Split the padding between the beginning and end of the row. - firstSpacer.width += missingSpace / 2; - lastSpacer.width += missingSpace / 2; - } else if (row.align == Blockly.ALIGN_RIGHT) { - // Add padding at the beginning of the row. - firstSpacer.width += missingSpace; - } else { - // Default to left-aligning. - lastSpacer.width += missingSpace; - } - row.width += missingSpace; -}; - /** * @override */ diff --git a/core/renderers/thrasos/info.js b/core/renderers/thrasos/info.js index b8772cbb9..6484d86ee 100644 --- a/core/renderers/thrasos/info.js +++ b/core/renderers/thrasos/info.js @@ -240,34 +240,6 @@ Blockly.thrasos.RenderInfo.prototype.getInRowSpacing_ = function(prev, next) { return this.constants_.MEDIUM_PADDING; }; -/** - * @override - */ -Blockly.thrasos.RenderInfo.prototype.addAlignmentPadding_ = function(row, missingSpace) { - var firstSpacer = row.getFirstSpacer(); - var lastSpacer = row.getLastSpacer(); - if (row.hasExternalInput || row.hasStatement) { - row.widthWithConnectedBlocks += missingSpace; - } - - // Decide where the extra padding goes. - if (row.align == Blockly.ALIGN_LEFT) { - // Add padding to the end of the row. - lastSpacer.width += missingSpace; - } else if (row.align == Blockly.ALIGN_CENTRE) { - // Split the padding between the beginning and end of the row. - firstSpacer.width += missingSpace / 2; - lastSpacer.width += missingSpace / 2; - } else if (row.align == Blockly.ALIGN_RIGHT) { - // Add padding at the beginning of the row. - firstSpacer.width += missingSpace; - } else { - // Default to left-aligning. - lastSpacer.width += missingSpace; - } - row.width += missingSpace; -}; - /** * @override */ diff --git a/core/renderers/zelos/info.js b/core/renderers/zelos/info.js index 22e46dfe5..6c17ac4a6 100644 --- a/core/renderers/zelos/info.js +++ b/core/renderers/zelos/info.js @@ -199,23 +199,6 @@ Blockly.zelos.RenderInfo.prototype.getSpacerRowHeight_ = function( return this.constants_.MEDIUM_PADDING; }; -/** - * Modify the given row to add the given amount of padding around its fields. - * The exact location of the padding is based on the alignment property of the - * last input in the field. - * @param {Blockly.blockRendering.Row} row The row to add padding to. - * @param {number} missingSpace How much padding to add. - * @protected - */ -Blockly.zelos.RenderInfo.prototype.addAlignmentPadding_ = function(row, - missingSpace) { - var lastSpacer = row.getLastSpacer(); - if (lastSpacer) { - lastSpacer.width += missingSpace; - row.width += missingSpace; - } -}; - /** * Adjust the x position of fields to bump all non-label fields in the first row * past the notch position. This must be called before ``computeBounds`` is diff --git a/tests/blocks/test_blocks.js b/tests/blocks/test_blocks.js index f7dc74ddb..501c55b62 100644 --- a/tests/blocks/test_blocks.js +++ b/tests/blocks/test_blocks.js @@ -149,7 +149,7 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT }, { "type": "test_align_dummy_right", - "message0": "text %1 long text %2", + "message0": "text right %1 long text right %2", "args0": [ { "type": "input_dummy", @@ -164,7 +164,7 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT }, { "type": "test_align_all", - "message0": "text %1 long text %2 text %3 much longer text", + "message0": "text %1 long text left %2 text centre %3 much longer text right", "args0": [ { "type": "input_dummy", @@ -183,7 +183,7 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT }, { "type": "test_align_with_external_input", - "message0": "text %1 long text %2 text %3 much longer text %4", + "message0": "text right %1 long text centre %2 text left %3 much longer text %4", "args0": [ { "type": "input_dummy",