Skip to content

Commit

Permalink
Backcompatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Dec 7, 2020
1 parent 8c5cc3e commit a57bcb8
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
101 changes: 100 additions & 1 deletion packages/block-library/src/navigation/deprecated.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,113 @@
/**
* External dependencies
*/
import { omit } from 'lodash';
import { mapValues, omit } from 'lodash';

/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';

const TYPOGRAPHY_PRESET_DEPRECATION_MAP = {
fontStyle: 'var:preset|font-style|',
fontWeight: 'var:preset|font-weight|',
textDecoration: 'var:preset|text-decoration|',
textTransform: 'var:preset|text-transform|',
};

export default [
{
attributes: {
orientation: {
type: 'string',
},
textColor: {
type: 'string',
},
customTextColor: {
type: 'string',
},
rgbTextColor: {
type: 'string',
},
backgroundColor: {
type: 'string',
},
customBackgroundColor: {
type: 'string',
},
rgbBackgroundColor: {
type: 'string',
},
itemsJustification: {
type: 'string',
},
showSubmenuIcon: {
type: 'boolean',
default: true,
},
},
supports: {
align: [ 'wide', 'full' ],
anchor: true,
html: false,
inserter: true,
fontSize: true,
__experimentalFontStyle: true,
__experimentalFontWeight: true,
__experimentalTextTransform: true,
color: true,
__experimentalFontFamily: true,
__experimentalTextDecoration: true,
},
save() {
return <InnerBlocks.Content />;
},
isEligible( attributes ) {
if ( ! attributes.style || ! attributes.style.typography ) {
return false;
}
for ( const styleAttribute in TYPOGRAPHY_PRESET_DEPRECATION_MAP ) {
const attributeValue =
attributes.style.typography[ styleAttribute ];
if (
attributeValue &&
attributeValue.startsWith(
TYPOGRAPHY_PRESET_DEPRECATION_MAP[ styleAttribute ]
)
) {
return true;
}
}
return false;
},
migrate( attributes ) {
return {
...attributes,
style: {
...attributes.style,
typography: mapValues(
attributes.style.typography,
( value, key ) => {
const prefix =
TYPOGRAPHY_PRESET_DEPRECATION_MAP[ key ];
if ( prefix && value.startsWith( prefix ) ) {
const newValue = value.slice( prefix.length );
if (
'textDecoration' === key &&
'strikethrough' === newValue
) {
return 'line-through';
}
return newValue;
}
return value;
}
),
},
};
},
},
{
attributes: {
className: {
Expand Down
26 changes: 26 additions & 0 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,29 @@ function register_block_core_navigation() {
}

add_action( 'init', 'register_block_core_navigation' );

function block_core_navigation_typographic_presets_backcompatibility( $parsed_block ) {
if ( 'core/navigation' === $parsed_block['blockName'] ) {
$attribute_to_prefix_map = array(
'fontStyle' => 'var:preset|font-style|',
'fontWeight' => 'var:preset|font-weight|',
'textDecoration' => 'var:preset|text-decoration|',
'textTransform' => 'var:preset|text-transform|',
);
foreach ( $attribute_to_prefix_map as $style_attribute => $prefix ) {
if ( ! empty( $parsed_block['attrs']['style']['typography'][ $style_attribute ] ) ) {
$prefix_len = strlen( $prefix );
$attribute_value = &$parsed_block['attrs']['style']['typography'][ $style_attribute ];
if ( 0 === strncmp( $attribute_value, $prefix, $prefix_len ) ) {
$attribute_value = substr( $attribute_value, $prefix_len );
}
if ( 'textDecoration' === $style_attribute && 'strikethrough' === $attribute_value ) {
$attribute_value = 'line-through';
}
}
}
}
return $parsed_block;
}

add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' );

0 comments on commit a57bcb8

Please sign in to comment.