diff --git a/demos/blockfactory/block_library_controller.js b/demos/blockfactory/block_library_controller.js index 7bb34e8d6..8eed54db0 100644 --- a/demos/blockfactory/block_library_controller.js +++ b/demos/blockfactory/block_library_controller.js @@ -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); diff --git a/demos/blockfactory/block_library_view.js b/demos/blockfactory/block_library_view.js index a47980408..2c91ce378 100644 --- a/demos/blockfactory/block_library_view.js +++ b/demos/blockfactory/block_library_view.js @@ -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; } diff --git a/demos/blockfactory/blocks.js b/demos/blockfactory/blocks.js index 34313cad1..9a983460f 100644 --- a/demos/blockfactory/blocks.js +++ b/demos/blockfactory/blocks.js @@ -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)); diff --git a/demos/blockfactory/factory.js b/demos/blockfactory/factory.js index 07ee889de..2e6ebc924 100644 --- a/demos/blockfactory/factory.js +++ b/demos/blockfactory/factory.js @@ -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;