PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.7.2
GenerateBlocks v1.7.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 / container / block-controls.js
generateblocks / src / blocks / container Last commit date
components 3 years ago css 3 years ago variations 3 years ago attributes.js 3 years ago block-controls.js 3 years ago block.js 3 years ago deprecated.js 5 years ago edit.js 3 years ago editor.scss 3 years ago migrate-sizing.js 3 years ago
block-controls.js
152 lines
1 import getIcon from '../../utils/get-icon';
2 import useInnerBlocksCount from '../../hooks/useInnerBlocksCount';
3
4 /**
5 * WordPress Dependencies
6 */
7 import { __ } from '@wordpress/i18n';
8 import { addFilter } from '@wordpress/hooks';
9 import { Fragment } from '@wordpress/element';
10 import { BlockControls, BlockAlignmentToolbar } from '@wordpress/block-editor';
11 import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
12 import { createHigherOrderComponent } from '@wordpress/compose';
13 import { cloneBlock, getBlockSupport, createBlock } from '@wordpress/blocks';
14 import { useDispatch, useSelect } from '@wordpress/data';
15 import InsertInnerContainerOnboard from '../../components/onboard-popover/onboards/insert-inner-container-onboard';
16
17 const WIDE_ALIGNMENTS = [ 'wide', 'full' ];
18
19 /**
20 * Add controls to the Container block toolbar.
21 *
22 * @param {Function} BlockEdit Block edit component.
23 * @return {Function} BlockEdit Modified block edit component.
24 */
25 const withBlockControls = createHigherOrderComponent(
26 ( BlockEdit ) => ( props ) => {
27 if ( 'generateblocks/container' !== props.name ) {
28 return <BlockEdit { ...props } />;
29 }
30
31 const { insertBlocks } = useDispatch( 'core/block-editor' );
32 const {
33 getBlockParentsByBlockName,
34 getBlockRootClientId,
35 getBlocksByClientId,
36 } = useSelect( ( select ) => select( 'core/block-editor' ), [] );
37
38 const {
39 attributes,
40 clientId,
41 setAttributes,
42 } = props;
43
44 const {
45 isGrid,
46 isQueryLoopItem,
47 align,
48 variantRole,
49 useInnerContainer,
50 } = attributes;
51
52 let parentGridId = false;
53
54 if ( typeof getBlockParentsByBlockName === 'function' ) {
55 parentGridId = getBlockParentsByBlockName( clientId, 'generateblocks/grid', true )[ 0 ];
56 } else {
57 parentGridId = getBlockRootClientId( clientId );
58 }
59
60 const hasParentBlock = getBlockRootClientId( clientId );
61 const innerBlocksCount = useInnerBlocksCount( clientId );
62
63 /**
64 * We don't define "align" support in block registration as we don't want it enabled for grid items.
65 * This allows us to enable support for regular non-grid item Containers.
66 */
67 const hasAlignmentSupport = getBlockSupport( '', 'align', true ) && ! isGrid;
68
69 return (
70 <Fragment>
71 { ! hasParentBlock && 0 === innerBlocksCount && '' === variantRole && ! useInnerContainer &&
72 <BlockControls>
73 <ToolbarGroup>
74 <ToolbarButton
75 icon={ getIcon( 'section' ) }
76 label={ __( 'Insert Inner Container', 'generateblocks' ) }
77 onClick={ () => {
78 insertBlocks(
79 createBlock( 'generateblocks/container', {
80 useGlobalMaxWidth: true,
81 marginLeft: 'auto',
82 marginRight: 'auto',
83 } ),
84 undefined,
85 clientId
86 );
87 } }
88 showTooltip
89 />
90 <InsertInnerContainerOnboard />
91 </ToolbarGroup>
92 </BlockControls>
93 }
94
95 { ! isQueryLoopItem && isGrid && parentGridId &&
96 <BlockControls>
97 <ToolbarGroup>
98 <ToolbarButton
99 className="gblocks-block-control-icon gblocks-add-grid-item"
100 icon={ getIcon( 'addContainer' ) }
101 label={ __( 'Duplicate Grid Item', 'generateblocks' ) }
102 onClick={ () => {
103 const thisBlock = getBlocksByClientId( clientId )[ 0 ];
104
105 const clonedBlock = cloneBlock(
106 thisBlock,
107 {
108 uniqueId: '',
109 }
110 );
111
112 insertBlocks( clonedBlock, undefined, parentGridId );
113 } }
114 showTooltip
115 />
116 </ToolbarGroup>
117 </BlockControls>
118 }
119
120 { hasAlignmentSupport &&
121 <BlockControls>
122 <BlockAlignmentToolbar
123 value={ align }
124 onChange={ ( value ) => {
125 setAttributes( {
126 align: value,
127 } );
128
129 if ( 'full' === value ) {
130 setAttributes( {
131 outerContainer: 'full',
132 } );
133 }
134 } }
135 controls={ WIDE_ALIGNMENTS }
136 />
137 </BlockControls>
138 }
139
140 <BlockEdit { ...props } />
141 </Fragment>
142 );
143 },
144 'withBlockControls'
145 );
146
147 addFilter(
148 'editor.BlockEdit',
149 'generateblocks/container-block-controls',
150 withBlockControls
151 );
152