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

Only show transforms to blocks that can be inserted on the root block; Order transforms by frecency; #7184

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
33 changes: 20 additions & 13 deletions packages/editor/src/components/block-switcher/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { castArray, get, some } from 'lodash';
import { castArray, filter, first, get, mapKeys, orderBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -36,19 +36,28 @@ export class BlockSwitcher extends Component {
}

render() {
const { blocks, onTransform, isLocked } = this.props;
const { blocks, onTransform, inserterItems } = this.props;
const { hoveredClassName } = this.state;

if ( ! blocks || ! blocks.length ) {
return null;
}

const allowedBlocks = getPossibleBlockTransformations( blocks );
const itemsByName = mapKeys( inserterItems, ( { name } ) => name );
const possibleBlockTransformations = orderBy(
filter(
getPossibleBlockTransformations( blocks ),
( block ) => !! itemsByName[ block.name ]
),
( block ) => itemsByName[ block.name ].frecency,
'desc'
);

const sourceBlockName = blocks[ 0 ].name;
const blockType = getBlockType( sourceBlockName );
const hasStyles = blocks.length === 1 && get( blockType, [ 'styles' ], [] ).length !== 0;

if ( ! hasStyles && ( ! allowedBlocks.length || isLocked ) ) {
if ( ! hasStyles && ! possibleBlockTransformations.length ) {
return null;
}

Expand Down Expand Up @@ -97,13 +106,13 @@ export class BlockSwitcher extends Component {
/>
</PanelBody>
}
{ allowedBlocks.length !== 0 && ! isLocked &&
{ possibleBlockTransformations.length !== 0 &&
<PanelBody
title={ __( 'Transform To:' ) }
initialOpen
>
<BlockTypesList
items={ allowedBlocks.map( ( destinationBlockType ) => ( {
items={ possibleBlockTransformations.map( ( destinationBlockType ) => ( {
id: destinationBlockType.name,
icon: destinationBlockType.icon,
title: destinationBlockType.title,
Expand Down Expand Up @@ -131,14 +140,12 @@ export class BlockSwitcher extends Component {
}

export default compose(
withSelect( ( select, ownProps ) => {
const { getBlock, getBlockRootClientId, getTemplateLock } = select( 'core/editor' );
withSelect( ( select, { clientIds } ) => {
const { getBlocksByClientId, getBlockRootClientId, getInserterItems } = select( 'core/editor' );
const rootClientId = getBlockRootClientId( first( castArray( clientIds ) ) );
return {
blocks: ownProps.clientIds.map( getBlock ),
isLocked: some(
castArray( ownProps.clientIds ),
( clientId ) => !! getTemplateLock( getBlockRootClientId( clientId ) )
),
blocks: getBlocksByClientId( clientIds ),
inserterItems: getInserterItems( rootClientId ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you looked into using canInsertBlockType instead of getInserterItems? The latter takes reusable blocks into account which is unnecessary in this case because you can't transform a block into an existing reusable block. It's better that we do as little work as possible to keep the UI feeling responsive.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have thought about this and it seemed getInserterItems was the best option.
We don't want just to know if a block can be inserted we want to know all the blocks that can be inserted so we can render the UI. We can not do a simple filter using canInsertBlockType inline because that would create a new array reference each time triggering unnecessary rerenders. So the alternative would be to create a new selector that just returns all the allowed blocks without the shared blocks. Besides the available blocks, we also need the frecency ( to sort the items ) and getInserterItems already provides it.
getInserterItems is cached and used in the inserter which is rerendered each time a block changes so I think each time we use the selector here we are just retrieving the value from the cache. If we had a specific selector we would not be able to share the cache.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍 – thanks for explaining!

};
} ),
withDispatch( ( dispatch, ownProps ) => ( {
Expand Down
16 changes: 14 additions & 2 deletions packages/editor/src/components/block-switcher/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ describe( 'BlockSwitcher', () => {
const blocks = [
headingBlock1,
];
const wrapper = shallow( <BlockSwitcher blocks={ blocks } /> );
const inserterItems = [
{ name: 'core/paragraph', frecency: 1 },
];

const wrapper = shallow( <BlockSwitcher blocks={ blocks } inserterItems={ inserterItems } /> );

expect( wrapper ).toMatchSnapshot();
} );
Expand Down Expand Up @@ -122,9 +126,17 @@ describe( 'BlockSwitcher', () => {
headingBlock1,
];

const inserterItems = [
{ name: 'core/quote', frecency: 1 },
{ name: 'core/cover-image', frecency: 2 },
{ name: 'core/paragraph', frecency: 3 },
{ name: 'core/heading', frecency: 4 },
{ name: 'core/text', frecency: 5 },
];

const onTransformStub = jest.fn();
const getDropdown = () => {
const blockSwitcher = shallow( <BlockSwitcher blocks={ blocks } onTransform={ onTransformStub } /> );
const blockSwitcher = shallow( <BlockSwitcher blocks={ blocks } onTransform={ onTransformStub } inserterItems={ inserterItems } /> );
return blockSwitcher.find( 'Dropdown' );
};

Expand Down