From 6db8b736d0820bb7e082afa3b35633213bf90252 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 20 Sep 2024 11:13:47 +0100 Subject: [PATCH 1/3] Implement default algorithm --- .../src/store/private-selectors.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/store/private-selectors.js b/packages/block-editor/src/store/private-selectors.js index 0e77e8e2ed433d..7c1a837d1667c9 100644 --- a/packages/block-editor/src/store/private-selectors.js +++ b/packages/block-editor/src/store/private-selectors.js @@ -16,6 +16,8 @@ import { getTemplateLock, getClientIdsWithDescendants, isNavigationMode, + getBlocksByName, + getBlockAttributes, } from './selectors'; import { checkAllowListRecursive, @@ -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; + } + + return ( + getBlocksByName( state, 'core/group' ).find( + ( clientId ) => + getBlockAttributes( state, clientId )?.tagName === 'main' + ) ?? '' + ); } From 11fc7d5e7c44db0484ae496867a878f00394710f Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 20 Sep 2024 11:14:31 +0100 Subject: [PATCH 2/3] Update setting to return undefined --- .../provider/use-block-editor-settings.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) 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 12a67e44262b21..bbdd6a4a996b32 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -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', @@ -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 { From e06a1c000ca0669b54bf927850f16ef347afdce8 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 20 Sep 2024 11:54:57 +0100 Subject: [PATCH 3/3] Return original default when it template-locked --- .../editor/src/components/provider/use-block-editor-settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 bbdd6a4a996b32..da5558634e7a72 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -147,7 +147,7 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) { function getSectionRootBlock() { if ( renderingMode === 'template-locked' ) { - return getBlocksByName( 'core/post-content' )?.[ 0 ]; + return getBlocksByName( 'core/post-content' )?.[ 0 ] ?? '' } // Allow default algorithm to determine the section root block.