Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions blockly_uncompressed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 138 additions & 0 deletions core/component_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Manager for all items registered with the workspace.
* @author kozbial@google.com (Monica Kozbial)
*/

'use strict';

goog.provide('Blockly.ComponentManager');


/**
* Manager for all items registered with the workspace.
* @constructor
*/
Blockly.ComponentManager = function() {
/**
* A map of the components registered with the workspace, mapped to id.
* @type {!Object<string, !Blockly.ComponentManager.ComponentDatum>}
* @private
*/
this.componentData_ = {};

/**
* A map of capabilities to component ids.
* @type {!Object<string, Array<string>>}
* @private
*/
this.capabilityToComponentIds_ = {};
};

/**
* An object storing component information.
* @typedef {{
* id: string,
* component: !Blockly.IComponent,
* capabilities: (
* !Array<string|!Blockly.ComponentManager.Capability<Blockly.IComponent>>),
* weight: number
* }}
*/
Blockly.ComponentManager.ComponentDatum;

/**
* Adds a component.
* @param {!Blockly.ComponentManager.ComponentDatum} componentInfo The data for
* the component to register.
* @template T
*/
Blockly.ComponentManager.prototype.addComponent = function(componentInfo) {
this.componentData_[componentInfo.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];
} else {
this.capabilityToComponentIds_[typeKey].push(componentInfo.id);
}
}
};

/**
* Gets the component with the given ID and the given type.
* @param {string} id The ID of the component to get.
* @return {!Blockly.IComponent|undefined} The component with the given name
* or undefined if not found.
*/
Blockly.ComponentManager.prototype.getComponent = function(id) {
return this.componentData_[id] && this.componentData_[id].component;
};

/**
* Gets all the components of the specified type.
* @param {!Blockly.ComponentManager.Capability<T>} capability The capability of the
* component.
* @param {boolean} sorted Whether to return list ordered by weights.
* @return {!Array<T>} The components that match the specified capability.
* @template T
*/
Blockly.ComponentManager.prototype.getComponents = function(capability, sorted) {
var typeKey = String(capability).toLowerCase();
var componentIds = this.capabilityToComponentIds_[typeKey];
if (!componentIds) {
return [];
}
var components = [];
if (sorted) {
var componentDataList = [];
var componentData = this.componentData_;
componentIds.forEach(function(id) {
componentDataList.push(componentData[id]);
});
componentDataList.sort(function(a, b) {
return a.weight - b.weight;
});
componentDataList.forEach(function(ComponentDatum) {
components.push(ComponentDatum.component);
});
} else {
var componentData = this.componentData_;
componentIds.forEach(function(id) {
components.push(componentData[id].component);
});
}
return components;
};

/**
* A name with the capability of the element stored in the generic.
* @param {string} name The name of the component capability.
* @constructor
* @template T
*/
Blockly.ComponentManager.Capability = function(name) {
/**
* @type {string}
* @private
*/
this.name_ = name;
};

/**
* Returns the name of the capability.
* @return {string} The name.
* @override
*/
Blockly.ComponentManager.Capability.prototype.toString = function() {
return this.name_;
};

/** @type {!Blockly.ComponentManager.Capability<!Blockly.IPositionable>} */
Blockly.ComponentManager.Capability.POSITIONABLE =
new Blockly.ComponentManager.Capability('positionable');
24 changes: 24 additions & 0 deletions core/interfaces/i_component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Interface for a workspace component that can be registered with
* the ComponentManager.
* @author kozbial@google.com (Monica Kozbial)
*/

'use strict';

goog.provide('Blockly.IComponent');


/**
* The interface for a workspace component that can be registered with the
* ComponentManager.
* @interface
*/
Blockly.IComponent = function() {};

22 changes: 0 additions & 22 deletions core/interfaces/i_plugin.js

This file was deleted.

6 changes: 3 additions & 3 deletions core/interfaces/i_positionable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

goog.provide('Blockly.IPositionable');

goog.require('Blockly.IPlugin');
goog.require('Blockly.IComponent');


/**
* Interface for a component that is positioned on top of the workspace.
* @extends {Blockly.IPlugin}
* @extends {Blockly.IComponent}
* @interface
*/
Blockly.IPositionable = function() {};
Expand All @@ -34,6 +34,6 @@ Blockly.IPositionable.prototype.position;
/**
* Returns the bounding rectangle of the UI element in pixel units relative to
* the Blockly injection div.
* @return {!Blockly.utils.Rect} The plugin’s bounding box.
* @return {!Blockly.utils.Rect} The UI elements’s bounding box.
*/
Blockly.IPositionable.prototype.getBoundingRectangle;
20 changes: 0 additions & 20 deletions core/interfaces/i_workspace_plugin.js

This file was deleted.

Loading