-
Notifications
You must be signed in to change notification settings - Fork 383
/
with-wrapper-props.js
98 lines (84 loc) · 2.87 KB
/
with-wrapper-props.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
/**
* WordPress dependencies
*/
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import { ALLOWED_BLOCKS, ALLOWED_CHILD_BLOCKS, TEXT_BLOCK_BORDER } from '../constants';
import { withAttributes, withBlockName, withHasSelectedInnerBlock } from './';
import { getPercentageFromPixels } from '../helpers';
const wrapperWithSelect = compose(
withAttributes,
withBlockName,
withHasSelectedInnerBlock,
);
/**
* Adds wrapper props to the blocks.
*
* @param {Object} BlockListBlock BlockListBlock element.
* @return {Function} Enhanced component.
*/
const withWrapperProps = ( BlockListBlock ) => {
return wrapperWithSelect( ( props ) => {
const { blockName, hasSelectedInnerBlock, attributes } = props;
// If it's not an allowed block then lets return original;
if ( -1 === ALLOWED_BLOCKS.indexOf( blockName ) ) {
return <BlockListBlock { ...props } />;
}
let wrapperProps;
// If we have an inner block selected let's add 'data-amp-selected=parent' to the wrapper.
if (
hasSelectedInnerBlock &&
(
'amp/amp-story-page' === blockName
)
) {
wrapperProps = {
...props.wrapperProps,
'data-amp-selected': 'parent',
};
return <BlockListBlock { ...props } wrapperProps={ wrapperProps } />;
}
const noCaption = ( 'core/image' === blockName && ! attributes.ampShowImageCaption ) ||
( 'core/video' === blockName && ! attributes.ampShowCaption );
// If we have image caption or font-family set, add these to wrapper properties.
wrapperProps = {
...props.wrapperProps,
'data-amp-caption': noCaption ? 'noCaption' : undefined,
'data-font-family': attributes.ampFontFamily || undefined,
};
if ( ALLOWED_CHILD_BLOCKS.includes( blockName ) ) {
let style = {};
if ( 'amp/amp-story-text' === blockName ) {
const textBlockBorderInPercentageTop = getPercentageFromPixels( 'y', TEXT_BLOCK_BORDER );
const textBlockBorderInPercentageLeft = getPercentageFromPixels( 'x', TEXT_BLOCK_BORDER );
style = {
top: `${ parseFloat( attributes.positionTop ) - textBlockBorderInPercentageTop }%`,
left: `${ parseFloat( attributes.positionLeft ) - textBlockBorderInPercentageLeft }%`,
};
} else {
style = {
top: `${ attributes.positionTop }%`,
left: `${ attributes.positionLeft }%`,
};
}
style.transform = `scale(var(--preview-scale)) translateX(var(--preview-translateX)) translateY(var(--preview-translateY)) rotate(${ attributes.rotationAngle || 0 }deg)`;
if ( 'amp/amp-story-cta' === blockName ) {
style.transform = `scale(var(--preview-scale))`;
}
if ( props.wrapperProps && props.wrapperProps.style ) {
style = {
...style,
...props.wrapperProps.style,
};
}
wrapperProps = {
...wrapperProps,
style,
};
}
return <BlockListBlock { ...props } wrapperProps={ wrapperProps } enableAnimation={ false } />;
} );
};
export default withWrapperProps;