PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.3.2
GenerateBlocks v1.3.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
css 5 years ago attributes.js 5 years ago block-controls.js 5 years ago block.js 5 years ago deprecated.js 5 years ago edit.js 5 years ago editor.scss 5 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 } from '@wordpress/blocks';
35
36 const hasWideAlignSupport = generateBlocksInfo.hasWideAlignSupport;
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 *
44 * @return {Function} BlockEdit Modified block edit component.
45 */
46 const withAdvancedControls = createHigherOrderComponent( ( BlockEdit ) => {
47 return ( props ) => {
48 const {
49 name,
50 attributes,
51 isSelected,
52 clientId,
53 setAttributes,
54 } = props;
55
56 const {
57 isGrid,
58 align,
59 } = attributes;
60
61 let parentGridId = false;
62
63 if ( typeof wp.data.select( 'core/block-editor' ).getBlockParentsByBlockName === 'function' ) {
64 parentGridId = wp.data.select( 'core/block-editor' ).getBlockParentsByBlockName( clientId, 'generateblocks/grid', true )[ 0 ];
65 } else {
66 parentGridId = wp.data.select( 'core/block-editor' ).getBlockRootClientId( clientId );
67 }
68
69 return (
70 <Fragment>
71 { isSelected && isGrid && parentGridId && 'generateblocks/container' === name &&
72 <BlockControls>
73 <ToolbarGroup>
74 <ToolbarButton
75 className="gblocks-block-control-icon gblocks-add-grid-item"
76 icon={ getIcon( 'addContainer' ) }
77 label={ __( 'Duplicate Grid Item', 'generateblocks' ) }
78 onClick={ () => {
79 const thisBlock = wp.data.select( 'core/block-editor' ).getBlocksByClientId( clientId )[ 0 ];
80 const clonedBlock = cloneBlock( thisBlock );
81
82 wp.data.dispatch( 'core/block-editor' ).insertBlocks( clonedBlock, undefined, parentGridId );
83 } }
84 showTooltip
85 />
86 </ToolbarGroup>
87
88 <ToolbarGroup>
89 <ToolbarButton
90 className="gblocks-block-control-icon"
91 icon={ getIcon( 'grid' ) }
92 label={ __( 'Select Parent Grid', 'generateblocks' ) }
93 onClick={ () => {
94 wp.data.dispatch( 'core/block-editor' ).selectBlock( parentGridId );
95 } }
96 showTooltip
97 />
98 </ToolbarGroup>
99 </BlockControls>
100 }
101
102 { isSelected && hasWideAlignSupport && ! isGrid && 'generateblocks/container' === name &&
103 <BlockControls>
104 <BlockAlignmentToolbar
105 value={ align }
106 onChange={ ( value ) => {
107 setAttributes( {
108 align: value,
109 } );
110
111 if ( 'full' === value ) {
112 setAttributes( {
113 outerContainer: 'full',
114 } );
115 }
116 } }
117 controls={ WIDE_ALIGNMENTS }
118 />
119 </BlockControls>
120 }
121
122 <BlockEdit { ...props } />
123 </Fragment>
124 );
125 };
126 }, 'withAdvancedControls' );
127
128 addFilter(
129 'editor.BlockEdit',
130 'generateblocks/container-block-controls',
131 withAdvancedControls
132 );
133
134