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 |