Skip to content

Commit

Permalink
Multi-selection: fix clearing with side click (#19787)
Browse files Browse the repository at this point in the history
* Multi-selection: fix clearing with side click

* Add e2e test
  • Loading branch information
ellatrix committed Jan 28, 2020
1 parent c49b051 commit f6470e6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const FocusCapture = forwardRef( ( {
containerRef,
noCapture,
hasMultiSelection,
multiSelectionContainer,
}, ref ) => {
const isNavigationMode = useSelect( ( select ) =>
select( 'core/block-editor' ).isNavigationMode()
Expand All @@ -54,7 +55,7 @@ const FocusCapture = forwardRef( ( {
// depending on the direction.
if ( ! selectedClientId ) {
if ( hasMultiSelection ) {
containerRef.current.focus();
multiSelectionContainer.current.focus();
return;
}

Expand Down
17 changes: 13 additions & 4 deletions packages/block-editor/src/components/writing-flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export default function WritingFlow( { children } ) {
const container = useRef();
const focusCaptureBeforeRef = useRef();
const focusCaptureAfterRef = useRef();
const multiSelectionContainer = useRef();

const entirelySelected = useRef();

Expand Down Expand Up @@ -386,7 +387,7 @@ export default function WritingFlow( { children } ) {
} else if ( isEscape ) {
setNavigationMode( true );
}
} else if ( hasMultiSelection && isTab && target === container.current ) {
} else if ( hasMultiSelection && isTab && target === multiSelectionContainer.current ) {
// See comment above.
noCapture.current = true;

Expand Down Expand Up @@ -502,7 +503,7 @@ export default function WritingFlow( { children } ) {

useEffect( () => {
if ( hasMultiSelection && ! isMultiSelecting ) {
container.current.focus();
multiSelectionContainer.current.focus();
}
}, [ hasMultiSelection, isMultiSelecting ] );

Expand All @@ -521,14 +522,21 @@ export default function WritingFlow( { children } ) {
containerRef={ container }
noCapture={ noCapture }
hasMultiSelection={ hasMultiSelection }
multiSelectionContainer={ multiSelectionContainer }
/>
<div
ref={ container }
onKeyDown={ onKeyDown }
onMouseDown={ onMouseDown }
tabIndex={ hasMultiSelection ? '0' : undefined }
aria-label={ hasMultiSelection ? __( 'Multiple selected blocks' ) : undefined }
>
<div
ref={ multiSelectionContainer }
tabIndex={ hasMultiSelection ? '0' : undefined }
aria-label={ hasMultiSelection ? __( 'Multiple selected blocks' ) : undefined }
// Needs to be positioned within the viewport, so focus to this
// element does not scroll the page.
style={ { position: 'fixed' } }
/>
{ children }
</div>
<FocusCapture
Expand All @@ -537,6 +545,7 @@ export default function WritingFlow( { children } ) {
containerRef={ container }
noCapture={ noCapture }
hasMultiSelection={ hasMultiSelection }
multiSelectionContainer={ multiSelectionContainer }
isReverse
/>
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ exports[`Multi-block selection should allow selecting outer edge if there is no

exports[`Multi-block selection should always expand single line selection 1`] = `""`;

exports[`Multi-block selection should clear selection when clicking next to blocks 1`] = `
"<!-- wp:paragraph -->
<p>1</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>2</p>
<!-- /wp:paragraph -->"
`;

exports[`Multi-block selection should copy and paste 1`] = `
"<!-- wp:paragraph -->
<p>1</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,31 @@ describe( 'Multi-block selection', () => {

expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should clear selection when clicking next to blocks', async () => {
await clickBlockAppender();
await page.keyboard.type( '1' );
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '2' );
await pressKeyWithModifier( 'shift', 'ArrowUp' );

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

const coord = await page.evaluate( () => {
const element = document.querySelector( '.wp-block-paragraph' );
const rect = element.getBoundingClientRect();
return {
x: rect.x - 1,
y: rect.y + ( rect.height / 2 ),
};
} );

await page.mouse.click( coord.x, coord.y );

await testNativeSelection();
expect( await getSelectedFlatIndices() ).toEqual( [] );

expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );

0 comments on commit f6470e6

Please sign in to comment.