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

Fix block multi selection in nested blocks #32536

Merged
merged 3 commits into from
Jun 10, 2021
Merged
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
16 changes: 16 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,22 @@ export function stopMultiSelect() {
* @param {string} end Last block of the multiselection.
*/
export function* multiSelect( start, end ) {
const startBlockRootClientId = yield controls.select(
blockEditorStoreName,
'getBlockRootClientId',
start
);
const endBlockRootClientId = yield controls.select(
blockEditorStoreName,
'getBlockRootClientId',
end
);

// Only allow block multi-selections at the same level.
if ( startBlockRootClientId !== endBlockRootClientId ) {
return;
}

yield {
type: 'MULTI_SELECT',
start,
Expand Down
50 changes: 47 additions & 3 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,60 @@ describe( 'actions', () => {
} );
} );
describe( 'multiSelect', () => {
it( 'should return MULTI_SELECT action', () => {
it( 'should return MULTI_SELECT action if blocks have the same root client id', () => {
const start = 'start';
const end = 'end';
const fulfillment = multiSelect( start, end );
expect( fulfillment.next().value ).toEqual( {
const multiSelectGenerator = multiSelect( start, end );

expect( multiSelectGenerator.next().value ).toEqual(
controls.select(
blockEditorStoreName,
'getBlockRootClientId',
start
)
);

expect( multiSelectGenerator.next( 'parent' ).value ).toEqual(
controls.select(
blockEditorStoreName,
'getBlockRootClientId',
end
)
);

expect( multiSelectGenerator.next( 'parent' ).value ).toEqual( {
type: 'MULTI_SELECT',
start,
end,
} );
} );

it( 'should return undefined if blocks have different root client ids', () => {
const start = 'start';
const end = 'end';
const multiSelectGenerator = multiSelect( start, end );

expect( multiSelectGenerator.next().value ).toEqual(
controls.select(
blockEditorStoreName,
'getBlockRootClientId',
start
)
);

expect( multiSelectGenerator.next( 'parent' ).value ).toEqual(
controls.select(
blockEditorStoreName,
'getBlockRootClientId',
end
)
);

expect( multiSelectGenerator.next( 'another parent' ) ).toEqual( {
done: true,
value: undefined,
} );
} );
} );

describe( 'clearSelectedBlock', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getEditedPostContent,
clickBlockToolbarButton,
clickButton,
clickMenuItem,
} from '@wordpress/e2e-test-utils';

async function getSelectedFlatIndices() {
Expand Down Expand Up @@ -272,6 +273,19 @@ describe( 'Multi-block selection', () => {

await testNativeSelection();
expect( await getSelectedFlatIndices() ).toEqual( [ 1, 2 ] );

// Group the blocks and test that multiselection also works for nested
// blocks. Checks for regressions of
// https://github.com/WordPress/gutenberg/issues/32056

await clickBlockToolbarButton( 'Options' );
await clickMenuItem( 'Group' );
await page.click( '[data-type="core/paragraph"]' );
await page.keyboard.down( 'Shift' );
await page.click( '[data-type="core/paragraph"]:nth-child(2)' );
await page.keyboard.up( 'Shift' );
await testNativeSelection();
expect( await getSelectedFlatIndices() ).toEqual( [ 2, 3 ] );
} );

it( 'should select by dragging', async () => {
Expand Down