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

Highlight priority transforms on multi-select #55972

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import BlockVariationTransformations from './block-variation-transformations';
* @param {Object[]} possibleBlockTransformations The available block transformations.
* @return {Record<string, Object[]>} The grouped block transformations.
*/
function useGroupedTransforms( possibleBlockTransformations ) {
export function useGroupedTransforms( possibleBlockTransformations ) {
const priorityContentTranformationBlocks = {
'core/paragraph': 1,
'core/heading': 2,
Expand Down
71 changes: 70 additions & 1 deletion packages/block-editor/src/components/block-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import { copy } from '@wordpress/icons';
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 {
useGroupedTransforms,
default as BlockTransformationsMenu,
} from './block-transformations-menu';
import { useBlockVariationTransforms } from './block-variation-transformations';
import BlockStylesMenu from './block-styles-menu';
import PatternTransformationsMenu from './pattern-transformations-menu';
Expand Down Expand Up @@ -289,4 +292,70 @@ export const BlockSwitcher = ( { clientIds } ) => {
);
};

export function BlockPriorityTransforms( { clientIds } ) {
const { replaceBlocks, multiSelect } = useDispatch( blockEditorStore );
const { possibleBlockTransformations, blocks } = useSelect(
( select ) => {
const {
getBlockRootClientId,
getBlockTransformItems,
getBlocksByClientId,
} = select( blockEditorStore );
const rootClientId = getBlockRootClientId(
Array.isArray( clientIds ) ? clientIds[ 0 ] : clientIds
);
const _blocks = getBlocksByClientId( clientIds );
return {
possibleBlockTransformations: getBlockTransformItems(
_blocks,
rootClientId
),
blocks: _blocks,
};
},
[ clientIds ]
);
const { priorityTextTransformations } = useGroupedTransforms(
possibleBlockTransformations
);
function selectForMultipleBlocks( insertedBlocks ) {
if ( insertedBlocks.length > 1 ) {
multiSelect(
insertedBlocks[ 0 ].clientId,
insertedBlocks[ insertedBlocks.length - 1 ].clientId
);
}
}
function onBlockTransform( name ) {
const newBlocks = switchToBlockType( blocks, name );
replaceBlocks( clientIds, newBlocks );
selectForMultipleBlocks( newBlocks );
}

if ( ! priorityTextTransformations.length ) {
return null;
}

// Only show the priority transforms if all blocks are of the same type.
if ( new Set( blocks.map( ( { name } ) => name ) ).size !== 1 ) {
return;
}

return (
<ToolbarGroup>
{ priorityTextTransformations.map( ( item ) => (
<ToolbarButton
key={ item.name }
title={ item.title }
icon={ <BlockIcon icon={ item.icon } showColors /> }
onClick={ ( event ) => {
event.preventDefault();
onBlockTransform( item.name );
} }
/>
) ) }
</ToolbarGroup>
);
}

export default BlockSwitcher;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import { ToolbarGroup } from '@wordpress/components';
*/
import BlockMover from '../block-mover';
import BlockParentSelector from '../block-parent-selector';
import BlockSwitcher from '../block-switcher';
import {
default as BlockSwitcher,
BlockPriorityTransforms,
} from '../block-switcher';
import BlockControls from '../block-controls';
import __unstableBlockToolbarLastItem from './block-toolbar-last-item';
import BlockSettingsMenu from '../block-settings-menu';
Expand Down Expand Up @@ -118,6 +121,9 @@ const BlockToolbar = ( { hideDragHandle } ) => {
</ToolbarGroup>
</div>
) }
{ isMultiToolbar && (
<BlockPriorityTransforms clientIds={ blockClientIds } />
) }
{ shouldShowVisualToolbar && isMultiToolbar && (
<BlockGroupToolbar />
) }
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/heading/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const transforms = {
from: [
{
type: 'block',
isMultiBlock: true,
Copy link
Member Author

Choose a reason for hiding this comment

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

Even in the normal transform menu, transforming multiple paragraphs to multiple headings doesn't really make sense.

blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
attributes.map( ( { content, anchor, align: textAlign } ) =>
Expand Down
Loading