Skip to content

Commit

Permalink
Hide 'Add to Reusable Blocks' button when 'core/block' is disabled (#…
Browse files Browse the repository at this point in the history
…10493)

* Hide 'Add to Reusable Blocks' button when 'core/block' is disabled

Hides the 'Add to Reusable Block' buton when 'core/block' is not in the
array returned by the `allowed_block_types` filter.

* Add comment explaining existing every() check
  • Loading branch information
noisysocks authored Oct 16, 2018
1 parent 7ab36ba commit 6e381ed
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,38 @@ export function ReusableBlockConvertButton( {

export default compose( [
withSelect( ( select, { clientIds } ) => {
const { getBlock, getReusableBlock } = select( 'core/editor' );
const { getBlock, canInsertBlockType, getReusableBlock } = select( 'core/editor' );
const {
getFreeformFallbackBlockName,
getUnregisteredFallbackBlockName,
} = select( 'core/blocks' );

const blocks = map( clientIds, ( clientId ) => getBlock( clientId ) );

// Hide 'Add to Reusable Blocks' on Classic blocks. Showing it causes a
// confusing UX, because of its similarity to the 'Convert to Blocks' button.
const isVisible = (
// Guard against the case where a regular block has *just* been converted to a
// reusable block and doesn't yet exist in the editor store.
every( blocks, ( block ) => !! block ) &&

// Hide 'Add to Reusable Blocks' when Reusable Blocks are disabled, i.e. when
// core/block is not in the allowed_block_types filter.
canInsertBlockType( 'core/block' ) &&

// Hide 'Add to Reusable Blocks' on Classic blocks. Showing it causes a
// confusing UX, because of its similarity to the 'Convert to Blocks' button.
( blocks.length !== 1 || (
blocks[ 0 ].name !== getFreeformFallbackBlockName() &&
blocks[ 0 ].name !== getUnregisteredFallbackBlockName()
) )
);

return {
isVisible,
isStaticBlock: isVisible && (
blocks.length !== 1 ||
! isReusableBlock( blocks[ 0 ] ) ||
! getReusableBlock( blocks[ 0 ].attributes.ref )
),
isVisible,
};
} ),
withDispatch( ( dispatch, { clientIds, onToggle = noop } ) => {
Expand Down

0 comments on commit 6e381ed

Please sign in to comment.