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

Input Interaction: always expand single line selection vertically #14487

Merged
merged 2 commits into from
Mar 22, 2019
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
29 changes: 15 additions & 14 deletions packages/dom/src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,6 @@ export function isVerticalEdge( container, isReverse ) {
}

const selection = window.getSelection();

// Only consider the selection at the edge if the direction is towards the
// edge.
if (
! selection.isCollapsed &&
isSelectionForward( selection ) === isReverse
) {
return false;
}

const range = selection.rangeCount ? selection.getRangeAt( 0 ) : null;

if ( ! range ) {
Expand All @@ -191,14 +181,25 @@ export function isVerticalEdge( container, isReverse ) {
return false;
}

const editableRect = container.getBoundingClientRect();

// Calculate a buffer that is half the line height. In some browsers, the
// selection rectangle may not fill the entire height of the line, so we add
// half the line height to the selection rectangle to ensure that it is well
// over its line boundary.
const { lineHeight } = window.getComputedStyle( container );
const buffer = parseInt( lineHeight, 10 ) / 2;
const computedStyle = window.getComputedStyle( container );
const lineHeight = parseInt( computedStyle.lineHeight, 10 );

// Only consider the multiline selection at the edge if the direction is
// towards the edge.
if (
! selection.isCollapsed &&
rangeRect.height > lineHeight &&
isSelectionForward( selection ) === isReverse
) {
return false;
}

const editableRect = container.getBoundingClientRect();
const buffer = lineHeight / 2;

// Too low.
if ( isReverse && rangeRect.top - buffer > editableRect.top ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ exports[`Multi-block selection should allow selecting outer edge if there is no
<!-- /wp:paragraph -->"
`;

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

exports[`Multi-block selection should only trigger multi-selection when at the end 1`] = `
"<!-- wp:paragraph -->
<p>1.</p>
Expand Down
13 changes: 13 additions & 0 deletions packages/e2e-tests/specs/multi-block-selection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ describe( 'Multi-block selection', () => {
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should always expand single line selection', async () => {
await clickBlockAppender();
await page.keyboard.press( 'Enter' );
await page.keyboard.type( '12' );
await page.keyboard.press( 'ArrowLeft' );
await pressKeyWithModifier( 'shift', 'ArrowRight' );
await pressKeyWithModifier( 'shift', 'ArrowUp' );
// This delete all blocks.
await page.keyboard.press( 'Backspace' );

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

it( 'should allow selecting outer edge if there is no sibling block', async () => {
await clickBlockAppender();
await page.keyboard.type( '1' );
Expand Down