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

RichText: don't set selection during selectionchange #13896

Merged
merged 2 commits into from
Feb 15, 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
6 changes: 6 additions & 0 deletions packages/e2e-tests/specs/__snapshots__/rich-text.test.js.snap
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of me wonders if this is the sort of thing where it'd be easier to just pass through whatever additional options verbatim, i.e. ...options from a second object argument. But I can also see the opposite argument of there being value in being explicit, especially in this case where it's something perhaps temporary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I definitely see this as something temporary.

} );
}

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
Original file line number Diff line number Diff line change
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