-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
index.js
526 lines (494 loc) · 12.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import {
FocalPointPicker,
IconButton,
PanelBody,
RangeControl,
ToggleControl,
Toolbar,
withNotices,
SVG,
Path,
} from '@wordpress/components';
import { Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import { compose } from '@wordpress/compose';
import {
BlockControls,
InspectorControls,
MediaPlaceholder,
MediaUpload,
MediaUploadCheck,
AlignmentToolbar,
PanelColorSettings,
RichText,
withColors,
getColorClassName,
} from '@wordpress/editor';
const blockAttributes = {
title: {
type: 'string',
source: 'html',
selector: 'p',
},
url: {
type: 'string',
},
contentAlign: {
type: 'string',
default: 'center',
},
id: {
type: 'number',
},
hasParallax: {
type: 'boolean',
default: false,
},
dimRatio: {
type: 'number',
default: 50,
},
overlayColor: {
type: 'string',
},
customOverlayColor: {
type: 'string',
},
backgroundType: {
type: 'string',
default: 'image',
},
focalPoint: {
type: 'object',
},
dimensions: {
type: 'object',
},
};
export const name = 'core/cover';
const ALLOWED_MEDIA_TYPES = [ 'image', 'video' ];
const IMAGE_BACKGROUND_TYPE = 'image';
const VIDEO_BACKGROUND_TYPE = 'video';
export const settings = {
title: __( 'Cover' ),
description: __( 'Add an image or video with a text overlay — great for headers.' ),
icon: <SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><Path d="M4 4h7V2H4c-1.1 0-2 .9-2 2v7h2V4zm6 9l-4 5h12l-3-4-2.03 2.71L10 13zm7-4.5c0-.83-.67-1.5-1.5-1.5S14 7.67 14 8.5s.67 1.5 1.5 1.5S17 9.33 17 8.5zM20 2h-7v2h7v7h2V4c0-1.1-.9-2-2-2zm0 18h-7v2h7c1.1 0 2-.9 2-2v-7h-2v7zM4 13H2v7c0 1.1.9 2 2 2h7v-2H4v-7z" /><Path d="M0 0h24v24H0z" fill="none" /></SVG>,
category: 'common',
attributes: blockAttributes,
supports: {
align: true,
},
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { content } ) => (
createBlock( 'core/cover', { title: content } )
),
},
{
type: 'block',
blocks: [ 'core/image' ],
transform: ( { caption, url, align, id } ) => (
createBlock( 'core/cover', {
title: caption,
url,
align,
id,
} )
),
},
{
type: 'block',
blocks: [ 'core/video' ],
transform: ( { caption, src, align, id } ) => (
createBlock( 'core/cover', {
title: caption,
url: src,
align,
id,
backgroundType: VIDEO_BACKGROUND_TYPE,
} )
),
},
],
to: [
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { title } ) => (
createBlock( 'core/heading', { content: title } )
),
},
{
type: 'block',
blocks: [ 'core/image' ],
isMatch: ( { backgroundType, url } ) => {
return ! url || backgroundType === IMAGE_BACKGROUND_TYPE;
},
transform: ( { title, url, align, id } ) => (
createBlock( 'core/image', {
caption: title,
url,
align,
id,
} )
),
},
{
type: 'block',
blocks: [ 'core/video' ],
isMatch: ( { backgroundType, url } ) => {
return ! url || backgroundType === VIDEO_BACKGROUND_TYPE;
},
transform: ( { title, url, align, id } ) => (
createBlock( 'core/video', {
caption: title,
src: url,
id,
align,
} )
),
},
],
},
edit: compose( [
withColors( { overlayColor: 'background-color' } ),
withNotices,
] )(
( { attributes, setAttributes, isSelected, className, noticeOperations, noticeUI, overlayColor, setOverlayColor } ) => {
const {
backgroundType,
contentAlign,
dimRatio,
dimensions,
focalPoint,
hasParallax,
id,
title,
url,
} = attributes;
const onSelectMedia = ( media ) => {
if ( ! media || ! media.url ) {
setAttributes( { url: undefined, id: undefined } );
return;
}
let mediaType;
// for media selections originated from a file upload.
if ( media.media_type ) {
if ( media.media_type === IMAGE_BACKGROUND_TYPE ) {
mediaType = IMAGE_BACKGROUND_TYPE;
} else {
// only images and videos are accepted so if the media_type is not an image we can assume it is a video.
// Videos contain the media type of 'file' in the object returned from the rest api.
mediaType = VIDEO_BACKGROUND_TYPE;
}
} else { // for media selections originated from existing files in the media library.
if (
media.type !== IMAGE_BACKGROUND_TYPE &&
media.type !== VIDEO_BACKGROUND_TYPE
) {
return;
}
mediaType = media.type;
}
setAttributes( {
url: media.url,
id: media.id,
backgroundType: mediaType,
dimensions: {
height: media.height,
width: media.width,
},
} );
};
const toggleParallax = () => setAttributes( { hasParallax: ! hasParallax } );
const setDimRatio = ( ratio ) => setAttributes( { dimRatio: ratio } );
const setTitle = ( newTitle ) => setAttributes( { title: newTitle } );
const style = {
...(
backgroundType === IMAGE_BACKGROUND_TYPE ?
backgroundImageStyles( url ) :
{}
),
backgroundColor: overlayColor.color,
};
if ( focalPoint ) {
style.backgroundPosition = `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`;
}
const controls = (
<Fragment>
<BlockControls>
{ !! url && (
<Fragment>
<AlignmentToolbar
value={ contentAlign }
onChange={ ( nextAlign ) => {
setAttributes( { contentAlign: nextAlign } );
} }
/>
<MediaUploadCheck>
<Toolbar>
<MediaUpload
onSelect={ onSelectMedia }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit media' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
</MediaUploadCheck>
</Fragment>
) }
</BlockControls>
{ !! url && (
<InspectorControls>
<PanelBody title={ __( 'Cover Settings' ) }>
{ IMAGE_BACKGROUND_TYPE === backgroundType && (
<ToggleControl
label={ __( 'Fixed Background' ) }
checked={ hasParallax }
onChange={ toggleParallax }
/>
) }
{ IMAGE_BACKGROUND_TYPE === backgroundType && ! hasParallax && (
<FocalPointPicker
label={ __( 'Focal Point Picker' ) }
url={ url }
dimensions={ dimensions }
value={ focalPoint }
onChange={ ( value ) => setAttributes( { focalPoint: value } ) }
/>
) }
<PanelColorSettings
title={ __( 'Overlay' ) }
initialOpen={ true }
colorSettings={ [ {
value: overlayColor.color,
onChange: setOverlayColor,
label: __( 'Overlay Color' ),
} ] }
>
<RangeControl
label={ __( 'Background Opacity' ) }
value={ dimRatio }
onChange={ setDimRatio }
min={ 0 }
max={ 100 }
step={ 10 }
/>
</PanelColorSettings>
</PanelBody>
</InspectorControls>
) }
</Fragment>
);
if ( ! url ) {
const hasTitle = ! RichText.isEmpty( title );
const icon = hasTitle ? undefined : 'format-image';
const label = hasTitle ? (
<RichText
tagName="h2"
value={ title }
onChange={ setTitle }
inlineToolbar
/>
) : __( 'Cover' );
return (
<Fragment>
{ controls }
<MediaPlaceholder
icon={ icon }
className={ className }
labels={ {
title: label,
instructions: __( 'Drag an image or a video, upload a new one or select a file from your library.' ),
} }
onSelect={ onSelectMedia }
accept="image/*,video/*"
allowedTypes={ ALLOWED_MEDIA_TYPES }
notices={ noticeUI }
onError={ noticeOperations.createErrorNotice }
/>
</Fragment>
);
}
const classes = classnames(
className,
contentAlign !== 'center' && `has-${ contentAlign }-content`,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
}
);
return (
<Fragment>
{ controls }
<div
data-url={ url }
style={ style }
className={ classes }
>
{ VIDEO_BACKGROUND_TYPE === backgroundType && (
<video
className="wp-block-cover__video-background"
autoPlay
muted
loop
src={ url }
/>
) }
{ ( ! RichText.isEmpty( title ) || isSelected ) && (
<RichText
tagName="p"
className="wp-block-cover-text"
placeholder={ __( 'Write title…' ) }
value={ title }
onChange={ setTitle }
inlineToolbar
/>
) }
</div>
</Fragment>
);
}
),
save( { attributes } ) {
const {
backgroundType,
contentAlign,
customOverlayColor,
dimRatio,
focalPoint,
hasParallax,
overlayColor,
title,
url,
} = attributes;
const overlayColorClass = getColorClassName( 'background-color', overlayColor );
const style = backgroundType === IMAGE_BACKGROUND_TYPE ?
backgroundImageStyles( url ) :
{};
if ( ! overlayColorClass ) {
style.backgroundColor = customOverlayColor;
}
if ( focalPoint && ! hasParallax ) {
style.backgroundPosition = `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`;
}
const classes = classnames(
dimRatioToClass( dimRatio ),
overlayColorClass,
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
[ `has-${ contentAlign }-content` ]: contentAlign !== 'center',
},
);
return (
<div className={ classes } style={ style }>
{ VIDEO_BACKGROUND_TYPE === backgroundType && url && ( <video
className="wp-block-cover__video-background"
autoPlay
muted
loop
src={ url }
/> ) }
{ ! RichText.isEmpty( title ) && (
<RichText.Content tagName="p" className="wp-block-cover-text" value={ title } />
) }
</div>
);
},
deprecated: [ {
attributes: {
...blockAttributes,
align: {
type: 'string',
},
},
supports: {
className: false,
},
save( { attributes } ) {
const { url, title, hasParallax, dimRatio, align, contentAlign, overlayColor, customOverlayColor } = attributes;
const overlayColorClass = getColorClassName( 'background-color', overlayColor );
const style = backgroundImageStyles( url );
if ( ! overlayColorClass ) {
style.backgroundColor = customOverlayColor;
}
const classes = classnames(
'wp-block-cover-image',
dimRatioToClass( dimRatio ),
overlayColorClass,
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
[ `has-${ contentAlign }-content` ]: contentAlign !== 'center',
},
align ? `align${ align }` : null,
);
return (
<div className={ classes } style={ style }>
{ ! RichText.isEmpty( title ) && (
<RichText.Content tagName="p" className="wp-block-cover-image-text" value={ title } />
) }
</div>
);
},
}, {
attributes: {
...blockAttributes,
align: {
type: 'string',
},
title: {
type: 'string',
source: 'html',
selector: 'h2',
},
},
save( { attributes } ) {
const { url, title, hasParallax, dimRatio, align } = attributes;
const style = backgroundImageStyles( url );
const classes = classnames(
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
},
align ? `align${ align }` : null,
);
return (
<section className={ classes } style={ style }>
<RichText.Content tagName="h2" value={ title } />
</section>
);
},
} ],
};
function dimRatioToClass( ratio ) {
return ( ratio === 0 || ratio === 50 ) ?
null :
'has-background-dim-' + ( 10 * Math.round( ratio / 10 ) );
}
function backgroundImageStyles( url ) {
return url ?
{ backgroundImage: `url(${ url })` } :
{};
}