-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathblock-controls.js
106 lines (97 loc) · 2.85 KB
/
block-controls.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
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import {
BlockControls,
MediaReplaceFlow,
__experimentalBlockAlignmentMatrixControl as BlockAlignmentMatrixControl,
__experimentalBlockFullHeightAligmentControl as FullHeightAlignmentControl,
} from '@wordpress/block-editor';
import { __, isRTL } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { ALLOWED_MEDIA_TYPES } from '../shared';
export default function CoverBlockControls( {
attributes,
setAttributes,
onSelectMedia,
currentSettings,
toggleUseFeaturedImage,
} ) {
const { contentPosition, id, useFeaturedImage, minHeight, minHeightUnit } =
attributes;
const { hasInnerBlocks, url } = currentSettings;
const [ prevMinHeightValue, setPrevMinHeightValue ] = useState( minHeight );
const [ prevMinHeightUnit, setPrevMinHeightUnit ] =
useState( minHeightUnit );
const isMinFullHeight = minHeightUnit === 'vh' && minHeight === 100;
const toggleMinFullHeight = () => {
if ( isMinFullHeight ) {
// If there aren't previous values, take the default ones.
if ( prevMinHeightUnit === 'vh' && prevMinHeightValue === 100 ) {
return setAttributes( {
minHeight: undefined,
minHeightUnit: undefined,
} );
}
// Set the previous values of height.
return setAttributes( {
minHeight: prevMinHeightValue,
minHeightUnit: prevMinHeightUnit,
} );
}
setPrevMinHeightValue( minHeight );
setPrevMinHeightUnit( minHeightUnit );
// Set full height.
return setAttributes( {
minHeight: 100,
minHeightUnit: 'vh',
} );
};
// Flip value horizontally to match the physical direction indicated by
// AlignmentMatrixControl with the logical direction indicated by cover
// block in RTL languages.
const flipHorizontalPosition = ( ltrContentPosition ) => {
return isRTL()
? ltrContentPosition.replace( /left|right/, ( match ) =>
match === 'left' ? 'right' : 'left'
)
: ltrContentPosition;
};
return (
<>
<BlockControls group="block">
<BlockAlignmentMatrixControl
label={ __( 'Change content position' ) }
value={ flipHorizontalPosition( contentPosition ) }
onChange={ ( nextPosition ) =>
setAttributes( {
contentPosition:
flipHorizontalPosition( nextPosition ),
} )
}
isDisabled={ ! hasInnerBlocks }
/>
<FullHeightAlignmentControl
isActive={ isMinFullHeight }
onToggle={ toggleMinFullHeight }
isDisabled={ ! hasInnerBlocks }
/>
</BlockControls>
<BlockControls group="other">
<MediaReplaceFlow
mediaId={ id }
mediaURL={ url }
allowedTypes={ ALLOWED_MEDIA_TYPES }
accept="image/*,video/*"
onSelect={ onSelectMedia }
onToggleFeaturedImage={ toggleUseFeaturedImage }
useFeaturedImage={ useFeaturedImage }
name={ ! url ? __( 'Add Media' ) : __( 'Replace' ) }
/>
</BlockControls>
</>
);
}