diff --git a/packages/block-editor/src/store/private-selectors.js b/packages/block-editor/src/store/private-selectors.js index c46778d889b3e0..b02f7e67bd6134 100644 --- a/packages/block-editor/src/store/private-selectors.js +++ b/packages/block-editor/src/store/private-selectors.js @@ -17,6 +17,8 @@ import { getClientIdsWithDescendants, isNavigationMode, getBlockRootClientId, + getBlocksByName, + getBlockAttributes, } from './selectors'; import { checkAllowListRecursive, @@ -563,10 +565,24 @@ export const getBlockStyles = createSelector( * * @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; + } + + return ( + getBlocksByName( state, 'core/group' ).find( + ( clientId ) => + getBlockAttributes( state, clientId )?.tagName === 'main' + ) ?? '' + ); } /** diff --git a/packages/editor/src/components/provider/use-block-editor-settings.js b/packages/editor/src/components/provider/use-block-editor-settings.js index f5c45f431e2c85..b9b09d01ccaf4c 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -133,8 +133,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', @@ -144,15 +143,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 {