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