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

Blocks: Add supports lock flag #39568

Merged
merged 8 commits into from
Mar 28, 2022
Merged
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
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
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 @@ -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