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

[Block Editor]: Fix selection by holding shift key for nested blocks #35668

Merged
merged 2 commits into from
Oct 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,29 @@ export function useMultiSelection( clientId ) {

if ( event.shiftKey ) {
const blockSelectionStart = getBlockSelectionStart();
// Handle the case where we select a single block by
// holding the `shiftKey` and don't mark this action
// as multiselection.
// By checking `blockSelectionStart` to be set, we handle the
// case where we select a single block. We also have to check
// the selectionEnd (clientId) not to be included in the
// `blockSelectionStart`'s parents because the click event is
// propagated.
const startParents = getBlockParents( blockSelectionStart );
if (
blockSelectionStart &&
blockSelectionStart !== clientId
blockSelectionStart !== clientId &&
! startParents?.includes( clientId )
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ellatrix I'm not sure if there is a more elegant way to handle this or in another place..

Copy link
Member

Choose a reason for hiding this comment

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

This is for when we shift+click in a parent of the selected block, right? What should the outcome be for that case? Do we need a test here too?

Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIU, it's for this case:

  1. Pressing shift + click within a rich-text block nested inside a nested block like Group

Currently, when you do that, you can't really edit the block anymore, see #33665. So this line is supposed to fix that behavior. In practice, for what I see, it highlights the text between the cursor location, and the location that's being shift-clicked.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a test here too?

I think it's covered by this e2e test, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's covered by this e2e test, isn't it?

Yes

) {
toggleRichText( node, false );
multiSelect( blockSelectionStart, clientId );
const startPath = [
...startParents,
blockSelectionStart,
];
const endPath = [
...getBlockParents( clientId ),
clientId,
];
const depth =
Math.min( startPath.length, endPath.length ) - 1;
multiSelect( startPath[ depth ], endPath[ depth ] );
Comment on lines +180 to +190
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like this is pretty much the same logic as

const startPath = [
...getBlockParents( clientId ),
clientId,
];
const endPath = [
...getBlockParents( endClientId ),
endClientId,
];
const depth =
Math.min( startPath.length, endPath.length ) - 1;
multiSelect( startPath[ depth ], endPath[ depth ] );

Should we extract it into a small helper method, or share it otherwise?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about it but in the second case we use the parents for another check before, so a shared util for our 2 cases would end up passing the start/end paths and just having the Math.min in there, so I'm not really sure it's worth it..

I'm going to merge and happy to follow up on this if it needs to 😄

event.preventDefault();
}
} else if ( hasMultiSelection() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
clickButton,
clickMenuItem,
saveDraft,
transformBlockTo,
} from '@wordpress/e2e-test-utils';

async function getSelectedFlatIndices() {
Expand Down Expand Up @@ -311,6 +312,58 @@ describe( 'Multi-block selection', () => {
` );
} );

it( 'should properly select multiple blocks if selected nested blocks belong to different parent', async () => {
await clickBlockAppender();
await page.keyboard.type( 'first' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'group' );
// Multiselect via keyboard.
await page.keyboard.down( 'Shift' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.up( 'Shift' );
await transformBlockTo( 'Group' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'second' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( 'group' );
await page.keyboard.down( 'Shift' );
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.up( 'Shift' );
await transformBlockTo( 'Group' );
await page.keyboard.press( 'ArrowDown' );

// Click the first paragraph in the first Group block while pressing `shift` key.
const firstParagraph = await page.waitForXPath( "//p[text()='first']" );
await page.keyboard.down( 'Shift' );
await firstParagraph.click();
await page.keyboard.up( 'Shift' );

await page.waitForSelector( '.is-multi-selected' );
const selectedBlocks = await page.$$( '.is-multi-selected' );
expect( selectedBlocks ).toHaveLength( 2 );
} );
it( 'should properly select part of nested rich text block while holding shift', async () => {
await clickBlockAppender();
await page.keyboard.type( 'rich text in group' );
await transformBlockTo( 'Group' );
await page.keyboard.press( 'ArrowDown' );

await page.keyboard.down( 'Shift' );
const paragraph = await page.$( '[data-type="core/paragraph"]' );
const { x, y } = await paragraph.boundingBox();
await page.mouse.move( x + 20, y );
await page.mouse.down();
await page.keyboard.up( 'Shift' );
await page.keyboard.type( 'hi' );
expect( await getEditedPostContent() ).toMatchInlineSnapshot( `
"<!-- wp:group -->
<div class=\\"wp-block-group\\"><!-- wp:paragraph -->
<p>hih text in group</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->"
` );
} );

it( 'should select by dragging', async () => {
await clickBlockAppender();
await page.keyboard.type( '1' );
Expand Down