PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.2
GenerateBlocks v2.0.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / blocks / shape / components / ShapeDividerControls.jsx
generateblocks / src / blocks / shape / components Last commit date
BlockSettings.jsx 1 year ago ShapeDividerControls.jsx 1 year ago
ShapeDividerControls.jsx
127 lines
1 import { useMemo } from '@wordpress/element';
2 import { SelectControl, ToggleControl } from '@wordpress/components';
3 import { __ } from '@wordpress/i18n';
4
5 import { useBlockStyles } from '@hooks/useBlockStyles';
6
7 function splitSpaces( value ) {
8 return value.split( /\s+(?![^()]*\))/ );
9 }
10
11 export function ShapeDividerControls( { getStyleValue, onStyleChange } ) {
12 const {
13 currentAtRule,
14 } = useBlockStyles();
15 const currentPosition = getStyleValue( 'position', currentAtRule );
16 const currentTopValue = getStyleValue( 'top', currentAtRule );
17 const currentBottomValue = getStyleValue( 'bottom', currentAtRule );
18 const currentTransformsValue = getStyleValue( 'transform', currentAtRule );
19 const locationValue = useMemo( () => {
20 if ( 'absolute' !== currentPosition ) {
21 return '';
22 }
23
24 if ( '0' === currentTopValue && '' === currentBottomValue ) {
25 return 'top';
26 }
27
28 if ( '0' === currentBottomValue && '' === currentTopValue ) {
29 return 'bottom';
30 }
31
32 return '';
33 }, [ currentPosition, currentTopValue, currentBottomValue ] );
34 const flippedHorizontallyValue = useMemo( () => {
35 if ( ! currentTransformsValue ) {
36 return false;
37 }
38
39 const existingTransforms = currentTransformsValue ? splitSpaces( currentTransformsValue ) : [];
40 const scaleValue = existingTransforms.find( ( transform ) => transform.startsWith( 'scale' ) ) ?? '';
41
42 if ( 'top' === locationValue ) {
43 return scaleValue.includes( '-1, -1' );
44 }
45
46 return scaleValue.includes( '-1, 1' );
47 }, [ currentTransformsValue, locationValue ] );
48
49 if ( ! locationValue ) {
50 return null;
51 }
52
53 return (
54 <>
55 <SelectControl
56 label={ __( 'Location', 'generateblocks' ) }
57 value={ locationValue }
58 options={ [
59 { label: __( 'Top', 'generateblocks' ), value: 'top' },
60 { label: __( 'Bottom', 'generateblocks' ), value: 'bottom' },
61 ] }
62 onChange={ ( value ) => {
63 const newValues = {};
64 const existingTransformsValue = getStyleValue( 'transform', currentAtRule );
65 const existingTransforms = existingTransformsValue ? splitSpaces( existingTransformsValue ) : [];
66 const scaleValue = existingTransforms.find( ( transform ) => transform.startsWith( 'scale' ) );
67 let newScaleValue = 'top' === value ? 'scale(1, -1)' : '';
68
69 if ( flippedHorizontallyValue ) {
70 if ( 'top' === value ) {
71 newScaleValue = 'scale(-1, -1)';
72 } else {
73 newScaleValue = 'scale(-1, 1)';
74 }
75 }
76
77 if ( 'top' === value ) {
78 newValues.top = '0';
79 newValues.bottom = '';
80 } else {
81 newValues.top = '';
82 newValues.bottom = '0';
83 }
84
85 if ( scaleValue ) {
86 newValues.transform = existingTransformsValue.replace( scaleValue, newScaleValue ).trim();
87 } else {
88 newValues.transform = existingTransformsValue ? `${ existingTransformsValue } ${ newScaleValue }` : newScaleValue;
89 newValues.transform = newValues.transform.trim();
90 }
91
92 onStyleChange( newValues, currentAtRule );
93 } }
94 />
95
96 <ToggleControl
97 label={ __( 'Flip Horizontal', 'generateblocks' ) }
98 checked={ flippedHorizontallyValue }
99 onChange={ ( value ) => {
100 const newValues = {};
101 const existingTransformsValue = getStyleValue( 'transform', currentAtRule );
102 const existingTransforms = existingTransformsValue ? splitSpaces( existingTransformsValue ) : [];
103 const scaleValue = existingTransforms.find( ( transform ) => transform.startsWith( 'scale' ) );
104 let newScaleValue = value ? 'scale(-1, 1)' : '';
105
106 if ( 'top' === locationValue ) {
107 if ( value ) {
108 newScaleValue = 'scale(-1, -1)';
109 } else {
110 newScaleValue = 'scale(1, -1)';
111 }
112 }
113
114 if ( scaleValue ) {
115 newValues.transform = existingTransformsValue.replace( scaleValue, newScaleValue ).trim();
116 } else {
117 newValues.transform = existingTransformsValue ? `${ existingTransformsValue } ${ newScaleValue }` : newScaleValue;
118 newValues.transform = newValues.transform.trim();
119 }
120
121 onStyleChange( newValues, currentAtRule );
122 } }
123 />
124 </>
125 );
126 }
127