PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.2
GenerateBlocks v1.8.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 / extend / block-controls / controls / alignment-matrix-control.js
generateblocks / src / extend / block-controls / controls Last commit date
alignment-matrix-control.js 2 years ago templates.js 2 years ago
alignment-matrix-control.js
107 lines
1 import AlignmentMatrix from '../../../components/alignment-matrix';
2 import templates from './templates';
3 import useDeviceAttributes from '../../../hooks/useDeviceAttributes';
4 import getIcon from '../../../utils/get-icon';
5 import { Button } from '@wordpress/components';
6 import { __ } from '@wordpress/i18n';
7 import { useEffect, useMemo, useState, useContext } from '@wordpress/element';
8 import { useSelect } from '@wordpress/data';
9 import getUniqueBlockNames from '../../../utils/get-unique-block-names';
10 import ControlsContext from '../../../block-context';
11
12 function AlignmentMatrixControl( { attributes, setAttributes } ) {
13 const [ activeCell, setActiveCell ] = useState( '' );
14 const [ deviceAttributes, setDeviceAttributes ] = useDeviceAttributes( attributes, setAttributes );
15 const { getBlock } = useSelect( ( select ) => select( 'core/block-editor' ), [] );
16 const { clientId } = useContext( ControlsContext );
17 const {
18 display,
19 flexDirection,
20 alignItems,
21 justifyContent,
22 } = deviceAttributes;
23
24 const directionTemplate = templates[ flexDirection || 'column' ];
25
26 const isDefault = '' === display || 'block' === display;
27 const isFlex = 'flex' === display;
28 const isRow = 'row' === flexDirection || 'row-reverse' === flexDirection;
29 const isColumn = 'column' === flexDirection || 'column-reverse' === flexDirection;
30
31 useEffect( () => {
32 if ( activeCell ) {
33 const uniqueBlockNames = getUniqueBlockNames( getBlock( clientId )?.innerBlocks );
34 const direction = 1 === uniqueBlockNames.length && 'generateblocks/button' === uniqueBlockNames[ 0 ]
35 ? 'row'
36 : 'column';
37
38 setDeviceAttributes( {
39 ...directionTemplate[ activeCell ],
40 display: 'flex',
41 flexDirection: ! flexDirection ? direction : flexDirection,
42 } );
43 }
44 }, [
45 activeCell,
46 flexDirection,
47 ] );
48
49 const realActiveCell = useMemo( () => (
50 Object
51 .entries( directionTemplate )
52 .reduce( ( realCell, [ key, cell ] ) => {
53 if ( cell.alignItems === alignItems && cell.justifyContent === justifyContent ) {
54 return key;
55 }
56
57 return realCell;
58 }, '' )
59 ), [ flexDirection, alignItems, justifyContent ] );
60
61 return (
62 <AlignmentMatrix
63 activeCell={ realActiveCell }
64 onChange={ setActiveCell }
65 direction={ isFlex && flexDirection }
66 >
67 <>
68 <Button
69 isPressed={ isDefault }
70 label={ __( 'Default', 'generateblocks' ) }
71 onClick={ () => {
72 setActiveCell( '' );
73 setDeviceAttributes( {
74 display: '',
75 flexDirection: '',
76 alignItems: '',
77 justifyContent: '',
78 } );
79 } }
80 >
81 { getIcon( 'container-default' ) }
82 </Button>
83 <Button
84 label={ __( 'Arrange blocks vertically', 'generateblocks' ) }
85 isPressed={ isFlex && isColumn }
86 onClick={ () => {
87 setDeviceAttributes( { display: 'flex', flexDirection: 'column' } );
88 } }
89 >
90 { getIcon( 'container-flex-column' ) }
91 </Button>
92 <Button
93 label={ __( 'Arrange blocks horizontally', 'generateblocks' ) }
94 onClick={ () => {
95 setDeviceAttributes( { display: 'flex', flexDirection: 'row' } );
96 } }
97 isPressed={ isFlex && isRow }
98 >
99 { getIcon( 'container-flex-row' ) }
100 </Button>
101 </>
102 </AlignmentMatrix>
103 );
104 }
105
106 export default AlignmentMatrixControl;
107