Skip to content

Commit

Permalink
RichText: don't set selection during selectionchange (#13896)
Browse files Browse the repository at this point in the history
* RichText: don't set selection during selectionchange

* Add e2e test
  • Loading branch information
ellatrix committed Feb 15, 2019
1 parent b58fed3 commit 2ce6115
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/e2e-tests/specs/__snapshots__/rich-text.test.js.snap
Expand Up @@ -30,6 +30,12 @@ exports[`RichText should not format text after code backtick 1`] = `
<!-- /wp:paragraph -->"
`;

exports[`RichText should not lose selection direction 1`] = `
"<!-- wp:paragraph -->
<p><strong>1</strong>2-3</p>
<!-- /wp:paragraph -->"
`;

exports[`RichText should only mutate text data on input 1`] = `
"<!-- wp:paragraph -->
<p>1<strong>2</strong>34</p>
Expand Down
21 changes: 21 additions & 0 deletions packages/e2e-tests/specs/rich-text.test.js
Expand Up @@ -159,4 +159,25 @@ describe( 'RichText', () => {

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

it( 'should not lose selection direction', async () => {
await clickBlockAppender();
await pressKeyWithModifier( 'primary', 'b' );
await page.keyboard.type( '1' );
await pressKeyWithModifier( 'primary', 'b' );
await page.keyboard.type( '23' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.down( 'Shift' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowLeft' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.up( 'Shift' );

// There should be no selection. The following should insert "-" without
// deleting the numbers.
await page.keyboard.type( '-' );

expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );
5 changes: 3 additions & 2 deletions packages/editor/src/components/rich-text/index.js
Expand Up @@ -169,13 +169,14 @@ export class RichText extends Component {
} );
}

applyRecord( record ) {
applyRecord( record, { domOnly } = {} ) {
apply( {
value: record,
current: this.editableRef,
multilineTag: this.multilineTag,
multilineWrapperTags: this.multilineWrapperTags,
prepareEditableTree: this.props.prepareEditableTree,
__unstableDomOnly: domOnly,
} );
}

Expand Down Expand Up @@ -421,7 +422,7 @@ export class RichText extends Component {
}

this.setState( { start, end, selectedFormat } );
this.applyRecord( { ...value, selectedFormat } );
this.applyRecord( { ...value, selectedFormat }, { domOnly: true } );

delete this.formatPlaceholder;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/rich-text/src/to-dom.js
Expand Up @@ -212,6 +212,7 @@ export function apply( {
multilineTag,
multilineWrapperTags,
prepareEditableTree,
__unstableDomOnly,
} ) {
// Construct a new element tree in memory.
const { body, selection } = toDom( {
Expand All @@ -223,7 +224,7 @@ export function apply( {

applyValue( body, current );

if ( value.start !== undefined ) {
if ( value.start !== undefined && ! __unstableDomOnly ) {
applySelection( selection, current );
}
}
Expand Down

0 comments on commit 2ce6115

Please sign in to comment.