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

Provide default algorithm for section root in block editor #65516

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
getTemplateLock,
getClientIdsWithDescendants,
isNavigationMode,
getBlocksByName,
getBlockAttributes,
} from './selectors';
import {
checkAllowListRecursive,
Expand Down Expand Up @@ -611,8 +613,22 @@ export function isZoomOutMode( state ) {
*
* @param {Object} state Editor state.
*
* @return {string|undefined} The section root client ID or undefined if not set.
* @return {string} The section root client ID.
*/
export function getSectionRootClientId( state ) {
return state.settings?.[ sectionRootClientIdKey ];
const settingsRootClientId = state.settings?.[ sectionRootClientIdKey ];

// Specifically check that the setting was not provided to avoid
// cases where the provided setting is an empty string to signify
// the "root block" of the editor.
if ( settingsRootClientId !== undefined ) {
return settingsRootClientId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we explicitly provide "" as a settingsRootClientId in the post editor today or do we leave that to the block-editor's algorithm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @wordpress/editor provides the setting, today Site and Post Editors would use the algorithm that looks for main.

However, if the rendering mode is template-locked (like when you are editing a Page in the Site Editor) then the default would be '' (empty string).

I think the question is here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main main question is about "performance". In other words, it seems unnecessary to run the fallback for post and page editors in "post-only" mode (not template-locked), for template-locked, I suspect that we already have something that sets the "post-content" block as section root.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. If we are in Post Editor (Posts or Pages or whatever) we are in post-only mode. Therefore we're going to be looking for a main tag which is very unlikely to exist.

Yes maybe I need to do some more digging to find where these editors end up with '' as the sectionRootClientId.

}

return (
getBlocksByName( state, 'core/group' ).find(
( clientId ) =>
getBlockAttributes( state, clientId )?.tagName === 'main'
) ?? ''
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {
} = select( coreStore );
const { get } = select( preferencesStore );
const { getBlockTypes } = select( blocksStore );
const { getBlocksByName, getBlockAttributes } =
select( blockEditorStore );
const { getBlocksByName } = select( blockEditorStore );
const siteSettings = canUser( 'read', {
kind: 'root',
name: 'site',
Expand All @@ -148,15 +147,11 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) {

function getSectionRootBlock() {
if ( renderingMode === 'template-locked' ) {
return getBlocksByName( 'core/post-content' )?.[ 0 ] ?? '';
return getBlocksByName( 'core/post-content' )?.[ 0 ] ?? ''
}

return (
getBlocksByName( 'core/group' ).find(
( clientId ) =>
getBlockAttributes( clientId )?.tagName === 'main'
) ?? ''
);
// Allow default algorithm to determine the section root block.
return undefined;
}

return {
Expand Down
Loading