PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.7.1
GenerateBlocks v1.7.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 / components / migrate-inner-container / utils.js
generateblocks / src / components / migrate-inner-container Last commit date
index.js 3 years ago utils.js 3 years ago
utils.js
348 lines
1 import hasNumericValue from '../../utils/has-numeric-value';
2 import sizingValue from '../../utils/sizingValue';
3 import { createBlock } from '@wordpress/blocks';
4
5 /**
6 * Returns migrated layout options for new Layout panel in 1.7.0.
7 *
8 * @param {Object} attributes The block attributes.
9 * @return {Object} The new attributes.
10 */
11 function getLayoutAttributes( attributes ) {
12 const {
13 isGrid,
14 verticalAlignment,
15 verticalAlignmentTablet,
16 verticalAlignmentMobile,
17 sizing,
18 bgImage,
19 bgOptions,
20 useDynamicData,
21 dynamicContentType,
22 zindex,
23 gradient,
24 gradientSelector,
25 shapeDividers,
26 linkType,
27 url,
28 dynamicLinkType,
29 advBackgrounds,
30 gpInlinePostMeta,
31 gpInlinePostMetaJustify,
32 gpInlinePostMetaJustifyTablet,
33 gpInlinePostMetaJustifyMobile,
34 } = attributes;
35
36 const layoutAttributes = {};
37 let setMinHeightFlex = false;
38
39 if ( sizingValue( 'minHeight', sizing ) && verticalAlignment && ! isGrid ) {
40 layoutAttributes.display = 'flex';
41 layoutAttributes.flexDirection = 'column';
42 layoutAttributes.justifyContent = verticalAlignment;
43 setMinHeightFlex = true;
44 }
45
46 if ( isGrid && verticalAlignment ) {
47 layoutAttributes.display = 'flex';
48 layoutAttributes.flexDirection = 'column';
49 layoutAttributes.justifyContent = verticalAlignment;
50 }
51
52 if ( ! isGrid ) {
53 if ( ! setMinHeightFlex && sizingValue( 'minHeightTablet', sizing ) && 'inherit' !== verticalAlignmentTablet ) {
54 layoutAttributes.displayTablet = 'flex';
55 layoutAttributes.flexDirectionTablet = 'column';
56 setMinHeightFlex = true;
57 }
58
59 if ( setMinHeightFlex && 'inherit' !== verticalAlignmentTablet ) {
60 layoutAttributes.justifyContentTablet = verticalAlignmentTablet;
61 }
62 }
63
64 if ( isGrid && verticalAlignmentTablet && 'inherit' !== verticalAlignmentTablet ) {
65 layoutAttributes.justifyContentTablet = verticalAlignmentTablet;
66 }
67
68 if ( ! isGrid ) {
69 if ( ! setMinHeightFlex && sizingValue( 'minHeightMobile', sizing ) && 'inherit' !== verticalAlignmentMobile ) {
70 layoutAttributes.displayMobile = 'flex';
71 layoutAttributes.flexDirectionMobile = 'column';
72 setMinHeightFlex = true;
73 }
74
75 if ( setMinHeightFlex && 'inherit' !== verticalAlignmentTablet ) {
76 layoutAttributes.justifyContentMobile = verticalAlignmentMobile;
77 }
78 }
79
80 if ( isGrid && verticalAlignmentMobile && 'inherit' !== verticalAlignmentMobile ) {
81 layoutAttributes.justifyContentMobile = verticalAlignmentMobile;
82 }
83
84 const hasBgImage = !! bgImage || ( useDynamicData && '' !== dynamicContentType );
85
86 if ( zindex || shapeDividers.length ) {
87 layoutAttributes.position = 'relative';
88 }
89
90 if (
91 ( hasBgImage && 'pseudo-element' === bgOptions.selector ) ||
92 ( gradient && 'pseudo-element' === gradientSelector ) ||
93 advBackgrounds?.length
94 ) {
95 layoutAttributes.position = 'relative';
96 layoutAttributes.overflowX = 'hidden';
97 layoutAttributes.overflowY = 'hidden';
98 }
99
100 // GB Pro features.
101 if ( 'hidden-link' === linkType && ( url || ( useDynamicData && dynamicLinkType ) ) ) {
102 layoutAttributes.position = 'relative';
103 }
104
105 // GP Premium feature.
106 if ( gpInlinePostMeta ) {
107 layoutAttributes.display = 'flex';
108 layoutAttributes.alignItems = 'center';
109 layoutAttributes.gpInlinePostMeta = false;
110
111 if ( gpInlinePostMetaJustify ) {
112 layoutAttributes.justifyContent = gpInlinePostMetaJustify;
113 layoutAttributes.gpInlinePostMetaJustify = '';
114 }
115
116 if ( gpInlinePostMetaJustifyTablet ) {
117 layoutAttributes.justifyContentTablet = gpInlinePostMetaJustifyTablet;
118 layoutAttributes.gpInlinePostMetaJustifyTablet = '';
119 }
120
121 if ( gpInlinePostMetaJustifyMobile ) {
122 layoutAttributes.justifyContentMobile = gpInlinePostMetaJustifyMobile;
123 layoutAttributes.gpInlinePostMetaJustifyMobile = '';
124 }
125 }
126
127 return layoutAttributes;
128 }
129
130 /**
131 * Check if we should wrap block contents in an additional Container.
132 *
133 * @param {Object} props The function props.
134 * @return {boolean} Whether to add an inner Container.
135 */
136 function shouldMigrateInnerContainer( props ) {
137 const {
138 attributes,
139 insideGridBlock,
140 childBlock,
141 } = props;
142
143 const {
144 outerContainer,
145 innerContainer,
146 innerZindex,
147 } = attributes;
148
149 let recommend = false;
150
151 if ( 'full' === outerContainer && 'contained' === innerContainer ) {
152 recommend = true;
153 }
154
155 if ( insideGridBlock ) {
156 recommend = false;
157 }
158
159 if ( childBlock && 'generateblocks/container' === childBlock.name ) {
160 // This block already has a contained inner Container.
161 if ( 'contained' === childBlock.attributes.outerContainer ) {
162 recommend = false;
163 }
164 }
165
166 if ( hasNumericValue( innerZindex ) ) {
167 recommend = true;
168 }
169
170 // Some effects target the inner container.
171 const effects = [ 'opacities', 'transitions', 'boxShadows', 'transforms', 'textShadows', 'filters' ];
172
173 effects.forEach( ( effect ) => {
174 const hasInnerContainerEffect =
175 attributes[ effect ] &&
176 attributes[ effect ].some( ( type ) => 'innerContainer' === type.target );
177
178 if ( hasInnerContainerEffect ) {
179 recommend = true;
180 }
181 } );
182
183 return recommend;
184 }
185
186 /**
187 * Wrap all child blocks in an additional Container block.
188 *
189 * @param {Object} props The function props.
190 */
191 function doInnerContainerMigration( props ) {
192 const {
193 attributes,
194 setAttributes,
195 parentBlock,
196 replaceBlocks,
197 insertBlocks,
198 clientId,
199 } = props;
200
201 const {
202 isGrid,
203 paddingTop,
204 paddingRight,
205 paddingBottom,
206 paddingLeft,
207 paddingTopTablet,
208 paddingRightTablet,
209 paddingBottomTablet,
210 paddingLeftTablet,
211 paddingTopMobile,
212 paddingRightMobile,
213 paddingBottomMobile,
214 paddingLeftMobile,
215 paddingUnit,
216 containerWidth,
217 outerContainer,
218 innerContainer,
219 innerZindex,
220 marginLeft,
221 marginRight,
222 useGlobalMaxWidth,
223 } = attributes;
224
225 const childBlocks = parentBlock.innerBlocks;
226 const hasDefaultContainerWidth = parseInt( containerWidth ) === parseInt( generateBlocksInfo.globalContainerWidth );
227 const layoutAttributes = getLayoutAttributes( attributes );
228
229 // We need to create new block instances for each inner block
230 // to prevent a bug where re-usable block content is removed
231 // during migration.
232 const newChildBlocks = childBlocks.map( ( block ) => {
233 return createBlock(
234 block.name,
235 block.attributes,
236 block.innerBlocks
237 );
238 } );
239
240 // Wrap our existing child blocks in a new Container block.
241 const newInnerBlocks = createBlock(
242 'generateblocks/container',
243 {
244 paddingTop,
245 paddingRight,
246 paddingBottom,
247 paddingLeft,
248 paddingTopTablet,
249 paddingRightTablet,
250 paddingBottomTablet,
251 paddingLeftTablet,
252 paddingTopMobile,
253 paddingRightMobile,
254 paddingBottomMobile,
255 paddingLeftMobile,
256 paddingUnit,
257 useGlobalMaxWidth: 'contained' === innerContainer ? !! hasDefaultContainerWidth : useGlobalMaxWidth,
258 sizing: {
259 maxWidth: 'contained' === innerContainer && ! hasDefaultContainerWidth && containerWidth ? containerWidth + 'px' : '',
260 },
261 marginLeft: 'auto',
262 marginRight: 'auto',
263 zindex: innerZindex,
264 position: innerZindex || 0 === innerZindex ? 'relative' : '',
265 },
266 newChildBlocks
267 );
268
269 const childClientIds = childBlocks.map( ( block ) => block.clientId );
270
271 if ( childClientIds.length > 0 ) {
272 replaceBlocks( childClientIds, newInnerBlocks );
273 } else {
274 insertBlocks( newInnerBlocks, 0, clientId );
275 }
276
277 // Update attributes for existing Container block.
278 setAttributes( {
279 useInnerContainer: false,
280 paddingTop: '',
281 paddingRight: '',
282 paddingBottom: '',
283 paddingLeft: '',
284 paddingTopTablet: '',
285 paddingRightTablet: '',
286 paddingBottomTablet: '',
287 paddingLeftTablet: '',
288 paddingTopMobile: '',
289 paddingRightMobile: '',
290 paddingBottomMobile: '',
291 paddingLeftMobile: '',
292 paddingUnit: generateBlocksDefaults.container.paddingUnit,
293 useGlobalMaxWidth: ! isGrid && 'contained' === outerContainer && !! hasDefaultContainerWidth,
294 marginLeft: ! isGrid && 'contained' === outerContainer ? 'auto' : marginLeft,
295 marginRight: ! isGrid && 'contained' === outerContainer ? 'auto' : marginRight,
296 sizing: {
297 ...attributes.sizing,
298 height: isGrid ? '100%' : '',
299 maxWidth: ! isGrid && 'contained' === outerContainer && ! hasDefaultContainerWidth && containerWidth ? containerWidth + 'px' : '',
300 },
301 ...layoutAttributes,
302 } );
303 }
304
305 /**
306 * Just updates current Container block attributes.
307 * Doesn't wrap inner blocks.
308 *
309 * @param {Object} props The function props.
310 */
311 function doSimpleMigration( props ) {
312 const {
313 attributes,
314 setAttributes,
315 } = props;
316
317 const {
318 isGrid,
319 outerContainer,
320 marginLeft,
321 marginRight,
322 containerWidth,
323 } = attributes;
324
325 const hasDefaultContainerWidth = parseInt( containerWidth ) === parseInt( generateBlocksInfo.globalContainerWidth );
326 const layoutAttributes = getLayoutAttributes( attributes );
327
328 setAttributes( {
329 useInnerContainer: false,
330 useGlobalMaxWidth: ! isGrid && 'contained' === outerContainer && !! hasDefaultContainerWidth,
331 marginLeft: ! isGrid && 'contained' === outerContainer ? 'auto' : marginLeft,
332 marginRight: ! isGrid && 'contained' === outerContainer ? 'auto' : marginRight,
333 sizing: {
334 ...attributes.sizing,
335 height: isGrid ? '100%' : '',
336 maxWidth: ! isGrid && 'contained' === outerContainer && ! hasDefaultContainerWidth && containerWidth ? containerWidth + 'px' : '',
337 },
338 ...layoutAttributes,
339 } );
340 }
341
342 export {
343 shouldMigrateInnerContainer,
344 doInnerContainerMigration,
345 getLayoutAttributes,
346 doSimpleMigration,
347 };
348