From cd735cb2fa1fe014f7be2ed2312d279cb212a45f Mon Sep 17 00:00:00 2001 From: mcsf Date: Tue, 6 Feb 2018 16:29:26 +0000 Subject: [PATCH] RichText: Adopt withSafeTimeout --- blocks/rich-text/index.js | 10 ++++++---- blocks/rich-text/patterns.js | 21 +++------------------ blocks/rich-text/test/index.js | 2 +- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/blocks/rich-text/index.js b/blocks/rich-text/index.js index a1f8a6709c695a..9d523af9512e0e 100644 --- a/blocks/rich-text/index.js +++ b/blocks/rich-text/index.js @@ -22,7 +22,7 @@ import 'element-closest'; */ import { createElement, Component, renderToString } from '@wordpress/element'; import { keycodes, createBlobURL } from '@wordpress/utils'; -import { Slot, Fill } from '@wordpress/components'; +import { withSafeTimeout, Slot, Fill } from '@wordpress/components'; /** * Internal dependencies @@ -72,7 +72,7 @@ export function getFormatProperties( formatName, parents ) { const DEFAULT_FORMATS = [ 'bold', 'italic', 'strikethrough', 'link' ]; -export default class RichText extends Component { +export class RichText extends Component { constructor( props ) { super( ...arguments ); @@ -267,11 +267,11 @@ export default class RichText extends Component { if ( isEmpty && this.props.onReplace ) { // Necessary to allow the paste bin to be removed without errors. - setTimeout( () => this.props.onReplace( content ) ); + this.props.setTimeout( () => this.props.onReplace( content ) ); } else if ( this.props.onSplit ) { // Necessary to get the right range. // Also done in the TinyMCE paste plugin. - setTimeout( () => this.splitContent( content ) ); + this.props.setTimeout( () => this.splitContent( content ) ); } event.preventDefault(); @@ -855,3 +855,5 @@ RichText.defaultProps = { formattingControls: DEFAULT_FORMATS, formatters: [], }; + +export default withSafeTimeout( RichText ); diff --git a/blocks/rich-text/patterns.js b/blocks/rich-text/patterns.js index 0ca2292ef5eb4e..fd20d944527de7 100644 --- a/blocks/rich-text/patterns.js +++ b/blocks/rich-text/patterns.js @@ -14,26 +14,11 @@ import { keycodes } from '@wordpress/utils'; */ import { getBlockTypes } from '../api/registration'; -/** - * Browser dependencies - */ -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; + const { setTimeout, onReplace } = this.props; const VK = tinymce.util.VK; const settings = editor.settings.wptextpattern || {}; @@ -74,9 +59,9 @@ export default function( editor ) { enter(); // Wait for the browser to insert the character. } else if ( keyCode === SPACE ) { - setSafeTimeout( editor, () => searchFirstText( spacePatterns ) ); + setTimeout( () => searchFirstText( spacePatterns ) ); } else if ( keyCode > 47 && ! ( keyCode >= 91 && keyCode <= 93 ) ) { - setSafeTimeout( editor, inline ); + setTimeout( inline ); } }, true ); diff --git a/blocks/rich-text/test/index.js b/blocks/rich-text/test/index.js index cd9bf8a04b04cf..70829ef3994d1e 100644 --- a/blocks/rich-text/test/index.js +++ b/blocks/rich-text/test/index.js @@ -6,7 +6,7 @@ import { shallow } from 'enzyme'; /** * Internal dependencies */ -import RichText, { createTinyMCEElement, isLinkBoundary, getFormatProperties } from '../'; +import { RichText, createTinyMCEElement, isLinkBoundary, getFormatProperties } from '../'; import { diffAriaProps, pickAriaProps } from '../aria'; describe( 'createTinyMCEElement', () => {