From d3f688d2cd71d613e16f0037abe1cb0c72037bb4 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Tue, 13 Mar 2018 20:29:33 -0400 Subject: [PATCH 1/2] Add fns to support stable scroll positioning --- core/flyout_base.js | 18 ++++++++++++++++++ core/toolbox.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/core/flyout_base.js b/core/flyout_base.js index f0f7f390..d068560c 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -590,6 +590,24 @@ Blockly.Flyout.prototype.stepScrollAnimation = function() { requestAnimationFrame(this.stepScrollAnimation.bind(this)); }; +/** + * Get the scaled scroll position. + * @return {number} The current scroll position. + */ +Blockly.Flyout.prototype.getScrollPos = function() { + var pos = this.horizontalLayout_ ? + -this.workspace_.scrollX : -this.workspace_.scrollY; + return pos / this.workspace_.scale; +}; + +/** + * Set the scroll position, scaling it. + * @param {number} pos The scroll position to set. + */ +Blockly.Flyout.prototype.setScrollPos = function(pos) { + this.scrollbar_.set(pos * this.workspace_.scale); +}; + /** * Delete blocks and background buttons from a previous showing of the flyout. * @private diff --git a/core/toolbox.js b/core/toolbox.js index 5e82daa1..a31886c1 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -337,6 +337,45 @@ Blockly.Toolbox.prototype.getSelectedItem = function() { return this.selectedItem_; }; +/** + * @return {string} The name of the currently selected category. + */ +Blockly.Toolbox.prototype.getSelectedCategoryName = function() { + return this.selectedItem_.name_; +}; + +/** + * @return {number} The distance flyout is scrolled below the top of the currently + * selected category. + */ +Blockly.Toolbox.prototype.getCategoryScrollOffset = function() { + var categoryPos = this.getCategoryPositionByName(this.getSelectedCategoryName()); + return this.flyout_.getScrollPos() - categoryPos; +}; + +/** + * Get the position of a category by name. + * @param {string} name The name of the category. + * @return {number} The position of the category. + */ +Blockly.Toolbox.prototype.getCategoryPositionByName = function(name) { + var scrollPositions = this.flyout_.categoryScrollPositions; + for (var i = 0; i < scrollPositions.length; i++) { + if (name === scrollPositions[i].categoryName) { + return scrollPositions[i].position; + } + } +}; + +/** + * Set the scroll position of the flyout. + * @param {number} pos The position to set. + */ +Blockly.Toolbox.prototype.setFlyoutScrollPos = function(pos) { + this.flyout_.setScrollPos(pos); +}; + + /** * Set the currently selected category. * @param {Blockly.Toolbox.Category} item The category to select. From dc821397805e025e4489eac91132849ff5766409 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Wed, 14 Mar 2018 20:56:05 -0400 Subject: [PATCH 2/2] Fix indent --- core/flyout_base.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/flyout_base.js b/core/flyout_base.js index d068560c..92b69e23 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -595,9 +595,9 @@ Blockly.Flyout.prototype.stepScrollAnimation = function() { * @return {number} The current scroll position. */ Blockly.Flyout.prototype.getScrollPos = function() { - var pos = this.horizontalLayout_ ? - -this.workspace_.scrollX : -this.workspace_.scrollY; - return pos / this.workspace_.scale; + var pos = this.horizontalLayout_ ? + -this.workspace_.scrollX : -this.workspace_.scrollY; + return pos / this.workspace_.scale; }; /**