From e8cac3589a095544390b79c9bfec0ed3898d84f3 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Wed, 3 May 2017 13:46:32 +0100 Subject: [PATCH] Editable: Adding a prop to opt-for formatting controls --- blocks/components/editable/format-toolbar.js | 39 +++++++++++--------- blocks/components/editable/index.js | 9 ++++- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/blocks/components/editable/format-toolbar.js b/blocks/components/editable/format-toolbar.js index 4dbbea38ee10d3..07ada655524e4e 100644 --- a/blocks/components/editable/format-toolbar.js +++ b/blocks/components/editable/format-toolbar.js @@ -24,6 +24,9 @@ const FORMATTING_CONTROLS = [ } ]; +// Default controls shown if no `enabledControls` prop provided +const DEFAULT_CONTROLS = [ 'bold', 'italic', 'strikethrough', 'link' ]; + class FormatToolbar extends wp.element.Component { constructor( props ) { super( ...arguments ); @@ -101,29 +104,31 @@ class FormatToolbar extends wp.element.Component { } render() { - const { formats, focusPosition } = this.props; + const { formats, focusPosition, enabledControls = DEFAULT_CONTROLS } = this.props; const linkStyle = focusPosition ? { position: 'absolute', ...focusPosition } : null; + const toolbarControls = FORMATTING_CONTROLS + .filter( control => enabledControls.indexOf( control.format ) !== -1 ) + .map( ( control ) => ( { + ...control, + onClick: this.toggleFormat( control.format ), + isActive: !! formats[ control.format ] + } ) ); + + if ( enabledControls.indexOf( 'link' ) !== -1 ) { + toolbarControls.push( { + icon: 'admin-links', + title: wp.i18n.__( 'Link' ), + onClick: this.addLink, + isActive: !! formats.link + } ); + } + 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 &&
} - + , element ];