Use registry for creating the no-category flyout (#4659)

* Use registry for creating the horizontal / vertical flyout
This commit is contained in:
Sam El-Husseini
2021-03-01 10:00:47 -08:00
committed by GitHub
parent 01035c3526
commit 5780399750
4 changed files with 43 additions and 41 deletions

View File

@@ -180,12 +180,9 @@ Blockly.registry.unregister = function(type, name) {
type = String(type).toLowerCase();
name = name.toLowerCase();
var typeRegistry = Blockly.registry.typeMap_[type];
if (!typeRegistry) {
console.warn('No type "' + type + '" found');
return;
}
if (!typeRegistry[name]) {
console.warn('No name "' + name + '" with type "' + type + '" found');
if (!typeRegistry || !typeRegistry[name]) {
console.warn('Unable to unregister [' + name + '][' + type + '] from the ' +
'registry.');
return;
}
delete Blockly.registry.typeMap_[type][name];
@@ -197,20 +194,24 @@ Blockly.registry.unregister = function(type, name) {
* @param {string|!Blockly.registry.Type<T>} type The type of the plugin.
* (e.g. Field, Renderer)
* @param {string} name The plugin's name. (Ex. field_angle, geras)
* @param {boolean=} opt_throwIfMissing Whether or not to throw an error if we
* are unable to find the plugin.
* @return {?function(new:T, ...?)|Object} The class or object with the given
* name and type or null if none exists.
* @template T
*/
Blockly.registry.getItem_ = function(type, name) {
Blockly.registry.getItem_ = function(type, name, opt_throwIfMissing) {
type = String(type).toLowerCase();
name = name.toLowerCase();
var typeRegistry = Blockly.registry.typeMap_[type];
if (!typeRegistry) {
console.warn('No type "' + type + '" found');
return null;
}
if (!typeRegistry[name]) {
console.warn('No name "' + name + '" with type "' + type + '" found');
if (!typeRegistry || !typeRegistry[name]) {
var msg = 'Unable to find [' + name + '][' + type + '] in the registry.';
if (opt_throwIfMissing) {
throw new Error(msg + ' You must require or register a ' + type +
' plugin.');
} else {
console.warn(msg);
}
return null;
}
return typeRegistry[name];
@@ -241,13 +242,15 @@ Blockly.registry.hasItem = function(type, name) {
* @param {string|!Blockly.registry.Type<T>} type The type of the plugin.
* (e.g. Field, Renderer)
* @param {string} name The plugin's name. (Ex. field_angle, geras)
* @param {boolean=} opt_throwIfMissing Whether or not to throw an error if we
* are unable to find the plugin.
* @return {?function(new:T, ...?)} The class with the given name and type or
* null if none exists.
* @template T
*/
Blockly.registry.getClass = function(type, name) {
Blockly.registry.getClass = function(type, name, opt_throwIfMissing) {
return /** @type {?function(new:T, ...?)} */ (
Blockly.registry.getItem_(type, name));
Blockly.registry.getItem_(type, name, opt_throwIfMissing));
};
/**
@@ -255,11 +258,14 @@ Blockly.registry.getClass = function(type, name) {
* @param {string|!Blockly.registry.Type<T>} type The type of the plugin.
* (e.g. Category)
* @param {string} name The plugin's name. (Ex. logic_category)
* @param {boolean=} opt_throwIfMissing Whether or not to throw an error if we
* are unable to find the object.
* @returns {T} The object with the given name and type or null if none exists.
* @template T
*/
Blockly.registry.getObject = function(type, name) {
return /** @type {T} */ (Blockly.registry.getItem_(type, name));
Blockly.registry.getObject = function(type, name, opt_throwIfMissing) {
return /** @type {T} */ (
Blockly.registry.getItem_(type, name, opt_throwIfMissing));
};
/**
@@ -268,10 +274,13 @@ Blockly.registry.getObject = function(type, name) {
* @param {!Blockly.registry.Type<T>} type The type of the plugin.
* @param {!Blockly.Options} options The option object to check for the given
* plugin.
* @param {boolean=} opt_throwIfMissing Whether or not to throw an error if we
* are unable to find the plugin.
* @return {?function(new:T, ...?)} The class for the plugin.
* @template T
*/
Blockly.registry.getClassFromOptions = function(type, options) {
Blockly.registry.getClassFromOptions = function(type, options,
opt_throwIfMissing) {
var typeName = type.toString();
var plugin = options.plugins[typeName] || Blockly.registry.DEFAULT;
@@ -279,5 +288,5 @@ Blockly.registry.getClassFromOptions = function(type, options) {
if (typeof plugin == 'function') {
return plugin;
}
return Blockly.registry.getClass(type, plugin);
return Blockly.registry.getClass(type, plugin, opt_throwIfMissing);
};

View File

@@ -351,15 +351,12 @@ Blockly.Toolbox.prototype.createFlyout_ = function() {
var FlyoutClass = null;
if (workspace.horizontalLayout) {
FlyoutClass = Blockly.registry.getClassFromOptions(
Blockly.registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, workspace.options);
Blockly.registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, workspace.options,
true);
} else {
FlyoutClass = Blockly.registry.getClassFromOptions(
Blockly.registry.Type.FLYOUTS_VERTICAL_TOOLBOX, workspace.options);
}
if (!FlyoutClass) {
throw new Error('Blockly.VerticalFlyout, Blockly.HorizontalFlyout or your own' +
' custom flyout must be required.');
Blockly.registry.Type.FLYOUTS_VERTICAL_TOOLBOX, workspace.options,
true);
}
return new FlyoutClass(workspaceOptions);
};

View File

@@ -51,7 +51,7 @@ Blockly.Workspace = function(opt_options) {
this.toolboxPosition = this.options.toolboxPosition;
var connectionCheckerClass = Blockly.registry.getClassFromOptions(
Blockly.registry.Type.CONNECTION_CHECKER, this.options);
Blockly.registry.Type.CONNECTION_CHECKER, this.options, true);
/**
* An object that encapsulates logic for safety, type, and dragging checks.
* @type {!Blockly.IConnectionChecker}

View File

@@ -17,6 +17,7 @@ goog.require('Blockly.BlockSvg');
goog.require('Blockly.browserEvents');
goog.require('Blockly.ConnectionDB');
goog.require('Blockly.constants');
goog.require('Blockly.ContextMenu');
goog.require('Blockly.ContextMenuRegistry');
goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockCreate');
@@ -81,7 +82,7 @@ Blockly.WorkspaceSvg = function(
Blockly.WorkspaceSvg.superClass_.constructor.call(this, options);
var MetricsManagerClass = Blockly.registry.getClassFromOptions(
Blockly.registry.Type.METRICS_MANAGER, options);
Blockly.registry.Type.METRICS_MANAGER, options, true);
/**
* Object in charge of calculating metrics for the workspace.
* @type {!Blockly.IMetricsManager}
@@ -819,11 +820,8 @@ Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) {
// Determine if there needs to be a category tree, or a simple list of
// blocks. This cannot be changed later, since the UI is very different.
if (this.options.hasCategories) {
if (!Blockly.Toolbox) {
throw Error('Missing require for Blockly.Toolbox');
}
var ToolboxClass = Blockly.registry.getClassFromOptions(
Blockly.registry.Type.TOOLBOX, this.options);
Blockly.registry.Type.TOOLBOX, this.options, true);
this.toolbox_ = new ToolboxClass(this);
}
if (this.grid_) {
@@ -833,7 +831,7 @@ Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) {
var CursorClass = Blockly.registry.getClassFromOptions(
Blockly.registry.Type.CURSOR, this.options);
this.markerManager_.setCursor(new CursorClass());
CursorClass && this.markerManager_.setCursor(new CursorClass());
this.renderer_.createDom(this.svgGroup_, this.getTheme());
return this.svgGroup_;
@@ -992,15 +990,13 @@ Blockly.WorkspaceSvg.prototype.addFlyout = function(tagName) {
}));
workspaceOptions.toolboxPosition = this.options.toolboxPosition;
if (this.horizontalLayout) {
if (!Blockly.HorizontalFlyout) {
throw Error('Missing require for Blockly.HorizontalFlyout');
}
this.flyout_ = new Blockly.HorizontalFlyout(workspaceOptions);
var HorizontalFlyout = Blockly.registry.getClassFromOptions(
Blockly.registry.Type.FLYOUTS_HORIZONTAL_TOOLBOX, this.options, true);
this.flyout_ = new HorizontalFlyout(workspaceOptions);
} else {
if (!Blockly.VerticalFlyout) {
throw Error('Missing require for Blockly.VerticalFlyout');
}
this.flyout_ = new Blockly.VerticalFlyout(workspaceOptions);
var VerticalFlyout = Blockly.registry.getClassFromOptions(
Blockly.registry.Type.FLYOUTS_VERTICAL_TOOLBOX, this.options, true);
this.flyout_ = new VerticalFlyout(workspaceOptions);
}
this.flyout_.autoClose = false;
this.flyout_.getWorkspace().setVisible(true);