diff --git a/editor/store/selectors.js b/editor/store/selectors.js index 16ade0c8ca4fc..e78ed06fb8321 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -563,6 +563,31 @@ export const getBlocks = createSelector( ] ); +/** + * Returns an array containing both top-level blocks + * and all blocks referenced by the existing top-level shared blocks. + * + * @param {Object} state Global application state. + * + * @return {Array} All top-level and referenced blocks. + */ +export const getBlocksTopLevelAndReferenced = createSelector( + ( state ) => { + const topLevelBlocks = getBlocks( state ); + const referencedBlocksIds = topLevelBlocks.map( + ( block ) => block.name === 'core/block' ? + get( getSharedBlock( state, block.attributes.ref ), [ 'clientId' ], null ) : + null, + ).filter( ( id ) => !! id ); + const referencedBlocks = getBlocksByClientId( state, referencedBlocksIds ); + return [ ...topLevelBlocks, ...referencedBlocks ]; + }, + ( state ) => [ + state.editor.present.blockOrder, + state.editor.present.blocksByClientId, + ] +); + /** * Returns the total number of blocks, or the total number of blocks with a specific name in a post. * The number returned includes nested blocks. @@ -1539,7 +1564,7 @@ export const getInserterItems = createSelector( let isDisabled = false; if ( ! hasBlockSupport( blockType.name, 'multiple', true ) ) { - isDisabled = some( getBlocks( state ), { name: blockType.name } ); + isDisabled = some( getBlocksTopLevelAndReferenced( state ), { name: blockType.name } ); } const isContextual = isArray( blockType.parent );