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

Multi-selection: fix clearing with side click #19787

Merged
merged 2 commits into from
Jan 28, 2020
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 @@ -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 @@ -196,6 +196,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 @@ -385,7 +386,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 @@ -501,7 +502,7 @@ export default function WritingFlow( { children } ) {

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

Expand All @@ -516,14 +517,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 @@ -532,6 +540,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();
} );
} );