Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix block factory in manual mode #6533

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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