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
migrateBorders.js
154 lines
| 1 | import isNumeric from '../../utils/is-numeric'; |
| 2 | import isBlockVersionLessThan from '../../utils/check-block-version'; |
| 3 | import { addToAttrsObject } from './utils'; |
| 4 | import hexToRGBA from '../../utils/hex-to-rgba'; |
| 5 | |
| 6 | /** |
| 7 | * Build an object with new migrated attributes and old attributes reverted to defaults. |
| 8 | * |
| 9 | * @param {Object} Props Function props. |
| 10 | * @param {Array} Props.attributesToMigrate The attributes we want to migrate. |
| 11 | * @param {Object} Props.attributes The existing block attributes. |
| 12 | * @param {Object} Props.defaults The block defaults. |
| 13 | * @return {Object} New attributes. |
| 14 | * @since 1.8.0 |
| 15 | */ |
| 16 | function buildBorderAttributes( { attributesToMigrate = [], attributes = {}, defaults = {} } ) { |
| 17 | function unitValue( name ) { |
| 18 | if ( name.startsWith( 'borderSize' ) ) { |
| 19 | return 'px'; |
| 20 | } |
| 21 | |
| 22 | if ( name.startsWith( 'borderRadius' ) ) { |
| 23 | return attributes.borderRadiusUnit; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | const newAttributes = {}; |
| 28 | const oldAttributes = {}; |
| 29 | |
| 30 | [ '', 'Tablet', 'Mobile' ].forEach( ( device ) => { |
| 31 | attributesToMigrate.forEach( ( attribute ) => { |
| 32 | const oldValue = attributes[ attribute + device ]; |
| 33 | |
| 34 | if ( isNumeric( oldValue ) ) { |
| 35 | let attributeName = attribute; |
| 36 | |
| 37 | switch ( attribute ) { |
| 38 | case 'borderSizeTop': |
| 39 | attributeName = 'borderTopWidth'; |
| 40 | break; |
| 41 | |
| 42 | case 'borderSizeRight': |
| 43 | attributeName = 'borderRightWidth'; |
| 44 | break; |
| 45 | |
| 46 | case 'borderSizeBottom': |
| 47 | attributeName = 'borderBottomWidth'; |
| 48 | break; |
| 49 | |
| 50 | case 'borderSizeLeft': |
| 51 | attributeName = 'borderLeftWidth'; |
| 52 | break; |
| 53 | |
| 54 | case 'borderRadiusTopLeft': |
| 55 | attributeName = 'borderTopLeftRadius'; |
| 56 | break; |
| 57 | |
| 58 | case 'borderRadiusTopRight': |
| 59 | attributeName = 'borderTopRightRadius'; |
| 60 | break; |
| 61 | |
| 62 | case 'borderRadiusBottomRight': |
| 63 | attributeName = 'borderBottomRightRadius'; |
| 64 | break; |
| 65 | |
| 66 | case 'borderRadiusBottomLeft': |
| 67 | attributeName = 'borderBottomLeftRadius'; |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | if ( attributeName ) { |
| 72 | newAttributes[ attributeName + device ] = oldValue + unitValue( attribute ); |
| 73 | const hasDefaults = Object.keys( defaults ).length; |
| 74 | |
| 75 | if ( attributeName.includes( 'Width' ) ) { |
| 76 | // We used to manually use "solid" is a borderWidth existed. |
| 77 | newAttributes[ attributeName.replace( 'Width', 'Style' ) + device ] = 'solid'; |
| 78 | |
| 79 | if ( attributes.borderColor ) { |
| 80 | newAttributes[ attributeName.replace( 'Width', 'Color' ) + device ] = hexToRGBA( attributes.borderColor, attributes.borderColorOpacity ); |
| 81 | |
| 82 | if ( hasDefaults ) { |
| 83 | oldAttributes.borderColor = defaults.borderColor?.default; |
| 84 | oldAttributes.borderColorOpacity = defaults.borderColorOpacity?.default; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if ( attributes.borderColorHover ) { |
| 89 | newAttributes[ attributeName.replace( 'Width', 'ColorHover' ) + device ] = hexToRGBA( attributes.borderColorHover, attributes.borderColorHoverOpacity ); |
| 90 | |
| 91 | if ( hasDefaults ) { |
| 92 | oldAttributes.borderColorHover = defaults.borderColorHover?.default; |
| 93 | oldAttributes.borderColorHoverOpacity = defaults.borderColorHoverOpacity?.default; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if ( attributes.borderColorCurrent ) { |
| 98 | newAttributes[ attributeName.replace( 'Width', 'ColorCurrent' ) + device ] = attributes.borderColorCurrent; |
| 99 | |
| 100 | if ( hasDefaults ) { |
| 101 | oldAttributes.borderColorCurrent = defaults.borderColorCurrent?.default; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if ( hasDefaults ) { |
| 107 | oldAttributes[ attribute + device ] = defaults[ attribute + device ]?.default |
| 108 | ? defaults[ attribute + device ].default |
| 109 | : ''; |
| 110 | |
| 111 | if ( attribute.startsWith( 'borderRadius' ) ) { |
| 112 | oldAttributes.borderRadiusUnit = defaults.borderRadiusUnit.default; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } ); |
| 118 | } ); |
| 119 | |
| 120 | return { newAttributes, oldAttributes }; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Build a borders object to be used by setAttributes with our new attributes. |
| 125 | * |
| 126 | * @param {Object} Props Function props. |
| 127 | * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run. |
| 128 | * @param {Object} Props.defaults The block defaults. |
| 129 | * @param {Array} Props.attributesToMigrate The attributes we want to migrate. |
| 130 | * @return {Object} New attributes. |
| 131 | * @since 1.8.0 |
| 132 | */ |
| 133 | export default function migrateBorders( { blockVersionLessThan, defaults = {}, attributesToMigrate = [] } ) { |
| 134 | return function( attrs, existingAttrs ) { |
| 135 | if ( isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) { |
| 136 | const newSpacing = buildBorderAttributes( { |
| 137 | attributesToMigrate, |
| 138 | attributes: { ...existingAttrs, ...attrs }, |
| 139 | defaults, |
| 140 | } ); |
| 141 | |
| 142 | attrs = addToAttrsObject( { |
| 143 | attrs, |
| 144 | attributeName: 'borders', |
| 145 | existingAttrs: existingAttrs.borders, |
| 146 | newAttrs: newSpacing.newAttributes, |
| 147 | oldAttrs: newSpacing.oldAttributes, |
| 148 | } ); |
| 149 | } |
| 150 | |
| 151 | return attrs; |
| 152 | }; |
| 153 | } |
| 154 |