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 / withGridLegacyMigration.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
withGridLegacyMigration.js
110 lines
1 import { useEffect } from '@wordpress/element';
2 import wasBlockJustInserted from '../utils/was-block-just-inserted';
3 import isBlockVersionLessThan from '../utils/check-block-version';
4 import hasNumericValue from '../utils/has-numeric-value';
5 import { migrationPipe, updateBlockVersion, setIsDynamic } from './migrations/utils';
6 import { isEmpty } from 'lodash';
7
8 /**
9 * Set our old defaults as static values.
10 *
11 * @param {Object} Props Function props.
12 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
13 * @param {Object} Props.oldDefaults Old defaults that were changed and need to be added to attributes.
14 * @return {Object} Updated attributes.
15 * @since 1.4.0
16 */
17 export function migrateOldGridDefaults( { blockVersionLessThan, oldDefaults } ) {
18 return function( attrs, existingAttrs, mode ) {
19 if ( 'css' === mode ) {
20 return attrs;
21 }
22
23 if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
24 const hasGlobalStyle = existingAttrs.useGlobalStyle && existingAttrs.globalStyleId;
25
26 if ( ! hasGlobalStyle && ! hasNumericValue( existingAttrs.horizontalGap ) ) {
27 attrs.horizontalGap = oldDefaults.horizontalGap;
28 }
29 }
30
31 return attrs;
32 };
33 }
34
35 /**
36 * Prevent layouts from switching to row-gap.
37 *
38 * @param {Object} Props Function props.
39 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
40 * @return {Object} Updated attributes.
41 * @since 1.7.0
42 */
43 export function migrateLegacyRowGap( { blockVersionLessThan } ) {
44 return function( attrs, existingAttrs, mode ) {
45 if ( 'css' === mode ) {
46 return attrs;
47 }
48
49 if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
50 attrs.useLegacyRowGap = true;
51 }
52
53 return attrs;
54 };
55 }
56
57 export const currentBlockVersion = 3;
58
59 /**
60 * Migrate our Grid attributes.
61 *
62 * @param {Object} Props Function props.
63 * @param {Object} Props.attributes The block attributes.
64 * @param {string} Props.mode The migration mode.
65 * @param {Object} Props.oldDefaults An object of old defaults keyed by version.
66 * @return {Object} Updated attributes.
67 * @since 1.8.0
68 */
69 export function migrateGridAttributes( { attributes, mode = '', oldDefaults = {} } ) {
70 return migrationPipe(
71 attributes,
72 [
73 setIsDynamic,
74 migrateOldGridDefaults( {
75 blockVersionLessThan: 2,
76 oldDefaults: oldDefaults.v1_4_0,
77 } ),
78 migrateLegacyRowGap( {
79 blockVersionLessThan: 3,
80 } ),
81 updateBlockVersion( currentBlockVersion ),
82 ],
83 mode
84 );
85 }
86
87 export default ( WrappedComponent ) => {
88 return ( props ) => {
89 const {
90 attributes,
91 setAttributes,
92 } = props;
93
94 useEffect( () => {
95 const newAttributes = migrateGridAttributes( {
96 attributes,
97 oldDefaults: {
98 v1_4_0: generateBlocksLegacyDefaults.v_1_4_0.gridContainer,
99 },
100 } );
101
102 if ( ! isEmpty( newAttributes ) ) {
103 setAttributes( newAttributes );
104 }
105 }, [] );
106
107 return ( <WrappedComponent { ...props } /> );
108 };
109 };
110