Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editable: Adding a prop to opt-for formatting controls #618

Merged
merged 1 commit into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions blocks/components/editable/format-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is only link being singled out here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it has a different behaviour (different onClick and isActive props)

toolbarControls.push( {
icon: 'admin-links',
title: wp.i18n.__( 'Link' ),
onClick: this.addLink,
isActive: !! formats.link
} );
}

return (
<div className="editable-format-toolbar">
<Toolbar
controls={
FORMATTING_CONTROLS
.map( ( control ) => ( {
...control,
onClick: this.toggleFormat( control.format ),
isActive: !! formats[ control.format ]
} ) )
.concat( [ {
icon: 'admin-links',
title: wp.i18n.__( 'Link' ),
onClick: this.addLink,
isActive: !! formats.link
} ] )
}
/>
<Toolbar controls={ toolbarControls } />

{ !! formats.link && this.state.isEditingLink &&
<form
Expand Down
9 changes: 7 additions & 2 deletions blocks/components/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ export default class Editable extends wp.element.Component {
}

render() {
const { tagName: Tag = 'div', style, focus, className, showAlignments = false } = this.props;
const { tagName: Tag = 'div', style, focus, className, showAlignments = false, formattingControls } = this.props;
const classes = classnames( 'blocks-editable', className );

let element = (
Expand All @@ -388,7 +388,12 @@ export default class Editable extends wp.element.Component {
isActive: this.isAlignmentActive( control.align )
} ) ) } />
}
<FormatToolbar focusPosition={ this.state.focusPosition } formats={ this.state.formats } onChange={ this.changeFormats } />
<FormatToolbar
focusPosition={ this.state.focusPosition }
formats={ this.state.formats }
onChange={ this.changeFormats }
enabledControls={ formattingControls }
/>
</Fill>,
element
];
Expand Down