From 2884bdfaa8d960beca9a287af2cee06dd992af38 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Thu, 5 Apr 2018 17:42:37 -0700 Subject: [PATCH] Use hideChaff instead of WidgetDiv.hide; move function closer to similar functions. --- core/workspace_svg.js | 99 +++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 50 deletions(-) diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 106ca5b9f..c023554e6 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -1498,6 +1498,55 @@ Blockly.WorkspaceSvg.prototype.scrollCenter = function() { this.scrollbar.set(x, y); }; +/** + * Scroll the workspace to center on the given block. + * @param {?string} id ID of block center on. + * @public + */ +Blockly.WorkspaceSvg.prototype.centerOnBlock = function(id) { + if (!this.scrollbar) { + console.warn('Tried to scroll a non-scrollable workspace.'); + return; + } + var block = this.getBlockById(id); + if (block) { + // XY is in workspace coordinates. + var xy = block.getRelativeToSurfaceXY(); + // Height/width is in workspace units. + var heightWidth = block.getHeightWidth(); + + // Center in workspace units. + var blockCenterX = xy.x + heightWidth.width / 2; + var blockCenterY = xy.y + heightWidth.height / 2; + + // Workspace scale, used to convert from workspace coordinates to pixels. + var scale = this.scale; + + // Center in pixels. 0, 0 is at the workspace origin. These numbers may + // be negative. + var pixelX = blockCenterX * scale; + var pixelY = blockCenterY * scale; + + var metrics = this.getMetrics(); + + // Scrolling to here would put the block in the top-left corner of the + // visible workspace. + var scrollToBlockX = pixelX - metrics.contentLeft; + var scrollToBlockY = pixelY - metrics.contentTop; + + // viewHeight and viewWidth are in pixels. + var halfViewWidth = metrics.viewWidth / 2; + var halfViewHeight = metrics.viewHeight / 2; + + // Put the block in the center of the visible workspace instead. + var scrollToCenterX = scrollToBlockX - halfViewWidth; + var scrollToCenterY = scrollToBlockY - halfViewHeight; + + Blockly.hideChaff(); + this.scrollbar.set(scrollToCenterX, scrollToCenterY); + } +}; + /** * Set the workspace's zoom factor. * @param {number} newScale Zoom factor. @@ -1638,56 +1687,6 @@ Blockly.WorkspaceSvg.getContentDimensionsBounded_ = function(ws, svgSize) { return dimensions; }; -/** - * Scroll the workspace to center on the given block. - * @param {?string} id ID of block center on. - * @public - */ -Blockly.WorkspaceSvg.prototype.centerOnBlock = function(id) { - if (!this.scrollbar) { - console.warn('Tried to scroll a non-scrollable workspace.'); - return; - } - var block = this.getBlockById(id); - if (block) { - // XY is in workspace coordinates. - var xy = block.getRelativeToSurfaceXY(); - // Height/width is in workspace units. - var heightWidth = block.getHeightWidth(); - - // Center in workspace units. - var blockCenterX = xy.x + heightWidth.width / 2; - var blockCenterY = xy.y + heightWidth.height / 2; - - // Workspace scale, used to convert from workspace coordinates to pixels. - var scale = this.scale; - - // Center in pixels. 0, 0 is at the workspace origin. These numbers may - // be negative. - var pixelX = blockCenterX * scale; - var pixelY = blockCenterY * scale; - - var metrics = this.getMetrics(); - - // Scrolling to here would put the block in the top-left corner of the - // visible workspace. - var scrollToBlockX = pixelX - metrics.contentLeft; - var scrollToBlockY = pixelY - metrics.contentTop; - - // viewHeight and viewWidth are in pixels. - var halfViewWidth = metrics.viewWidth / 2; - var halfViewHeight = metrics.viewHeight / 2; - - // Put the block in the center of the visible workspace instead. - var scrollToCenterX = scrollToBlockX - halfViewWidth; - var scrollToCenterY = scrollToBlockY - halfViewHeight; - - Blockly.WidgetDiv.hide(true); - this.scrollbar.set(scrollToCenterX, scrollToCenterY); - } -}; - - /** * Return an object with all the metrics required to size scrollbars for a * top level workspace. The following properties are computed: