PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.1.1
GenerateBlocks v2.1.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 / editor / toolbar-appenders.js
generateblocks / src / editor Last commit date
disable-blocks.js 1 year ago editor.scss 1 year ago global-max-width.js 1 year ago index.js 1 year ago stores.js 1 year ago style-html-attribute.js 1 year ago toolbar-appenders.js 1 year ago
toolbar-appenders.js
149 lines
1 import { ToolbarButton } from '@wordpress/components';
2 import { addFilter, applyFilters } from '@wordpress/hooks';
3 import { __ } from '@wordpress/i18n';
4 import { useSelect, useDispatch } from '@wordpress/data';
5 import { createBlock } from '@wordpress/blocks';
6 import { createHigherOrderComponent } from '@wordpress/compose';
7 import { BlockControls } from '@wordpress/block-editor';
8
9 import { isEmpty } from 'lodash';
10
11 import useInnerBlocksCount from '@hooks/useInnerBlocksCount';
12
13 function AddInnerContainerIcon() {
14 return (
15 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false">
16 <path d="M22.006,22.006L20.665,22.006L20.665,17.629L22.006,17.629L22.006,22.006ZM22.006,14.814L20.665,14.814L20.665,9.185L22.006,9.185L22.006,14.814ZM22.006,6.372L20.672,6.372L20.672,3.328L17.628,3.328L17.628,1.994L21.38,1.994C21.725,1.994 22.006,2.274 22.006,2.619L22.006,6.372ZM6.371,1.994L6.371,3.331L1.994,3.331L1.994,1.994L6.371,1.994ZM14.814,3.331L9.186,3.331L9.186,1.994L14.814,1.994L14.814,3.331Z" style={ { fillOpacity: 0.5 } } />
17 <path d="M14,6.5L16.5,6.5L16.5,4L17.5,4L17.5,6.5L20,6.5L20,7.5L17.5,7.5L17.5,10L16.5,10L16.5,7.5L14,7.5L14,6.5Z" />
18 <path d="M1.993,9L7.701,9L7.701,10.268L1.993,10.268L1.993,9ZM14.993,13.439L13.725,13.439L13.725,10.268L10.554,10.268L10.554,9L14.359,9C14.709,9 14.993,9.284 14.993,9.634L14.993,13.439ZM13.725,16.292L14.993,16.292L14.993,22L13.725,22L13.725,16.292Z" style={ { fillRule: 'nonzero' } } />
19 </svg>
20 );
21 }
22
23 function AddOuterContainerIcon() {
24 return (
25 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false">
26 <path d="M21.375,22L17.625,22L17.625,20.75L20.75,20.75L20.75,17.625L22,17.625L22,21.375C22,21.72 21.72,22 21.375,22ZM9.188,20.75L14.813,20.75L14.813,22L9.188,22L9.188,20.75ZM6.375,22L2.625,22C2.282,22 2,21.718 2,21.375L2,17.625L3.25,17.625L3.25,20.75L6.375,20.75L6.375,22ZM2,9.187L3.25,9.187L3.25,14.812L2,14.812L2,9.187ZM3.25,6.375L2,6.375L2,2.625C2,2.28 2.28,2 2.625,2L6.375,2L6.375,3.25L3.25,3.25L3.25,6.375ZM9.188,2L14.813,2L14.813,3.25L9.188,3.25L9.188,2ZM22,6.375L20.75,6.375L20.75,3.25L17.625,3.25L17.625,2L21.375,2C21.72,2 22,2.28 22,2.625L22,6.375ZM20.75,9.187L22,9.187L22,14.812L20.75,14.812L20.75,9.187Z" style={ { fillRule: 'nonzero' } } />
27 <path d="M14,6.5L16.5,6.5L16.5,4L17.5,4L17.5,6.5L20,6.5L20,7.5L17.5,7.5L17.5,10L16.5,10L16.5,7.5L14,7.5L14,6.5Z" />
28 </svg>
29 );
30 }
31
32 const withToolbarAppenders = createHigherOrderComponent( ( BlockEdit ) => {
33 return ( props ) => {
34 const {
35 name,
36 } = props;
37
38 const {
39 getBlocksByClientId,
40 getSelectedBlockClientIds,
41 getBlockRootClientId,
42 } = useSelect( ( select ) => select( 'core/block-editor' ), [] );
43
44 const {
45 replaceBlocks,
46 insertBlocks,
47 } = useDispatch( 'core/block-editor' );
48
49 const clientIds = getSelectedBlockClientIds();
50 const clientId = clientIds.length ? clientIds[ 0 ] : props.clientId;
51 const innerBlocksCount = useInnerBlocksCount( clientId );
52 const blocksSelection = getBlocksByClientId( clientIds );
53 const hasParentBlock = getBlockRootClientId( clientId );
54 const useV1Blocks = generateBlocksEditor.useV1Blocks;
55
56 if ( useV1Blocks ) {
57 return <BlockEdit { ...props } />;
58 }
59
60 const onConvertToContainer = () => {
61 if ( ! blocksSelection.length ) {
62 return;
63 }
64
65 const newChildBlocks = blocksSelection.map( ( block ) => {
66 return createBlock(
67 block.name,
68 block.attributes,
69 block.innerBlocks
70 );
71 } );
72
73 const newBlocks = createBlock(
74 'generateblocks/element',
75 {},
76 newChildBlocks
77 );
78
79 if ( ! isEmpty( newBlocks ) ) {
80 replaceBlocks( clientIds, newBlocks );
81 }
82 };
83
84 let buttons = '';
85
86 if (
87 'generateblocks/element' === name &&
88 ! hasParentBlock &&
89 0 === innerBlocksCount &&
90 1 === clientIds.length
91 ) {
92 buttons = <>
93 { buttons }
94 <ToolbarButton
95 icon={ AddInnerContainerIcon }
96 label={ __( 'Add Inner Container', 'generateblocks' ) }
97 onClick={ () => {
98 insertBlocks(
99 createBlock( 'generateblocks/element', {
100 styles: {
101 maxWidth: 'var(--gb-container-width)',
102 marginLeft: 'auto',
103 marginRight: 'auto',
104 },
105 } ),
106 undefined,
107 clientId
108 );
109 } }
110 showTooltip
111 />
112 </>;
113 }
114
115 buttons = <>
116 { buttons }
117 <ToolbarButton
118 icon={ AddOuterContainerIcon }
119 label={ __( 'Add to Container', 'generateblocks' ) }
120 onClick={ () => onConvertToContainer( '' ) }
121 />
122 </>;
123
124 buttons = applyFilters(
125 'generateblocks.editor.toolbarAppenders',
126 buttons,
127 props
128 );
129
130 return (
131 <>
132 { !! buttons &&
133 <BlockControls group="parent">
134 { buttons }
135 </BlockControls>
136 }
137
138 <BlockEdit { ...props } />
139 </>
140 );
141 };
142 }, 'withToolbarAppenders' );
143
144 addFilter(
145 'editor.BlockEdit',
146 'generateblocks/blockControls/containerAppenders',
147 withToolbarAppenders
148 );
149