Skip to content

Commit

Permalink
Fix paste after focus (#16857)
Browse files Browse the repository at this point in the history
* Fix paste after focus

* Fixed type in comment
  • Loading branch information
ellatrix authored and gziolo committed Aug 29, 2019
1 parent dadc791 commit a0c190e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/rich-text/src/component/index.js
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.
this.rafId = window.requestAnimationFrame( this.onSelectionChange );

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

Expand Down

0 comments on commit a0c190e

Please sign in to comment.