mirror of
https://github.com/google/blockly.git
synced 2026-01-09 18:10:08 +01:00
Updating behavior of register functions and adding unregister. (#3085)
* Updating behavior of register functions and adding unregister. * Updates jsdoc comments.
This commit is contained in:
@@ -128,6 +128,19 @@ Blockly.Extensions.registerMutator = function(name, mixinObj, opt_helperFn,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Unregisters the extension registered with the given name.
|
||||
* @param {string} name The name of the extension to unregister.
|
||||
*/
|
||||
Blockly.Extensions.unregister = function(name) {
|
||||
if (Blockly.Extensions.ALL_[name]) {
|
||||
Blockly.Extensions.ALL_[name] = undefined;
|
||||
} else {
|
||||
console.warn('No extension mapping for name "' + name +
|
||||
'" found to unregister');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Applies an extension method to a block. This should only be called during
|
||||
* block construction.
|
||||
|
||||
@@ -38,20 +38,24 @@ goog.provide('Blockly.fieldRegistry');
|
||||
Blockly.fieldRegistry.typeMap_ = {};
|
||||
|
||||
/**
|
||||
* Registers a field type. May also override an existing field type.
|
||||
* Registers a field type.
|
||||
* Blockly.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 {!{fromJson: Function}} fieldClass The field class containing a
|
||||
* fromJson function that can construct an instance of the field.
|
||||
* @throws {Error} if the type name is empty, or the fieldClass is not an
|
||||
* object containing a fromJson function.
|
||||
* @throws {Error} if the type name is empty, the field is already
|
||||
* registered, or the fieldClass is not an object containing a fromJson
|
||||
* function.
|
||||
*/
|
||||
Blockly.fieldRegistry.register = function(type, fieldClass) {
|
||||
if ((typeof type != 'string') || (type.trim() == '')) {
|
||||
throw Error('Invalid field type "' + type + '". The type must be a' +
|
||||
' non-empty string.');
|
||||
}
|
||||
if (Blockly.fieldRegistry.typeMap_[type]) {
|
||||
throw Error('Error: Field "' + type + '" is already registered.');
|
||||
}
|
||||
if (!fieldClass || (typeof fieldClass.fromJson != 'function')) {
|
||||
throw Error('Field "' + fieldClass + '" must have a fromJson function');
|
||||
}
|
||||
@@ -59,6 +63,19 @@ Blockly.fieldRegistry.register = function(type, fieldClass) {
|
||||
Blockly.fieldRegistry.typeMap_[type] = fieldClass;
|
||||
};
|
||||
|
||||
/**
|
||||
* Unregisters the field registered with the given type.
|
||||
* @param {string} type The field type name as used in the JSON definition.
|
||||
*/
|
||||
Blockly.fieldRegistry.unregister = function(type) {
|
||||
if (Blockly.fieldRegistry.typeMap_[type]) {
|
||||
Blockly.fieldRegistry.typeMap_[type] = undefined;
|
||||
} else {
|
||||
console.warn('No field mapping for type "' + type +
|
||||
'" found to unregister');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Construct a Field from a JSON arg object.
|
||||
* Finds the appropriate registered field by the type name as registered using
|
||||
|
||||
@@ -61,6 +61,19 @@ Blockly.blockRendering.register = function(name, rendererClass) {
|
||||
Blockly.blockRendering.rendererMap_[name] = rendererClass;
|
||||
};
|
||||
|
||||
/**
|
||||
* Unregisters the renderer registered with the given name.
|
||||
* @param {string} name The name of the renderer.
|
||||
*/
|
||||
Blockly.blockRendering.unregister = function(name) {
|
||||
if (Blockly.blockRendering.rendererMap_[name]) {
|
||||
Blockly.blockRendering.rendererMap_[name] = undefined;
|
||||
} else {
|
||||
console.warn('No renderer mapping for name "' + name +
|
||||
'" found to unregister');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Turn on the blocks debugger.
|
||||
* @package
|
||||
|
||||
@@ -57,11 +57,11 @@ suite('Field Registry', function() {
|
||||
Blockly.fieldRegistry.register(CustomFieldType.fromJson, '');
|
||||
}, 'Invalid field type');
|
||||
});
|
||||
// TODO (#2788): What do you want it to do if you overwrite a key?
|
||||
test('Overwrite a Key', function() {
|
||||
Blockly.fieldRegistry.register('field_custom_test', CustomFieldType);
|
||||
|
||||
Blockly.fieldRegistry.register('field_custom_test', CustomFieldType);
|
||||
chai.assert.throws(function() {
|
||||
Blockly.fieldRegistry.register('field_custom_test', CustomFieldType);
|
||||
}, 'already registered');
|
||||
});
|
||||
test('Null Value', function() {
|
||||
chai.assert.throws(function() {
|
||||
|
||||
Reference in New Issue
Block a user