Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,8 @@ function selection( state = {}, action ) {

return { clientId: blockToSelect.clientId };
}
case 'RESET_BLOCKS':
return {};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty basic solution at the moment and it doesn't cover that there might be cases where selection should be retained. Maybe if the client id for the currently selected block exists in the blocks array from the RESET_BLOCKS action?

It looks like RESET_BLOCKS is used for synchronizing templates, so that looks like a case where selection could be unexpectedly lost.

However, with multi-selections, trying to retain a selection might cause a situation where selectionStart is retained but selectionEnd is cleared or vice versa.

}

return state;
Expand Down
13 changes: 13 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,19 @@ describe( 'state', () => {
expect( state1 ).toEqual( original );
expect( state2 ).toEqual( original );
} );

it( 'should remove selection when blocks are reset', () => {
const original = deepFreeze( { clientId: 'chicken' } );
const action = {
type: 'RESET_BLOCKS',
blocks: [],
};
const state1 = selectionStart( original, action );
const state2 = selectionEnd( original, action );

expect( state1 ).toEqual( {} );
expect( state2 ).toEqual( {} );
} );
} );

describe( 'preferences()', () => {
Expand Down