Skip to content

Commit

Permalink
Blocks: Add supports lock flag (#39568)
Browse files Browse the repository at this point in the history
Add documentation
Make flag experimental

Author:    George Mamadashvili <[email protected]>
Date:      Mon Mar 28 18:01:27 2022 +0400
  • Loading branch information
Mamaduka authored and draganescu committed Mar 30, 2022
1 parent 105c46d commit 7417b7b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
14 changes: 14 additions & 0 deletions docs/reference-guides/block-api/block-supports.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,20 @@ supports: {
}
```

## __experimentalLock

- Type: `boolean`
- Default value: `true`

A block may want to disable the ability to toggle the lock state. It can be locked/unlocked by a user from the block "Options" dropdown by default. To disable this behavior, set `__experimentalLock` to `false`.

```js
supports: {
// Remove support for locking UI.
__experimentalLock: false
}
```

## spacing

- Type: `Object`
Expand Down
13 changes: 13 additions & 0 deletions docs/reference-guides/data/data-core-block-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ _Returns_

- `boolean`: Whether the given block type is allowed to be inserted.

### canLockBlockType

Determines if the given block type can be locked/unlocked by a user.

_Parameters_

- _state_ `Object`: Editor state.
- _nameOrType_ `(string|Object)`: Block name or type object.

_Returns_

- `boolean`: Whether a given block type can be locked/unlocked.

### canMoveBlock

Determines if the given block is allowed to be moved.
Expand Down
9 changes: 8 additions & 1 deletion packages/block-editor/src/components/block-lock/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ import BlockLockModal from './modal';
import { store as blockEditorStore } from '../../store';

export default function BlockLockMenuItem( { clientId } ) {
const { isLocked } = useSelect(
const { canLockBlock, isLocked } = useSelect(
( select ) => {
const {
canMoveBlock,
canRemoveBlock,
canLockBlockType,
getBlockName,
getBlockRootClientId,
} = select( blockEditorStore );
const rootClientId = getBlockRootClientId( clientId );

return {
canLockBlock: canLockBlockType( getBlockName( clientId ) ),
isLocked:
! canMoveBlock( clientId, rootClientId ) ||
! canRemoveBlock( clientId, rootClientId ),
Expand All @@ -37,6 +40,10 @@ export default function BlockLockMenuItem( { clientId } ) {
false
);

if ( ! canLockBlock ) {
return null;
}

const label = isLocked ? __( 'Unlock' ) : __( 'Lock' );

return (
Expand Down
15 changes: 13 additions & 2 deletions packages/block-editor/src/components/block-lock/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ import { store as blockEditorStore } from '../../store';

export default function BlockLockToolbar( { clientId } ) {
const blockInformation = useBlockDisplayInformation( clientId );
const { canMove, canRemove } = useSelect(
const { canMove, canRemove, canLockBlock } = useSelect(
( select ) => {
const { canMoveBlock, canRemoveBlock } = select( blockEditorStore );
const {
canMoveBlock,
canRemoveBlock,
canLockBlockType,
getBlockName,
} = select( blockEditorStore );

return {
canMove: canMoveBlock( clientId ),
canRemove: canRemoveBlock( clientId ),
canLockBlock: canLockBlockType( getBlockName( clientId ) ),
};
},
[ clientId ]
Expand All @@ -33,6 +39,10 @@ export default function BlockLockToolbar( { clientId } ) {
false
);

if ( ! canLockBlock ) {
return null;
}

if ( canMove && canRemove ) {
return null;
}
Expand All @@ -48,6 +58,7 @@ export default function BlockLockToolbar( { clientId } ) {
blockInformation.title
) }
onClick={ toggleModal }
aria-disabled={ ! canLockBlock }
/>
</ToolbarGroup>
{ isModalOpen && (
Expand Down
17 changes: 17 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,23 @@ export function canMoveBlocks( state, clientIds, rootClientId = null ) {
);
}

/**
* Determines if the given block type can be locked/unlocked by a user.
*
* @param {Object} state Editor state.
* @param {(string|Object)} nameOrType Block name or type object.
*
* @return {boolean} Whether a given block type can be locked/unlocked.
*/
export function canLockBlockType( state, nameOrType ) {
if ( ! hasBlockSupport( nameOrType, '__experimentalLock', true ) ) {
return false;
}

// Use block editor settings as the default value.
return !! state.settings?.__experimentalCanLockBlocks;
}

/**
* Returns information about how recently and frequently a block has been inserted.
*
Expand Down

0 comments on commit 7417b7b

Please sign in to comment.