Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Hide template part and post content blocks in some site editor contexts #58928

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 0 additions & 43 deletions packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
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 @@ -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';
Expand Down
3 changes: 3 additions & 0 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
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
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 ] );
}
Loading