From 7685dad40ad8f791f2badafc1b531164660a9156 Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Thu, 18 Apr 2019 09:58:43 -0700 Subject: [PATCH] update extension event handling for categoryInfo --- src/containers/blocks.jsx | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/containers/blocks.jsx b/src/containers/blocks.jsx index 0b95ebc89..e7a4780df 100644 --- a/src/containers/blocks.jsx +++ b/src/containers/blocks.jsx @@ -414,20 +414,33 @@ class Blocks extends React.Component { // workspace to be 'undone' here. this.workspace.clearUndo(); } - handleExtensionAdded (blocksInfo) { - // select JSON from each block info object then reject the pseudo-blocks which don't have JSON, like separators - // this actually defines blocks and MUST run regardless of the UI state - this.ScratchBlocks.defineBlocksWithJsonArray(blocksInfo.map(blockInfo => blockInfo.json).filter(x => x)); + handleExtensionAdded (categoryInfo) { + // scratch-blocks implements a menu or custom field as a special kind of block + // these actually define blocks and MUST run regardless of the UI state + // skip pseudo-block items that don't have JSON, such as the '---' separator + const jsonIsNotEmpty = (json => json); + this.ScratchBlocks.defineBlocksWithJsonArray( + Object.getOwnPropertyNames(categoryInfo.customFieldTypes) + .map(fieldTypeName => categoryInfo.customFieldTypes[fieldTypeName].scratchBlocksDefinition.json) + .filter(jsonIsNotEmpty)); + this.ScratchBlocks.defineBlocksWithJsonArray( + categoryInfo.menus + .map(menuInfo => menuInfo.json) + .filter(jsonIsNotEmpty)); + this.ScratchBlocks.defineBlocksWithJsonArray( + categoryInfo.blocks + .map(blockInfo => blockInfo.json) + .filter(jsonIsNotEmpty)); - // Update the toolbox with new blocks + // Update the toolbox with new blocks if possible const toolboxXML = this.getToolboxXML(); if (toolboxXML) { this.props.updateToolboxState(toolboxXML); } } - handleBlocksInfoUpdate (blocksInfo) { + handleBlocksInfoUpdate (categoryInfo) { // @todo Later we should replace this to avoid all the warnings from redefining blocks. - this.handleExtensionAdded(blocksInfo); + this.handleExtensionAdded(categoryInfo); } handleCategorySelected (categoryId) { const extension = extensionData.find(ext => ext.extensionId === categoryId);