/** * Internal dependencies */ import './style.scss'; import { registerBlock, query } from 'api'; import Editable from 'components/editable'; // TODO: Revisit when we have a common components solution import Dashicon from '../../../editor/components/dashicon'; import Button from '../../../editor/components/button'; const { attr, children } = query; /** * Returns an attribute setter with behavior that if the target value is * already the assigned attribute value, it will be set to undefined. * * @param {string} align Alignment value * @return {Function} Attribute setter */ function toggleAlignment( align ) { return ( attributes, setAttributes ) => { const nextAlign = attributes.align === align ? undefined : align; setAttributes( { align: nextAlign } ); }; } registerBlock( 'core/image', { title: wp.i18n.__( 'Image' ), icon: 'format-image', category: 'common', attributes: { url: attr( 'img', 'src' ), alt: attr( 'img', 'alt' ), caption: children( 'figcaption' ), align: ( node ) => ( node.className.match( /\balign(\S+)/ ) || [] )[ 1 ] }, controls: [ { icon: 'align-left', title: wp.i18n.__( 'Align left' ), isActive: ( { align } ) => 'left' === align, onClick: toggleAlignment( 'left' ) }, { icon: 'align-center', title: wp.i18n.__( 'Align center' ), isActive: ( { align } ) => 'center' === align, onClick: toggleAlignment( 'center' ) }, { icon: 'align-right', title: wp.i18n.__( 'Align right' ), isActive: ( { align } ) => 'right' === align, onClick: toggleAlignment( 'right' ) }, { icon: 'align-full-width', title: wp.i18n.__( 'Wide width' ), isActive: ( { align } ) => 'wide' === align, onClick: toggleAlignment( 'wide' ) } ], getEditWrapperProps( attributes ) { const { align } = attributes; if ( 'left' === align || 'right' === align || 'wide' === align ) { return { 'data-align': align }; } }, edit( { attributes, setAttributes, focus, setFocus } ) { const { url, alt, caption } = attributes; if ( ! url ) { return (