Skip to content

Commit

Permalink
fix: Let block factory overwrite user defined blocks. (#8605)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnesky authored Oct 2, 2024
1 parent 5fd337b commit 9b3603a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
4 changes: 2 additions & 2 deletions demos/blockfactory/block_library_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 13 additions & 13 deletions demos/blockfactory/block_library_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions demos/blockfactory/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
5 changes: 3 additions & 2 deletions demos/blockfactory/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9b3603a

Please sign in to comment.