Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zoom out: don't select last block #61484

Merged
merged 1 commit into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading