mirror of
https://github.com/google/blockly.git
synced 2025-12-13 12:50:07 +01:00
fix: Let block factory overwrite user defined blocks. (#8605)
This commit is contained in:
@@ -109,9 +109,9 @@ BlockLibraryController.prototype.clearBlockLibrary = function() {
|
||||
BlockLibraryController.prototype.saveToBlockLibrary = function() {
|
||||
var blockType = this.getCurrentBlockType();
|
||||
// If user has not changed the name of the starter block.
|
||||
if (blockType === 'block_type') {
|
||||
if (reservedBlockFactoryBlocks.has(blockType) || blockType === 'block_type') {
|
||||
// Do not save block if it has the default type, 'block_type'.
|
||||
var msg = 'You cannot save a block under the name "block_type". Try ' +
|
||||
var msg = `You cannot save a block under the name "${blockType}". Try ` +
|
||||
'changing the name before saving. Then, click on the "Block Library"' +
|
||||
' button to view your saved blocks.';
|
||||
alert(msg);
|
||||
|
||||
@@ -104,36 +104,36 @@ BlockLibraryView.prototype.updateButtons =
|
||||
// User is editing a block.
|
||||
|
||||
if (!isInLibrary) {
|
||||
// Block type has not been saved to library yet. Disable the delete button
|
||||
// and allow user to save.
|
||||
// Block type has not been saved to the library yet.
|
||||
// Disable the delete button.
|
||||
this.saveButton.textContent = 'Save "' + blockType + '"';
|
||||
this.saveButton.disabled = false;
|
||||
this.deleteButton.disabled = true;
|
||||
} else {
|
||||
// Block type has already been saved. Disable the save button unless the
|
||||
// there are unsaved changes (checked below).
|
||||
// A version of the block type has already been saved.
|
||||
// Enable the delete button.
|
||||
this.saveButton.textContent = 'Update "' + blockType + '"';
|
||||
this.saveButton.disabled = true;
|
||||
this.deleteButton.disabled = false;
|
||||
}
|
||||
this.deleteButton.textContent = 'Delete "' + blockType + '"';
|
||||
|
||||
// If changes to block have been made and are not saved, make button
|
||||
// green to encourage user to save the block.
|
||||
this.saveButton.classList.remove('button_alert', 'button_warn');
|
||||
if (!savedChanges) {
|
||||
var buttonFormatClass = 'button_warn';
|
||||
var buttonFormatClass;
|
||||
|
||||
// If block type is the default, 'block_type', make button red to alert
|
||||
// user.
|
||||
if (blockType === 'block_type') {
|
||||
var isReserved = reservedBlockFactoryBlocks.has(blockType);
|
||||
if (isReserved || blockType === 'block_type') {
|
||||
// Make button red to alert user that the block type can't be saved.
|
||||
buttonFormatClass = 'button_alert';
|
||||
} else {
|
||||
// Block type has not been saved to library yet or has unsaved changes.
|
||||
// Make the button green to encourage the user to save the block.
|
||||
buttonFormatClass = 'button_warn';
|
||||
}
|
||||
this.saveButton.classList.add(buttonFormatClass);
|
||||
this.saveButton.disabled = false;
|
||||
|
||||
} else {
|
||||
// No changes to save.
|
||||
this.saveButton.classList.remove('button_alert', 'button_warn');
|
||||
this.saveButton.disabled = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -914,3 +914,7 @@ function inputNameCheck(referenceBlock) {
|
||||
'There are ' + count + ' input blocks\n with this name.' : null;
|
||||
referenceBlock.setWarningText(msg);
|
||||
}
|
||||
|
||||
// Make a set of all of block types that are required for the block factory.
|
||||
var reservedBlockFactoryBlocks =
|
||||
new Set(Object.getOwnPropertyNames(Blockly.Blocks));
|
||||
|
||||
@@ -187,8 +187,9 @@ BlockFactory.updatePreview = function() {
|
||||
// Don't let the user create a block type that already exists,
|
||||
// because it doesn't work.
|
||||
var warnExistingBlock = function(blockType) {
|
||||
if (blockType in Blockly.Blocks) {
|
||||
var text = `You can't make a block called ${blockType} in this tool because that name already exists.`;
|
||||
if (reservedBlockFactoryBlocks.has(blockType)) {
|
||||
var text = `You can't make a block called ${blockType} in this tool ` +
|
||||
`because that name is reserved.`;
|
||||
FactoryUtils.getRootBlock(BlockFactory.mainWorkspace).setWarningText(text);
|
||||
console.error(text);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user