fix: Let block factory overwrite user defined blocks. (#8605)

This commit is contained in:
John Nesky
2024-10-01 17:00:59 -07:00
committed by GitHub
parent 5fd337bb4d
commit 9b3603a3ea
4 changed files with 22 additions and 17 deletions

View File

@@ -109,9 +109,9 @@ BlockLibraryController.prototype.clearBlockLibrary = function() {
BlockLibraryController.prototype.saveToBlockLibrary = function() { BlockLibraryController.prototype.saveToBlockLibrary = function() {
var blockType = this.getCurrentBlockType(); var blockType = this.getCurrentBlockType();
// If user has not changed the name of the starter block. // 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'. // 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"' + 'changing the name before saving. Then, click on the "Block Library"' +
' button to view your saved blocks.'; ' button to view your saved blocks.';
alert(msg); alert(msg);

View File

@@ -104,36 +104,36 @@ BlockLibraryView.prototype.updateButtons =
// User is editing a block. // User is editing a block.
if (!isInLibrary) { if (!isInLibrary) {
// Block type has not been saved to library yet. Disable the delete button // Block type has not been saved to the library yet.
// and allow user to save. // Disable the delete button.
this.saveButton.textContent = 'Save "' + blockType + '"'; this.saveButton.textContent = 'Save "' + blockType + '"';
this.saveButton.disabled = false;
this.deleteButton.disabled = true; this.deleteButton.disabled = true;
} else { } else {
// Block type has already been saved. Disable the save button unless the // A version of the block type has already been saved.
// there are unsaved changes (checked below). // Enable the delete button.
this.saveButton.textContent = 'Update "' + blockType + '"'; this.saveButton.textContent = 'Update "' + blockType + '"';
this.saveButton.disabled = true;
this.deleteButton.disabled = false; this.deleteButton.disabled = false;
} }
this.deleteButton.textContent = 'Delete "' + blockType + '"'; this.deleteButton.textContent = 'Delete "' + blockType + '"';
// If changes to block have been made and are not saved, make button this.saveButton.classList.remove('button_alert', 'button_warn');
// green to encourage user to save the block.
if (!savedChanges) { if (!savedChanges) {
var buttonFormatClass = 'button_warn'; var buttonFormatClass;
// If block type is the default, 'block_type', make button red to alert var isReserved = reservedBlockFactoryBlocks.has(blockType);
// user. if (isReserved || blockType === 'block_type') {
if (blockType === 'block_type') { // Make button red to alert user that the block type can't be saved.
buttonFormatClass = 'button_alert'; 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.classList.add(buttonFormatClass);
this.saveButton.disabled = false; this.saveButton.disabled = false;
} else { } else {
// No changes to save. // No changes to save.
this.saveButton.classList.remove('button_alert', 'button_warn');
this.saveButton.disabled = true; this.saveButton.disabled = true;
} }

View File

@@ -914,3 +914,7 @@ function inputNameCheck(referenceBlock) {
'There are ' + count + ' input blocks\n with this name.' : null; 'There are ' + count + ' input blocks\n with this name.' : null;
referenceBlock.setWarningText(msg); 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));

View File

@@ -187,8 +187,9 @@ BlockFactory.updatePreview = function() {
// Don't let the user create a block type that already exists, // Don't let the user create a block type that already exists,
// because it doesn't work. // because it doesn't work.
var warnExistingBlock = function(blockType) { var warnExistingBlock = function(blockType) {
if (blockType in Blockly.Blocks) { if (reservedBlockFactoryBlocks.has(blockType)) {
var text = `You can't make a block called ${blockType} in this tool because that name already exists.`; var text = `You can't make a block called ${blockType} in this tool ` +
`because that name is reserved.`;
FactoryUtils.getRootBlock(BlockFactory.mainWorkspace).setWarningText(text); FactoryUtils.getRootBlock(BlockFactory.mainWorkspace).setWarningText(text);
console.error(text); console.error(text);
return true; return true;