mirror of
https://github.com/google/blockly.git
synced 2026-01-10 18:37:09 +01:00
Positionables bugfix (#4685)
* Apply fixes to positionable logic * Update variable name and add @private annotation
This commit is contained in:
@@ -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<string, !Blockly.PluginManager.PluginData>}
|
||||
* @type {!Object<string, !Blockly.PluginManager.PluginDatum>}
|
||||
* @private
|
||||
*/
|
||||
this.pluginData_ = {};
|
||||
|
||||
/**
|
||||
* A map of types to plugin ids.
|
||||
* @type {!Object<string, Array<string>>}
|
||||
* @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;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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]
|
||||
});
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user