Skip to content

Commit

Permalink
RichText: Adopt withSafeTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Feb 9, 2018
1 parent a87280a commit cd735cb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 23 deletions.
10 changes: 6 additions & 4 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -855,3 +855,5 @@ RichText.defaultProps = {
formattingControls: DEFAULT_FORMATS,
formatters: [],
};

export default withSafeTimeout( RichText );
21 changes: 3 additions & 18 deletions blocks/rich-text/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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 );

Expand Down
2 changes: 1 addition & 1 deletion blocks/rich-text/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit cd735cb

Please sign in to comment.