From 910d2dc5672e60a53f1e476b5d6d18f6759c8ebc Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 1 May 2017 16:19:58 +0100 Subject: [PATCH] Format Controls: Leverage the Toolbar component - clear the `setTimeout` on unmount - don't mutate the state object --- blocks/components/editable/format-toolbar.js | 53 +++++++++----------- blocks/components/editable/index.js | 2 +- editor/modes/visual-editor/block.js | 2 +- languages/gutenberg.pot | 16 +++--- 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/blocks/components/editable/format-toolbar.js b/blocks/components/editable/format-toolbar.js index 10922323926388..4dbbea38ee10d3 100644 --- a/blocks/components/editable/format-toolbar.js +++ b/blocks/components/editable/format-toolbar.js @@ -1,14 +1,10 @@ -/** - * External dependencies - */ -import classNames from 'classnames'; - /** * Internal dependencies */ // TODO: We mustn't import by relative path traversing from blocks to editor // as we're doing here; instead, we should consider a common components path. import IconButton from '../../../editor/components/icon-button'; +import Toolbar from '../../../editor/components/toolbar'; const FORMATTING_CONTROLS = [ { @@ -42,6 +38,12 @@ class FormatToolbar extends wp.element.Component { this.updateLinkValue = this.updateLinkValue.bind( this ); } + componentWillUnmout() { + if ( this.editTimeout ) { + clearTimeout( this.editTimeout ); + } + } + componentWillReceiveProps( nextProps ) { const newState = { linkValue: nextProps.formats.link ? nextProps.formats.link.value : '' @@ -57,8 +59,7 @@ class FormatToolbar extends wp.element.Component { } toggleFormat( format ) { - return ( event ) => { - event.stopPropagation(); + return () => { this.props.onChange( { [ format ]: ! this.props.formats[ format ] } ); @@ -70,7 +71,7 @@ class FormatToolbar extends wp.element.Component { this.props.onChange( { link: { value: '' } } ); // Debounce the call to avoid the reset in willReceiveProps - setTimeout( () => this.setState( { isEditingLink: true } ) ); + this.editTimeout = setTimeout( () => this.setState( { isEditingLink: true } ) ); } } @@ -107,26 +108,22 @@ class FormatToolbar extends wp.element.Component { return (
- + ( { + ...control, + onClick: this.toggleFormat( control.format ), + isActive: !! formats[ control.format ] + } ) ) + .concat( [ { + icon: 'admin-links', + title: wp.i18n.__( 'Link' ), + onClick: this.addLink, + isActive: !! formats.link + } ] ) + } + /> { !! formats.link && this.state.isEditingLink &&