Skip to content

Commit

Permalink
Ensure editor still exists after timeout (#4524)
Browse files Browse the repository at this point in the history
* Ensure editor still exists after timeout

* Add doc for setSafeTimeout
  • Loading branch information
ellatrix authored Jan 17, 2018
1 parent 0940478 commit 64b6017
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions blocks/editable/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ const { setTimeout } = window;

const { ESCAPE, ENTER, SPACE, BACKSPACE } = keycodes;

/**
* Sets a timeout and checks if the given editor still exists.
*
* @param {Editor} editor TinyMCE editor instance.
* @param {Function} callback The function to call.
*/
function setSafeTimeout( editor, callback ) {
setTimeout( () => ! editor.removed && callback() );
}

export default function( editor ) {
const getContent = this.getContent.bind( this );
const { onReplace } = this.props;
Expand Down Expand Up @@ -64,9 +74,9 @@ export default function( editor ) {
enter();
// Wait for the browser to insert the character.
} else if ( keyCode === SPACE ) {
setTimeout( () => searchFirstText( spacePatterns ) );
setSafeTimeout( editor, () => searchFirstText( spacePatterns ) );
} else if ( keyCode > 47 && ! ( keyCode >= 91 && keyCode <= 93 ) ) {
setTimeout( inline );
setSafeTimeout( editor, inline );
}
}, true );

Expand Down

0 comments on commit 64b6017

Please sign in to comment.