Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Hide template part and post content blocks in some site edito…
…r contexts (#58928) Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: aaronrobertshaw <aaronrobertshaw@git.wordpress.org> Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
- Loading branch information
1 parent
fd75a20
commit 0e9f254
Showing
3 changed files
with
84 additions
and
43 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
81 changes: 81 additions & 0 deletions
81
packages/editor/src/components/provider/use-hide-bocks-from-inserter.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,81 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { addFilter, removeFilter } from '@wordpress/hooks'; | ||
|
||
// These post types are "structural" block lists. | ||
// We should be allowed to use | ||
// the post content and template parts blocks within them. | ||
const POST_TYPES_ALLOWING_POST_CONTENT_TEMPLATE_PART = [ | ||
'wp_block', | ||
'wp_template', | ||
'wp_template_part', | ||
]; | ||
|
||
/** | ||
* In some specific contexts, | ||
* the template part and post content blocks need to be hidden. | ||
* | ||
* @param {string} postType Post Type | ||
*/ | ||
export function useHideBlocksFromInserter( postType ) { | ||
useEffect( () => { | ||
/* | ||
* Prevent adding template part in the editor. | ||
*/ | ||
addFilter( | ||
'blockEditor.__unstableCanInsertBlockType', | ||
'removeTemplatePartsFromInserter', | ||
( canInsert, blockType ) => { | ||
if ( | ||
! POST_TYPES_ALLOWING_POST_CONTENT_TEMPLATE_PART.includes( | ||
postType | ||
) && | ||
blockType.name === 'core/template-part' | ||
) { | ||
return false; | ||
} | ||
return canInsert; | ||
} | ||
); | ||
|
||
/* | ||
* Prevent adding post content block (except in query block) in the editor. | ||
*/ | ||
addFilter( | ||
'blockEditor.__unstableCanInsertBlockType', | ||
'removePostContentFromInserter', | ||
( | ||
canInsert, | ||
blockType, | ||
rootClientId, | ||
{ getBlockParentsByBlockName } | ||
) => { | ||
if ( | ||
! POST_TYPES_ALLOWING_POST_CONTENT_TEMPLATE_PART.includes( | ||
postType | ||
) && | ||
blockType.name === 'core/post-content' | ||
) { | ||
return ( | ||
getBlockParentsByBlockName( rootClientId, 'core/query' ) | ||
.length > 0 | ||
); | ||
} | ||
return canInsert; | ||
} | ||
); | ||
|
||
return () => { | ||
removeFilter( | ||
'blockEditor.__unstableCanInsertBlockType', | ||
'removeTemplatePartsFromInserter' | ||
); | ||
removeFilter( | ||
'blockEditor.__unstableCanInsertBlockType', | ||
'removePostContentFromInserter' | ||
); | ||
}; | ||
}, [ postType ] ); | ||
} |
0e9f254
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in 0e9f254.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7914967912
📝 Reported issues:
/test/e2e/specs/editor/blocks/navigation-frontend-interactivity.spec.js