From e07a3f03a9884d87aa352cfbf2f6b2ee66a46330 Mon Sep 17 00:00:00 2001 From: Sam El-Husseini Date: Thu, 17 Oct 2019 15:06:22 -0500 Subject: [PATCH] Fix 14 warnings related to workspace svg (#3273) * Fix warnings related to workspace svg --- core/options.js | 7 ++++--- core/workspace.js | 2 +- core/workspace_svg.js | 42 ++++++++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/core/options.js b/core/options.js index ed081da42..c02b44cc7 100644 --- a/core/options.js +++ b/core/options.js @@ -154,14 +154,15 @@ Blockly.Options.prototype.parentWorkspace = null; /** * If set, sets the translation of the workspace to match the scrollbars. + * @return {void} */ -Blockly.Options.prototype.setMetrics = null; +Blockly.Options.prototype.setMetrics; /** * Return an object with the metrics required to size the workspace. - * @return {Object} Contains size and position metrics, or null. + * @return {!Object} Contains size and position metrics. */ -Blockly.Options.prototype.getMetrics = null; +Blockly.Options.prototype.getMetrics; /** * Parse the user-specified move options, using reasonable defaults where diff --git a/core/workspace.js b/core/workspace.js index 8cecaea90..abb4ab85d 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -44,7 +44,7 @@ Blockly.Workspace = function(opt_options) { this.id = Blockly.utils.genUid(); Blockly.Workspace.WorkspaceDB_[this.id] = this; /** @type {!Blockly.Options} */ - this.options = opt_options || {}; + this.options = opt_options || /** @type {!Blockly.Options} */ ({}); /** @type {boolean} */ this.RTL = !!this.options.RTL; /** @type {boolean} */ diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 9f75e6520..1bbd16165 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -59,8 +59,10 @@ goog.require('Blockly.Xml'); Blockly.WorkspaceSvg = function(options, opt_blockDragSurface, opt_wsDragSurface) { Blockly.WorkspaceSvg.superClass_.constructor.call(this, options); + /** @type {function():!Object} */ this.getMetrics = options.getMetrics || Blockly.WorkspaceSvg.getTopLevelWorkspaceMetrics_; + /** @type {function(!Object)} */ this.setMetrics = options.setMetrics || Blockly.WorkspaceSvg.setTopLevelWorkspaceMetrics_; @@ -75,12 +77,12 @@ Blockly.WorkspaceSvg = function(options, } this.useWorkspaceDragSurface_ = - this.workspaceDragSurface_ && Blockly.utils.is3dSupported(); + !!this.workspaceDragSurface_ && Blockly.utils.is3dSupported(); /** * List of currently highlighted blocks. Block highlighting is often used to * visually mark blocks currently being executed. - * @type !Array. + * @type {!Array.} * @private */ this.highlightedBlocks_ = []; @@ -566,7 +568,7 @@ Blockly.WorkspaceSvg.prototype.getSvgXY = function(element) { * @package */ Blockly.WorkspaceSvg.prototype.getOriginOffsetInPixels = function() { - return Blockly.utils.getInjectionDivXY_(this.svgBlockCanvas_); + return Blockly.utils.getInjectionDivXY_(this.getCanvas()); }; /** @@ -927,18 +929,18 @@ Blockly.WorkspaceSvg.prototype.updateScreenCalculationsIfScrolled = /** * Get the SVG element that forms the drawing surface. - * @return {!SVGElement} SVG element. + * @return {!SVGGElement} SVG group element. */ Blockly.WorkspaceSvg.prototype.getCanvas = function() { - return this.svgBlockCanvas_; + return /** @type {!SVGGElement} */ (this.svgBlockCanvas_); }; /** * Get the SVG element that forms the bubble surface. - * @return {!SVGGElement} SVG element. + * @return {!SVGGElement} SVG group element. */ Blockly.WorkspaceSvg.prototype.getBubbleCanvas = function() { - return this.svgBubbleCanvas_; + return /** @type {!SVGGElement} */ (this.svgBubbleCanvas_); }; /** @@ -1034,12 +1036,13 @@ Blockly.WorkspaceSvg.prototype.setupDragSurface = function() { // Figure out where we want to put the canvas back. The order // in the is important because things are layered. - var previousElement = this.svgBlockCanvas_.previousSibling; + var previousElement = + /** @type {Element} */ (this.svgBlockCanvas_.previousSibling); var width = parseInt(this.getParentSvg().getAttribute('width'), 10); var height = parseInt(this.getParentSvg().getAttribute('height'), 10); - var coord = Blockly.utils.getRelativeXY(this.svgBlockCanvas_); - this.workspaceDragSurface_.setContentsAndShow(this.svgBlockCanvas_, - this.svgBubbleCanvas_, previousElement, width, height, this.scale); + var coord = Blockly.utils.getRelativeXY(this.getCanvas()); + this.workspaceDragSurface_.setContentsAndShow(this.getCanvas(), + this.getBubbleCanvas(), previousElement, width, height, this.scale); this.workspaceDragSurface_.translateSurface(coord.x, coord.y); }; @@ -1324,10 +1327,10 @@ Blockly.WorkspaceSvg.prototype.deleteVariableById = function(id) { * Create a new variable with the given name. Update the flyout to show the * new variable immediately. * @param {string} name The new variable's name. - * @param {string=} opt_type The type of the variable like 'int' or 'string'. + * @param {?string=} opt_type The type of the variable like 'int' or 'string'. * Does not need to be unique. Field_variable can filter variables based on * their type. This will default to '' which is a specific type. - * @param {string=} opt_id The unique ID of the variable. This will default to + * @param {?string=} opt_id The unique ID of the variable. This will default to * a UUID. * @return {Blockly.VariableModel} The newly created variable. * @package @@ -1974,7 +1977,7 @@ Blockly.WorkspaceSvg.prototype.centerOnBlock = function(id) { return; } - var block = this.getBlockById(id); + var block = id ? this.getBlockById(id) : null; if (!block) { return; } @@ -2362,6 +2365,17 @@ Blockly.WorkspaceSvg.setTopLevelWorkspaceMetrics_ = function(xyRatio) { this.translate(x, y); }; +/** + * Find the block on this workspace with the specified ID. + * @param {string} id ID of block to find. + * @return {Blockly.BlockSvg} The sought after block, or null if not found. + * @override + */ +Blockly.WorkspaceSvg.prototype.getBlockById = function(id) { + return /** @type {Blockly.BlockSvg} */ ( + Blockly.WorkspaceSvg.superClass_.getBlockById.call(this, id)); +}; + /** * Finds the top-level blocks and returns them. Blocks are optionally sorted * by position; top to bottom (with slight LTR or RTL bias).