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
  • Loading branch information
Mamaduka authored Mar 28, 2022
1 parent dc2592e commit 3cec236
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 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: 5 additions & 4 deletions packages/block-editor/src/components/block-lock/menu-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ import BlockLockModal from './modal';
import { store as blockEditorStore } from '../../store';

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

return {
canLockBlocks: getSettings().__experimentalCanLockBlocks,
canLockBlock: canLockBlockType( getBlockName( clientId ) ),
isLocked:
! canMoveBlock( clientId, rootClientId ) ||
! canRemoveBlock( clientId, rootClientId ),
Expand All @@ -39,7 +40,7 @@ export default function BlockLockMenuItem( { clientId } ) {
false
);

if ( ! canLockBlocks ) {
if ( ! canLockBlock ) {
return null;
}

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

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

return {
canMove: canMoveBlock( clientId ),
canRemove: canRemoveBlock( clientId ),
canLockBlocks: getSettings().__experimentalCanLockBlocks,
canLockBlock: canLockBlockType( getBlockName( clientId ) ),
};
},
[ clientId ]
Expand All @@ -36,7 +39,7 @@ export default function BlockLockToolbar( { clientId } ) {
false
);

if ( ! canLockBlocks ) {
if ( ! canLockBlock ) {
return null;
}

Expand All @@ -55,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 @@ -1539,6 +1539,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 3cec236

Please sign in to comment.