From bea85cda1c8a87c68d14d3170ac986fe18e605b0 Mon Sep 17 00:00:00 2001 From: Monica Kozbial Date: Mon, 8 Mar 2021 11:20:30 -0800 Subject: [PATCH] Positionables bugfix (#4685) * Apply fixes to positionable logic * Update variable name and add @private annotation --- core/plugin_manager.js | 39 +++++++++++++++++++++++++++------------ core/trashcan.js | 11 +++++------ core/workspace_svg.js | 4 ++-- core/zoom_controls.js | 10 +++++----- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/core/plugin_manager.js b/core/plugin_manager.js index 186d9c4b1..5e62092bd 100644 --- a/core/plugin_manager.js +++ b/core/plugin_manager.js @@ -21,15 +21,17 @@ goog.provide('Blockly.PluginManager'); Blockly.PluginManager = function() { /** * A map of the plugins registered with the workspace, mapped to id. - * @type {!Object} + * @type {!Object} + * @private */ this.pluginData_ = {}; /** * A map of types to plugin ids. * @type {!Object>} + * @private */ - this.typeToPluginId_ = {}; + this.typeToPluginIds_ = {}; }; /** @@ -41,21 +43,21 @@ Blockly.PluginManager = function() { * weight: number * }} */ -Blockly.PluginManager.PluginData; +Blockly.PluginManager.PluginDatum; /** * Adds a plugin. - * @param {!Blockly.PluginManager.PluginData} pluginDataObject The plugin. + * @param {!Blockly.PluginManager.PluginDatum} pluginDataObject The plugin. * @template T */ Blockly.PluginManager.prototype.addPlugin = function(pluginDataObject) { this.pluginData_[pluginDataObject.id] = pluginDataObject; for (var i = 0, type; (type = pluginDataObject.types[i]); i++) { var typeKey = String(type).toLowerCase(); - if (this.typeToPluginId_[typeKey] === undefined) { - this.typeToPluginId_[typeKey] = [pluginDataObject.id]; + if (this.typeToPluginIds_[typeKey] === undefined) { + this.typeToPluginIds_[typeKey] = [pluginDataObject.id]; } else { - this.typeToPluginId_[typeKey].push(pluginDataObject.id); + this.typeToPluginIds_[typeKey].push(pluginDataObject.id); } } }; @@ -79,16 +81,29 @@ Blockly.PluginManager.prototype.getPlugin = function(id) { * @template T */ Blockly.PluginManager.prototype.getPlugins = function(type, sorted) { - var plugins = []; var typeKey = String(type).toLowerCase(); - var pluginIds = this.typeToPluginId_[typeKey]; - for (var i = 0, id; pluginIds && (id = pluginIds[i]); i++) { - plugins.push(this.pluginData_[id].plugin); + var pluginIds = this.typeToPluginIds_[typeKey]; + if (!pluginIds) { + return []; } + var plugins = []; if (sorted) { - plugins.sort(function(a, b) { + var pluginDataList = []; + var pluginData = this.pluginData_; + pluginIds.forEach(function(id) { + pluginDataList.push(pluginData[id]); + }); + pluginDataList.sort(function(a, b) { return a.weight - b.weight; }); + pluginDataList.forEach(function(pluginDatum) { + plugins.push(pluginDatum.plugin); + }); + } else { + var pluginData = this.pluginData_; + pluginIds.forEach(function(id) { + plugins.push(pluginData[id].plugin); + }); } return plugins; }; diff --git a/core/trashcan.js b/core/trashcan.js index 8848e0cc0..ba0311ee8 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -433,7 +433,7 @@ Blockly.Trashcan.prototype.emptyContents = function() { }; /** - * Position the trashcan. + * Positions the trashcan. * It is positioned in the opposite corner to the corner the * categories/toolbox starts at. * @param {!Blockly.MetricsManager.UiMetrics} metrics The workspace metrics. @@ -445,22 +445,21 @@ Blockly.Trashcan.prototype.position = function(metrics, savedPositions) { if (!this.verticalSpacing_) { return; } - if (metrics.toolboxMetrics.position == Blockly.utils.toolbox.Position.LEFT || (this.workspace_.horizontalLayout && !this.workspace_.RTL)) { - // Toolbox starts in the left corner. + // Right corner placement. this.left_ = metrics.viewMetrics.width + metrics.absoluteMetrics.left - this.WIDTH_ - this.MARGIN_SIDE_ - Blockly.Scrollbar.scrollbarThickness; } else { - // Toolbox starts in the right corner. + // Left corner placement. this.left_ = this.MARGIN_SIDE_ + Blockly.Scrollbar.scrollbarThickness; } var height = this.BODY_HEIGHT_ + this.LID_HEIGHT_; // Upper corner placement - var minTop = this.top_ = this.verticalSpacing_; + var minTop = this.top_ = metrics.absoluteMetrics.top + this.verticalSpacing_; // Bottom corner placement - var maxTop = metrics.viewMetrics.height + metrics.absoluteMetrics.top - + var maxTop = metrics.absoluteMetrics.top + metrics.viewMetrics.height - height - this.verticalSpacing_; var placeBottom = metrics.toolboxMetrics.position !== Blockly.utils.toolbox.Position.BOTTOM; diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 3769aa926..1b0a05655 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -996,7 +996,7 @@ Blockly.WorkspaceSvg.prototype.addTrashcan = function() { this.pluginManager_.addPlugin({ id: 'trashcan', plugin: this.trashcan, - weight: 0, + weight: 1, types: [Blockly.PluginManager.Type.POSITIONABLE] }); }; @@ -1016,7 +1016,7 @@ Blockly.WorkspaceSvg.prototype.addZoomControls = function() { this.pluginManager_.addPlugin({ id: 'zoomControls', plugin: this.zoomControls_, - weight: 0, + weight: 2, types: [Blockly.PluginManager.Type.POSITIONABLE] }); }; diff --git a/core/zoom_controls.js b/core/zoom_controls.js index 1b96ec9ee..521a838e2 100644 --- a/core/zoom_controls.js +++ b/core/zoom_controls.js @@ -213,7 +213,7 @@ Blockly.ZoomControls.prototype.getBoundingRectangle = function() { /** - * Position the zoom controls. + * Positions the zoom controls. * It is positioned in the opposite corner to the corner the * categories/toolbox starts at. * @param {!Blockly.MetricsManager.UiMetrics} metrics The workspace metrics. @@ -227,18 +227,18 @@ Blockly.ZoomControls.prototype.position = function(metrics, savedPositions) { } if (metrics.toolboxMetrics.position == Blockly.utils.toolbox.Position.LEFT || (this.workspace_.horizontalLayout && !this.workspace_.RTL)) { - // Zoom controls start in the left corner. + // Right corner placement. this.left_ = metrics.viewMetrics.width + metrics.absoluteMetrics.left - this.WIDTH_ - this.MARGIN_SIDE_ - Blockly.Scrollbar.scrollbarThickness; } else { - // Zoom controls start in the right corner. + // Left corner placement. this.left_ = this.MARGIN_SIDE_ + Blockly.Scrollbar.scrollbarThickness; } // Upper corner placement - var minTop = this.top_ = this.verticalSpacing_; + var minTop = this.top_ = metrics.absoluteMetrics.top + this.verticalSpacing_; // Bottom corner placement - var maxTop = metrics.viewMetrics.height + metrics.absoluteMetrics.top - + var maxTop = metrics.absoluteMetrics.top + metrics.viewMetrics.height - this.HEIGHT_ - this.verticalSpacing_; var placeBottom = metrics.toolboxMetrics.position !== Blockly.utils.toolbox.Position.BOTTOM;