migrateBorders.js
2 years ago
migrateGlobalStyleAttrs.js
2 years ago
migrateIconPadding.js
2 years ago
migrateSizing.js
2 years ago
migrateSpacing.js
2 years ago
migrateTypography.js
2 years ago
migratingIconSizing.js
2 years ago
utils.js
2 years ago
migrateIconPadding.js
90 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 buildPaddingAttributes( { attributes, defaults = {} } ) { |
| 15 | const newAttributes = {}; |
| 16 | const oldAttributes = {}; |
| 17 | const attributesToMigrate = [ 'iconPaddingTop', 'iconPaddingRight', 'iconPaddingBottom', 'iconPaddingLeft' ]; |
| 18 | |
| 19 | [ '', 'Tablet', 'Mobile' ].forEach( ( device ) => { |
| 20 | attributesToMigrate.forEach( ( attribute ) => { |
| 21 | const oldValue = attributes[ attribute + device ]; |
| 22 | |
| 23 | if ( attributes.hasIcon && isNumeric( oldValue ) ) { |
| 24 | let newAttributeName = ''; |
| 25 | |
| 26 | switch ( attribute ) { |
| 27 | case 'iconPaddingTop': |
| 28 | newAttributeName = 'paddingTop'; |
| 29 | break; |
| 30 | |
| 31 | case 'iconPaddingRight': |
| 32 | newAttributeName = 'paddingRight'; |
| 33 | break; |
| 34 | |
| 35 | case 'iconPaddingBottom': |
| 36 | newAttributeName = 'paddingBottom'; |
| 37 | break; |
| 38 | |
| 39 | case 'iconPaddingLeft': |
| 40 | newAttributeName = 'paddingLeft'; |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | if ( newAttributeName ) { |
| 45 | newAttributes[ newAttributeName + device ] = oldValue + attributes.iconPaddingUnit; |
| 46 | |
| 47 | if ( Object.keys( defaults ).length ) { |
| 48 | oldAttributes[ attribute + device ] = defaults[ attribute + device ]?.default |
| 49 | ? defaults[ attribute + device ].default |
| 50 | : ''; |
| 51 | oldAttributes.iconPaddingUnit = defaults.iconPaddingUnit.default; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } ); |
| 56 | } ); |
| 57 | |
| 58 | return { newAttributes, oldAttributes }; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Build an iconStyles padding object to be used by setAttributes with our new attributes. |
| 63 | * |
| 64 | * @param {Object} Props Function props. |
| 65 | * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run. |
| 66 | * @param {Object} Props.defaults The block defaults. |
| 67 | * @return {Object} New attributes. |
| 68 | * @since 1.8.0 |
| 69 | */ |
| 70 | export default function migrateIconPadding( { blockVersionLessThan, defaults = {} } ) { |
| 71 | return function( attrs, existingAttrs ) { |
| 72 | if ( isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) { |
| 73 | const newPadding = buildPaddingAttributes( { |
| 74 | attributes: { ...existingAttrs, ...attrs }, |
| 75 | defaults, |
| 76 | } ); |
| 77 | |
| 78 | attrs = addToAttrsObject( { |
| 79 | attrs, |
| 80 | attributeName: 'iconStyles', |
| 81 | existingAttrs: existingAttrs.iconStyles, |
| 82 | newAttrs: newPadding.newAttributes, |
| 83 | oldAttrs: newPadding.oldAttributes, |
| 84 | } ); |
| 85 | } |
| 86 | |
| 87 | return attrs; |
| 88 | }; |
| 89 | } |
| 90 |