Skip to content

Commit 29462aa

Browse files
committed
Format Controls: Leverage the Toolbar component
- clear the `setTimeout` on unmount - don't mutate the state object
1 parent f18b6e0 commit 29462aa

File tree

4 files changed

+35
-38
lines changed

4 files changed

+35
-38
lines changed

blocks/components/editable/format-toolbar.js

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
/**
2-
* External dependencies
3-
*/
4-
import classNames from 'classnames';
5-
61
/**
72
* Internal dependencies
83
*/
94
// TODO: We mustn't import by relative path traversing from blocks to editor
105
// as we're doing here; instead, we should consider a common components path.
116
import IconButton from '../../../editor/components/icon-button';
7+
import Toolbar from '../../../editor/components/toolbar';
128

139
const FORMATTING_CONTROLS = [
1410
{
@@ -42,6 +38,12 @@ class FormatToolbar extends wp.element.Component {
4238
this.updateLinkValue = this.updateLinkValue.bind( this );
4339
}
4440

41+
componentWillUnmout() {
42+
if ( this.editTimeout ) {
43+
clearTimeout( this.editTimeout );
44+
}
45+
}
46+
4547
componentWillReceiveProps( nextProps ) {
4648
const newState = {
4749
linkValue: nextProps.formats.link ? nextProps.formats.link.value : ''
@@ -57,8 +59,7 @@ class FormatToolbar extends wp.element.Component {
5759
}
5860

5961
toggleFormat( format ) {
60-
return ( event ) => {
61-
event.stopPropagation();
62+
return () => {
6263
this.props.onChange( {
6364
[ format ]: ! this.props.formats[ format ]
6465
} );
@@ -70,7 +71,7 @@ class FormatToolbar extends wp.element.Component {
7071
this.props.onChange( { link: { value: '' } } );
7172

7273
// Debounce the call to avoid the reset in willReceiveProps
73-
setTimeout( () => this.setState( { isEditingLink: true } ) );
74+
this.editTimeout = setTimeout( () => this.setState( { isEditingLink: true } ) );
7475
}
7576
}
7677

@@ -107,26 +108,22 @@ class FormatToolbar extends wp.element.Component {
107108

108109
return (
109110
<div className="editable-format-toolbar">
110-
<ul className="editor-toolbar">
111-
{ FORMATTING_CONTROLS.map( ( control, index ) => (
112-
<IconButton
113-
key={ index }
114-
icon={ control.icon }
115-
label={ control.title }
116-
onClick={ this.toggleFormat( control.format ) }
117-
className={ classNames( 'editor-toolbar__control', {
118-
'is-active': !! formats[ control.format ]
119-
} ) } />
120-
) ) }
121-
<IconButton
122-
icon="admin-links"
123-
label={ wp.i18n.__( 'Link' ) }
124-
onClick={ this.addLink }
125-
className={ classNames( 'editor-toolbar__control', {
126-
'is-active': !! formats.link
127-
} ) }
128-
/>
129-
</ul>
111+
<Toolbar
112+
controls={
113+
FORMATTING_CONTROLS
114+
.map( ( control ) => ( {
115+
...control,
116+
onClick: this.toggleFormat( control.format ),
117+
isActive: !! formats[ control.format ]
118+
} ) )
119+
.concat( [ {
120+
icon: 'admin-links',
121+
title: wp.i18n.__( 'Link' ),
122+
onClick: this.addLink,
123+
isActive: !! formats.link
124+
} ] )
125+
}
126+
/>
130127

131128
{ !! formats.link && this.state.isEditingLink &&
132129
<form

blocks/components/editable/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ export default class Editable extends wp.element.Component {
347347
} );
348348

349349
this.setState( {
350-
formats: merge( this.state.formats, formats )
350+
formats: merge( {}, this.state.formats, formats )
351351
} );
352352
}
353353

editor/modes/visual-editor/block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class VisualEditorBlock extends wp.element.Component {
140140

141141
// Disable reason: Each block can be selected by clicking on it
142142

143-
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role */
143+
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
144144
return (
145145
<div
146146
ref={ this.bindBlockNode }

languages/gutenberg.pot

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ msgstr ""
33
"Content-Type: text/plain; charset=utf-8\n"
44
"X-Generator: babel-plugin-wp-i18n\n"
55

6-
#: blocks/components/editable/format-toolbar.js:123
7-
msgid "Link"
6+
#: blocks/components/editable/format-toolbar.js:12
7+
msgid "Bold"
88
msgstr ""
99

10-
#: blocks/components/editable/format-toolbar.js:142
11-
msgid "Paste URL or type"
10+
#: blocks/components/editable/format-toolbar.js:121
11+
msgid "Link"
1212
msgstr ""
1313

14-
#: blocks/components/editable/format-toolbar.js:16
15-
msgid "Bold"
14+
#: blocks/components/editable/format-toolbar.js:139
15+
msgid "Paste URL or type"
1616
msgstr ""
1717

18-
#: blocks/components/editable/format-toolbar.js:21
18+
#: blocks/components/editable/format-toolbar.js:17
1919
msgid "Italic"
2020
msgstr ""
2121

22-
#: blocks/components/editable/format-toolbar.js:26
22+
#: blocks/components/editable/format-toolbar.js:22
2323
msgid "Strikethrough"
2424
msgstr ""
2525

0 commit comments

Comments
 (0)