Skip to content

Commit

Permalink
fix: fix block factory in manual mode (#6533)
Browse files Browse the repository at this point in the history
  • Loading branch information
maribethb authored Oct 12, 2022
1 parent a9b0b19 commit a64d6e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion demos/blockfactory/block_definition_extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ BlockDefinitionExtractor.colourBlockFromHue_ = function(hue) {
var colourBlock = BlockDefinitionExtractor.newDomElement_(
'block', {type: 'colour_hue'});
colourBlock.append(BlockDefinitionExtractor.newDomElement_('mutation', {
colour: Blockly.hueToRgb(hue)
colour: Blockly.utils.colour.hueToHex(hue)
}));
colourBlock.append(BlockDefinitionExtractor.newDomElement_(
'field', {name: 'HUE'}, hue.toString()));
Expand Down
52 changes: 28 additions & 24 deletions demos/blockfactory/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,42 +178,46 @@ BlockFactory.updatePreview = function() {
return;
}

// Backup Blockly.Blocks definitions so we can delete them all
// before instantiating user-defined block. This avoids a collision
// between the main workspace and preview if the user creates a
// 'factory_base' block, for instance.
var originalBlocks = Object.assign(Object.create(null), Blockly.Blocks);
try {
// Delete existing blocks.
for (var key in Blockly.Blocks) {
delete Blockly.Blocks[key];
// 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.`;
FactoryUtils.getRootBlock(BlockFactory.mainWorkspace).setWarningText(text);
console.error(text);
return true;
}
return false;
}

var blockType = 'block_type';
var blockCreated = false;
try {
if (format === 'JSON') {
var json = JSON.parse(code);
Blockly.Blocks[json.type || BlockFactory.UNNAMED] = {
blockType = json.type || BlockFactory.UNNAMED;
if (warnExistingBlock(blockType)) {
return;
}
Blockly.Blocks[blockType] = {
init: function() {
this.jsonInit(json);
}
};
} else if (format === 'JavaScript') {
try {
blockType = FactoryUtils.getBlockTypeFromJsDefinition(code);
if (warnExistingBlock(blockType)) {
return;
}
eval(code);
} catch (e) {
// TODO: Display error in the UI
console.error("Error while evaluating JavaScript formatted block definition", e);
return;
}
}

// Look for newly-created block(s) (ideally just one).
var createdTypes = Object.getOwnPropertyNames(Blockly.Blocks);
if (createdTypes.length < 1) {
return;
} else if (createdTypes.length > 1) {
console.log('Unexpectedly found more than one block definition');
}
var blockType = createdTypes[0];
blockCreated = true;

// Create the preview block.
var previewBlock = BlockFactory.previewWorkspace.newBlock(blockType);
Expand Down Expand Up @@ -247,12 +251,12 @@ BlockFactory.updatePreview = function() {
BlockFactory.updateBlocksFlag = false
BlockFactory.updateBlocksFlagDelayed = false
} finally {
// Remove all newly-created block(s).
for (var key in Blockly.Blocks) {
delete Blockly.Blocks[key];
// Remove the newly-created block.
// We have to check if the block was actually created so that we don't remove
// one of the built-in blocks, like factory_base.
if (blockCreated) {
delete Blockly.Blocks[blockType];
}
// Restore original blocks.
Object.assign(Blockly.Blocks, originalBlocks);
}
};

Expand Down

0 comments on commit a64d6e9

Please sign in to comment.