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 / button / components / BlockControls.js
generateblocks / src / blocks / button / components Last commit date
BlockControls.js 3 years ago ButtonContentRenderer.js 3 years ago ComponentCSS.js 4 years ago ConditionalColors.js 3 years ago InspectorAdvancedControls.js 3 years ago
BlockControls.js
222 lines
1 import { ToolbarButton, ToolbarGroup, Dropdown, ToggleControl } from '@wordpress/components';
2 import { __ } from '@wordpress/i18n';
3 import { cloneBlock, createBlock } from '@wordpress/blocks';
4 import { BlockControls, URLInput, AlignmentToolbar } from '@wordpress/block-editor';
5 import { useDispatch, useSelect } from '@wordpress/data';
6 import { link, plus } from '@wordpress/icons';
7 import { applyFilters } from '@wordpress/hooks';
8 import getIcon from '../../../utils/get-icon';
9 import isFlexItem from '../../../utils/is-flex-item';
10 import getAttribute from '../../../utils/get-attribute';
11 import typographyOptions from '../../../extend/inspector-control/controls/typography/options';
12 import getDeviceType from '../../../utils/get-device-type';
13
14 export default ( props ) => {
15 const {
16 attributes,
17 clientId,
18 setAttributes,
19 } = props;
20
21 const { insertBlocks, replaceBlocks } = useDispatch( 'core/block-editor' );
22 const {
23 getBlockParentsByBlockName,
24 getBlocksByClientId,
25 getBlockAttributes,
26 getSelectedBlockClientIds,
27 getBlock,
28 } = useSelect( ( select ) => select( 'core/block-editor' ), [] );
29
30 const {
31 url,
32 target,
33 relNoFollow,
34 relSponsored,
35 useDynamicData,
36 dynamicLinkType,
37 display,
38 displayTablet,
39 displayMobile,
40 buttonType,
41 } = attributes;
42
43 const POPOVER_PROPS = {
44 className: 'block-editor-block-settings-menu__popover',
45 position: 'bottom right',
46 };
47
48 const deviceType = getDeviceType();
49 const hasDynamicLink = useDynamicData && dynamicLinkType;
50 const showAppender = applyFilters( 'generateblocks.editor.showButtonAppender', true, props );
51 const showButtonContainer = applyFilters( 'generateblocks.editor.showButtonContainerControl', true, props );
52 const showButtonLinkControl = applyFilters( 'generateblocks.editor.showButtonLinkControl', 'link' === buttonType, props );
53 let containerId = false;
54 const buttonContainerId = getBlockParentsByBlockName( clientId, 'generateblocks/button-container', true )[ 0 ];
55 const containerVariantId = getBlockParentsByBlockName( clientId, 'generateblocks/container', true )[ 0 ];
56
57 if ( buttonContainerId ) {
58 containerId = buttonContainerId;
59 } else if ( containerVariantId ) {
60 const containerAttributes = getBlockAttributes( containerVariantId );
61
62 if ( 'button-container' === containerAttributes.variantRole ) {
63 containerId = containerVariantId;
64 }
65 }
66
67 return (
68 <>
69 <BlockControls>
70 { !! showButtonContainer && ! containerId &&
71 <ToolbarGroup>
72 <ToolbarButton
73 icon={ getIcon( 'button-container' ) }
74 label={ __( 'Add to Button Container', 'generateblocks' ) }
75 onClick={ () => {
76 const selectedBlockIds = getSelectedBlockClientIds();
77
78 const selectedBlocks = selectedBlockIds.map( ( blockId ) => {
79 const block = getBlock( blockId );
80
81 return createBlock(
82 block.name,
83 block.attributes,
84 block.innerBlocks
85 );
86 } );
87
88 const groupedBlocks = createBlock(
89 'generateblocks/container',
90 {
91 display: 'flex',
92 variantRole: 'button-container',
93 },
94 selectedBlocks
95 );
96
97 replaceBlocks( selectedBlockIds, groupedBlocks );
98 } }
99 showTooltip
100 />
101 </ToolbarGroup>
102 }
103
104 <ToolbarGroup>
105 { showAppender && containerId &&
106 <ToolbarButton
107 className="gblocks-add-new-button"
108 icon={ plus }
109 label={ __( 'Add Button', 'generateblocks' ) }
110 onClick={ () => {
111 const thisBlock = getBlocksByClientId( clientId )[ 0 ];
112
113 const clonedBlock = cloneBlock(
114 thisBlock,
115 {
116 uniqueId: '',
117 }
118 );
119
120 insertBlocks( clonedBlock, undefined, containerId );
121 } }
122 showTooltip
123 />
124 }
125 </ToolbarGroup>
126
127 { ! isFlexItem( { device: deviceType, display, displayTablet, displayMobile } ) &&
128 <AlignmentToolbar
129 value={ getAttribute( 'alignment', { attributes, deviceType } ) }
130 onChange={ ( value ) => {
131 setAttributes( {
132 [ getAttribute( 'alignment', { attributes, deviceType }, true ) ]: value,
133 } );
134 } }
135 alignmentControls={ typographyOptions.alignments }
136 />
137 }
138
139 <ToolbarGroup>
140 { ( ! useDynamicData || hasDynamicLink ) && showButtonLinkControl &&
141 <Dropdown
142 contentClassName="gblocks-button-link-dropdown"
143 popoverProps={ POPOVER_PROPS }
144 renderToggle={ ( { isOpen, onToggle } ) => (
145 <ToolbarButton
146 icon={ link }
147 label={ ! url ? __( 'Add Link', 'generateblocks' ) : __( 'Change Link', 'generateblocks' ) }
148 onClick={ onToggle }
149 aria-expanded={ isOpen }
150 isPressed={ !! url }
151 />
152 ) }
153 renderContent={ () => (
154 <>
155 { ! useDynamicData &&
156 <URLInput
157 className={ 'gblocks-button-link' }
158 value={ url }
159 onChange={ ( value ) => {
160 setAttributes( {
161 url: value,
162 hasUrl: !! value,
163 } );
164 } }
165 />
166 }
167
168 { !! useDynamicData &&
169 <div style={ {
170 width: '300px',
171 'font-style': 'italic',
172 'margin-bottom': ( !! dynamicLinkType ? '15px' : '0' ),
173 } }>
174 { __( 'This button is using a dynamic link.', 'generateblocks' ) }
175 </div>
176 }
177
178 { applyFilters( 'generateblocks.editor.urlInputMoreOptions', '', attributes ) }
179
180 { ( !! url || hasDynamicLink ) &&
181 <>
182 <ToggleControl
183 label={ __( 'Open link in a new tab', 'generateblocks' ) }
184 checked={ target || '' }
185 onChange={ ( value ) => {
186 setAttributes( {
187 target: value,
188 } );
189 } }
190 />
191
192 <ToggleControl
193 label={ __( 'Add rel="nofollow"', 'generateblocks' ) }
194 checked={ relNoFollow || '' }
195 onChange={ ( value ) => {
196 setAttributes( {
197 relNoFollow: value,
198 } );
199 } }
200 />
201
202 <ToggleControl
203 label={ __( 'Add rel="sponsored"', 'generateblocks' ) }
204 checked={ relSponsored || '' }
205 onChange={ ( value ) => {
206 setAttributes( {
207 relSponsored: value,
208 } );
209 } }
210 />
211 </>
212 }
213 </>
214 ) }
215 />
216 }
217 </ToolbarGroup>
218 </BlockControls>
219 </>
220 );
221 };
222