From 0014ad32576fb1b175eebd2ff4a9c5ac537a20d2 Mon Sep 17 00:00:00 2001 From: Monica Kozbial <6621618+moniika@users.noreply.github.com> Date: Thu, 10 Jun 2021 16:03:43 -0700 Subject: [PATCH] Add id to component interface (#4887) --- core/bubble_dragger.js | 9 +++---- core/component_manager.js | 41 ++++++++++++++++++-------------- core/flyout_base.js | 3 +-- core/insertion_marker_manager.js | 9 +++---- core/interfaces/i_component.js | 5 ++++ core/toolbox/toolbox.js | 7 +++++- core/trashcan.js | 8 ++++++- core/zoom_controls.js | 8 ++++++- 8 files changed, 55 insertions(+), 35 deletions(-) diff --git a/core/bubble_dragger.js b/core/bubble_dragger.js index fd4b67b00..e372648cf 100644 --- a/core/bubble_dragger.js +++ b/core/bubble_dragger.js @@ -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_); diff --git a/core/component_manager.js b/core/component_manager.js index 0e2600acd..d8b215504 100644 --- a/core/component_manager.js +++ b/core/component_manager.js @@ -43,10 +43,10 @@ Blockly.ComponentManager = function() { /** * An object storing component information. * @typedef {{ - * id: string, * component: !Blockly.IComponent, * capabilities: ( - * !Array>), + * !Array> + * ), * 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 - * } capability The capability to add. + * @param {string|!Blockly.ComponentManager.Capability} 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 - * } capability The capability to remove. + * @param {string|!Blockly.ComponentManager.Capability} 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 - * } capability The capability to check for. + * @param {string|!Blockly.ComponentManager.Capability} 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(); diff --git a/core/flyout_base.js b/core/flyout_base.js index 8aa2a91e3..43ea6da1d 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -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: [ diff --git a/core/insertion_marker_manager.js b/core/insertion_marker_manager.js index da369adda..0a4eaca3a 100644 --- a/core/insertion_marker_manager.js +++ b/core/insertion_marker_manager.js @@ -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)) diff --git a/core/interfaces/i_component.js b/core/interfaces/i_component.js index 093f5b64c..983c07962 100644 --- a/core/interfaces/i_component.js +++ b/core/interfaces/i_component.js @@ -22,3 +22,8 @@ goog.provide('Blockly.IComponent'); */ Blockly.IComponent = function() {}; +/** + * The unique id for this component. + * @type {string} + */ +Blockly.IComponent.id; diff --git a/core/toolbox/toolbox.js b/core/toolbox/toolbox.js index 4b3087421..aceb9a393 100644 --- a/core/toolbox/toolbox.js +++ b/core/toolbox/toolbox.js @@ -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: [ diff --git a/core/trashcan.js b/core/trashcan.js index 57fb1155a..9acc99004 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -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} @@ -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: [ diff --git a/core/zoom_controls.js b/core/zoom_controls.js index 796a04b5f..b86111a3b 100644 --- a/core/zoom_controls.js +++ b/core/zoom_controls.js @@ -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]