Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block variations transformations #26687

Merged
merged 21 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions packages/block-editor/src/components/block-card/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
*/
import BlockIcon from '../block-icon';

function BlockCard( { blockType } ) {
function BlockCard( { blockType: { icon, title, description } } ) {
return (
<div className="block-editor-block-card">
<BlockIcon icon={ blockType.icon } showColors />
<BlockIcon icon={ icon } showColors />
<div className="block-editor-block-card__content">
<h2 className="block-editor-block-card__title">
{ blockType.title }
</h2>
<h2 className="block-editor-block-card__title">{ title }</h2>
<span className="block-editor-block-card__description">
{ blockType.description }
{ description }
</span>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/block-inspector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import InspectorAdvancedControls from '../inspector-advanced-controls';
import BlockStyles from '../block-styles';
import MultiSelectionInspector from '../multi-selection-inspector';
import DefaultStylePicker from '../default-style-picker';
import BlockVariationTransforms from '../block-variation-transforms';
const BlockInspector = ( {
blockType,
count,
Expand Down Expand Up @@ -66,6 +67,7 @@ const BlockInspector = ( {
return (
<div className="block-editor-block-inspector">
<BlockCard blockType={ blockType } />
<BlockVariationTransforms blockClientId={ selectedBlockClientId } />
{ hasBlockStyles && (
<div>
<PanelBody title={ __( 'Styles' ) }>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* External dependencies
*/
import { isMatch } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
DropdownMenu,
MenuGroup,
MenuItemsChoice,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';
import { chevronDown } from '@wordpress/icons';

export const getMatchingVariationName = ( blockAttributes, variations ) => {
if ( ! variations || ! blockAttributes ) return;
const matches = variations.filter( ( { attributes } ) => {
if ( ! attributes || ! Object.keys( attributes ).length ) return false;
return isMatch( blockAttributes, attributes );
} );
if ( matches.length !== 1 ) return;
return matches[ 0 ].name;
};

function __experimentalBlockVariationTransforms( { blockClientId } ) {
const [ selectedValue, setSelectedValue ] = useState();
const { updateBlockAttributes } = useDispatch( 'core/block-editor' );
const { variations, blockAttributes } = useSelect(
( select ) => {
const { getBlockVariations } = select( 'core/blocks' );
const { getBlockName, getBlockAttributes } = select(
'core/block-editor'
);
const blockName = blockClientId && getBlockName( blockClientId );
return {
variations:
blockName && getBlockVariations( blockName, 'transform' ),
blockAttributes: getBlockAttributes( blockClientId ),
};
},
[ blockClientId ]
);
useEffect( () => {
setSelectedValue(
getMatchingVariationName( blockAttributes, variations )
);
}, [ blockAttributes, variations ] );
if ( ! variations?.length ) return null;

const selectOptions = variations.map(
( { name, title, description } ) => ( {
value: name,
label: title,
info: description,
} )
);
const onSelectVariation = ( variationName ) => {
updateBlockAttributes( blockClientId, {
...variations.find( ( { name } ) => name === variationName )
.attributes,
} );
};
const baseClass = 'block-editor-block-variation-transforms';
return (
<DropdownMenu
className={ baseClass }
label={ __( 'Transform to variation' ) }
text={ __( 'Transform to variation' ) }
popoverProps={ {
position: 'bottom center',
className: `${ baseClass }__popover`,
} }
icon={ chevronDown }
toggleProps={ { iconPosition: 'right' } }
>
{ () => (
<div className={ `${ baseClass }__container` }>
<MenuGroup>
<MenuItemsChoice
choices={ selectOptions }
value={ selectedValue }
onSelect={ onSelectVariation }
/>
</MenuGroup>
</div>
) }
</DropdownMenu>
);
}

export default __experimentalBlockVariationTransforms;
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Internal dependencies
*/
import { getMatchingVariationName } from '../index';

describe( 'BlockVariationTransforms', () => {
describe( 'getMatchingVariationName', () => {
describe( 'should not find a match', () => {
it( 'when no variations or attributes passed', () => {
expect(
getMatchingVariationName( null, { content: 'hi' } )
).toBeUndefined();
expect( getMatchingVariationName( {} ) ).toBeUndefined();
} );
it( 'when no variation matched', () => {
const variations = [
{ name: 'one', attributes: { level: 1 } },
{ name: 'two', attributes: { level: 2 } },
];
expect(
getMatchingVariationName( { level: 4 }, variations )
).toBeUndefined();
} );
it( 'when more than one match found', () => {
const variations = [
{ name: 'one', attributes: { level: 1 } },
{ name: 'two', attributes: { level: 1, content: 'hi' } },
];
expect(
getMatchingVariationName(
{ level: 1, content: 'hi', other: 'prop' },
variations
)
).toBeUndefined();
} );
it( 'when variation is a superset of attributes', () => {
const variations = [
{ name: 'one', attributes: { level: 1, content: 'hi' } },
];
expect(
getMatchingVariationName(
{ level: 1, other: 'prop' },
variations
)
).toBeUndefined();
} );
} );
describe( 'should find a match', () => {
it( 'when variation has one attribute', () => {
const variations = [
{ name: 'one', attributes: { level: 1 } },
{ name: 'two', attributes: { level: 2 } },
];
expect(
getMatchingVariationName(
{ level: 2, content: 'hi', other: 'prop' },
variations
)
).toEqual( 'two' );
} );
it( 'when variation has many attributes', () => {
const variations = [
{ name: 'one', attributes: { level: 1, content: 'hi' } },
{ name: 'two', attributes: { level: 2 } },
];
expect(
getMatchingVariationName(
{ level: 1, content: 'hi', other: 'prop' },
variations
)
).toEqual( 'one' );
} );
} );
} );
} );
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export { BlockNavigationBlockFill as __experimentalBlockNavigationBlockFill } fr
export { default as __experimentalBlockNavigationEditor } from './block-navigation/editor';
export { default as __experimentalBlockNavigationTree } from './block-navigation/tree';
export { default as __experimentalBlockVariationPicker } from './block-variation-picker';
export { default as __experimentalBlockVariationTransforms } from './block-variation-transforms';
export { default as BlockVerticalAlignmentToolbar } from './block-vertical-alignment-toolbar';
export { default as ButtonBlockerAppender } from './button-block-appender';
export { default as ColorPalette } from './color-palette';
Expand Down
25 changes: 2 additions & 23 deletions packages/block-library/src/navigation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,20 @@ import metadata from './block.json';
import edit from './edit';
import save from './save';
import deprecated from './deprecated';
import variations from './variations';

const { name } = metadata;

export { metadata, name };

export const settings = {
title: __( 'Navigation' ),

icon,

description: __(
'A collection of blocks that allow visitors to get around your site.'
),

keywords: [ __( 'menu' ), __( 'navigation' ), __( 'links' ) ],

variations: [
{
name: 'horizontal',
isDefault: true,
title: __( 'Navigation (horizontal)' ),
description: __( 'Links shown in a row.' ),
attributes: { orientation: 'horizontal' },
},
{
name: 'vertical',
title: __( 'Navigation (vertical)' ),
description: __( 'Links shown in a column.' ),
attributes: { orientation: 'vertical' },
},
],

variations,
example: {
innerBlocks: [
{
Expand Down Expand Up @@ -71,10 +53,7 @@ export const settings = {
},
],
},

edit,

save,

deprecated,
};
Loading