Skip to content
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
76 changes: 42 additions & 34 deletions packages/block-editor/src/components/block-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,53 @@ import { stack } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import useBlockDisplayInformation from '../use-block-display-information';
import BlockIcon from '../block-icon';
import BlockTransformationsMenu from './block-transformations-menu';
import BlockStylesMenu from './block-styles-menu';

const BlockSwitcher = ( { clientIds } ) => {
const { replaceBlocks } = useDispatch( 'core/block-editor' );
const {
blocks,
possibleBlockTransformations,
hasBlockStyles,
icon,
} = useSelect(
export const BlockSwitcherDropdownMenu = ( { clientIds, blocks } ) => {
const { replaceBlocks } = useDispatch( blockEditorStore );
const blockInformation = useBlockDisplayInformation( blocks[ 0 ].clientId );
const { possibleBlockTransformations, hasBlockStyles, icon } = useSelect(
( select ) => {
const {
getBlocksByClientId,
getBlockRootClientId,
getBlockTransformItems,
} = select( 'core/block-editor' );
const { getBlockRootClientId, getBlockTransformItems } = select(
blockEditorStore
);
const { getBlockStyles, getBlockType } = select( blocksStore );
const rootClientId = getBlockRootClientId(
castArray( clientIds )[ 0 ]
);
const _blocks = getBlocksByClientId( clientIds );
const isSingleBlockSelected = _blocks?.length === 1;
const firstBlock = !! _blocks?.length ? _blocks[ 0 ] : null;
const [ { name: firstBlockName } ] = blocks;
const _isSingleBlockSelected = blocks.length === 1;
const styles =
isSingleBlockSelected && getBlockStyles( firstBlock.name );
// When selection consists of blocks of multiple types, display an
// appropriate icon to communicate the non-uniformity.
const isSelectionOfSameType =
uniq( ( _blocks || [] ).map( ( { name } ) => name ) ).length ===
1;
_isSingleBlockSelected && getBlockStyles( firstBlockName );
let _icon;
if ( _isSingleBlockSelected ) {
_icon = blockInformation?.icon; // Take into account active block variations.
} else {
const isSelectionOfSameType =
uniq( blocks.map( ( { name } ) => name ) ).length === 1;
// When selection consists of blocks of multiple types, display an
// appropriate icon to communicate the non-uniformity.
_icon = isSelectionOfSameType
? getBlockType( firstBlockName )?.icon
: stack;
}
return {
blocks: _blocks,
possibleBlockTransformations:
_blocks && getBlockTransformItems( _blocks, rootClientId ),
possibleBlockTransformations: getBlockTransformItems(
blocks,
rootClientId
),
hasBlockStyles: !! styles?.length,
icon: isSelectionOfSameType
? getBlockType( firstBlock.name )?.icon
: stack,
icon: _icon,
};
},
[ clientIds ]
[ clientIds, blocks, blockInformation?.icon ]
);

if ( ! blocks?.length ) return null;

const onTransform = ( name ) =>
replaceBlocks( clientIds, switchToBlockType( blocks, name ) );

const hasPossibleBlockTransformations = !! possibleBlockTransformations.length;
if ( ! hasBlockStyles && ! hasPossibleBlockTransformations ) {
return (
Expand All @@ -83,7 +80,6 @@ const BlockSwitcher = ( { clientIds } ) => {
</ToolbarGroup>
);
}

const blockSwitcherLabel =
1 === blocks.length
? __( 'Change block type or style' )
Expand Down Expand Up @@ -152,4 +148,16 @@ const BlockSwitcher = ( { clientIds } ) => {
);
};

export const BlockSwitcher = ( { clientIds } ) => {
const blocks = useSelect(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is it necessary? Can't it be consolidated in the existing useSelect hook in the component. Those React hooks limitations aren't great but maybe there is a bit different way.

@gziolo gziolo Jan 5, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the original implementation, would it be possible to return the client id when there is a single block or the first block's client it when there is multiple blocks of the same type and pass it to useBlockDisplayInformation?

You wouldn't have to expose the icon anymore in such case as it would be calculated from the client id. Unless you want to use a general block type icon when there are multiple blocks selected 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why is it necessary?

This is just to avoid calling useBlockDisplayInformation if there are no blocks. It could be in one component but I personally don't see it bad... It just bails early.

Unless you want to use a general block type icon when there are multiple blocks selected

I do want this because when we have multiple selected blocks we had two options:

  1. Show the general block type icon, which seems fine for me as is informative and cheaper to calculate.
  2. Use the hook for every block to find out if the multiple selected blocks are of the same variation as well. For example if we had two embed Twitter blocks it should show Twitter icon. On the other hand if we had one Twitter and one Youtube, we should fallback to general block type as with the first clientId would show Twitter icon..

@gziolo gziolo Jan 6, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is just to avoid calling useBlockDisplayInformation if there are no blocks. It could be in one component but I personally don't see it bad... It just bails early.

Doesn't it create another level of components which add a layer of complexity? Is there something else that could be included there so there is no need to pass down all selected blocks?

I do want this because when we have multiple selected blocks we had two options:

Show the general block type icon, which seems fine for me as is informative and cheaper to calculate.

Yes, it makes sense 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is there something else that could be included there so there is no need to pass down all selected blocks?

Blocks are needed for various things like the transforms preview and the check for being of the same type.

Doesn't it create another level of components which add a layer of complexity?

Just to make sure I understand correctly - Your comment regards on why I have two components BlockSwitcher and BlockSwitcherDropdownMenu, correct?

If that's it, the only reason is just to bail early. I've no strong opinion on that though. I could use a single component, but I'd just have to call the useBlockDisplayInformation always. Do you believe I should do that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's hard to tell because there are multiple conditions to take into account and limitations of React hooks make it only harder. I will test tomorrow with the intent to merge it as is.

( select ) =>
select( blockEditorStore ).getBlocksByClientId( clientIds ),
[ clientIds ]
);

return !! blocks?.length ? (
<BlockSwitcherDropdownMenu clientIds={ clientIds } blocks={ blocks } />
) : null;
};

export default BlockSwitcher;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BlockSwitcher should render disabled block switcher with multi block of different types when no transforms 1`] = `
exports[` BlockSwitcherDropdownMenu should render disabled block switcher with multi block of different types when no transforms 1`] = `
<ToolbarGroup>
<ForwardRef(ToolbarButton)
className="block-editor-block-switcher__no-switcher-icon"
Expand All @@ -25,15 +25,15 @@ exports[`BlockSwitcher should render disabled block switcher with multi block of
</ToolbarGroup>
`;

exports[`BlockSwitcher should render enabled block switcher with multi block when transforms exist 1`] = `
exports[` BlockSwitcherDropdownMenu should render enabled block switcher with multi block when transforms exist 1`] = `
<ToolbarGroup>
<ForwardRef(ToolbarItem)>
<Component />
</ForwardRef(ToolbarItem)>
</ToolbarGroup>
`;

exports[`BlockSwitcher should render switcher with blocks 1`] = `
exports[` BlockSwitcherDropdownMenu should render switcher with blocks 1`] = `
<ToolbarGroup>
<ForwardRef(ToolbarItem)>
<Component />
Expand Down
40 changes: 25 additions & 15 deletions packages/block-editor/src/components/block-switcher/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ import { stack } from '@wordpress/icons';
/**
* Internal dependencies
*/
import BlockSwitcher from '../';
import { BlockSwitcher, BlockSwitcherDropdownMenu } from '../';

jest.mock( '@wordpress/data/src/components/use-select', () => jest.fn() );

describe( 'BlockSwitcher', () => {
test( 'should not render block switcher without blocks', () => {
useSelect.mockImplementation( () => ( {} ) );
const wrapper = shallow( <BlockSwitcher /> );
expect( wrapper.html() ).toBeNull();
} );
} );
describe( ' BlockSwitcherDropdownMenu', () => {
const headingBlock1 = {
attributes: {
content: [ 'How are you?' ],
Expand Down Expand Up @@ -97,56 +104,59 @@ describe( 'BlockSwitcher', () => {
unregisterBlockType( 'core/paragraph' );
} );

test( 'should not render block switcher without blocks', () => {
useSelect.mockImplementation( () => ( {} ) );
const wrapper = shallow( <BlockSwitcher /> );
expect( wrapper.html() ).toBeNull();
} );

test( 'should render switcher with blocks', () => {
useSelect.mockImplementation( () => ( {
blocks: [ headingBlock1 ],
possibleBlockTransformations: [
{ name: 'core/heading', frecency: 1 },
{ name: 'core/paragraph', frecency: 1 },
],
} ) );
const wrapper = shallow( <BlockSwitcher /> );
const wrapper = shallow(
<BlockSwitcherDropdownMenu blocks={ [ headingBlock1 ] } />
);
expect( wrapper ).toMatchSnapshot();
} );

test( 'should render disabled block switcher with multi block of different types when no transforms', () => {
useSelect.mockImplementation( () => ( {
blocks: [ headingBlock1, textBlock ],
possibleBlockTransformations: [],
icon: stack,
} ) );
const wrapper = shallow( <BlockSwitcher /> );
const wrapper = shallow(
<BlockSwitcherDropdownMenu
blocks={ [ headingBlock1, textBlock ] }
/>
);
expect( wrapper ).toMatchSnapshot();
} );

test( 'should render enabled block switcher with multi block when transforms exist', () => {
useSelect.mockImplementation( () => ( {
blocks: [ headingBlock1, headingBlock2 ],
possibleBlockTransformations: [
{ name: 'core/heading', frecency: 1 },
{ name: 'core/paragraph', frecency: 1 },
],
} ) );
const wrapper = shallow( <BlockSwitcher /> );
const wrapper = shallow(
<BlockSwitcherDropdownMenu
blocks={ [ headingBlock1, headingBlock2 ] }
/>
);
expect( wrapper ).toMatchSnapshot();
} );

describe( 'Dropdown', () => {
beforeAll( () => {
useSelect.mockImplementation( () => ( {
blocks: [ headingBlock1 ],
possibleBlockTransformations: [
{ name: 'core/paragraph', frecency: 3 },
],
} ) );
} );
const getDropdown = () => mount( <BlockSwitcher /> ).find( 'Dropdown' );
const getDropdown = () =>
mount(
<BlockSwitcherDropdownMenu blocks={ [ headingBlock1 ] } />
).find( 'Dropdown' );

test( 'should dropdown exist', () => {
expect( getDropdown() ).toHaveLength( 1 );
Expand Down