Add id to component interface (#4887)

This commit is contained in:
Monica Kozbial
2021-06-10 16:03:43 -07:00
committed by GitHub
parent 9eaca6a1e1
commit 0014ad3257
8 changed files with 55 additions and 35 deletions

View File

@@ -162,12 +162,9 @@ Blockly.BubbleDragger.prototype.shouldDelete_ = function(dragTarget) {
var couldDeleteBubble = this.draggingBubble_.isDeletable();
if (couldDeleteBubble && dragTarget) {
// TODO(#4881) use hasCapability instead of getComponents
var deleteAreas = this.workspace_.getComponentManager().getComponents(
Blockly.ComponentManager.Capability.DELETE_AREA, false);
var isDeleteArea = deleteAreas.some(function(deleteArea) {
return dragTarget === deleteArea;
});
var componentManager = this.workspace_.getComponentManager();
var isDeleteArea = componentManager.hasCapability(dragTarget.id,
Blockly.ComponentManager.Capability.DELETE_AREA);
if (isDeleteArea) {
return (/** @type {!Blockly.IDeleteArea} */ (dragTarget))
.wouldDeleteBubble(this.draggingBubble_);

View File

@@ -43,10 +43,10 @@ Blockly.ComponentManager = function() {
/**
* An object storing component information.
* @typedef {{
* id: string,
* component: !Blockly.IComponent,
* capabilities: (
* !Array<string|!Blockly.ComponentManager.Capability<!Blockly.IComponent>>),
* !Array<string|!Blockly.ComponentManager.Capability<!Blockly.IComponent>>
* ),
* weight: number
* }}
*/
@@ -58,26 +58,28 @@ Blockly.ComponentManager.ComponentDatum;
* the component to register.
* @param {boolean=} opt_allowOverrides True to prevent an error when overriding
* an already registered item.
* @template T
*/
Blockly.ComponentManager.prototype.addComponent = function(
componentInfo, opt_allowOverrides) {
// Don't throw an error if opt_allowOverrides is true.
if (!opt_allowOverrides && this.componentData_[componentInfo.id]) {
var id = componentInfo.component.id;
if (!opt_allowOverrides && this.componentData_[id]) {
throw Error(
'Plugin "' + componentInfo.id + '" with capabilities "' +
this.componentData_[componentInfo.id].capabilities +
'" already added.');
'Plugin "' + id + '" with capabilities "' +
this.componentData_[id].capabilities + '" already added.');
}
this.componentData_[componentInfo.id] = componentInfo;
this.componentData_[id] = componentInfo;
var stringCapabilities = [];
for (var i = 0; i < componentInfo.capabilities.length; i++) {
var capability = String(componentInfo.capabilities[i]).toLowerCase();
stringCapabilities.push(capability);
if (this.capabilityToComponentIds_[capability] === undefined) {
this.capabilityToComponentIds_[capability] = [componentInfo.id];
this.capabilityToComponentIds_[capability] = [id];
} else {
this.capabilityToComponentIds_[capability].push(componentInfo.id);
this.capabilityToComponentIds_[capability].push(id);
}
}
this.componentData_[id].capabilities = stringCapabilities;
};
/**
@@ -100,11 +102,11 @@ Blockly.ComponentManager.prototype.removeComponent = function(id) {
/**
* Adds a capability to a existing registered component.
* @param {string} id The ID of the component to add the capability to.
* @param {string|!Blockly.ComponentManager.Capability<!Blockly.IComponent>
* } capability The capability to add.
* @param {string|!Blockly.ComponentManager.Capability<T>} capability The
* capability to add.
* @template T
*/
Blockly.ComponentManager.prototype.addCapability = function(id, capability) {
capability = String(capability).toLowerCase();
if (!this.getComponent(id)) {
throw Error('Cannot add capability, "' + capability + '". Plugin "' +
id + '" has not been added to the ComponentManager');
@@ -114,6 +116,7 @@ Blockly.ComponentManager.prototype.addCapability = function(id, capability) {
capability + '"');
return;
}
capability = String(capability).toLowerCase();
this.componentData_[id].capabilities.push(capability);
this.capabilityToComponentIds_[capability].push(id);
};
@@ -121,11 +124,11 @@ Blockly.ComponentManager.prototype.addCapability = function(id, capability) {
/**
* Removes a capability from an existing registered component.
* @param {string} id The ID of the component to remove the capability from.
* @param {string|!Blockly.ComponentManager.Capability<!Blockly.IComponent>
* } capability The capability to remove.
* @param {string|!Blockly.ComponentManager.Capability<T>} capability The
* capability to remove.
* @template T
*/
Blockly.ComponentManager.prototype.removeCapability = function(id, capability) {
capability = String(capability).toLowerCase();
if (!this.getComponent(id)) {
throw Error('Cannot remove capability, "' + capability + '". Plugin "' +
id + '" has not been added to the ComponentManager');
@@ -135,6 +138,7 @@ Blockly.ComponentManager.prototype.removeCapability = function(id, capability) {
capability + '" to remove');
return;
}
capability = String(capability).toLowerCase();
this.componentData_[id].capabilities.splice(
this.componentData_[id].capabilities.indexOf(capability), 1);
this.capabilityToComponentIds_[capability].splice(
@@ -144,9 +148,10 @@ Blockly.ComponentManager.prototype.removeCapability = function(id, capability) {
/**
* Returns whether the component with this id has the specified capability.
* @param {string} id The ID of the component to check.
* @param {string|!Blockly.ComponentManager.Capability<!Blockly.IComponent>
* } capability The capability to check for.
* @param {string|!Blockly.ComponentManager.Capability<T>} capability The
* capability to check for.
* @return {boolean} Whether the component has the capability.
* @template T
*/
Blockly.ComponentManager.prototype.hasCapability = function(id, capability) {
capability = String(capability).toLowerCase();

View File

@@ -75,7 +75,7 @@ Blockly.Flyout = function(workspaceOptions) {
* The unique id for this component.
* @type {string}
*/
this.id = 'flyout' + this.workspace_.id;
this.id = Blockly.utils.genUid();
/**
* Is RTL vs LTR.
@@ -311,7 +311,6 @@ Blockly.Flyout.prototype.init = function(targetWorkspace) {
this.workspace_.createPotentialVariableMap();
targetWorkspace.getComponentManager().addComponent({
id: this.id,
component: this,
weight: 1,
capabilities: [

View File

@@ -460,12 +460,9 @@ Blockly.InsertionMarkerManager.prototype.shouldDelete_ = function(
!this.topBlock_.getParent() && this.topBlock_.isDeletable();
if (couldDeleteBlock && dragTarget) {
// TODO(#4881) use hasCapability instead of getComponents
var deleteAreas = this.workspace_.getComponentManager().getComponents(
Blockly.ComponentManager.Capability.DELETE_AREA, false);
var isDeleteArea = deleteAreas.some(function(deleteArea) {
return dragTarget === deleteArea;
});
var componentManager = this.workspace_.getComponentManager();
var isDeleteArea = componentManager.hasCapability(dragTarget.id,
Blockly.ComponentManager.Capability.DELETE_AREA);
if (isDeleteArea) {
return (
/** @type {!Blockly.IDeleteArea} */ (dragTarget))

View File

@@ -22,3 +22,8 @@ goog.provide('Blockly.IComponent');
*/
Blockly.IComponent = function() {};
/**
* The unique id for this component.
* @type {string}
*/
Blockly.IComponent.id;

View File

@@ -64,6 +64,12 @@ Blockly.Toolbox = function(workspace) {
*/
this.workspace_ = workspace;
/**
* The unique id for this component.
* @type {string}
*/
this.id = Blockly.utils.genUid();
/**
* The JSON describing the contents of this toolbox.
* @type {!Blockly.utils.toolbox.ToolboxInfo}
@@ -193,7 +199,6 @@ Blockly.Toolbox.prototype.init = function() {
'background-color');
themeManager.subscribe(this.HtmlDiv, 'toolboxForegroundColour', 'color');
this.workspace_.getComponentManager().addComponent({
id: 'toolbox',
component: this,
weight: 1,
capabilities: [

View File

@@ -25,6 +25,7 @@ goog.require('Blockly.IPositionable');
goog.require('Blockly.Options');
goog.require('Blockly.registry');
goog.require('Blockly.uiPosition');
goog.require('Blockly.utils');
goog.require('Blockly.utils.dom');
goog.require('Blockly.utils.Rect');
goog.require('Blockly.utils.Svg');
@@ -53,6 +54,12 @@ Blockly.Trashcan = function(workspace) {
*/
this.workspace_ = workspace;
/**
* The unique id for this component.
* @type {string}
*/
this.id = Blockly.utils.genUid();
/**
* A list of XML (stored as strings) representing blocks in the trashcan.
* @type {!Array<string>}
@@ -363,7 +370,6 @@ Blockly.Trashcan.prototype.init = function() {
this.flyout.init(this.workspace_);
}
this.workspace_.getComponentManager().addComponent({
id: 'trashcan',
component: this,
weight: 1,
capabilities: [

View File

@@ -23,6 +23,7 @@ goog.require('Blockly.Events.Click');
goog.require('Blockly.IPositionable');
goog.require('Blockly.Touch');
goog.require('Blockly.uiPosition');
goog.require('Blockly.utils');
goog.require('Blockly.utils.dom');
goog.require('Blockly.utils.Rect');
goog.require('Blockly.utils.Svg');
@@ -43,6 +44,12 @@ Blockly.ZoomControls = function(workspace) {
*/
this.workspace_ = workspace;
/**
* The unique id for this component.
* @type {string}
*/
this.id = Blockly.utils.genUid();
/**
* A handle to use to unbind the mouse down event handler for zoom reset
* button. Opaque data returned from Blockly.bindEventWithChecks_.
@@ -191,7 +198,6 @@ Blockly.ZoomControls.prototype.createDom = function() {
*/
Blockly.ZoomControls.prototype.init = function() {
this.workspace_.getComponentManager().addComponent({
id: 'zoomControls',
component: this,
weight: 2,
capabilities: [Blockly.ComponentManager.Capability.POSITIONABLE]