From 2bd411e4ad1cee6695d7058a43f24da655ff07f7 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Tue, 19 Nov 2024 11:45:07 +0000 Subject: [PATCH 1/2] Try more forgiving algorithm --- .../provider/use-block-editor-settings.js | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 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 f5c45f431e2c8..ce024c3d92c41 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -133,7 +133,7 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) { } = select( coreStore ); const { get } = select( preferencesStore ); const { getBlockTypes } = select( blocksStore ); - const { getBlocksByName, getBlockAttributes } = + const { getBlocks, getBlocksByName, getBlockAttributes } = select( blockEditorStore ); const siteSettings = canUser( 'read', { kind: 'root', @@ -143,16 +143,56 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) { : undefined; function getSectionRootBlock() { + // 1. Post Content block. if ( renderingMode === 'template-locked' ) { return getBlocksByName( 'core/post-content' )?.[ 0 ] ?? ''; } - return ( + // 2. Group with `
`. + const groupBlockWithMainTagName = getBlocksByName( 'core/group' ).find( ( clientId ) => getBlockAttributes( clientId )?.tagName === 'main' - ) ?? '' + ) ?? ''; + + if ( groupBlockWithMainTagName ) { + return groupBlockWithMainTagName; + } + + const rootBlocks = getBlocks(); + + // 3. Group block inbetween a Header and Footer template part. + const groupBlockIndex = rootBlocks?.findIndex( + ( block ) => block.name === 'core/group' + ); + + const previousBlock = rootBlocks[ groupBlockIndex - 1 ]; + const nextBlock = rootBlocks[ groupBlockIndex + 1 ]; + + if ( + groupBlockIndex !== -1 && + previousBlock.name === 'core/template-part' && + previousBlock.attributes.slug === 'header' && + nextBlock.name === 'core/template-part' && + nextBlock.attributes.slug === 'footer' + ) { + return rootBlocks[ groupBlockIndex ].clientId; + } + + // 4. Group or Cover block at the root. + const groupOrCoverBlockIndex = rootBlocks.findIndex( + ( block ) => + block.name === 'core/group' || + block.name === 'core/cover' ); + + if ( groupOrCoverBlockIndex !== -1 ) { + return rootBlocks[ groupOrCoverBlockIndex ].clientId; + } + + // Default to empty to indicate the root of the block editor + // should be treated as the section root. + return ''; } return { From be3b918c8c63b41882add831dabbb9750982d816 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Tue, 19 Nov 2024 11:53:51 +0000 Subject: [PATCH 2/2] =?UTF-8?q?Don=E2=80=99t=20allow=20Cover=20to=20act=20?= =?UTF-8?q?as=20a=20section=20root?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/provider/use-block-editor-settings.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 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 ce024c3d92c41..aa218851c3cf8 100644 --- a/packages/editor/src/components/provider/use-block-editor-settings.js +++ b/packages/editor/src/components/provider/use-block-editor-settings.js @@ -179,15 +179,13 @@ function useBlockEditorSettings( settings, postType, postId, renderingMode ) { return rootBlocks[ groupBlockIndex ].clientId; } - // 4. Group or Cover block at the root. - const groupOrCoverBlockIndex = rootBlocks.findIndex( - ( block ) => - block.name === 'core/group' || - block.name === 'core/cover' + // 4. Group block at the root. + const groupBlock = rootBlocks.find( + ( block ) => block.name === 'core/group' ); - if ( groupOrCoverBlockIndex !== -1 ) { - return rootBlocks[ groupOrCoverBlockIndex ].clientId; + if ( groupBlock ) { + return groupBlock.clientId; } // Default to empty to indicate the root of the block editor