PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.3
GenerateBlocks v1.8.3
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 / hoc / withButtonContainerLegacyMigration.js
generateblocks / src / hoc Last commit date
migrations 2 years ago index.js 2 years ago withButtonContainerLegacyMigration.js 2 years ago withButtonLegacyMigration.js 2 years ago withContainerLegacyMigration.js 2 years ago withDeviceType.js 3 years ago withDocumentation.js 4 years ago withGridLegacyMigration.js 2 years ago withHeadlineLegacyMigration.js 2 years ago withImageLegacyMigration.js 2 years ago withSetAttributes.js 2 years ago withUniqueId.js 4 years ago
withButtonContainerLegacyMigration.js
98 lines
1 import { useEffect } from '@wordpress/element';
2 import { getBlockType } from '@wordpress/blocks';
3 import isBlockVersionLessThan from '../utils/check-block-version';
4 import wasBlockJustInserted from '../utils/was-block-just-inserted';
5 import { migrationPipe, updateBlockVersion, setIsDynamic } from './migrations/utils';
6 import { isEmpty } from 'lodash';
7 import migrateSpacing from './migrations/migrateSpacing';
8
9 /**
10 * Migrate our stack and fillHorizontal space attributes to their devices.
11 *
12 * @param {Object} Props Function props.
13 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
14 * @return {Object} Updated attributes.
15 * @since 1.4.0
16 */
17 export function migrateStackFill( { blockVersionLessThan } ) {
18 return function( attrs, existingAttrs, mode ) {
19 if ( 'css' === mode ) {
20 return attrs;
21 }
22
23 if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
24 if ( existingAttrs.stack || existingAttrs.fillHorizontalSpace ) {
25 if ( existingAttrs.stack ) {
26 attrs.stackTablet = true;
27 attrs.stackMobile = true;
28 }
29
30 if ( existingAttrs.fillHorizontalSpace ) {
31 attrs.fillHorizontalSpaceTablet = true;
32 attrs.fillHorizontalSpaceMobile = true;
33 }
34 }
35 }
36
37 return attrs;
38 };
39 }
40
41 export const currentBlockVersion = 3;
42
43 /**
44 * Migrate our Button Container attributes.
45 *
46 * @param {Object} Props Function props.
47 * @param {Object} Props.attributes The block attributes.
48 * @param {Object} Props.defaults The block defaults.
49 * @param {string} Props.mode The migration mode.
50 * @return {Object} Updated attributes.
51 * @since 1.8.0
52 */
53 export function migrateButtonContainerAttributes( { attributes, defaults, mode = '' } ) {
54 return migrationPipe(
55 attributes,
56 [
57 setIsDynamic,
58 migrateStackFill( {
59 blockVersionLessThan: 2,
60 } ),
61 migrateSpacing( {
62 blockVersionLessThan: 3,
63 defaults,
64 attributesToMigrate: [
65 'marginTop',
66 'marginRight',
67 'marginBottom',
68 'marginLeft',
69 ],
70 } ),
71 updateBlockVersion( currentBlockVersion ),
72 ],
73 mode
74 );
75 }
76
77 export default ( WrappedComponent ) => {
78 return ( props ) => {
79 const {
80 attributes,
81 setAttributes,
82 } = props;
83
84 useEffect( () => {
85 const newAttributes = migrateButtonContainerAttributes( {
86 attributes,
87 defaults: getBlockType( 'generateblocks/button-container' )?.attributes,
88 } );
89
90 if ( ! isEmpty( newAttributes ) ) {
91 setAttributes( newAttributes );
92 }
93 }, [] );
94
95 return ( <WrappedComponent { ...props } /> );
96 };
97 };
98