11/**
22 * External dependencies
33 */
4- import { isEmpty } from 'lodash' ;
4+ import { omit } from 'lodash' ;
55
66/**
77 * WordPress dependencies
@@ -17,32 +17,23 @@ import classnames from 'classnames';
1717import './editor.scss' ;
1818import './style.scss' ;
1919import { createBlock } from '../../api' ;
20- import RichText from '../../rich-text' ;
2120import AlignmentToolbar from '../../alignment-toolbar' ;
2221import MediaUpload from '../../media-upload' ;
2322import ImagePlaceholder from '../../image-placeholder' ;
2423import BlockControls from '../../block-controls' ;
2524import BlockAlignmentToolbar from '../../block-alignment-toolbar' ;
2625import InspectorControls from '../../inspector-controls' ;
26+ import InnerBlocks from '../../inner-blocks' ;
2727
2828const validAlignments = [ 'left' , 'center' , 'right' , 'wide' , 'full' ] ;
2929
3030const blockAttributes = {
31- title : {
32- type : 'array' ,
33- source : 'children' ,
34- selector : 'p' ,
35- } ,
3631 url : {
3732 type : 'string' ,
3833 } ,
3934 align : {
4035 type : 'string' ,
4136 } ,
42- contentAlign : {
43- type : 'string' ,
44- default : 'center' ,
45- } ,
4637 id : {
4738 type : 'number' ,
4839 } ,
@@ -97,8 +88,8 @@ export const settings = {
9788 }
9889 } ,
9990
100- edit ( { attributes, setAttributes, isSelected , className } ) {
101- const { url , title , align, contentAlign , id, hasParallax, dimRatio } = attributes ;
91+ edit ( { attributes, setAttributes, className } ) {
92+ const { align, url , id, hasParallax, dimRatio } = attributes ;
10293 const updateAlignment = ( nextAlign ) => setAttributes ( { align : nextAlign } ) ;
10394 const onSelectImage = ( media ) => setAttributes ( { url : media . url , id : media . id } ) ;
10495 const toggleParallax = ( ) => setAttributes ( { hasParallax : ! hasParallax } ) ;
@@ -107,7 +98,6 @@ export const settings = {
10798 const style = backgroundImageStyles ( url ) ;
10899 const classes = classnames (
109100 className ,
110- contentAlign !== 'center' && `has-${ contentAlign } -content` ,
111101 dimRatioToClass ( dimRatio ) ,
112102 {
113103 'has-background-dim' : dimRatio !== 0 ,
@@ -117,7 +107,6 @@ export const settings = {
117107
118108 const alignmentToolbar = (
119109 < AlignmentToolbar
120- value = { contentAlign }
121110 onChange = { ( nextAlign ) => {
122111 setAttributes ( { contentAlign : nextAlign } ) ;
123112 } }
@@ -172,16 +161,8 @@ export const settings = {
172161 ) ;
173162
174163 if ( ! url ) {
175- const hasTitle = ! isEmpty ( title ) ;
176- const icon = hasTitle ? undefined : 'format-image' ;
177- const label = hasTitle ? (
178- < RichText
179- tagName = "h2"
180- value = { title }
181- onChange = { ( value ) => setAttributes ( { title : value } ) }
182- inlineToolbar
183- />
184- ) : __ ( 'Cover Image' ) ;
164+ const icon = 'format-image' ;
165+ const label = __ ( 'Cover Image' ) ;
185166
186167 return (
187168 < Fragment >
@@ -201,74 +182,127 @@ export const settings = {
201182 style = { style }
202183 className = { classes }
203184 >
204- { title || isSelected ? (
205- < RichText
206- tagName = "p"
207- className = "wp-block-cover-image-text"
208- placeholder = { __ ( 'Write title…' ) }
209- value = { title }
210- onChange = { ( value ) => setAttributes ( { title : value } ) }
211- inlineToolbar
185+ < div className = "wp-block-cover-image__inner-container" >
186+ < InnerBlocks
187+ template = { [
188+ [ 'core/paragraph' , {
189+ align : 'center' ,
190+ fontSize : 'large' ,
191+ placeholder : __ ( 'Write title…' ) ,
192+ textColor : '#fff' ,
193+ } ] ,
194+ ] }
195+ allowedBlocks = { [ 'core/button' , 'core/heading' , 'core/paragraph' , 'core/subhead' ] }
212196 />
213- ) : null }
197+ </ div >
214198 </ div >
215199 </ Fragment >
216200 ) ;
217201 } ,
218202
219203 save ( { attributes, className } ) {
220- const { url, title , hasParallax, dimRatio, align, contentAlign } = attributes ;
204+ const { url, hasParallax, dimRatio, align } = attributes ;
221205 const style = backgroundImageStyles ( url ) ;
222206 const classes = classnames (
223207 className ,
224208 dimRatioToClass ( dimRatio ) ,
225209 {
226210 'has-background-dim' : dimRatio !== 0 ,
227211 'has-parallax' : hasParallax ,
228- [ `has-${ contentAlign } -content` ] : contentAlign !== 'center' ,
229212 } ,
230213 align ? `align${ align } ` : null ,
231214 ) ;
232215
233216 return (
234217 < div className = { classes } style = { style } >
235- { title && title . length > 0 && (
236- < p className = "wp-block-cover-image-text" > { title } </ p >
237- ) }
218+ < div className = "wp-block-cover-image__inner-container" >
219+ < InnerBlocks . Content / >
220+ </ div >
238221 </ div >
239222 ) ;
240223 } ,
241224
242- deprecated : [ {
243- attributes : {
244- ...blockAttributes ,
245- title : {
246- type : 'array' ,
247- source : 'children' ,
248- selector : 'h2' ,
225+ deprecated : [
226+ {
227+ attributes : {
228+ ...blockAttributes ,
229+ title : {
230+ type : 'array' ,
231+ source : 'children' ,
232+ selector : 'p' ,
233+ } ,
234+ contentAlign : {
235+ type : 'string' ,
236+ default : 'center' ,
237+ } ,
238+ } ,
239+
240+ save ( { attributes, className } ) {
241+ const { url, title, hasParallax, dimRatio, align, contentAlign } = attributes ;
242+ const style = backgroundImageStyles ( url ) ;
243+ const classes = classnames (
244+ className ,
245+ dimRatioToClass ( dimRatio ) ,
246+ {
247+ 'has-background-dim' : dimRatio !== 0 ,
248+ 'has-parallax' : hasParallax ,
249+ [ `has-${ contentAlign } -content` ] : contentAlign !== 'center' ,
250+ } ,
251+ align ? `align${ align } ` : null ,
252+ ) ;
253+
254+ return (
255+ < div className = { classes } style = { style } >
256+ { title && title . length > 0 && (
257+ < p className = "wp-block-cover-image-text" > { title } </ p >
258+ ) }
259+ </ div >
260+ ) ;
249261 } ,
250- } ,
251262
252- save ( { attributes, className } ) {
253- const { url, title, hasParallax, dimRatio, align } = attributes ;
254- const style = backgroundImageStyles ( url ) ;
255- const classes = classnames (
256- className ,
257- dimRatioToClass ( dimRatio ) ,
258- {
259- 'has-background-dim' : dimRatio !== 0 ,
260- 'has-parallax' : hasParallax ,
263+ migrate ( attributes ) {
264+ return [
265+ omit ( attributes , [ 'title' , 'contentAlign' ] ) ,
266+ [ createBlock ( 'core/paragraph' , {
267+ content : attributes . title ,
268+ align : attributes . contentAlign ,
269+ fontSize : 'large' ,
270+ placeholder : __ ( 'Write title…' ) ,
271+ } ) ] ,
272+ ] ;
273+ } ,
274+ } ,
275+ {
276+ attributes : {
277+ ...blockAttributes ,
278+ title : {
279+ type : 'array' ,
280+ source : 'children' ,
281+ selector : 'h2' ,
261282 } ,
262- align ? `align${ align } ` : null ,
263- ) ;
283+ } ,
264284
265- return (
266- < section className = { classes } style = { style } >
267- < h2 > { title } </ h2 >
268- </ section >
269- ) ;
285+ save ( { attributes, className } ) {
286+ const { url, title, hasParallax, dimRatio, align } = attributes ;
287+ const style = backgroundImageStyles ( url ) ;
288+ const classes = classnames (
289+ className ,
290+ dimRatioToClass ( dimRatio ) ,
291+ {
292+ 'has-background-dim' : dimRatio !== 0 ,
293+ 'has-parallax' : hasParallax ,
294+ } ,
295+ align ? `align${ align } ` : null ,
296+ ) ;
297+
298+ return (
299+ < section className = { classes } style = { style } >
300+ < h2 > { title } </ h2 >
301+ </ section >
302+ ) ;
303+ } ,
270304 } ,
271- } ] ,
305+ ] ,
272306} ;
273307
274308function dimRatioToClass ( ratio ) {
0 commit comments