From 160d6597a6df4a0b3b7a964765ad542b82052d0a Mon Sep 17 00:00:00 2001 From: kozbial Date: Wed, 9 Jun 2021 16:25:14 -0700 Subject: [PATCH 1/5] Add id to component interface --- core/bubble_dragger.js | 8 ++------ core/component_manager.js | 15 +++++++-------- core/flyout_base.js | 7 ++++++- core/insertion_marker_manager.js | 8 ++------ core/interfaces/i_component.js | 5 +++++ core/toolbox/toolbox.js | 7 ++++++- core/trashcan.js | 8 +++++++- core/zoom_controls.js | 8 +++++++- 8 files changed, 42 insertions(+), 24 deletions(-) diff --git a/core/bubble_dragger.js b/core/bubble_dragger.js index 38c33915805..d3ca0fe37b2 100644 --- a/core/bubble_dragger.js +++ b/core/bubble_dragger.js @@ -157,12 +157,8 @@ 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 isDeleteArea = this.workspace_.getComponentManager().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 e5b7c41924b..c1e6af8e020 100644 --- a/core/component_manager.js +++ b/core/component_manager.js @@ -43,7 +43,6 @@ Blockly.ComponentManager = function() { /** * An object storing component information. * @typedef {{ - * id: string, * component: !Blockly.IComponent, * capabilities: ( * !Array>), @@ -63,19 +62,19 @@ Blockly.ComponentManager.ComponentDatum; 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; for (var i = 0, type; (type = componentInfo.capabilities[i]); i++) { var typeKey = String(type).toLowerCase(); if (this.capabilityToComponentIds_[typeKey] === undefined) { - this.capabilityToComponentIds_[typeKey] = [componentInfo.id]; + this.capabilityToComponentIds_[typeKey] = [id]; } else { - this.capabilityToComponentIds_[typeKey].push(componentInfo.id); + this.capabilityToComponentIds_[typeKey].push(id); } } }; diff --git a/core/flyout_base.js b/core/flyout_base.js index 06ceecbbb15..7109dd05b98 100644 --- a/core/flyout_base.js +++ b/core/flyout_base.js @@ -70,6 +70,12 @@ Blockly.Flyout = function(workspaceOptions) { // Keep the workspace visibility consistent with the flyout's visibility. this.workspace_.setVisible(this.isVisible_); + /** + * The unique id for this component. + * @type {string} + */ + this.id = Blockly.utils.genUid(); + /** * Is RTL vs LTR. * @type {boolean} @@ -304,7 +310,6 @@ Blockly.Flyout.prototype.init = function(targetWorkspace) { this.workspace_.createPotentialVariableMap(); targetWorkspace.getComponentManager().addComponent({ - id: 'flyout' + this.workspace_.id, component: this, weight: 1, capabilities: [ diff --git a/core/insertion_marker_manager.js b/core/insertion_marker_manager.js index 1a7988c2421..5b0861f272d 100644 --- a/core/insertion_marker_manager.js +++ b/core/insertion_marker_manager.js @@ -459,12 +459,8 @@ 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 isDeleteArea = this.workspace_.getComponentManager().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 093f5b64cd5..983c079627c 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 64a1a51dbc9..76c0e3a7156 100644 --- a/core/toolbox/toolbox.js +++ b/core/toolbox/toolbox.js @@ -63,6 +63,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} @@ -192,7 +198,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 7781a79bd93..22fbb5cf813 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -24,6 +24,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'); @@ -52,6 +53,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} @@ -362,7 +369,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 530a2414244..b1cd2abf81b 100644 --- a/core/zoom_controls.js +++ b/core/zoom_controls.js @@ -22,6 +22,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'); @@ -42,6 +43,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_. @@ -190,7 +197,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] From c735f2dee1f70f120c00df72b9d887a6267cb31a Mon Sep 17 00:00:00 2001 From: kozbial Date: Wed, 9 Jun 2021 19:41:03 -0700 Subject: [PATCH 2/5] Update typings --- core/component_manager.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/component_manager.js b/core/component_manager.js index c1e6af8e020..4844ba5fd4d 100644 --- a/core/component_manager.js +++ b/core/component_manager.js @@ -82,8 +82,9 @@ Blockly.ComponentManager.prototype.addComponent = function( /** * 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(); @@ -103,8 +104,9 @@ 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(); @@ -126,9 +128,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(); From 1de963afc7a4a78014ed753f8509b3a2acf628a2 Mon Sep 17 00:00:00 2001 From: kozbial Date: Wed, 9 Jun 2021 19:48:15 -0700 Subject: [PATCH 3/5] take 2 at fixing types --- core/bubble_dragger.js | 6 ++++-- core/component_manager.js | 13 ++++++------- core/insertion_marker_manager.js | 6 ++++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/core/bubble_dragger.js b/core/bubble_dragger.js index d3ca0fe37b2..b35d910f562 100644 --- a/core/bubble_dragger.js +++ b/core/bubble_dragger.js @@ -157,8 +157,10 @@ Blockly.BubbleDragger.prototype.shouldDelete_ = function(dragTarget) { var couldDeleteBubble = this.draggingBubble_.isDeletable(); if (couldDeleteBubble && dragTarget) { - var isDeleteArea = this.workspace_.getComponentManager().hasCapability( - dragTarget.id, Blockly.ComponentManager.Capability.DELETE_AREA); + var componentManager = this.workspace_.getComponentManager(); + var isDeleteArea = componentManager.hasCapability(dragTarget.id, + /** @type {!Blockly.ComponentManager.Capability} */ + (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 4844ba5fd4d..ba7edbbf9a2 100644 --- a/core/component_manager.js +++ b/core/component_manager.js @@ -82,8 +82,8 @@ Blockly.ComponentManager.prototype.addComponent = function( /** * 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) { @@ -104,8 +104,8 @@ 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) { @@ -128,10 +128,9 @@ 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/insertion_marker_manager.js b/core/insertion_marker_manager.js index 5b0861f272d..c1f6749cb80 100644 --- a/core/insertion_marker_manager.js +++ b/core/insertion_marker_manager.js @@ -459,8 +459,10 @@ Blockly.InsertionMarkerManager.prototype.shouldDelete_ = function( !this.topBlock_.getParent() && this.topBlock_.isDeletable(); if (couldDeleteBlock && dragTarget) { - var isDeleteArea = this.workspace_.getComponentManager().hasCapability( - dragTarget.id, Blockly.ComponentManager.Capability.DELETE_AREA); + var componentManager = this.workspace_.getComponentManager(); + var isDeleteArea = componentManager.hasCapability(dragTarget.id, + /** @type {!Blockly.ComponentManager.Capability} */ + (Blockly.ComponentManager.Capability.DELETE_AREA)); if (isDeleteArea) { return ( /** @type {!Blockly.IDeleteArea} */ (dragTarget)) From febfed26d22e3162f487c527ac3434df56d5a797 Mon Sep 17 00:00:00 2001 From: kozbial Date: Thu, 10 Jun 2021 11:48:28 -0700 Subject: [PATCH 4/5] fixes --- core/component_manager.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/component_manager.js b/core/component_manager.js index f64c8251787..d8b21550437 100644 --- a/core/component_manager.js +++ b/core/component_manager.js @@ -69,14 +69,17 @@ Blockly.ComponentManager.prototype.addComponent = function( this.componentData_[id].capabilities + '" already added.'); } 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] = [id]; } else { this.capabilityToComponentIds_[capability].push(id); } } + this.componentData_[id].capabilities = stringCapabilities; }; /** @@ -104,7 +107,6 @@ Blockly.ComponentManager.prototype.removeComponent = function(id) { * @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); }; @@ -126,7 +129,6 @@ Blockly.ComponentManager.prototype.addCapability = function(id, capability) { * @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'); @@ -136,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( From a2a2ade3d5ec0353bbd07a55d3a27edad290c0de Mon Sep 17 00:00:00 2001 From: kozbial Date: Thu, 10 Jun 2021 11:51:42 -0700 Subject: [PATCH 5/5] remove cast --- core/bubble_dragger.js | 3 +-- core/insertion_marker_manager.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/core/bubble_dragger.js b/core/bubble_dragger.js index 830b5b61944..e372648cf20 100644 --- a/core/bubble_dragger.js +++ b/core/bubble_dragger.js @@ -164,8 +164,7 @@ Blockly.BubbleDragger.prototype.shouldDelete_ = function(dragTarget) { if (couldDeleteBubble && dragTarget) { var componentManager = this.workspace_.getComponentManager(); var isDeleteArea = componentManager.hasCapability(dragTarget.id, - /** @type {!Blockly.ComponentManager.Capability} */ - (Blockly.ComponentManager.Capability.DELETE_AREA)); + Blockly.ComponentManager.Capability.DELETE_AREA); if (isDeleteArea) { return (/** @type {!Blockly.IDeleteArea} */ (dragTarget)) .wouldDeleteBubble(this.draggingBubble_); diff --git a/core/insertion_marker_manager.js b/core/insertion_marker_manager.js index 4aa0c527fd8..0a4eaca3a59 100644 --- a/core/insertion_marker_manager.js +++ b/core/insertion_marker_manager.js @@ -462,8 +462,7 @@ Blockly.InsertionMarkerManager.prototype.shouldDelete_ = function( if (couldDeleteBlock && dragTarget) { var componentManager = this.workspace_.getComponentManager(); var isDeleteArea = componentManager.hasCapability(dragTarget.id, - /** @type {!Blockly.ComponentManager.Capability} */ - (Blockly.ComponentManager.Capability.DELETE_AREA)); + Blockly.ComponentManager.Capability.DELETE_AREA); if (isDeleteArea) { return ( /** @type {!Blockly.IDeleteArea} */ (dragTarget))