-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query Block: Make nested post blocks uneditable (#32505)
- Loading branch information
1 parent
0b6ca99
commit 2d12d52
Showing
7 changed files
with
162 additions
and
28 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import queryMetaData from '../query/block.json'; | ||
const { name: queryBlockName } = queryMetaData; | ||
|
||
/** | ||
* Hook that determines if a Post block is editable or not. | ||
* The returned value is used to determine if the specific | ||
* Post block will be rendered in `readonly` mode or not. | ||
* | ||
* For now this is checking if a Post block is nested in | ||
* a Query block. If it is, the block should not be editable. | ||
* | ||
* @param {string} clientId The ID of the block to be checked. | ||
* @return {boolean} Whether the block can be edited or not. | ||
*/ | ||
export function useIsEditablePostBlock( clientId ) { | ||
return useSelect( | ||
( select ) => { | ||
const { getBlockParents, getBlockName } = select( | ||
blockEditorStore | ||
); | ||
const blockParents = getBlockParents( clientId ); | ||
const hasQueryParent = blockParents.some( | ||
( parentClientId ) => | ||
getBlockName( parentClientId ) === queryBlockName | ||
); | ||
return ! hasQueryParent; | ||
}, | ||
[ clientId ] | ||
); | ||
} | ||
|
||
export default { useIsEditablePostBlock }; |