diff --git a/blocks/editable/patterns.js b/blocks/editable/patterns.js index ec86fff0d1a4d..0ca2292ef5eb4 100644 --- a/blocks/editable/patterns.js +++ b/blocks/editable/patterns.js @@ -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; @@ -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 );