Skip to content

Commit

Permalink
RichText: don't set selection in componentDidUpdate during pending st…
Browse files Browse the repository at this point in the history
…ore update (#19055)
  • Loading branch information
ellatrix authored Dec 11, 2019
1 parent fdd6953 commit 166f4c6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ exports[`RichText should keep internal selection after blur 1`] = `
<!-- /wp:paragraph -->"
`;

exports[`RichText should make bold after split and merge 1`] = `
"<!-- wp:paragraph -->
<p>1<strong>2</strong></p>
<!-- /wp:paragraph -->"
`;

exports[`RichText should not format text after code backtick 1`] = `
"<!-- wp:paragraph -->
<p>A <code>backtick</code> and more.</p>
Expand Down
11 changes: 11 additions & 0 deletions packages/e2e-tests/specs/editor/various/rich-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,15 @@ describe( 'RichText', () => {

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

it( 'should make bold after split and merge', async () => {
await clickBlockAppender();
await page.keyboard.type( '1' );
await page.keyboard.press( 'Enter' );
await page.keyboard.press( 'Backspace' );
await pressKeyWithModifier( 'primary', 'b' );
await page.keyboard.type( '2' );

expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );
18 changes: 10 additions & 8 deletions packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,12 +922,17 @@ class RichText extends Component {
value !== this.value
);

const selectionChanged = (
selectionStart !== prevProps.selectionStart &&
selectionStart !== this.record.start
) || (
selectionEnd !== prevProps.selectionEnd &&
selectionEnd !== this.record.end
);

// Check if the selection changed.
shouldReapply = shouldReapply || (
isSelected && ! prevProps.isSelected && (
this.record.start !== selectionStart ||
this.record.end !== selectionEnd
)
isSelected && ! prevProps.isSelected && selectionChanged
);

const prefix = 'format_prepare_props_';
Expand All @@ -949,10 +954,7 @@ class RichText extends Component {
this.record.start = selectionStart;
this.record.end = selectionEnd;
this.applyRecord( this.record );
} else if (
this.record.start !== selectionStart ||
this.record.end !== selectionEnd
) {
} else if ( selectionChanged ) {
this.record = {
...this.record,
start: selectionStart,
Expand Down

0 comments on commit 166f4c6

Please sign in to comment.