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

Fix paste after focus #16857

Merged
merged 2 commits into from
Aug 2, 2019
Merged
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
9 changes: 8 additions & 1 deletion packages/rich-text/src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class RichText extends Component {

componentWillUnmount() {
document.removeEventListener( 'selectionchange', this.onSelectionChange );
window.cancelAnimationFrame( this.rafId );
}

setRef( node ) {
Expand Down Expand Up @@ -287,7 +288,7 @@ class RichText extends Component {
this.recalculateBoundaryStyle();

// We know for certain that on focus, the old selection is invalid. It
// will be recalculated on `selectionchange`.
// will be recalculated on the next animation frame.
const index = undefined;
const activeFormats = undefined;

Expand All @@ -300,6 +301,12 @@ class RichText extends Component {
this.props.onSelectionChange( index, index );
this.setState( { activeFormats } );

// Update selection as soon as possible, which is at the next animation
// frame. The event listener for selection changes may be added too late
// at this point, but this focus event is still too early to calculate
// the selection.
gziolo marked this conversation as resolved.
Show resolved Hide resolved
this.rafId = window.requestAnimationFrame( this.onSelectionChange );

document.addEventListener( 'selectionchange', this.onSelectionChange );
}

Expand Down