From 244733467de0ca92dd667afab5a9aa1aacfcad70 Mon Sep 17 00:00:00 2001 From: Katelyn Mann Date: Wed, 10 Aug 2016 18:00:15 -0700 Subject: [PATCH] Fix for #498. Recalculate the things that use screen coordinates (delete area and screen transformation matrix) when a scroll happened. This is not done using a scroll handler since the update is expensive (getScreenCTM and getClientBoundingRect) and we don't need to do it until the scroll is done and the user is interacting with blocks again. --- core/block_svg.js | 1 + core/workspace_svg.js | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index 7369235d5..719c967e5 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -533,6 +533,7 @@ Blockly.BlockSvg.prototype.onMouseDown_ = function(e) { if (this.isInFlyout) { return; } + this.workspace.updateScreenCalculationsIfScrolled(); this.workspace.markFocused(); Blockly.terminateDrag_(); this.select(); diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 8e06fb415..1ce23e54f 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -147,6 +147,15 @@ Blockly.WorkspaceSvg.prototype.scrollbar = null; */ Blockly.WorkspaceSvg.prototype.lastSound_ = null; +/** + * Last known position of the page scroll. + * This is used to determine whether we have recalculated screen coordinate + * stuff since the page scrolled. + * @type {!goog.math.Coordinate} + * @private + */ +Blockly.WorkspaceSvg.prototype.lastRecordedPageScroll_ = null; + /** * Inverted screen CTM, for use in mouseToSvg. * @type {SVGMatrix} @@ -343,6 +352,16 @@ Blockly.WorkspaceSvg.prototype.addFlyout_ = function() { this.svgGroup_.insertBefore(svgFlyout, this.svgBlockCanvas_); }; +/** + * Update items that use screen coordinate calculations + * because something has changed (e.g. scroll position, window size). + * @private + */ +Blockly.WorkspaceSvg.prototype.updateScreenCalculations_ = function() { + this.updateInverseScreenCTM(); + this.recordDeleteAreas(); +}; + /** * Resize the parts of the workspace that change when the workspace * contents (e.g. block positions) change. This will also scroll the @@ -382,11 +401,25 @@ Blockly.WorkspaceSvg.prototype.resize = function() { if (this.scrollbar) { this.scrollbar.resize(); } - - this.updateInverseScreenCTM(); - this.recordDeleteAreas(); + this.updateScreenCalculations_(); }; +/** + * Resizes and repositions workspace chrome if the page has a new + * scroll position. + * @package + */ +Blockly.WorkspaceSvg.prototype.updateScreenCalculationsIfScrolled + = function() { + /* eslint-disable indent */ + var currScroll = goog.dom.getDocumentScroll(); + if (!goog.math.Coordinate.equals(this.lastRecordedPageScroll_, + currScroll)) { + this.lastRecordedPageScroll_ = currScroll; + this.updateScreenCalculations_(); + } +}; /* eslint-enable indent */ + /** * Get the SVG element that forms the drawing surface. * @return {!Element} SVG element.