-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathindex.js
619 lines (556 loc) · 15.3 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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { ReactElement } from 'react';
import { isFunction, isObject, isString } from 'lodash';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { SelectControl, ToggleControl, Notice, PanelBody } from '@wordpress/components';
import { InspectorControls } from '@wordpress/block-editor';
import { select } from '@wordpress/data';
import { cloneElement, isValidElement } from '@wordpress/element';
/**
* Internal dependencies
*/
import { TEXT_BLOCKS, MEDIA_BLOCKS, DEFAULT_HEIGHT, DEFAULT_WIDTH } from '../constants';
import { MIN_FONT_SIZE, MAX_FONT_SIZE } from '../../common/constants';
const ampLayoutOptions = [
{
value: 'nodisplay',
label: __( 'No Display', 'amp' ),
notAvailable: [
'core-embed/vimeo',
'core-embed/dailymotion',
'core-embed/reddit',
'core-embed/soundcloud',
],
},
{
// Not supported by amp-audio and amp-pixel.
value: 'fixed',
label: __( 'Fixed', 'amp' ),
notAvailable: [
'core-embed/soundcloud',
],
},
{
// To ensure your AMP element displays, you must specify a width and height for the containing element.
value: 'responsive',
label: __( 'Responsive', 'amp' ),
notAvailable: [
'core-embed/soundcloud',
],
},
{
value: 'fixed-height',
label: __( 'Fixed Height', 'amp' ),
notAvailable: [],
},
{
value: 'fill',
label: __( 'Fill', 'amp' ),
notAvailable: [
'core-embed/soundcloud',
],
},
{
value: 'flex-item',
label: __( 'Flex Item', 'amp' ),
notAvailable: [
'core-embed/soundcloud',
],
},
{
value: 'intrinsic',
label: __( 'Intrinsic', 'amp' ),
notAvailable: [
'core-embed/youtube',
'core-embed/facebook',
'core-embed/instagram',
'core-embed/vimeo',
'core-embed/dailymotion',
'core-embed/reddit',
'core-embed/soundcloud',
],
},
];
/**
* Add AMP attributes to every core block.
*
* @param {Object} settings Block settings.
* @param {string} name Block name.
* @return {Object} Modified block settings.
*/
export const addAMPAttributes = ( settings, name ) => {
if ( ! isObject( settings ) || ! isString( name ) ) {
return settings;
}
// AMP Carousel settings.
if ( 'core/gallery' === name ) {
if ( ! settings.attributes ) {
settings.attributes = {};
}
settings.attributes.ampCarousel = {
type: 'boolean',
default: ! select( 'amp/block-editor' )?.hasThemeSupport(), // @todo We could just default this to false now even in Reader mode since block styles are loaded.
};
settings.attributes.ampLightbox = {
type: 'boolean',
default: false,
};
}
// Add AMP Lightbox settings.
if ( 'core/image' === name ) {
if ( ! settings.attributes ) {
settings.attributes = {};
}
settings.attributes.ampLightbox = {
type: 'boolean',
default: false,
};
}
// Layout settings for embeds and media blocks.
if ( 0 === name.indexOf( 'core-embed' ) || MEDIA_BLOCKS.includes( name ) ) {
if ( ! settings.attributes ) {
settings.attributes = {};
}
settings.attributes.ampLayout = {
type: 'string',
};
settings.attributes.ampNoLoading = {
type: 'boolean',
};
}
return settings;
};
/**
* Removes `amp-fit-text` related attributes on blocks via block deprecation.
*
* @param {Object} settings Block settings.
* @param {string} name Block name.
* @return {Object} Modified block settings.
*/
export const removeAmpFitTextFromBlocks = ( settings, name ) => {
if ( ! isObject( settings ) || ! isString( name ) ) {
return settings;
}
if ( TEXT_BLOCKS.includes( name ) ) {
if ( ! settings.deprecated ) {
settings.deprecated = [];
}
settings.deprecated.unshift( {
supports: settings.supports,
attributes: {
...( settings.attributes || {} ),
ampFitText: {
type: 'boolean',
default: false,
},
minFont: {
default: MIN_FONT_SIZE,
source: 'attribute',
selector: 'amp-fit-text',
attribute: 'min-font-size',
},
maxFont: {
default: MAX_FONT_SIZE,
source: 'attribute',
selector: 'amp-fit-text',
attribute: 'max-font-size',
},
height: {
// Needs to be higher than the maximum font size, which defaults to MAX_FONT_SIZE
default: 'core/image' === name ? 200 : Math.ceil( MAX_FONT_SIZE / 10 ) * 10,
source: 'attribute',
selector: 'amp-fit-text',
attribute: 'height',
},
},
save( props ) {
/* eslint-disable react/prop-types */
const { attributes } = props;
const fitTextProps = { layout: 'fixed-height' };
if ( attributes.minFont ) {
fitTextProps[ 'min-font-size' ] = attributes.minFont;
}
if ( attributes.maxFont ) {
fitTextProps[ 'max-font-size' ] = attributes.maxFont;
}
if ( attributes.height ) {
fitTextProps.height = attributes.height;
}
/* eslint-enable react/prop-types */
fitTextProps.children = settings.save( props );
return <amp-fit-text { ...fitTextProps } />;
},
isEligible( { ampFitText } ) {
return undefined !== ampFitText;
},
migrate( attributes ) {
const deprecatedAttrs = [ 'ampFitText', 'minFont', 'maxFont', 'height' ];
deprecatedAttrs.forEach( ( attr ) => delete attributes[ attr ] );
return attributes;
},
} );
}
return settings;
};
/**
* Remove the `class` attribute from `amp-fit-text` elements so that it can be deprecated successfully.
*
* The `class` attribute is added by the `core/generated-class-name/save-props` block editor filter; it is unwanted and
* interferes with successful deprecation of the block. By filtering the saved element the `class` attribute can be
* removed and the deprecation of the block and proceed without error.
*
* @see removeAmpFitTextFromBlocks
* @param {ReactElement} element Block save result.
* @return {ReactElement} Modified block if it is of `amp-fit-text` type, otherwise the original element is returned.
*/
export const removeClassFromAmpFitTextBlocks = ( element ) => {
if ( isValidElement( element ) && 'amp-fit-text' === element.type && undefined !== element.props.className ) {
const { className, ...props } = element.props;
props.className = null;
element = cloneElement( element, props );
}
return element;
};
/**
* Get layout options depending on the block.
*
* @param {string} block Block name.
* @return {Object[]} Options.
*/
export const getLayoutOptions = ( block ) => {
const layoutOptions = [
{
value: '',
label: __( 'Default', 'amp' ),
},
];
for ( const option of ampLayoutOptions ) {
const isLayoutAvailable = ! option.notAvailable.includes( block );
if ( isLayoutAvailable ) {
layoutOptions.push( {
value: option.value,
label: option.label,
} );
}
}
return layoutOptions;
};
/**
* Filters blocks edit function of all blocks.
*
* @param {Function} BlockEdit function.
* @return {Function} Edit function.
*/
export const filterBlocksEdit = ( BlockEdit ) => {
if ( ! isFunction( BlockEdit ) ) {
return BlockEdit;
}
const EnhancedBlockEdit = function( props ) {
const { attributes: { ampLayout }, name } = props;
let inspectorControls;
if ( 'core/gallery' === name ) {
inspectorControls = setUpGalleryInspectorControls( props );
} else if ( 'core/image' === name ) {
inspectorControls = setUpImageInspectorControls( props );
} else if ( MEDIA_BLOCKS.includes( name ) || 0 === name.indexOf( 'core-embed/' ) ) {
inspectorControls = setUpInspectorControls( props );
}
// Return just inspector controls in case of 'nodisplay'.
if ( ampLayout && 'nodisplay' === ampLayout ) {
return [
inspectorControls,
];
}
return (
<>
<BlockEdit { ...props } />
{ inspectorControls }
</>
);
};
EnhancedBlockEdit.propTypes = {
attributes: PropTypes.shape( {
text: PropTypes.string,
ampLayout: PropTypes.string,
} ),
setAttributes: PropTypes.func.isRequired,
name: PropTypes.string,
};
return EnhancedBlockEdit;
};
/**
* Set width and height in case of image block.
*
* @param {Object} props Props.
* @param {Function} props.setAttributes Callback to set attributes.
* @param {Object} props.attributes Attributes.
* @param {string} layout Layout.
*/
export const setImageBlockLayoutAttributes = ( props, layout ) => {
const { attributes, setAttributes } = props;
switch ( layout ) {
case 'fixed-height':
if ( ! attributes.height ) {
setAttributes( { height: DEFAULT_HEIGHT } );
}
// Lightbox doesn't work with fixed height, so unset it.
if ( attributes.ampLightbox ) {
setAttributes( { ampLightbox: false } );
}
break;
case 'fixed':
if ( ! attributes.height ) {
setAttributes( { height: DEFAULT_HEIGHT } );
}
if ( ! attributes.width ) {
setAttributes( { width: DEFAULT_WIDTH } );
}
break;
default:
break;
}
};
/**
* Default setup for inspector controls.
*
* @param {Object} props Props.
* @return {ReactElement} Inspector Controls.
*/
export const setUpInspectorControls = ( props ) => {
const { isSelected } = props;
if ( ! isSelected ) {
return null;
}
return (
<InspectorControls>
<PanelBody title={ __( 'AMP Settings', 'amp' ) }>
<AmpLayoutControl { ...props } />
<AmpNoloadingToggle { ...props } />
</PanelBody>
</InspectorControls>
);
};
setUpInspectorControls.propTypes = {
isSelected: PropTypes.bool,
};
/**
* Get AMP Layout select control.
*
* @deprecated As of v2.1. Blocks with the `ampLayout` attribute will still be able to use the control.
* @param {Object} props Props.
* @return {ReactElement} Element.
*/
export const AmpLayoutControl = ( props ) => {
const { name, attributes: { ampLayout }, setAttributes } = props;
if ( undefined === ampLayout ) {
return null;
}
let label = __( 'AMP Layout', 'amp' );
if ( 'core/image' === name ) {
label = __( 'AMP Layout (modifies width/height)', 'amp' );
}
return (
<>
<Notice
status="warning"
isDismissible={ false }
>
<span dangerouslySetInnerHTML={ {
__html: sprintf(
/* translators: placeholder is link to support forum. */
__( 'The AMP Layout setting is deprecated and is slated for removal. Please <a href="%s" target="_blank" rel="noreferrer">report</a> if you need it.', 'amp' ),
'https://wordpress.org/support/plugin/amp/#new-topic-0',
),
} } />
</Notice>
<SelectControl
label={ label }
value={ ampLayout }
options={ getLayoutOptions( name ) }
onChange={ ( value ) => {
setAttributes( { ampLayout: value } );
if ( 'core/image' === props.name ) {
setImageBlockLayoutAttributes( props, value );
}
} }
/>
</>
);
};
AmpLayoutControl.propTypes = {
name: PropTypes.string,
attributes: PropTypes.shape( {
ampLayout: PropTypes.string,
} ),
setAttributes: PropTypes.func.isRequired,
};
/**
* Get AMP Noloading toggle control.
*
* @deprecated As of v2.1. Blocks with the `ampNoLoading` attribute will still be able to use the control.
* @param {Object} props Props.
* @return {ReactElement} Element.
*/
export const AmpNoloadingToggle = ( props ) => {
const { attributes: { ampNoLoading }, setAttributes } = props;
if ( undefined === ampNoLoading ) {
return null;
}
const label = __( 'AMP Noloading', 'amp' );
return (
<>
<Notice
status="warning"
isDismissible={ false }
>
<span dangerouslySetInnerHTML={ {
__html: sprintf(
/* translators: placeholder is link to support forum. */
__( 'The AMP Noloading setting is deprecated and is slated for removal. Please <a href="%s" target="_blank" rel="noreferrer">report</a> if you need it.', 'amp' ),
'https://wordpress.org/support/plugin/amp/#new-topic-0',
),
} } />
</Notice>
<ToggleControl
label={ label }
checked={ ampNoLoading }
onChange={ () => setAttributes( { ampNoLoading: ! ampNoLoading } ) }
/>
</>
);
};
AmpNoloadingToggle.propTypes = {
attributes: PropTypes.shape( {
ampNoLoading: PropTypes.bool,
} ),
setAttributes: PropTypes.func.isRequired,
};
/**
* Get AMP Lightbox toggle control.
*
* @param {Object} props Props.
* @return {ReactElement} Element.
*/
const AmpLightboxToggle = ( props ) => {
const { attributes: { ampLightbox, linkTo, ampLayout }, setAttributes } = props;
return (
<ToggleControl
label={ __( 'Add lightbox effect', 'amp' ) }
checked={ ampLightbox }
onChange={ ( nextValue ) => {
setAttributes( { ampLightbox: ! ampLightbox } );
if ( nextValue ) {
// Lightbox doesn't work with fixed height, so change.
if ( 'fixed-height' === ampLayout ) {
setAttributes( { ampLayout: 'fixed' } );
}
// In case of lightbox set linking images to 'none'.
if ( linkTo && 'none' !== linkTo ) {
setAttributes( { linkTo: 'none' } );
}
}
} }
/>
);
};
AmpLightboxToggle.propTypes = {
attributes: PropTypes.shape( {
ampLightbox: PropTypes.bool,
ampLayout: PropTypes.string,
linkTo: PropTypes.string,
} ),
setAttributes: PropTypes.func.isRequired,
};
/**
* Get AMP Carousel toggle control.
*
* @param {Object} props Props.
* @param {Object} props.attributes Block attributes.
* @param {Object} props.attributes.ampCarousel AMP Carousel toggle value.
* @param {Function} props.setAttributes Callback to update attributes.
* @return {Object} Element.
*/
const AmpCarouselToggle = ( props ) => {
const { attributes: { ampCarousel }, setAttributes } = props;
return (
<ToggleControl
label={ __( 'Display as carousel', 'amp' ) }
checked={ ampCarousel }
onChange={ () => setAttributes( { ampCarousel: ! ampCarousel } ) }
/>
);
};
AmpCarouselToggle.propTypes = {
attributes: PropTypes.shape( {
ampCarousel: PropTypes.bool,
} ),
setAttributes: PropTypes.func.isRequired,
};
/**
* Set up inspector controls for Image block.
*
* @param {Object} props Props.
* @param {boolean} props.isSelected Whether the current block has been selected or not.
* @return {Object} Inspector Controls.
*/
const setUpImageInspectorControls = ( props ) => {
const { isSelected } = props;
if ( ! isSelected ) {
return null;
}
return (
<InspectorControls>
<PanelBody title={ __( 'AMP Settings', 'amp' ) }>
<AmpLayoutControl { ...props } />
<AmpNoloadingToggle { ...props } />
<AmpLightboxToggle { ...props } />
</PanelBody>
</InspectorControls>
);
};
setUpImageInspectorControls.propTypes = {
isSelected: PropTypes.bool,
};
/**
* Set up inspector controls for Gallery block.
* Adds ampCarousel attribute for displaying the output as amp-carousel.
*
* @param {Object} props Props.
* @param {boolean} props.isSelected Whether the current block has been selected or not.
* @return {Object} Inspector controls.
*/
const setUpGalleryInspectorControls = ( props ) => {
const { isSelected } = props;
if ( ! isSelected ) {
return null;
}
return (
<InspectorControls>
<PanelBody title={ __( 'AMP Settings', 'amp' ) }>
<AmpCarouselToggle { ...props } />
<AmpLightboxToggle { ...props } />
</PanelBody>
</InspectorControls>
);
};
setUpGalleryInspectorControls.propTypes = {
isSelected: PropTypes.bool,
};
/**
* Determines whether AMP is enabled for the current post or not.
*
* For regular posts, this is based on the AMP toggle control and also
* the default status based on the template mode.
*
* @return {boolean} Whether AMP is enabled.
*/
export const isAMPEnabled = () => {
const { getEditedPostAttribute } = select( 'core/editor' );
return getEditedPostAttribute( 'amp_enabled' ) || false;
};