PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.2
GenerateBlocks v2.0.2
2.4.1 2.4.0 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 / migrations / migrateSpacing.js
generateblocks / src / hoc / migrations Last commit date
migrateBorders.js 3 years ago migrateGlobalStyleAttrs.js 3 years ago migrateIconPadding.js 3 years ago migrateSizing.js 3 years ago migrateSpacing.js 3 years ago migrateTypography.js 3 years ago migratingIconSizing.js 3 years ago utils.js 3 years ago
migrateSpacing.js
91 lines
1 import isNumeric from '../../utils/is-numeric';
2 import isBlockVersionLessThan from '../../utils/check-block-version';
3 import { addToAttrsObject } from './utils';
4
5 /**
6 * Build an object with new migrated attributes and old attributes reverted to defaults.
7 *
8 * @param {Object} Props Function props.
9 * @param {Array} Props.attributesToMigrate The attributes we want to migrate.
10 * @param {Object} Props.attributes The existing block attributes.
11 * @param {Object} Props.defaults The block defaults.
12 * @return {Object} New attributes.
13 * @since 1.8.0
14 */
15 function buildSpacingAttributes( { attributesToMigrate = [], attributes = {}, defaults = {} } ) {
16 function unitValue( name ) {
17 if ( name.startsWith( 'padding' ) ) {
18 return attributes.paddingUnit;
19 }
20
21 if ( name.startsWith( 'margin' ) ) {
22 return attributes.marginUnit;
23 }
24 }
25
26 const newAttributes = {};
27 const oldAttributes = {};
28
29 [ '', 'Tablet', 'Mobile' ].forEach( ( device ) => {
30 attributesToMigrate.forEach( ( attribute ) => {
31 const oldValue = attributes[ attribute + device ];
32
33 if ( oldValue || isNumeric( oldValue ) ) {
34 if ( attribute ) {
35 newAttributes[ attribute + device ] = isNumeric( oldValue )
36 ? oldValue + unitValue( attribute )
37 : oldValue;
38
39 if ( Object.keys( defaults ).length ) {
40 oldAttributes[ attribute + device ] = defaults[ attribute + device ]?.default
41 ? defaults[ attribute + device ].default
42 : '';
43
44 if ( attribute.startsWith( 'padding' ) ) {
45 oldAttributes.paddingUnit = defaults.paddingUnit.default;
46 }
47
48 if ( attribute.startsWith( 'margin' ) ) {
49 oldAttributes.marginUnit = defaults.marginUnit.default;
50 }
51 }
52 }
53 }
54 } );
55 } );
56
57 return { newAttributes, oldAttributes };
58 }
59
60 /**
61 * Build an iconStyles padding object to be used by setAttributes with our new attributes.
62 *
63 * @param {Object} Props Function props.
64 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
65 * @param {Object} Props.defaults The block defaults.
66 * @param {Array} Props.attributesToMigrate The attributes we want to migrate.
67 * @return {Object} New attributes.
68 * @since 1.8.0
69 */
70 export default function migrateSpacing( { blockVersionLessThan, defaults = {}, attributesToMigrate = [] } ) {
71 return function( attrs, existingAttrs ) {
72 if ( isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
73 const newSpacing = buildSpacingAttributes( {
74 attributesToMigrate,
75 attributes: { ...existingAttrs, ...attrs },
76 defaults,
77 } );
78
79 attrs = addToAttrsObject( {
80 attrs,
81 attributeName: 'spacing',
82 existingAttrs: existingAttrs.spacing,
83 newAttrs: newSpacing.newAttributes,
84 oldAttrs: newSpacing.oldAttributes,
85 } );
86 }
87
88 return attrs;
89 };
90 }
91