Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow block creators to disable block deletion via block support. #18118

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,14 @@ multiple: false,
- `reusable` (default `true`): A block may want to disable the ability of being converted into a reusable block.
By default all blocks can be converted to a reusable block. If supports reusable is set to false, the option to convert the block into a reusable block will not appear.

```js
// Use the block just once per post
removal: false,
```

- `removal` (default `true`): You may want to disable the user's ability to remove a block.
By default, all blocks can be removed. If supports removal is set to false, UI for removing the block will not appear.

```js
// Don't allow the block to be converted into a reusable block.
reusable: false,
Expand Down
13 changes: 12 additions & 1 deletion packages/block-editor/src/components/block-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { cloneBlock, hasBlockSupport, switchToBlockType } from '@wordpress/block
function BlockActions( {
canDuplicate,
canInsertDefaultBlock,
canRemove,
children,
isLocked,
onDuplicate,
Expand All @@ -25,6 +26,7 @@ function BlockActions( {
return children( {
canDuplicate,
canInsertDefaultBlock,
canRemove,
isLocked,
onDuplicate,
onGroup,
Expand Down Expand Up @@ -55,6 +57,13 @@ export default compose( [
);
} );

const canRemove = every( blocks, ( block ) => {
return (
!! block &&
hasBlockSupport( block.name, 'removal', true )
);
} );

const canInsertDefaultBlock = canInsertBlockType(
getDefaultBlockName(),
rootClientId
Expand All @@ -63,6 +72,7 @@ export default compose( [
return {
blocks,
canDuplicate,
canRemove,
canInsertDefaultBlock,
extraProps: props,
isLocked: !! getTemplateLock( rootClientId ),
Expand All @@ -76,6 +86,7 @@ export default compose( [
blocks,
isLocked,
canDuplicate,
canRemove,
} = props;

const {
Expand Down Expand Up @@ -108,7 +119,7 @@ export default compose( [
}
},
onRemove() {
if ( ! isLocked ) {
if ( ! isLocked && canRemove ) {
removeBlocks( clientIds );
}
},
Expand Down
6 changes: 5 additions & 1 deletion packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { BACKSPACE, DELETE, ENTER, ESCAPE } from '@wordpress/keycodes';
import {
getBlockType,
getSaveElement,
hasBlockSupport,
isReusableBlock,
isUnmodifiedDefaultBlock,
getUnregisteredTypeHandlerName,
Expand Down Expand Up @@ -675,6 +676,7 @@ const applyWithSelect = withSelect(
);

const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
const { name: blockName } = ownProps;
const {
updateBlockAttributes,
selectBlock,
Expand Down Expand Up @@ -718,7 +720,9 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
insertBlocks( blocks, index + 1, rootClientId );
},
onRemove( clientId ) {
removeBlock( clientId );
if ( hasBlockSupport( blockName, 'removal', true ) ) {
removeBlock( clientId );
}
},
onMerge( forward ) {
const { clientId } = ownProps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function BlockSettingsMenu( { clientIds } ) {
onInsertAfter,
onInsertBefore,
onRemove,
canRemove,
} ) => (
<Toolbar>
<DropdownMenu
Expand Down Expand Up @@ -112,6 +113,7 @@ export function BlockSettingsMenu( { clientIds } ) {
<MenuGroup>
{ ! isLocked && (
<MenuItem
disabled={ ! canRemove }
Copy link
Member Author

@noahtallen noahtallen Oct 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in php, we might use a filter:

$disabled = apply_filters( 'should_disable_block_remove_button', false, $block_name );

I wonder what the best approach for doing something like that would be here. I'm familiar-ish with slot fill, but not familiar with JS filters/actions in Gutenberg.

Do we need to implement a store for something like how we remove settings panels?

className="editor-block-settings-menu__control block-editor-block-settings-menu__control"
onClick={ flow( onClose, onRemove ) }
icon="trash"
Expand Down