migrations
2 years ago
index.js
3 years ago
withButtonContainerLegacyMigration.js
3 years ago
withButtonLegacyMigration.js
3 years ago
withContainerLegacyMigration.js
3 years ago
withDeviceType.js
3 years ago
withDocumentation.js
4 years ago
withGridLegacyMigration.js
3 years ago
withHeadlineLegacyMigration.js
3 years ago
withImageLegacyMigration.js
3 years ago
withSetAttributes.js
3 years ago
withUniqueId.js
4 years ago
withHeadlineLegacyMigration.js
150 lines
| 1 | import { useEffect } from '@wordpress/element'; |
| 2 | import { getBlockType } from '@wordpress/blocks'; |
| 3 | import isBlockVersionLessThan from '../utils/check-block-version'; |
| 4 | import wasBlockJustInserted from '../utils/was-block-just-inserted'; |
| 5 | import flexboxAlignment from '../utils/flexbox-alignment'; |
| 6 | import migrateSpacing from './migrations/migrateSpacing'; |
| 7 | import migrateBorders from './migrations/migrateBorders'; |
| 8 | import migrateTypography from './migrations/migrateTypography'; |
| 9 | import migrateIconSizing from './migrations/migratingIconSizing'; |
| 10 | import migrateIconPadding from './migrations/migrateIconPadding'; |
| 11 | import { migrationPipe, updateBlockVersion } from './migrations/utils'; |
| 12 | import { isEmpty } from 'lodash'; |
| 13 | |
| 14 | /** |
| 15 | * Set our layout attributes for old Headline blocks. |
| 16 | * |
| 17 | * @param {Object} attrs Attributes from previous migrations. |
| 18 | * @param {Object} existingAttrs Pre-existing block attributes. |
| 19 | * @param {string} mode The migration mode. |
| 20 | * @return {Object} Updated attributes. |
| 21 | * @since 1.7.0 |
| 22 | */ |
| 23 | export function migrateFlex( attrs, existingAttrs, mode ) { |
| 24 | if ( 'css' === mode ) { |
| 25 | return attrs; |
| 26 | } |
| 27 | |
| 28 | if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, 2 ) ) { |
| 29 | if ( existingAttrs.hasIcon ) { |
| 30 | attrs.display = 'flex'; |
| 31 | } |
| 32 | |
| 33 | [ '', 'Tablet', 'Mobile' ].forEach( ( device ) => { |
| 34 | if ( existingAttrs[ 'inlineWidth' + device ] ) { |
| 35 | attrs[ 'display' + device ] = existingAttrs.hasIcon ? 'inline-flex' : 'inline-block'; |
| 36 | } |
| 37 | |
| 38 | if ( existingAttrs.hasIcon ) { |
| 39 | if ( 'above' !== existingAttrs[ 'iconLocation' + device ] && existingAttrs[ 'alignment' + device ] ) { |
| 40 | attrs[ 'justifyContent' + device ] = flexboxAlignment( existingAttrs.alignment + device ); |
| 41 | } |
| 42 | |
| 43 | if ( 'inline' === existingAttrs[ 'iconLocation' + device ] && existingAttrs[ 'iconVerticalAlignment' + device ] ) { |
| 44 | attrs[ 'alignItems' + device ] = flexboxAlignment( existingAttrs.iconVerticalAlignment + device ); |
| 45 | } |
| 46 | |
| 47 | if ( 'above' === existingAttrs[ 'iconLocation' + device ] ) { |
| 48 | attrs[ 'flexDirection' + device ] = 'column'; |
| 49 | } |
| 50 | } |
| 51 | } ); |
| 52 | } |
| 53 | |
| 54 | return attrs; |
| 55 | } |
| 56 | |
| 57 | export const currentBlockVersion = 3; |
| 58 | |
| 59 | /** |
| 60 | * Migrate our Headline attributes. |
| 61 | * |
| 62 | * @param {Object} Props Function props. |
| 63 | * @param {Object} Props.attributes The block attributes. |
| 64 | * @param {Object} Props.defaults The block defaults. |
| 65 | * @param {string} Props.mode The migration mode. |
| 66 | * @return {Object} Updated attributes. |
| 67 | * @since 1.8.0 |
| 68 | */ |
| 69 | export function migrateHeadlineAttributes( { attributes, defaults, mode } ) { |
| 70 | return migrationPipe( |
| 71 | attributes, |
| 72 | [ |
| 73 | migrateFlex, |
| 74 | migrateSpacing( { |
| 75 | blockVersionLessThan: 3, |
| 76 | defaults, |
| 77 | attributesToMigrate: [ |
| 78 | 'paddingTop', |
| 79 | 'paddingRight', |
| 80 | 'paddingBottom', |
| 81 | 'paddingLeft', |
| 82 | 'marginTop', |
| 83 | 'marginRight', |
| 84 | 'marginBottom', |
| 85 | 'marginLeft', |
| 86 | ], |
| 87 | } ), |
| 88 | migrateBorders( { |
| 89 | blockVersionLessThan: 3, |
| 90 | defaults, |
| 91 | attributesToMigrate: [ |
| 92 | 'borderSizeTop', |
| 93 | 'borderSizeRight', |
| 94 | 'borderSizeBottom', |
| 95 | 'borderSizeLeft', |
| 96 | 'borderRadiusTopRight', |
| 97 | 'borderRadiusBottomRight', |
| 98 | 'borderRadiusBottomLeft', |
| 99 | 'borderRadiusTopLeft', |
| 100 | ], |
| 101 | } ), |
| 102 | migrateTypography( { |
| 103 | blockVersionLessThan: 3, |
| 104 | defaults, |
| 105 | attributesToMigrate: [ |
| 106 | 'fontFamily', |
| 107 | 'fontSize', |
| 108 | 'lineHeight', |
| 109 | 'letterSpacing', |
| 110 | 'fontWeight', |
| 111 | 'textTransform', |
| 112 | 'alignment', |
| 113 | ], |
| 114 | } ), |
| 115 | migrateIconSizing( { |
| 116 | blockVersionLessThan: 3, |
| 117 | defaults, |
| 118 | } ), |
| 119 | migrateIconPadding( { |
| 120 | blockVersionLessThan: 3, |
| 121 | defaults, |
| 122 | } ), |
| 123 | updateBlockVersion( currentBlockVersion ), |
| 124 | ], |
| 125 | mode |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | export default ( WrappedComponent ) => { |
| 130 | return ( props ) => { |
| 131 | const { |
| 132 | attributes, |
| 133 | setAttributes, |
| 134 | } = props; |
| 135 | |
| 136 | useEffect( () => { |
| 137 | const newAttributes = migrateHeadlineAttributes( { |
| 138 | attributes, |
| 139 | defaults: getBlockType( 'generateblocks/button' )?.attributes, |
| 140 | } ); |
| 141 | |
| 142 | if ( ! isEmpty( newAttributes ) ) { |
| 143 | setAttributes( newAttributes ); |
| 144 | } |
| 145 | }, [] ); |
| 146 | |
| 147 | return ( <WrappedComponent { ...props } /> ); |
| 148 | }; |
| 149 | }; |
| 150 |