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

List blocks can now save changes to their content #598

Merged
merged 9 commits into from
May 5, 2017
24 changes: 11 additions & 13 deletions blocks/components/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import FormatToolbar from './format-toolbar';
import Toolbar from '../../../editor/components/toolbar';

const KEYCODE_BACKSPACE = 8;
const formatMap = {
strong: 'bold',
em: 'italic',
del: 'strikethrough'

const alignmentMap = {
alignleft: 'left',
alignright: 'right',
aligncenter: 'center'
};

const ALIGNMENT_CONTROLS = [
Expand Down Expand Up @@ -221,21 +222,17 @@ export default class Editable extends wp.element.Component {
}

onNodeChange( { element, parents } ) {
let alignment = null;
const formats = {};
parents.forEach( ( node ) => {
const tag = node.nodeName.toLowerCase();

if ( formatMap.hasOwnProperty( tag ) ) {
formats[ formatMap[ tag ] ] = true;
} else if ( tag === 'a' ) {
if ( tag === 'a' ) {
formats.link = { value: node.getAttribute( 'href' ), node };
}

if ( tag === 'p' ) {
alignment = node.style.textAlign || 'left';
}
} );
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: a bit more performant maybe

const link = parents.find( ( node ) = node.nodeName.toLowerCase() === 'a' );
if ( link ) {
  formats.link = { value: node.getAttribute( 'href' ), node };
}

Copy link
Author

Choose a reason for hiding this comment

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

I have added your suggested efficiency improvement

const activeFormats = this.editor.formatter.matchAll( [ 'bold', 'italic', 'strikethrough' ] );
activeFormats.forEach( ( activeFormat ) => formats[ activeFormat ] = true );
const alignments = this.editor.formatter.matchAll( [ 'alignleft', 'aligncenter', 'alignright' ] );
const alignment = alignments.length > 0 ? alignmentMap[ alignments[ 0 ] ] : null;

const focusPosition = this.getRelativePosition( element );
const bookmark = this.editor.selection.getBookmark( 2, true );
Expand Down Expand Up @@ -284,6 +281,7 @@ export default class Editable extends wp.element.Component {

componentWillUpdate( nextProps ) {
if ( this.editor && this.props.tagName !== nextProps.tagName ) {
this.onChange();
this.editor.destroy();
}
}
Expand Down
59 changes: 19 additions & 40 deletions blocks/library/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import './style.scss';
import { registerBlock, query as hpq } from 'api';
import Editable from 'components/editable';

const { children, prop, query } = hpq;
const { children, prop } = hpq;

registerBlock( 'core/list', {
title: wp.i18n.__( 'List' ),
Expand All @@ -14,72 +14,51 @@ registerBlock( 'core/list', {

attributes: {
nodeName: prop( 'ol,ul', 'nodeName' ),
items: query( 'li', {
value: children()
} )
values: children( 'ol,ul' )
},

controls: [
{
icon: 'editor-alignleft',
title: wp.i18n.__( 'Align left' ),
isActive: ( { align } ) => ! align || 'left' === align,
icon: 'editor-ul',
title: wp.i18n.__( 'Convert to unordered' ),
isActive: ( { nodeName = 'OL' } ) => nodeName === 'UL',
onClick( attributes, setAttributes ) {
setAttributes( { align: undefined } );
setAttributes( { nodeName: 'UL' } );
}
},
{
icon: 'editor-aligncenter',
title: wp.i18n.__( 'Align center' ),
isActive: ( { align } ) => 'center' === align,
icon: 'editor-ol',
title: wp.i18n.__( 'Convert to ordered' ),
isActive: ( { nodeName = 'OL' } ) => nodeName === 'OL',
onClick( attributes, setAttributes ) {
setAttributes( { align: 'center' } );
}
},
{
icon: 'editor-alignright',
title: wp.i18n.__( 'Align right' ),
isActive: ( { align } ) => 'right' === align,
onClick( attributes, setAttributes ) {
setAttributes( { align: 'right' } );
}
},
{
icon: 'editor-justify',
title: wp.i18n.__( 'Justify' ),
isActive: ( { align } ) => 'justify' === align,
onClick( attributes, setAttributes ) {
setAttributes( { align: 'justify' } );
setAttributes( { nodeName: 'OL' } );
}
}
],

edit( { attributes, focus, setFocus } ) {
const { nodeName = 'OL', items = [], align } = attributes;
const content = items.map( ( item, i ) => {
return <li key={ i }>{ item.value }</li>;
} );

edit( { attributes, setAttributes, focus, setFocus } ) {
const { nodeName = 'OL', values = [] } = attributes;
return (
<Editable
tagName={ nodeName.toLowerCase() }
style={ align ? { textAlign: align } : null }
value={ content }
onChange={ ( nextValues ) => {
setAttributes( { values: nextValues } );
} }
value={ values }
focus={ focus }
onFocus={ setFocus }
showAlignments
className="blocks-list" />
);
},

save( { attributes } ) {
const { nodeName = 'OL', items = [] } = attributes;
const { nodeName = 'OL', values = [] } = attributes;

return wp.element.createElement(
nodeName.toLowerCase(),
null,
items.map( ( item, index ) => (
<li key={ index }>{ item.value }</li>
) )
values
);
}
} );