PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.0.0
GenerateBlocks v2.0.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 / migratingIconSizing.js
generateblocks / src / hoc / migrations Last commit date
migrateBorders.js 3 years ago migrateGlobalStyleAttrs.js 2 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
migratingIconSizing.js
66 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 {Object} Props.attributes The existing block attributes.
10 * @param {Object} Props.defaults The block defaults.
11 * @return {Object} New attributes.
12 * @since 1.8.0
13 */
14 function buildIconSizingAttributes( { attributes = {}, defaults = {} } ) {
15 const newAttributes = {};
16 const oldAttributes = {};
17
18 [ '', 'Tablet', 'Mobile' ].forEach( ( device ) => {
19 const oldValue = attributes[ 'iconSize' + device ];
20
21 if ( attributes.hasIcon && ( oldValue || isNumeric( oldValue ) ) ) {
22 newAttributes[ 'width' + device ] = oldValue + attributes.iconSizeUnit;
23 newAttributes[ 'height' + device ] = oldValue + attributes.iconSizeUnit;
24
25 if ( Object.keys( defaults ).length ) {
26 oldAttributes[ 'iconSize' + device ] = defaults[ 'iconSize' + device ]?.default
27 ? defaults[ 'iconSize' + device ].default
28 : '';
29 oldAttributes.iconSizeUnit = defaults.iconSizeUnit.default;
30 }
31 }
32 } );
33
34 return { newAttributes, oldAttributes };
35 }
36
37 /**
38 * Build an iconStyles object with icon sizing to be used by setAttributes with our new attributes.
39 *
40 * @param {Object} Props Function props.
41 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
42 * @param {Object} Props.defaults The block defaults.
43 * @return {Object} New attributes.
44 * @since 1.8.0
45 */
46 export default function migrateIconSizing( { blockVersionLessThan, defaults = {} } ) {
47 return function( attrs, existingAttrs ) {
48 if ( isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
49 const newSizing = buildIconSizingAttributes( {
50 attributes: { ...existingAttrs, ...attrs },
51 defaults,
52 } );
53
54 attrs = addToAttrsObject( {
55 attrs,
56 attributeName: 'iconStyles',
57 existingAttrs: existingAttrs.iconStyles,
58 newAttrs: newSizing.newAttributes,
59 oldAttrs: newSizing.oldAttributes,
60 } );
61 }
62
63 return attrs;
64 };
65 }
66