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

Try: Block controls rendering by block's edit #830

Merged
merged 2 commits into from
May 26, 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
17 changes: 17 additions & 0 deletions blocks/block-controls/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* External dependencies
*/
import { Fill } from 'react-slot-fill';

/**
* WordPress dependencies
*/
import Toolbar from 'components/toolbar';

export default function BlockControls( { controls } ) {
return (
<Fill name="Formatting.Toolbar">
<Toolbar controls={ controls } />
</Fill>
);
}
34 changes: 19 additions & 15 deletions blocks/library/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isString } from 'lodash';
import './style.scss';
import { registerBlock, createBlock, query } from '../../api';
import Editable from '../../editable';
import BlockControls from '../../block-controls';

const { children, prop } = query;

Expand All @@ -24,18 +25,6 @@ registerBlock( 'core/heading', {
nodeName: prop( 'h1,h2,h3,h4,h5,h6', 'nodeName' ),
},

controls: [
...'123456'.split( '' ).map( ( level ) => ( {
icon: 'heading',
title: wp.i18n.sprintf( wp.i18n.__( 'Heading %s' ), level ),
isActive: ( { nodeName } ) => 'H' + level === nodeName,
onClick( attributes, setAttributes ) {
setAttributes( { nodeName: 'H' + level } );
},
subscript: level,
} ) ),
],

transforms: {
from: [
{
Expand Down Expand Up @@ -90,17 +79,32 @@ registerBlock( 'core/heading', {
edit( { attributes, setAttributes, focus, setFocus, mergeBlocks } ) {
const { content, nodeName = 'H2' } = attributes;

return (
return [
focus && (
<BlockControls
key="controls"
controls={
'123456'.split( '' ).map( ( level ) => ( {
icon: 'heading',
title: wp.i18n.sprintf( wp.i18n.__( 'Heading %s' ), level ),
isActive: 'H' + level === nodeName,
onClick: () => setAttributes( { nodeName: 'H' + level } ),
subscript: level,
} ) )
}
/>
),
<Editable
key="editable"
tagName={ nodeName.toLowerCase() }
value={ content }
focus={ focus }
onFocus={ setFocus }
onChange={ ( value ) => setAttributes( { content: value } ) }
onMerge={ mergeBlocks }
inline
/>
);
/>,
];
},

save( { attributes } ) {
Expand Down