Skip to content

Commit

Permalink
Writing Flow: fix empty horizontal arrow nav (#16846)
Browse files Browse the repository at this point in the history
* Writing Flow: fix horizontal arrow nav

* Add e2e test

* Add inline comment
  • Loading branch information
ellatrix authored Aug 9, 2019
1 parent 4af9221 commit c7faf14
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/dom/src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ function isEdge( container, isReverse, onlyVertical ) {
const side = isReverseDir ? 'left' : 'right';
const testRect = getRectangleFromRange( testRange );

return Math.round( testRect[ side ] ) === Math.round( rangeRect[ side ] );
// Allow the position to be 1px off.
return Math.abs( testRect[ side ] - rangeRect[ side ] ) <= 1;
}

/**
Expand Down Expand Up @@ -352,11 +353,17 @@ function caretRangeFromPoint( doc, x, y ) {
* @return {?Range} The best range for the given point.
*/
function hiddenCaretRangeFromPoint( doc, x, y, container ) {
const originalZIndex = container.style.zIndex;
const originalPosition = container.style.position;

// A z-index only works if the element position is not static.
container.style.zIndex = '10000';
container.style.position = 'relative';

const range = caretRangeFromPoint( doc, x, y );

container.style.zIndex = null;
container.style.zIndex = originalZIndex;
container.style.position = originalPosition;

return range;
}
Expand Down
14 changes: 14 additions & 0 deletions packages/e2e-tests/specs/__snapshots__/writing-flow.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,20 @@ exports[`Writing Flow should navigate empty paragraph 1`] = `
<!-- /wp:paragraph -->"
`;
exports[`Writing Flow should navigate empty paragraphs 1`] = `
"<!-- wp:paragraph -->
<p>1</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>3</p>
<!-- /wp:paragraph -->"
`;
exports[`Writing Flow should not create extra line breaks in multiline value 1`] = `
"<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
Expand Down
14 changes: 14 additions & 0 deletions packages/e2e-tests/specs/writing-flow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,18 @@ describe( 'Writing Flow', () => {

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

it( 'should navigate empty paragraphs', async () => {
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.type( '1' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.type( '3' );

expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );
4 changes: 4 additions & 0 deletions packages/rich-text/src/to-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ export function toTree( {
type: 'span',
attributes: {
'data-rich-text-placeholder': placeholder,
// Necessary to prevent the placeholder from catching
// selection. The placeholder is also not editable after
// all.
contenteditable: 'false',
},
} );
}
Expand Down

0 comments on commit c7faf14

Please sign in to comment.