migrations
2 years ago
index.js
1 year ago
withButtonContainerLegacyMigration.js
2 years ago
withButtonLegacyMigration.js
2 years ago
withContainerLegacyMigration.js
2 years ago
withDeviceType.js
3 years ago
withDynamicTag.js
1 year ago
withGridLegacyMigration.js
2 years ago
withHeadlineLegacyMigration.js
2 years ago
withHtmlAttributes.js
1 year ago
withImageLegacyMigration.js
2 years ago
withSetAttributes.js
2 years ago
withStyles.js
1 year ago
withUniqueId.js
2 years ago
withButtonContainerLegacyMigration.js
98 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 { migrationPipe, updateBlockVersion, setIsDynamic } from './migrations/utils'; |
| 6 | import { isEmpty } from 'lodash'; |
| 7 | import migrateSpacing from './migrations/migrateSpacing'; |
| 8 | |
| 9 | /** |
| 10 | * Migrate our stack and fillHorizontal space attributes to their devices. |
| 11 | * |
| 12 | * @param {Object} Props Function props. |
| 13 | * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run. |
| 14 | * @return {Object} Updated attributes. |
| 15 | * @since 1.4.0 |
| 16 | */ |
| 17 | export function migrateStackFill( { blockVersionLessThan } ) { |
| 18 | return function( attrs, existingAttrs, mode ) { |
| 19 | if ( 'css' === mode ) { |
| 20 | return attrs; |
| 21 | } |
| 22 | |
| 23 | if ( ! wasBlockJustInserted( existingAttrs ) && isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) { |
| 24 | if ( existingAttrs.stack || existingAttrs.fillHorizontalSpace ) { |
| 25 | if ( existingAttrs.stack ) { |
| 26 | attrs.stackTablet = true; |
| 27 | attrs.stackMobile = true; |
| 28 | } |
| 29 | |
| 30 | if ( existingAttrs.fillHorizontalSpace ) { |
| 31 | attrs.fillHorizontalSpaceTablet = true; |
| 32 | attrs.fillHorizontalSpaceMobile = true; |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | return attrs; |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | export const currentBlockVersion = 3; |
| 42 | |
| 43 | /** |
| 44 | * Migrate our Button Container attributes. |
| 45 | * |
| 46 | * @param {Object} Props Function props. |
| 47 | * @param {Object} Props.attributes The block attributes. |
| 48 | * @param {Object} Props.defaults The block defaults. |
| 49 | * @param {string} Props.mode The migration mode. |
| 50 | * @return {Object} Updated attributes. |
| 51 | * @since 1.8.0 |
| 52 | */ |
| 53 | export function migrateButtonContainerAttributes( { attributes, defaults, mode = '' } ) { |
| 54 | return migrationPipe( |
| 55 | attributes, |
| 56 | [ |
| 57 | setIsDynamic, |
| 58 | migrateStackFill( { |
| 59 | blockVersionLessThan: 2, |
| 60 | } ), |
| 61 | migrateSpacing( { |
| 62 | blockVersionLessThan: 3, |
| 63 | defaults, |
| 64 | attributesToMigrate: [ |
| 65 | 'marginTop', |
| 66 | 'marginRight', |
| 67 | 'marginBottom', |
| 68 | 'marginLeft', |
| 69 | ], |
| 70 | } ), |
| 71 | updateBlockVersion( currentBlockVersion ), |
| 72 | ], |
| 73 | mode |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | export default ( WrappedComponent ) => { |
| 78 | return ( props ) => { |
| 79 | const { |
| 80 | attributes, |
| 81 | setAttributes, |
| 82 | } = props; |
| 83 | |
| 84 | useEffect( () => { |
| 85 | const newAttributes = migrateButtonContainerAttributes( { |
| 86 | attributes, |
| 87 | defaults: getBlockType( 'generateblocks/button-container' )?.attributes, |
| 88 | } ); |
| 89 | |
| 90 | if ( ! isEmpty( newAttributes ) ) { |
| 91 | setAttributes( newAttributes ); |
| 92 | } |
| 93 | }, [] ); |
| 94 | |
| 95 | return ( <WrappedComponent { ...props } /> ); |
| 96 | }; |
| 97 | }; |
| 98 |