From ed245a3c9e64cd60a98e4ef523689970b01c7fde Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Tue, 6 Aug 2019 11:44:03 -0700 Subject: [PATCH] Remove dealWithOffsetFields --- core/field.js | 11 ++++++++-- core/field_checkbox.js | 2 +- core/field_dropdown.js | 12 +++++++---- core/field_image.js | 16 +++++++-------- .../block_render_draw.js | 20 ------------------- 5 files changed, 26 insertions(+), 35 deletions(-) diff --git a/core/field.js b/core/field.js index 3e218c538..244a3e8d7 100644 --- a/core/field.js +++ b/core/field.js @@ -143,6 +143,12 @@ Blockly.Field.TEXT_DEFAULT_HEIGHT = 12.5; */ Blockly.Field.X_PADDING = 10; +/** + * The default offset between the left of the text element and the left of the + * border rect, if the border rect exists. + * @type {[type]} + */ +Blockly.Field.DEFAULT_TEXT_OFFSET = Blockly.Field.X_PADDING / 2; /** * Name of field. Unique within each block. * Static labels are usually unnamed. @@ -306,7 +312,7 @@ Blockly.Field.prototype.createBorderRect_ = function() { { 'rx': 4, 'ry': 4, - 'x': -Blockly.Field.X_PADDING / 2, + 'x': 0, 'y': 0, 'height': this.size_.height, 'width': this.size_.width @@ -320,12 +326,13 @@ Blockly.Field.prototype.createBorderRect_ = function() { * @protected */ Blockly.Field.prototype.createTextElement_ = function() { + var xOffset = this.borderRect_ ? Blockly.Field.DEFAULT_TEXT_OFFSET : 0; this.textElement_ = Blockly.utils.dom.createSvgElement('text', { 'class': 'blocklyText', // The y position is the baseline of the text. 'y': Blockly.Field.TEXT_DEFAULT_HEIGHT, - 'x': 0 + 'x': xOffset }, this.fieldGroup_); this.textContent_ = document.createTextNode(''); this.textElement_.appendChild(this.textContent_); diff --git a/core/field_checkbox.js b/core/field_checkbox.js index caae6d9eb..f75f0a800 100644 --- a/core/field_checkbox.js +++ b/core/field_checkbox.js @@ -84,7 +84,7 @@ Blockly.FieldCheckbox.CHECK_CHAR = '\u2713'; * @type {number} * @const */ -Blockly.FieldCheckbox.CHECK_X_OFFSET = -3; +Blockly.FieldCheckbox.CHECK_X_OFFSET = Blockly.Field.DEFAULT_TEXT_OFFSET - 3; /** * Used to correctly position the check mark. diff --git a/core/field_dropdown.js b/core/field_dropdown.js index 6d1d5ed6f..210e424ca 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -502,11 +502,15 @@ Blockly.FieldDropdown.prototype.renderSelectedImage_ = function() { this.size_.width = imageWidth + arrowWidth + Blockly.Field.X_PADDING; if (this.sourceBlock_.RTL) { - this.imageElement_.setAttribute('x', arrowWidth); - this.textElement_.setAttribute('x', -1); + var imageX = Blockly.Field.DEFAULT_TEXT_OFFSET + arrowWidth; + var arrowX = Blockly.Field.DEFAULT_TEXT_OFFSET - 1; + this.imageElement_.setAttribute('x', imageX); + this.textElement_.setAttribute('x', arrowX); } else { + var arrowX = imageWidth + arrowWidth + Blockly.Field.DEFAULT_TEXT_OFFSET + 1; this.textElement_.setAttribute('text-anchor', 'end'); - this.textElement_.setAttribute('x', imageWidth + arrowWidth + 1); + this.textElement_.setAttribute('x', arrowX); + this.imageElement_.setAttribute('x', Blockly.Field.DEFAULT_TEXT_OFFSET); } }; @@ -517,7 +521,7 @@ Blockly.FieldDropdown.prototype.renderSelectedImage_ = function() { Blockly.FieldDropdown.prototype.renderSelectedText_ = function() { this.textContent_.nodeValue = this.getDisplayText_(); this.textElement_.setAttribute('text-anchor', 'start'); - this.textElement_.setAttribute('x', 0); + this.textElement_.setAttribute('x', Blockly.Field.DEFAULT_TEXT_OFFSET); // Height and width include the border rect. this.size_.height = Blockly.Field.BORDER_RECT_DEFAULT_HEIGHT; this.size_.width = diff --git a/core/field_image.js b/core/field_image.js index 8608a6b5b..b0a13ec0c 100644 --- a/core/field_image.js +++ b/core/field_image.js @@ -99,6 +99,14 @@ Blockly.FieldImage.fromJson = function(options) { return new Blockly.FieldImage(src, width, height, alt, null, flipRtl); }; +/** + * Vertical padding below the image, which is included in the reported height of + * the field. + * @type {number} + * @private + */ +Blockly.FieldImage.Y_PADDING = 1; + /** * Editable fields usually show some sort of UI indicating they are * editable. This field should not. @@ -116,14 +124,6 @@ Blockly.FieldImage.prototype.EDITABLE = false; */ Blockly.FieldImage.prototype.isDirty_ = false; -/** - * Vertical padding below the image, which is included in the reported height of - * the field. - * @type {number} - * @private - */ -Blockly.FieldImage.Y_PADDING = 1; - /** * Create the block UI for this image. * @package diff --git a/core/renderers/block_rendering_rewrite/block_render_draw.js b/core/renderers/block_rendering_rewrite/block_render_draw.js index c9b683632..c4f5b2ec4 100644 --- a/core/renderers/block_rendering_rewrite/block_render_draw.js +++ b/core/renderers/block_rendering_rewrite/block_render_draw.js @@ -300,24 +300,6 @@ Blockly.blockRendering.Drawer.prototype.drawInternals_ = function() { } }; -/** - * Some fields are terrible and render offset from where they claim to be - * rendered. This function calculates an x offset for fields that need it. - * No one is happy about this. - * @param {!Blockly.Field} field The field to find an offset for. - * @return {number} How far to offset the field in the x direction. - * @private - */ -Blockly.blockRendering.Drawer.prototype.dealWithOffsetFields_ = function(field) { - if (field instanceof Blockly.FieldDropdown || - field instanceof Blockly.FieldTextInput || - field instanceof Blockly.FieldColour || - field instanceof Blockly.FieldCheckbox) { - return 5; - } - return 0; -}; - /** * Push a field or icon's new position to its SVG root. * @param {!Blockly.blockRendering.Icon|!Blockly.blockRendering.Field} fieldInfo @@ -341,8 +323,6 @@ Blockly.blockRendering.Drawer.prototype.layoutField_ = function(fieldInfo) { svgGroup.setAttribute('transform', 'translate(' + xPos + ',' + yPos + ')'); fieldInfo.icon.computeIconLocation(); } else { - xPos += this.dealWithOffsetFields_(fieldInfo.field); - svgGroup.setAttribute('transform', 'translate(' + xPos + ',' + yPos + ')'); }