Migrate core/field_registry.js to named requires

This commit is contained in:
Aaron Dodson
2021-07-22 08:34:55 -07:00
parent 063dc8b804
commit 222b234802

View File

@@ -15,25 +15,26 @@
goog.module('Blockly.fieldRegistry');
goog.module.declareLegacyNamespace();
goog.require('Blockly.registry');
goog.requireType('Blockly.Field');
goog.requireType('Blockly.IRegistrableField');
/* eslint-disable-next-line no-unused-vars */
const Field = goog.requireType('Blockly.Field');
/* eslint-disable-next-line no-unused-vars */
const IRegistrableField = goog.requireType('Blockly.IRegistrableField');
const registry = goog.require('Blockly.registry');
/**
* Registers a field type.
* Blockly.fieldRegistry.fromJson uses this registry to
* fieldRegistry.fromJson uses this registry to
* find the appropriate field type.
* @param {string} type The field type name as used in the JSON definition.
* @param {!Blockly.IRegistrableField} fieldClass The field class containing a
* @param {!IRegistrableField} fieldClass The field class containing a
* fromJson function that can construct an instance of the field.
* @throws {Error} if the type name is empty, the field is already
* registered, or the fieldClass is not an object containing a fromJson
* function.
*/
const register = function(type, fieldClass) {
Blockly.registry.register(Blockly.registry.Type.FIELD, type, fieldClass);
registry.register(registry.Type.FIELD, type, fieldClass);
};
exports.register = register;
@@ -42,22 +43,22 @@ exports.register = register;
* @param {string} type The field type name as used in the JSON definition.
*/
const unregister = function(type) {
Blockly.registry.unregister(Blockly.registry.Type.FIELD, type);
registry.unregister(registry.Type.FIELD, type);
};
exports.unregister = unregister;
/**
* Construct a Field from a JSON arg object.
* Finds the appropriate registered field by the type name as registered using
* Blockly.fieldRegistry.register.
* fieldRegistry.register.
* @param {!Object} options A JSON object with a type and options specific
* to the field type.
* @return {?Blockly.Field} The new field instance or null if a field wasn't
* @return {?Field} The new field instance or null if a field wasn't
* found with the given type name
*/
const fromJson = function(options) {
const fieldObject = /** @type {?Blockly.IRegistrableField} */ (
Blockly.registry.getObject(Blockly.registry.Type.FIELD, options['type']));
const fieldObject = /** @type {?IRegistrableField} */ (
registry.getObject(registry.Type.FIELD, options['type']));
if (!fieldObject) {
console.warn('Blockly could not create a field of type ' + options['type'] +
'. The field is probably not being registered. This could be because' +