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

Display toolbar of controls on selected block #381

Merged
merged 5 commits into from
Apr 7, 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
3 changes: 2 additions & 1 deletion blocks/components/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ export default class Editable extends wp.element.Component {
}

render() {
const { tagName: Tag = 'div' } = this.props;
const { tagName: Tag = 'div', style } = this.props;

return (
<Tag
ref={ this.bindNode }
style={ style }
className="blocks-editable" />
);
}
Expand Down
5 changes: 2 additions & 3 deletions editor/blocks/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ wp.blocks.registerBlock( 'core/list', {
)
},

edit( { attributes, onChange } ) {
edit( { attributes } ) {
const { listType = 'ol', items = [] } = attributes;
const value = items.map( item => {
return `<li>${ item.value }</li>`;
Expand All @@ -25,8 +25,7 @@ wp.blocks.registerBlock( 'core/list', {
return (
<Editable
tagName={ listType }
value={ value }
onChange={ onChange } />
value={ value } />
);
},

Expand Down
49 changes: 43 additions & 6 deletions editor/blocks/text/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { html } = wp.blocks.query;
const { html, prop } = wp.blocks.query;
const Editable = wp.blocks.Editable;

wp.blocks.registerBlock( 'core/text', {
Expand All @@ -9,20 +9,57 @@ wp.blocks.registerBlock( 'core/text', {
category: 'common',

attributes: {
value: html( 'p' )
content: html( 'p' ),
align: prop( 'p', 'style.textAlign' )
},

edit( { attributes, onChange } ) {
controls: [
{
icon: 'editor-alignleft',
title: wp.i18n.__( 'Align left' ),
isActive: ( { align } ) => ! align || 'left' === align,
onClick( attributes, setAttributes ) {
setAttributes( { align: undefined } );
}
},
{
icon: 'editor-aligncenter',
title: wp.i18n.__( 'Align center' ),
isActive: ( { align } ) => 'center' === align,
onClick( attributes, setAttributes ) {
setAttributes( { align: 'center' } );
}
},
{
icon: 'editor-alignright',
title: wp.i18n.__( 'Align right' ),
isActive: ( { align } ) => 'right' === align,
onClick( attributes, setAttributes ) {
setAttributes( { align: 'right' } );
}
}
],

edit( { attributes, setAttributes } ) {
const { content, align } = attributes;

return (
<Editable
tagName="p"
value={ attributes.value }
onChange={ ( value ) => onChange( { value } ) }
value={ content }
onChange={ ( value ) => setAttributes( { content: value } ) }
style={ align ? { textAlign: align } : null }
/>
);
},

save( { attributes } ) {
return <p dangerouslySetInnerHTML={ { __html: attributes.value } } />;
const { align, content } = attributes;

return (
<p
style={ align ? { textAlign: align } : null }
dangerouslySetInnerHTML={ { __html: content } } />
);
}
} );
38 changes: 38 additions & 0 deletions editor/components/toolbar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* External dependencies
*/
import classNames from 'classnames';

/**
* Internal dependencies
*/
import './style.scss';
import Dashicon from '../dashicon';

function Toolbar( { controls } ) {
if ( ! controls || ! controls.length ) {
return null;
}

return (
<ul className="editor-toolbar">
{ controls.map( ( control, index ) => (
<button
key={ index }
className={ classNames( 'editor-toolbar__control', {
'is-active': control.isActive && control.isActive()
} ) }
title={ control.title }
onClick={ ( event ) => {
event.stopPropagation();
control.onClick();
} }
>
<Dashicon icon={ control.icon } />
</button>
) ) }
</ul>
);
}

export default Toolbar;
35 changes: 35 additions & 0 deletions editor/components/toolbar/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.editor-toolbar {
margin: 0;
border: 1px solid $light-gray-500;
box-shadow: 0px 3px 20px rgba( 18, 24, 30, .1 ), 0px 1px 3px rgba( 18, 24, 30, .1 );
background-color: $white;
}

.editor-toolbar__control {
margin: 2px;
margin-left: 0;
padding: 6px;
background: none;
border: 1px solid transparent;
outline: none;
color: $dark-gray-500;
cursor: pointer;

&:first-child {
margin-left: 2px;
}

&.is-active,
&:hover {
border-color: $dark-gray-500;
}

&.is-active {
background-color: $dark-gray-500;
color: $white;
}
}

.editor-toolbar__control .dashicon {
display: block;
}
44 changes: 33 additions & 11 deletions editor/modes/visual-editor/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import { connect } from 'react-redux';
import classnames from 'classnames';

/**
* Internal dependencies
*/
import Toolbar from '../../components/toolbar';

function VisualEditorBlock( props ) {
const { block } = props;
const settings = wp.blocks.getBlockSettings( block.blockType );
Expand All @@ -17,22 +22,31 @@ function VisualEditorBlock( props ) {
return null;
}

function onAttributesChange( attributes ) {
props.onChange( {
const { isHovered } = props;
const isSelected = props.isSelected;
const className = classnames( 'editor-visual-editor__block', {
'is-selected': isSelected,
'is-hovered': isHovered
} );

const { onChange, onSelect, onDeselect, onMouseEnter, onMouseLeave } = props;

function setAttributes( attributes ) {
onChange( {
attributes: {
...block.attributes,
...attributes
}
} );
}

const { isSelected, isHovered } = props;
const className = classnames( 'editor-visual-editor__block', {
'is-selected': isSelected,
'is-hovered': isHovered
} );

const { onSelect, onDeselect, onMouseEnter, onMouseLeave } = props;
function maybeDeselect( event ) {
// Annoyingly React does not support focusOut and we're forced to check
// related target to ensure it's not a child when blur fires.
if ( ! event.currentTarget.contains( event.relatedTarget ) ) {
onDeselect();
}
}

// Disable reason: Each block can receive focus but must be able to contain
// block children. Tab keyboard navigation enabled by tabIndex assignment.
Expand All @@ -42,15 +56,23 @@ function VisualEditorBlock( props ) {
<div
tabIndex="0"
onFocus={ onSelect }
onBlur={ onDeselect }
onBlur={ maybeDeselect }
onKeyDown={ onDeselect }
onMouseEnter={ onMouseEnter }
onMouseLeave={ onMouseLeave }
className={ className }
>
{ isSelected && settings.controls ? (
<Toolbar
controls={ settings.controls.map( ( control ) => ( {
...control,
onClick: () => control.onClick( block.attributes, setAttributes ),
isActive: () => control.isActive( block.attributes )
} ) ) } />
) : null }
<BlockEdit
attributes={ block.attributes }
onChange={ onAttributesChange } />
setAttributes={ setAttributes } />
</div>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
Expand Down
8 changes: 8 additions & 0 deletions editor/modes/visual-editor/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
}

.editor-visual-editor__block {
position: relative;
padding: 15px;
border: 2px solid transparent;
transition: 0.2s border-color;
Expand All @@ -28,3 +29,10 @@
border: 2px solid $light-gray-500;
}
}

.editor-visual-editor__block .editor-toolbar {
position: absolute;
bottom: 100%;
margin-bottom: -4px;
left: 8px;
}
12 changes: 12 additions & 0 deletions languages/gutenberg.pot
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ msgstr ""
msgid "List"
msgstr ""

#: editor/blocks/text/index.js:19
msgid "Align left"
msgstr ""

#: editor/blocks/text/index.js:27
msgid "Align center"
msgstr ""

#: editor/blocks/text/index.js:35
msgid "Align right"
msgstr ""

#: editor/header/mode-switcher/index.js:30
msgid "Visual"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
"dependencies": {
"classnames": "^2.2.5",
"hpq": "^1.1.1",
"hpq": "^1.2.0",
"jed": "^1.1.1",
"lodash": "^4.17.4",
"react-redux": "^5.0.3",
Expand Down