-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Editor: Introduce the 'useBlockLock' hook (#40210)
* Block Editor: Introduce the 'useBlockLock' hook * Use the new hook * Use 'canLock'
- Loading branch information
Showing
6 changed files
with
55 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as BlockLockMenuItem } from './menu-item'; | ||
export { default as BlockLockModal } from './modal'; | ||
export { default as BlockLockToolbar } from './toolbar'; | ||
export { default as useBlockLock } from './use-block-lock'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/block-editor/src/components/block-lock/use-block-lock.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
/** | ||
* Return details about the block lock status. | ||
* | ||
* @param {string} clientId The block client Id. | ||
* @param {boolean} checkRoot Optional. Use root client ID when checking lock status. | ||
* | ||
* @return {Object} Block lock status | ||
*/ | ||
export default function useBlockLock( clientId, checkRoot = false ) { | ||
return useSelect( | ||
( select ) => { | ||
const { | ||
canMoveBlock, | ||
canRemoveBlock, | ||
canLockBlockType, | ||
getBlockName, | ||
getBlockRootClientId, | ||
} = select( blockEditorStore ); | ||
const rootClientId = checkRoot | ||
? getBlockRootClientId( clientId ) | ||
: null; | ||
|
||
return { | ||
canMove: canMoveBlock( clientId, rootClientId ), | ||
canRemove: canRemoveBlock( clientId, rootClientId ), | ||
canLock: canLockBlockType( getBlockName( clientId ) ), | ||
}; | ||
}, | ||
[ clientId, checkRoot ] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters