From a5a4e5161c28f7b70427f0437fd6ada8304d65fd Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Thu, 17 Oct 2019 16:30:48 -0500 Subject: [PATCH] Fix 11 warnings related to block_svg (#3276) * Fix 11 warnings related to block_svg --- core/block_svg.js | 43 +++++++++++++++++++-------------- core/comment.js | 8 +++--- core/field.js | 10 ++++++++ core/gesture.js | 2 +- core/keyboard_nav/navigation.js | 2 +- core/workspace_svg.js | 2 +- 6 files changed, 42 insertions(+), 25 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index c9859f334..d3d5ffa24 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -97,6 +97,9 @@ Blockly.BlockSvg = function(workspace, prototypeName, opt_id) { /** @type {boolean} */ this.rendered = false; + /** @type {!Blockly.WorkspaceSvg} */ + this.workspace = workspace; + /** * Whether to move the block to the drag surface when it is dragged. * True if it should move, false if it should be translated directly. @@ -104,7 +107,7 @@ Blockly.BlockSvg = function(workspace, prototypeName, opt_id) { * @private */ this.useDragSurface_ = - Blockly.utils.is3dSupported() && !!workspace.blockDragSurface_; + Blockly.utils.is3dSupported() && !!workspace.getBlockDragSurface(); Blockly.Tooltip.bindMouseEvents(this.svgPath_); Blockly.BlockSvg.superClass_.constructor.call(this, @@ -254,14 +257,15 @@ Blockly.BlockSvg.prototype.initSvg = function() { } this.updateColour(); this.updateMovable(); - if (!this.workspace.options.readOnly && !this.eventsInit_) { + var svg = this.getSvgRoot(); + if (!this.workspace.options.readOnly && !this.eventsInit_ && svg) { Blockly.bindEventWithChecks_( - this.getSvgRoot(), 'mousedown', this, this.onMouseDown_); + svg, 'mousedown', this, this.onMouseDown_); } this.eventsInit_ = true; - if (!this.getSvgRoot().parentNode) { - this.workspace.getCanvas().appendChild(this.getSvgRoot()); + if (!svg.parentNode) { + this.workspace.getCanvas().appendChild(svg); } }; @@ -355,7 +359,7 @@ Blockly.BlockSvg.prototype.getIcons = function() { /** * Set parent of this block to be a new block or null. - * @param {Blockly.BlockSvg} newParent New parent block. + * @param {Blockly.Block} newParent New parent block. * @override */ Blockly.BlockSvg.prototype.setParent = function(newParent) { @@ -405,7 +409,7 @@ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() { var y = 0; var dragSurfaceGroup = this.useDragSurface_ ? - this.workspace.blockDragSurface_.getGroup() : null; + this.workspace.getBlockDragSurface().getGroup() : null; var element = this.getSvgRoot(); if (element) { @@ -417,9 +421,9 @@ Blockly.BlockSvg.prototype.getRelativeToSurfaceXY = function() { // If this element is the current element on the drag surface, include // the translation of the drag surface itself. if (this.useDragSurface_ && - this.workspace.blockDragSurface_.getCurrentBlock() == element) { + this.workspace.getBlockDragSurface().getCurrentBlock() == element) { var surfaceTranslation = - this.workspace.blockDragSurface_.getSurfaceTranslation(); + this.workspace.getBlockDragSurface().getSurfaceTranslation(); x += surfaceTranslation.x; y += surfaceTranslation.y; } @@ -480,9 +484,12 @@ Blockly.BlockSvg.prototype.moveToDragSurface_ = function() { // This is in workspace coordinates. var xy = this.getRelativeToSurfaceXY(); this.clearTransformAttributes_(); - this.workspace.blockDragSurface_.translateSurface(xy.x, xy.y); + this.workspace.getBlockDragSurface().translateSurface(xy.x, xy.y); // Execute the move on the top-level SVG component - this.workspace.blockDragSurface_.setBlocksAndShow(this.getSvgRoot()); + var svg = this.getSvgRoot(); + if (svg) { + this.workspace.getBlockDragSurface().setBlocksAndShow(svg); + } }; /** @@ -508,7 +515,7 @@ Blockly.BlockSvg.prototype.moveOffDragSurface_ = function(newXY) { } // Translate to current position, turning off 3d. this.translate(newXY.x, newXY.y); - this.workspace.blockDragSurface_.clearAndHide(this.workspace.getCanvas()); + this.workspace.getBlockDragSurface().clearAndHide(this.workspace.getCanvas()); }; /** @@ -521,7 +528,7 @@ Blockly.BlockSvg.prototype.moveOffDragSurface_ = function(newXY) { */ Blockly.BlockSvg.prototype.moveDuringDrag = function(newLoc) { if (this.useDragSurface_) { - this.workspace.blockDragSurface_.translateSurface(newLoc.x, newLoc.y); + this.workspace.getBlockDragSurface().translateSurface(newLoc.x, newLoc.y); } else { this.svgGroup_.translate_ = 'translate(' + newLoc.x + ',' + newLoc.y + ')'; this.svgGroup_.setAttribute('transform', @@ -680,7 +687,7 @@ Blockly.BlockSvg.prototype.setCollapsed = function(collapsed) { */ Blockly.BlockSvg.prototype.tab = function(start, forward) { var list = this.createTabList_(); - var i = list.indexOf(start); + var i = start != null ? list.indexOf(start) : -1; if (i == -1) { // No start location, start at the beginning or end. i = forward ? -1 : list.length; @@ -693,7 +700,7 @@ Blockly.BlockSvg.prototype.tab = function(start, forward) { parent.tab(this, forward); } } else if (target instanceof Blockly.Field) { - target.showEditor_(); + target.showEditor(); } else { target.tab(null, forward); } @@ -701,7 +708,7 @@ Blockly.BlockSvg.prototype.tab = function(start, forward) { /** * Create an ordered list of all text fields and connected inputs. - * @return {!Array.} The ordered list. + * @return {!Array.} The ordered list. * @private */ Blockly.BlockSvg.prototype.createTabList_ = function() { @@ -1033,7 +1040,7 @@ Blockly.BlockSvg.prototype.dispose = function(healStack, animate) { for (var i = 0; i < icons.length; i++) { icons[i].dispose(); } - Blockly.BlockSvg.superClass_.dispose.call(this, healStack); + Blockly.BlockSvg.superClass_.dispose.call(this, !!healStack); Blockly.utils.dom.removeNode(this.svgGroup_); blockWorkspace.resizeContents(); @@ -1101,7 +1108,7 @@ Blockly.BlockSvg.prototype.setBorderColour_ = function() { * @private */ Blockly.BlockSvg.prototype.setShadowColour_ = function() { - var shadowColour = this.getColourShadow(); + var shadowColour = this.getColourShadow() || ''; this.svgPathLight_.style.display = 'none'; this.svgPathDark_.setAttribute('fill', shadowColour); diff --git a/core/comment.js b/core/comment.js index fe9494630..803e1459c 100644 --- a/core/comment.js +++ b/core/comment.js @@ -252,22 +252,22 @@ Blockly.Comment.prototype.createEditableBubble_ = function() { /** * Show a non-editable bubble. * @private + * @suppress {checkTypes} Suppress `this` type mismatch. */ Blockly.Comment.prototype.createNonEditableBubble_ = function() { // TODO (#2917): It would be great if the comment could support line breaks. - Blockly.Warning.prototype.createBubble.call( - /** @type {Blockly.Warning} */ (this)); + Blockly.Warning.prototype.createBubble.call(this); }; /** * Dispose of the bubble. * @private + * @suppress {checkTypes} Suppress `this` type mismatch. */ Blockly.Comment.prototype.disposeBubble_ = function() { if (this.paragraphElement_) { // We're using the warning UI so we have to let it dispose. - Blockly.Warning.prototype.disposeBubble.call( - /** @type {Blockly.Warning} */ (this)); + Blockly.Warning.prototype.disposeBubble.call(this); return; } diff --git a/core/field.js b/core/field.js index ee88f82f7..5e5216533 100644 --- a/core/field.js +++ b/core/field.js @@ -612,6 +612,16 @@ Blockly.Field.prototype.render_ = function() { } }; +/** + * Show an editor when the field is clicked only if the field is clickable. + * @package + */ +Blockly.Field.prototype.showEditor = function() { + if (this.isClickable()) { + this.showEditor_(); + } +}; + /** * Updates the width of the field. Redirects to updateSize_(). * @deprecated May 2019 Use Blockly.Field.updateSize_() to force an update diff --git a/core/gesture.js b/core/gesture.js index df369b9fe..c0071aaf9 100644 --- a/core/gesture.js +++ b/core/gesture.js @@ -723,7 +723,7 @@ Blockly.Gesture.prototype.doBubbleClick_ = function() { * @private */ Blockly.Gesture.prototype.doFieldClick_ = function() { - this.startField_.showEditor_(); + this.startField_.showEditor(); this.bringBlockToFront_(); }; diff --git a/core/keyboard_nav/navigation.js b/core/keyboard_nav/navigation.js index 07b34d68b..aad1c5204 100644 --- a/core/keyboard_nav/navigation.js +++ b/core/keyboard_nav/navigation.js @@ -924,7 +924,7 @@ Blockly.navigation.handleEnterForWS_ = function() { var curNode = cursor.getCurNode(); var nodeType = curNode.getType(); if (nodeType == Blockly.ASTNode.types.FIELD) { - curNode.getLocation().showEditor_(); + curNode.getLocation().showEditor(); } else if (curNode.isConnection() || nodeType == Blockly.ASTNode.types.WORKSPACE) { Blockly.navigation.markAtCursor_(); diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 1bbd16165..fd9fba79a 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -533,7 +533,7 @@ Blockly.WorkspaceSvg.prototype.isVisible = function() { * The origin (0,0) is the top-left corner of the Blockly SVG. * @param {!Element} element Element to find the coordinates of. * @return {!Blockly.utils.Coordinate} Object with .x and .y properties. - * @private + * @package */ Blockly.WorkspaceSvg.prototype.getSvgXY = function(element) { var x = 0;