migrateBorders.js
3 years ago
migrateGlobalStyleAttrs.js
3 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
migrateGlobalStyleAttrs.js
92 lines
| 1 | import { addFilter } from '@wordpress/hooks'; |
| 2 | import { migrateButtonAttributes } from '../withButtonLegacyMigration'; |
| 3 | import { migrateButtonContainerAttributes } from '../withButtonContainerLegacyMigration'; |
| 4 | import { migrateContainerAttributes } from '../withContainerLegacyMigration'; |
| 5 | import { migrateHeadlineAttributes } from '../withHeadlineLegacyMigration'; |
| 6 | import { migrateGridAttributes } from '../withGridLegacyMigration'; |
| 7 | import { migrateImageAttributes } from '../withImageLegacyMigration'; |
| 8 | import { isEmpty, isObject } from 'lodash'; |
| 9 | |
| 10 | /** |
| 11 | * This ensures that Global Styles pass the correct attributes |
| 12 | * to the CSS generation even when attributes change names. |
| 13 | */ |
| 14 | addFilter( |
| 15 | 'generateblocks.editor.cssAttrs', |
| 16 | 'generateblocks/migrateCssAttrs', |
| 17 | ( attributes, props ) => { |
| 18 | const { |
| 19 | name, |
| 20 | } = props; |
| 21 | |
| 22 | if ( ! attributes.useGlobalStyle ) { |
| 23 | return attributes; |
| 24 | } |
| 25 | |
| 26 | let migrateFunction = null; |
| 27 | |
| 28 | switch ( name ) { |
| 29 | case 'generateblocks/button': |
| 30 | migrateFunction = migrateButtonAttributes; |
| 31 | break; |
| 32 | |
| 33 | case 'generateblocks/button-container': |
| 34 | migrateFunction = migrateButtonContainerAttributes; |
| 35 | break; |
| 36 | |
| 37 | case 'generateblocks/container': |
| 38 | migrateFunction = migrateContainerAttributes; |
| 39 | break; |
| 40 | |
| 41 | case 'generateblocks/headline': |
| 42 | migrateFunction = migrateHeadlineAttributes; |
| 43 | break; |
| 44 | |
| 45 | case 'generateblocks/grid': |
| 46 | migrateFunction = migrateGridAttributes; |
| 47 | break; |
| 48 | |
| 49 | case 'generateblocks/image': |
| 50 | migrateFunction = migrateImageAttributes; |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | if ( null !== migrateFunction ) { |
| 55 | // This ensures that old attributes (coming from Global Styles) are migrated |
| 56 | // to their new attribute names so styling continues to work in the editor. |
| 57 | const migratedAttributes = migrateFunction( { |
| 58 | attributes: { |
| 59 | ...attributes, |
| 60 | // Use the global style block version if possible. |
| 61 | blockVersion: attributes.globalBlockVersion || 1, |
| 62 | }, |
| 63 | mode: 'css', |
| 64 | } ); |
| 65 | |
| 66 | const newAttributes = {}; |
| 67 | |
| 68 | // We only need these migrated attributes if a local attribute doesn't exist. |
| 69 | Object.entries( migratedAttributes ).forEach( ( [ attributeName, attributeValue ] ) => { |
| 70 | if ( isObject( attributeValue ) ) { |
| 71 | Object.entries( attributeValue ).forEach( ( [ objectProperty, objectValue ] ) => { |
| 72 | if ( isEmpty( attributes[ attributeName ]?.[ objectProperty ] ) ) { |
| 73 | newAttributes[ attributeName ] = { |
| 74 | ...attributes[ attributeName ], |
| 75 | ...newAttributes[ attributeName ], |
| 76 | [ objectProperty ]: objectValue, |
| 77 | }; |
| 78 | } |
| 79 | } ); |
| 80 | } else if ( isEmpty( attributes[ attributeName ] ) ) { |
| 81 | newAttributes[ attributeName ] = attributeValue; |
| 82 | } |
| 83 | } ); |
| 84 | |
| 85 | return { ...attributes, ...newAttributes }; |
| 86 | } |
| 87 | |
| 88 | return attributes; |
| 89 | }, |
| 90 | 1000 |
| 91 | ); |
| 92 |