From ec052bbf390f11019433170ed8362042ea0d93d6 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 12 Feb 2024 12:02:06 +0100 Subject: [PATCH 1/2] Editor: Hide template part and post content blocks in some site editor contexts --- packages/edit-post/src/index.js | 43 ---------- .../editor/src/components/provider/index.js | 3 + .../provider/use-hide-bocks-from-inserter.js | 81 +++++++++++++++++++ 3 files changed, 84 insertions(+), 43 deletions(-) create mode 100644 packages/editor/src/components/provider/use-hide-bocks-from-inserter.js diff --git a/packages/edit-post/src/index.js b/packages/edit-post/src/index.js index 08bc7c5aa7002c..e73e6c54f46b2a 100644 --- a/packages/edit-post/src/index.js +++ b/packages/edit-post/src/index.js @@ -9,7 +9,6 @@ import { import deprecated from '@wordpress/deprecated'; import { createRoot } from '@wordpress/element'; import { dispatch, select } from '@wordpress/data'; -import { addFilter } from '@wordpress/hooks'; import { store as preferencesStore } from '@wordpress/preferences'; import { registerLegacyWidgetBlock, @@ -93,48 +92,6 @@ export function initializeEditor( } ); } - /* - * Prevent adding template part in the post editor. - * Only add the filter when the post editor is initialized, not imported. - * Also only add the filter(s) after registerCoreBlocks() - * so that common filters in the block library are not overwritten. - */ - addFilter( - 'blockEditor.__unstableCanInsertBlockType', - 'removeTemplatePartsFromInserter', - ( canInsert, blockType ) => { - if ( blockType.name === 'core/template-part' ) { - return false; - } - return canInsert; - } - ); - - /* - * Prevent adding post content block (except in query block) in the post editor. - * Only add the filter when the post editor is initialized, not imported. - * Also only add the filter(s) after registerCoreBlocks() - * so that common filters in the block library are not overwritten. - */ - addFilter( - 'blockEditor.__unstableCanInsertBlockType', - 'removePostContentFromInserter', - ( - canInsert, - blockType, - rootClientId, - { getBlockParentsByBlockName } - ) => { - if ( blockType.name === 'core/post-content' ) { - return ( - getBlockParentsByBlockName( rootClientId, 'core/query' ) - .length > 0 - ); - } - return canInsert; - } - ); - // Show a console log warning if the browser is not in Standards rendering mode. const documentMode = document.compatMode === 'CSS1Compat' ? 'Standards' : 'Quirks'; diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index e1db657cd7251b..9f0d25e1576353 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -23,6 +23,7 @@ import useBlockEditorSettings from './use-block-editor-settings'; import { unlock } from '../../lock-unlock'; import DisableNonPageContentBlocks from './disable-non-page-content-blocks'; import NavigationBlockEditingMode from './navigation-block-editing-mode'; +import { useHideBlocksFromInserter } from './use-hide-bocks-from-inserter'; const { ExperimentalBlockEditorProvider } = unlock( blockEditorPrivateApis ); const { PatternsMenuItems } = unlock( editPatternsPrivateApis ); @@ -229,6 +230,8 @@ export const ExperimentalEditorProvider = withRegistryProvider( setRenderingMode( settings.defaultRenderingMode ?? 'post-only' ); }, [ settings.defaultRenderingMode, setRenderingMode ] ); + useHideBlocksFromInserter( post.type ); + if ( ! isReady ) { return null; } diff --git a/packages/editor/src/components/provider/use-hide-bocks-from-inserter.js b/packages/editor/src/components/provider/use-hide-bocks-from-inserter.js new file mode 100644 index 00000000000000..ce9feb9d0a51df --- /dev/null +++ b/packages/editor/src/components/provider/use-hide-bocks-from-inserter.js @@ -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 post 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 post 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 ] ); +} From 9ad5632422fbf7c7b8f0335dd625612e3dbd4284 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 15 Feb 2024 10:27:59 +0100 Subject: [PATCH 2/2] Fix comments --- .../src/components/provider/use-hide-bocks-from-inserter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/editor/src/components/provider/use-hide-bocks-from-inserter.js b/packages/editor/src/components/provider/use-hide-bocks-from-inserter.js index ce9feb9d0a51df..c3f9158602f9b0 100644 --- a/packages/editor/src/components/provider/use-hide-bocks-from-inserter.js +++ b/packages/editor/src/components/provider/use-hide-bocks-from-inserter.js @@ -22,7 +22,7 @@ const POST_TYPES_ALLOWING_POST_CONTENT_TEMPLATE_PART = [ export function useHideBlocksFromInserter( postType ) { useEffect( () => { /* - * Prevent adding template part in the post editor. + * Prevent adding template part in the editor. */ addFilter( 'blockEditor.__unstableCanInsertBlockType', @@ -41,7 +41,7 @@ export function useHideBlocksFromInserter( postType ) { ); /* - * Prevent adding post content block (except in query block) in the post editor. + * Prevent adding post content block (except in query block) in the editor. */ addFilter( 'blockEditor.__unstableCanInsertBlockType',