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
migrateSizing.js
85 lines
| 1 | import wasBlockJustInserted from '../../utils/was-block-just-inserted'; |
| 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 | * @return {Object} New attributes. |
| 11 | * @since 1.8.0 |
| 12 | */ |
| 13 | function buildSizingAttributes( { attributes } ) { |
| 14 | const newAttributes = {}; |
| 15 | const oldAttributes = {}; |
| 16 | |
| 17 | // Only migrate these if we're an active Grid item. |
| 18 | if ( attributes.isGrid ) { |
| 19 | if ( attributes.width ) { |
| 20 | newAttributes.width = attributes.width + '%'; |
| 21 | oldAttributes.width = ''; |
| 22 | } |
| 23 | |
| 24 | if ( attributes.widthTablet || attributes.autoWidthTablet ) { |
| 25 | newAttributes.widthTablet = attributes.autoWidthTablet ? 'auto' : attributes.widthTablet + '%'; |
| 26 | oldAttributes.widthTablet = ''; |
| 27 | oldAttributes.autoWidthTablet = false; |
| 28 | } |
| 29 | |
| 30 | if ( attributes.widthMobile || attributes.autoWidthMobile ) { |
| 31 | newAttributes.widthMobile = attributes.autoWidthMobile ? 'auto' : attributes.widthMobile + '%'; |
| 32 | oldAttributes.widthMobile = ''; |
| 33 | oldAttributes.autoWidthMobile = false; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | if ( attributes.minHeight ) { |
| 38 | newAttributes.minHeight = attributes.minHeight + attributes.minHeightUnit; |
| 39 | oldAttributes.minHeight = false; |
| 40 | oldAttributes.minHeightUnit = 'px'; |
| 41 | } |
| 42 | |
| 43 | if ( attributes.minHeightTablet ) { |
| 44 | newAttributes.minHeightTablet = attributes.minHeightTablet + attributes.minHeightUnitTablet; |
| 45 | oldAttributes.minHeightTablet = false; |
| 46 | oldAttributes.minHeightUnitTablet = 'px'; |
| 47 | } |
| 48 | |
| 49 | if ( attributes.minHeightMobile ) { |
| 50 | newAttributes.minHeightMobile = attributes.minHeightMobile + attributes.minHeightUnitMobile; |
| 51 | oldAttributes.minHeightMobile = false; |
| 52 | oldAttributes.minHeightUnitMobile = 'px'; |
| 53 | } |
| 54 | |
| 55 | return { newAttributes, oldAttributes }; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Build a sizing object to be used by setAttributes with our new attributes. |
| 60 | * |
| 61 | * @param {Object} Props Function props. |
| 62 | * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run. |
| 63 | * @return {Object} New attributes. |
| 64 | * @since 1.7.0 |
| 65 | */ |
| 66 | export default function migrateSizing( { blockVersionLessThan } ) { |
| 67 | return function( attrs, existingAttrs ) { |
| 68 | if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) { |
| 69 | const newSizing = buildSizingAttributes( { |
| 70 | attributes: { ...existingAttrs, ...attrs }, |
| 71 | } ); |
| 72 | |
| 73 | attrs = addToAttrsObject( { |
| 74 | attrs, |
| 75 | attributeName: 'sizing', |
| 76 | existingAttrs: existingAttrs.sizing, |
| 77 | newAttrs: newSizing.newAttributes, |
| 78 | oldAttrs: newSizing.oldAttributes, |
| 79 | } ); |
| 80 | } |
| 81 | |
| 82 | return attrs; |
| 83 | }; |
| 84 | } |
| 85 |