Skip to content

Commit

Permalink
Editor: Hide template part and post content blocks in some site edito…
Browse files Browse the repository at this point in the history
…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
4 people committed Feb 15, 2024
1 parent fd75a20 commit 0e9f254
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 43 deletions.
43 changes: 0 additions & 43 deletions packages/edit-post/src/index.js
Expand Up @@ -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,
Expand Down Expand Up @@ -95,48 +94,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';
Expand Down
3 changes: 3 additions & 0 deletions packages/editor/src/components/provider/index.js
Expand Up @@ -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 );
Expand Down Expand Up @@ -229,6 +230,8 @@ export const ExperimentalEditorProvider = withRegistryProvider(
setRenderingMode( settings.defaultRenderingMode ?? 'post-only' );
}, [ settings.defaultRenderingMode, setRenderingMode ] );

useHideBlocksFromInserter( post.type );

if ( ! isReady ) {
return null;
}
Expand Down
@@ -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 ] );
}

1 comment on commit 0e9f254

@github-actions
Copy link

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:

Please sign in to comment.