From c6463acc70532bc5b4066e763678b70edc4bbb37 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Tue, 6 Aug 2019 11:03:53 -0700 Subject: [PATCH] Cleanup and comments --- core/field.js | 17 +++++++++++------ core/field_colour.js | 2 +- core/field_dropdown.js | 14 ++++++++------ core/field_image.js | 7 +++++++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/core/field.js b/core/field.js index 946da2d09..3e218c538 100644 --- a/core/field.js +++ b/core/field.js @@ -136,9 +136,12 @@ Blockly.Field.BORDER_RECT_DEFAULT_HEIGHT = 16; */ Blockly.Field.TEXT_DEFAULT_HEIGHT = 12.5; -Blockly.Field.WIDTH_PADDING = 10; - -Blockly.Field.HEIGHT_PADDING = 10; +/** + * The padding added to the width by the border rect, if it exists. + * @type {number} + * @package + */ +Blockly.Field.X_PADDING = 10; /** * Name of field. Unique within each block. @@ -297,11 +300,13 @@ Blockly.Field.prototype.initView = function() { Blockly.Field.prototype.createBorderRect_ = function() { this.size_.height = Math.max(this.size_.height, Blockly.Field.BORDER_RECT_DEFAULT_HEIGHT); + this.size_.width = + Math.max(this.size_.width, Blockly.Field.X_PADDING); this.borderRect_ = Blockly.utils.dom.createSvgElement('rect', { 'rx': 4, 'ry': 4, - 'x': -Blockly.Field.WIDTH_PADDING / 2, + 'x': -Blockly.Field.X_PADDING / 2, 'y': 0, 'height': this.size_.height, 'width': this.size_.width @@ -318,7 +323,7 @@ Blockly.Field.prototype.createTextElement_ = function() { this.textElement_ = Blockly.utils.dom.createSvgElement('text', { 'class': 'blocklyText', - // This may just be trying to vertically center the text? + // The y position is the baseline of the text. 'y': Blockly.Field.TEXT_DEFAULT_HEIGHT, 'x': 0 }, this.fieldGroup_); @@ -587,7 +592,7 @@ Blockly.Field.prototype.updateSize_ = function() { var textWidth = Blockly.Field.getCachedWidth(this.textElement_); var totalWidth = textWidth; if (this.borderRect_) { - totalWidth += Blockly.Field.WIDTH_PADDING; + totalWidth += Blockly.Field.X_PADDING; this.borderRect_.setAttribute('width', totalWidth); } this.size_.width = totalWidth; diff --git a/core/field_colour.js b/core/field_colour.js index d2acbf9ca..0dab6ac69 100644 --- a/core/field_colour.js +++ b/core/field_colour.js @@ -78,7 +78,7 @@ Blockly.FieldColour.fromJson = function(options) { * @private * @const */ -Blockly.FieldColour.DEFAULT_WIDTH = 16 + Blockly.Field.WIDTH_PADDING; +Blockly.FieldColour.DEFAULT_WIDTH = 16 + Blockly.Field.X_PADDING; /** * Default height of a colour field. diff --git a/core/field_dropdown.js b/core/field_dropdown.js index 88a368c8f..6d1d5ed6f 100644 --- a/core/field_dropdown.js +++ b/core/field_dropdown.js @@ -99,9 +99,11 @@ Blockly.FieldDropdown.CHECKMARK_OVERHANG = 25; Blockly.FieldDropdown.MAX_MENU_HEIGHT_VH = 0.45; /** - * Used to position the imageElement_ correctly. + * The y offset from the top of the field to the top of the image, if an image + * is selected. * @type {number} * @const + * @private */ Blockly.FieldDropdown.IMAGE_Y_OFFSET = 5; @@ -109,6 +111,7 @@ Blockly.FieldDropdown.IMAGE_Y_OFFSET = 5; * The total vertical padding above and below an image. * @type {number} * @const + * @private */ Blockly.FieldDropdown.IMAGE_Y_PADDING = Blockly.FieldDropdown.IMAGE_Y_OFFSET * 2; @@ -160,8 +163,6 @@ Blockly.FieldDropdown.prototype.initView = function() { } else { this.textElement_.appendChild(this.arrow_); } - - this.contentDimensions_ = new Blockly.utils.Size(0, 0); }; /** @@ -493,11 +494,12 @@ Blockly.FieldDropdown.prototype.renderSelectedImage_ = function() { var arrowWidth = Blockly.Field.getCachedWidth(this.arrow_); - // Height and width include the border rect. var imageHeight = Number(this.imageJson_.height); var imageWidth = Number(this.imageJson_.width); + + // Height and width include the border rect. this.size_.height = imageHeight + Blockly.FieldDropdown.IMAGE_Y_PADDING; - this.size_.width = imageWidth + arrowWidth + Blockly.Field.WIDTH_PADDING; + this.size_.width = imageWidth + arrowWidth + Blockly.Field.X_PADDING; if (this.sourceBlock_.RTL) { this.imageElement_.setAttribute('x', arrowWidth); @@ -519,7 +521,7 @@ Blockly.FieldDropdown.prototype.renderSelectedText_ = function() { // Height and width include the border rect. this.size_.height = Blockly.Field.BORDER_RECT_DEFAULT_HEIGHT; this.size_.width = - Blockly.Field.getCachedWidth(this.textElement_) + Blockly.Field.WIDTH_PADDING; + Blockly.Field.getCachedWidth(this.textElement_) + Blockly.Field.X_PADDING; }; /** diff --git a/core/field_image.js b/core/field_image.js index f154e8b36..8608a6b5b 100644 --- a/core/field_image.js +++ b/core/field_image.js @@ -65,6 +65,7 @@ Blockly.FieldImage = function(src, width, height, throw Error('Height and width values of an image field must be greater' + ' than 0.'); } + // Store the image height, since it is different from the field height. this.imageHeight_ = imageHeight; this.size_ = new Blockly.utils.Size(imageWidth, imageHeight + Blockly.FieldImage.Y_PADDING); @@ -115,6 +116,12 @@ 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; /**