PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.0
GenerateBlocks v1.8.0
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 2 years ago css 2 years ago attributes.js 2 years ago block-controls.js 2 years ago block.js 2 years ago deprecated.js 3 years ago edit.js 2 years ago editor.scss 2 years ago transforms.js 2 years ago
block-controls.js
91 lines
1 import getIcon from '../../utils/get-icon';
2
3 /**
4 * WordPress Dependencies
5 */
6 import { __ } from '@wordpress/i18n';
7 import { addFilter } from '@wordpress/hooks';
8 import { Fragment } from '@wordpress/element';
9 import { BlockControls } from '@wordpress/block-editor';
10 import { ToolbarGroup, ToolbarButton } from '@wordpress/components';
11 import { createHigherOrderComponent } from '@wordpress/compose';
12 import { cloneBlock } from '@wordpress/blocks';
13 import { useDispatch, useSelect } from '@wordpress/data';
14
15 /**
16 * Add controls to the Container block toolbar.
17 *
18 * @param {Function} BlockEdit Block edit component.
19 * @return {Function} BlockEdit Modified block edit component.
20 */
21 const withBlockControls = createHigherOrderComponent(
22 ( BlockEdit ) => ( props ) => {
23 if ( 'generateblocks/container' !== props.name ) {
24 return <BlockEdit { ...props } />;
25 }
26
27 const { insertBlocks } = useDispatch( 'core/block-editor' );
28 const {
29 getBlockParentsByBlockName,
30 getBlockRootClientId,
31 getBlocksByClientId,
32 } = useSelect( ( select ) => select( 'core/block-editor' ), [] );
33
34 const {
35 attributes,
36 clientId,
37 } = props;
38
39 const {
40 isGrid,
41 isQueryLoopItem,
42 } = attributes;
43
44 let parentGridId = false;
45
46 if ( typeof getBlockParentsByBlockName === 'function' ) {
47 parentGridId = getBlockParentsByBlockName( clientId, 'generateblocks/grid', true )[ 0 ];
48 } else {
49 parentGridId = getBlockRootClientId( clientId );
50 }
51
52 return (
53 <Fragment>
54 { ! isQueryLoopItem && isGrid && parentGridId &&
55 <BlockControls>
56 <ToolbarGroup>
57 <ToolbarButton
58 className="gblocks-block-control-icon gblocks-add-grid-item"
59 icon={ getIcon( 'addContainer' ) }
60 label={ __( 'Duplicate Grid Item', 'generateblocks' ) }
61 onClick={ () => {
62 const thisBlock = getBlocksByClientId( clientId )[ 0 ];
63
64 const clonedBlock = cloneBlock(
65 thisBlock,
66 {
67 uniqueId: '',
68 }
69 );
70
71 insertBlocks( clonedBlock, undefined, parentGridId );
72 } }
73 showTooltip
74 />
75 </ToolbarGroup>
76 </BlockControls>
77 }
78
79 <BlockEdit { ...props } />
80 </Fragment>
81 );
82 },
83 'withBlockControls'
84 );
85
86 addFilter(
87 'editor.BlockEdit',
88 'generateblocks/container-block-controls',
89 withBlockControls
90 );
91