Skip to content

Commit

Permalink
Merge pull request #423 from WordPress/add/heading-block
Browse files Browse the repository at this point in the history
Add basic heading block
  • Loading branch information
ellatrix authored Apr 17, 2017
2 parents 3f978c9 + f90d96e commit e0b5bfa
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
10 changes: 9 additions & 1 deletion blocks/components/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,22 @@ export default class Editable extends wp.element.Component {
this.editor.selection.moveToBookmark( bookmark );
}

componentWillUpdate( nextProps ) {
if ( this.editor && this.props.tagName !== nextProps.tagName ) {
this.editor.destroy();
}
}

componentWillUnmount() {
if ( this.editor ) {
this.editor.destroy();
}
}

componentDidUpdate( prevProps ) {
if ( this.props.value !== prevProps.value ) {
if ( this.props.tagName !== prevProps.tagName ) {
this.initialize();
} else if ( this.props.value !== prevProps.value ) {
this.updateContent();
}
}
Expand Down
56 changes: 56 additions & 0 deletions blocks/library/heading/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Internal dependencies
*/
import { registerBlock, query } from 'api';
import Editable from 'components/editable';

const { html, prop } = query;

registerBlock( 'core/heading', {
title: wp.i18n.__( 'Heading' ),

icon: 'heading',

category: 'common',

attributes: {
content: html( 'h1,h2,h3,h4,h5,h6' ),
tag: prop( 'h1,h2,h3,h4,h5,h6', 'nodeName' ),
align: prop( 'h1,h2,h3,h4,h5,h6', 'style.textAlign' )
},

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

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

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

save( { attributes } ) {
const { align, tag: Tag, content } = attributes;

return (
<Tag
style={ align ? { textAlign: align } : null }
dangerouslySetInnerHTML={ { __html: content } } />
);
}
} );
1 change: 1 addition & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './freeform';
import './heading';
import './image';
import './text';
import './list';
3 changes: 2 additions & 1 deletion editor/components/icon-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import './style.scss';
import Button from '../button';
import Dashicon from '../dashicon';

function IconButton( { icon, label, className, ...additionalProps } ) {
function IconButton( { icon, children, label, className, ...additionalProps } ) {
const classes = classnames( 'editor-icon-button', className );

return (
<Button { ...additionalProps } aria-label={ label } className={ classes }>
<Dashicon icon={ icon } />
{ children ? <span>{ children }</span> : null }
</Button>
);
}
Expand Down
6 changes: 5 additions & 1 deletion editor/components/icon-button/style.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.editor-icon-button {
display: flex;
align-items: center;
width: 36px;
min-width: 36px;
height: 36px;
border: none;
background: none;
Expand All @@ -14,4 +14,8 @@
color: $blue-medium;
}
}

span {
font-size: 12px;
}
}
4 changes: 3 additions & 1 deletion editor/components/toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ function Toolbar( { controls } ) {
} }
className={ classNames( 'editor-toolbar__control', {
'is-active': control.isActive && control.isActive()
} ) } />
} ) }>
{ control.text }
</IconButton>
) ) }
</ul>
);
Expand Down
8 changes: 8 additions & 0 deletions languages/gutenberg.pot
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ msgstr ""
msgid "Freeform"
msgstr ""

#: blocks/library/heading/index.js:10
msgid "Heading"
msgstr ""

#: blocks/library/heading/index.js:26
msgid "Heading %s"
msgstr ""

#: blocks/library/image/index.js:10
msgid "Image"
msgstr ""
Expand Down

0 comments on commit e0b5bfa

Please sign in to comment.