diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index 61acae16fcca2d..d6eaac9c7e8c9b 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -1612,42 +1612,31 @@ export const __unstableSetEditorMode = // When switching to zoom-out mode, we need to select the parent section if ( mode === 'zoom-out' ) { const firstSelectedClientId = select.getBlockSelectionStart(); - const allBlocks = select.getBlocks(); - const { sectionRootClientId } = unlock( registry.select( STORE_NAME ).getSettings() ); - if ( sectionRootClientId ) { - const sectionClientIds = - select.getBlockOrder( sectionRootClientId ); - const lastSectionClientId = - sectionClientIds[ sectionClientIds.length - 1 ]; - if ( sectionClientIds ) { - if ( firstSelectedClientId ) { - const parents = select.getBlockParents( - firstSelectedClientId - ); - const firstSectionClientId = parents.find( ( parent ) => + if ( firstSelectedClientId ) { + let sectionClientId; + + if ( sectionRootClientId ) { + const sectionClientIds = + select.getBlockOrder( sectionRootClientId ); + sectionClientId = select + .getBlockParents( firstSelectedClientId ) + .find( ( parent ) => sectionClientIds.includes( parent ) ); - if ( firstSectionClientId ) { - dispatch.selectBlock( firstSectionClientId ); - } else { - dispatch.selectBlock( lastSectionClientId ); - } - } else { - dispatch.selectBlock( lastSectionClientId ); - } + } else { + sectionClientId = select.getBlockHierarchyRootClientId( + firstSelectedClientId + ); + } + + if ( sectionClientId ) { + dispatch.selectBlock( sectionClientId ); + } else { + dispatch.clearSelectedBlock(); } - } else if ( firstSelectedClientId ) { - const rootClientId = select.getBlockHierarchyRootClientId( - firstSelectedClientId - ); - dispatch.selectBlock( rootClientId ); - } else { - // If there's no block selected and no sectionRootClientId, select the last root block. - const lastRootBlock = allBlocks[ allBlocks.length - 1 ]; - dispatch.selectBlock( lastRootBlock?.clientId ); } }