Skip to content

Commit

Permalink
Zoom out: don't select last block (#61484)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
  • Loading branch information
ellatrix and ellatrix authored May 9, 2024
1 parent 91b2221 commit e492737
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

Expand Down

0 comments on commit e492737

Please sign in to comment.