|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { Placeholder } from 'components'; |
| 5 | +import { __ } from 'i18n'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Internal dependencies |
| 9 | + */ |
| 10 | +import './style.scss'; |
| 11 | +import { registerBlockType, query } from '../../api'; |
| 12 | +import Editable from '../../editable'; |
| 13 | +import MediaUploadButton from '../../media-upload-button'; |
| 14 | +import BlockControls from '../../block-controls'; |
| 15 | +import BlockAlignmentToolbar from '../../block-alignment-toolbar'; |
| 16 | + |
| 17 | +const { text } = query; |
| 18 | + |
| 19 | +const validAlignments = [ 'left', 'center', 'right', 'wide', 'full' ]; |
| 20 | + |
| 21 | +registerBlockType( 'core/cover-image', { |
| 22 | + title: __( 'Cover Image' ), |
| 23 | + |
| 24 | + icon: 'format-image', |
| 25 | + |
| 26 | + category: 'common', |
| 27 | + |
| 28 | + attributes: { |
| 29 | + title: text( 'h2' ), |
| 30 | + }, |
| 31 | + |
| 32 | + getEditWrapperProps( attributes ) { |
| 33 | + const { align } = attributes; |
| 34 | + if ( -1 !== validAlignments.indexOf( align ) ) { |
| 35 | + return { 'data-align': align }; |
| 36 | + } |
| 37 | + }, |
| 38 | + |
| 39 | + edit( { attributes, setAttributes, focus, setFocus } ) { |
| 40 | + const { url, title, align } = attributes; |
| 41 | + const updateAlignment = ( nextAlign ) => setAttributes( { align: nextAlign } ); |
| 42 | + const controls = ( |
| 43 | + focus && ( |
| 44 | + <BlockControls key="controls"> |
| 45 | + <BlockAlignmentToolbar |
| 46 | + value={ align } |
| 47 | + onChange={ updateAlignment } |
| 48 | + controls={ validAlignments } |
| 49 | + /> |
| 50 | + </BlockControls> |
| 51 | + ) |
| 52 | + ); |
| 53 | + |
| 54 | + if ( ! url ) { |
| 55 | + const uploadButtonProps = { isLarge: true }; |
| 56 | + const setMediaUrl = ( media ) => setAttributes( { url: media.url } ); |
| 57 | + return [ |
| 58 | + controls, |
| 59 | + <Placeholder |
| 60 | + key="placeholder" |
| 61 | + instructions={ __( 'Drag image here or insert from media library' ) } |
| 62 | + icon="format-image" |
| 63 | + label={ __( 'Image' ) } |
| 64 | + className="blocks-image"> |
| 65 | + <MediaUploadButton |
| 66 | + buttonProps={ uploadButtonProps } |
| 67 | + onSelect={ setMediaUrl } |
| 68 | + type="image" |
| 69 | + autoOpen |
| 70 | + > |
| 71 | + { __( 'Insert from Media Library' ) } |
| 72 | + </MediaUploadButton> |
| 73 | + </Placeholder>, |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + const style = { backgroundImage: `url(${ url })` }; |
| 78 | + |
| 79 | + return [ |
| 80 | + controls, |
| 81 | + <section key="cover-image" className="blocks-cover-image"> |
| 82 | + <section className="cover-image" data-url={ url } style={ style }> |
| 83 | + { title || !! focus ? ( |
| 84 | + <Editable |
| 85 | + tagName="h2" |
| 86 | + placeholder={ __( 'Write title' ) } |
| 87 | + value={ title } |
| 88 | + formattingControls={ [] } |
| 89 | + focus={ focus } |
| 90 | + onFocus={ setFocus } |
| 91 | + onChange={ ( value ) => setAttributes( { title: value } ) } /> |
| 92 | + ) : null } |
| 93 | + </section> |
| 94 | + </section>, |
| 95 | + ]; |
| 96 | + }, |
| 97 | + |
| 98 | + save( { attributes } ) { |
| 99 | + const { url, title } = attributes; |
| 100 | + const style = { |
| 101 | + backgroundImage: `url(${ url })`, |
| 102 | + }; |
| 103 | + |
| 104 | + return ( |
| 105 | + <section className="blocks-cover-image"> |
| 106 | + <section className="cover-image" style={ style }> |
| 107 | + <h2>{ title }</h2> |
| 108 | + </section> |
| 109 | + </section> |
| 110 | + ); |
| 111 | + }, |
| 112 | +} ); |
0 commit comments