PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.3
GenerateBlocks v1.8.3
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / hoc / migrations / migrateTypography.js
generateblocks / src / hoc / migrations Last commit date
migrateBorders.js 3 years ago migrateGlobalStyleAttrs.js 2 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
migrateTypography.js
99 lines
1 import isNumeric from '../../utils/is-numeric';
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 {Array} Props.attributesToMigrate The attributes we want to migrate.
10 * @param {Object} Props.attributes The existing block attributes.
11 * @param {Object} Props.defaults The block defaults.
12 * @return {Object} New attributes.
13 * @since 1.8.0
14 */
15 function buildTypographyAttributes( { attributesToMigrate, attributes = {}, defaults = {} } ) {
16 function unitValue( name ) {
17 if ( 'fontSize' === name ) {
18 return attributes.fontSizeUnit;
19 }
20
21 if ( 'lineHeight' === name ) {
22 return attributes.lineHeightUnit;
23 }
24
25 if ( 'letterSpacing' === name ) {
26 return 'em';
27 }
28 }
29
30 const newAttributes = {};
31 const oldAttributes = {};
32
33 [ '', 'Tablet', 'Mobile' ].forEach( ( device ) => {
34 attributesToMigrate.forEach( ( attribute ) => {
35 const oldValue = attributes[ attribute + device ];
36
37 if ( oldValue || isNumeric( oldValue ) ) {
38 const unit = unitValue( attribute );
39
40 if ( isNumeric( oldValue ) && unit ) {
41 newAttributes[ attribute + device ] = oldValue + unit;
42 } else if ( 'alignment' === attribute ) {
43 newAttributes[ 'textAlign' + device ] = oldValue;
44 } else {
45 newAttributes[ attribute + device ] = oldValue;
46 }
47
48 if ( Object.keys( defaults ).length ) {
49 oldAttributes[ attribute + device ] = defaults[ attribute + device ]?.default
50 ? defaults[ attribute + device ].default
51 : '';
52
53 if ( attribute.startsWith( 'fontSize' ) ) {
54 oldAttributes.fontSizeUnit = defaults.fontSizeUnit.default;
55 }
56
57 if ( attribute.startsWith( 'lineHeight' ) ) {
58 oldAttributes.lineHeightUnit = defaults.lineHeightUnit.default;
59 }
60 }
61 }
62 } );
63 } );
64
65 return { newAttributes, oldAttributes };
66 }
67
68 /**
69 * Build a typography object to be used by setAttributes with our new attributes.
70 *
71 * @param {Object} Props Function props.
72 * @param {number} Props.blockVersionLessThan The version blocks should be less than for this to run.
73 * @param {Object} Props.defaults The block defaults.
74 * @param {Array} Props.attributesToMigrate The attributes we want to migrate.
75 * @return {Object} New attributes.
76 * @since 1.8.0
77 */
78 export default function migrateTypography( { blockVersionLessThan, defaults = {}, attributesToMigrate = [] } ) {
79 return function( attrs, existingAttrs ) {
80 if ( isBlockVersionLessThan( existingAttrs.blockVersion, blockVersionLessThan ) ) {
81 const newTypography = buildTypographyAttributes( {
82 attributesToMigrate,
83 attributes: { ...existingAttrs, ...attrs },
84 defaults,
85 } );
86
87 attrs = addToAttrsObject( {
88 attrs,
89 attributeName: 'typography',
90 existingAttrs: existingAttrs.typography,
91 newAttrs: newTypography.newAttributes,
92 oldAttrs: newTypography.oldAttributes,
93 } );
94 }
95
96 return attrs;
97 };
98 }
99