diff --git a/packages/block-editor/src/components/block-inspector/index.js b/packages/block-editor/src/components/block-inspector/index.js index 1986ad8730c121..24c0389bdeaa42 100644 --- a/packages/block-editor/src/components/block-inspector/index.js +++ b/packages/block-editor/src/components/block-inspector/index.js @@ -100,6 +100,8 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => { getBlockName, getContentLockingParent, getTemplateLock, + getClosestSectionBlock, + getBlockEditingMode, } = unlock( select( blockEditorStore ) ); const _selectedBlockClientId = getSelectedBlockClientId(); const _selectedBlockName = @@ -107,6 +109,15 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => { const _blockType = _selectedBlockName && getBlockType( _selectedBlockName ); + const closestSectionBlock = getClosestSectionBlock( + _selectedBlockClientId + ); + + const closestContentOnlySectionBlock = + getBlockEditingMode( closestSectionBlock ) === 'contentOnly' + ? closestSectionBlock + : undefined; + return { count: getSelectedBlockCount(), selectedBlockClientId: _selectedBlockClientId, @@ -117,7 +128,8 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => { ( getTemplateLock( _selectedBlockClientId ) === 'contentOnly' || _selectedBlockName === 'core/block' ? _selectedBlockClientId - : undefined ), + : undefined ) || + closestContentOnlySectionBlock, }; }, [] ); diff --git a/packages/block-editor/src/store/private-selectors.js b/packages/block-editor/src/store/private-selectors.js index 7e997217675d3b..f69b10cc0541a4 100644 --- a/packages/block-editor/src/store/private-selectors.js +++ b/packages/block-editor/src/store/private-selectors.js @@ -567,3 +567,24 @@ export function getSectionClientIds( state ) { const sectionRootClientId = getSectionRootClientId( state ); return getBlockOrder( state, sectionRootClientId ); } + +/** + * Retrieves the closest "section" block to the given client ID. + * + * @param {Object} state - The current state. + * @param {string} clientId - The client ID to start the search from. + * @return {string|undefined} The client ID of the closest section block, or undefined if not found. + */ +export function getClosestSectionBlock( state, clientId ) { + let current = clientId; + let result; + const sectionClientIds = getSectionClientIds( state ); + while ( current ) { + if ( sectionClientIds.includes( current ) ) { + result = current; + break; + } + current = state.blocks.parents.get( current ); + } + return result; +}