Skip to content

Commit

Permalink
Refs #7957 - Inserter: take into account referenced blocks
Browse files Browse the repository at this point in the history
When deriving whether a block should be disabled or not,
the inserter should take into account the blocks referenced by
the existing top-level shared blocks.
  • Loading branch information
oandregal committed Jul 26, 2018
1 parent 1a0b22a commit 52fe46b
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 52fe46b

Please sign in to comment.