Skip to content

Commit

Permalink
Reusable Blocks: Hide 'Convert to Blocks' when locked
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Mar 31, 2022
1 parent 2a2938f commit 773732d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
28 changes: 19 additions & 9 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import {
useEntityBlockEditor,
useEntityProp,
Expand All @@ -25,6 +25,7 @@ import {
InspectorControls,
useBlockProps,
Warning,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';
import { ungroup } from '@wordpress/icons';
Expand All @@ -40,6 +41,11 @@ export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
);
const isMissing = hasResolved && ! record;

const canRemove = useSelect(
( select ) => select( blockEditorStore ).canRemoveBlock( clientId ),
[ clientId ]
);

const {
__experimentalConvertBlockToStatic: convertBlockToStatic,
} = useDispatch( reusableBlocksStore );
Expand Down Expand Up @@ -104,14 +110,18 @@ export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
<RecursionProvider>
<div { ...blockProps }>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
onClick={ () => convertBlockToStatic( clientId ) }
label={ __( 'Convert to regular blocks' ) }
icon={ ungroup }
showTooltip
/>
</ToolbarGroup>
{ canRemove && (
<ToolbarGroup>
<ToolbarButton
onClick={ () =>
convertBlockToStatic( clientId )
}
label={ __( 'Convert to regular blocks' ) }
icon={ ungroup }
showTooltip
/>
</ToolbarGroup>
) }
</BlockControls>
<InspectorControls>
<PanelBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import { store as coreStore } from '@wordpress/core-data';
import { store as reusableBlocksStore } from '../../store';

function ReusableBlocksManageButton( { clientId } ) {
const { isVisible } = useSelect(
const { canRemove, isVisible } = useSelect(
( select ) => {
const { getBlock } = select( blockEditorStore );
const { getBlock, canRemoveBlock } = select( blockEditorStore );
const { canUser } = select( coreStore );
const reusableBlock = getBlock( clientId );

return {
canRemove: canRemoveBlock( clientId ),
isVisible:
!! reusableBlock &&
isReusableBlock( reusableBlock ) &&
Expand Down Expand Up @@ -53,9 +54,11 @@ function ReusableBlocksManageButton( { clientId } ) {
>
{ __( 'Manage Reusable blocks' ) }
</MenuItem>
<MenuItem onClick={ () => convertBlockToStatic( clientId ) }>
{ __( 'Convert to regular blocks' ) }
</MenuItem>
{ canRemove && (
<MenuItem onClick={ () => convertBlockToStatic( clientId ) }>
{ __( 'Convert to regular blocks' ) }
</MenuItem>
) }
</BlockSettingsMenuControls>
);
}
Expand Down

0 comments on commit 773732d

Please sign in to comment.