Skip to content
Merged
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
Commands: Combine selectors in 'useTransformCommands'
  • Loading branch information
Mamaduka committed Dec 28, 2023
commit ee8df806fe13353a9b0b5052b2cceefa63a2403c
39 changes: 18 additions & 21 deletions packages/block-editor/src/components/use-block-commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,37 @@ import BlockIcon from '../block-icon';
import { store as blockEditorStore } from '../../store';

export const useTransformCommands = () => {
const { clientIds } = useSelect( ( select ) => {
const { getSelectedBlockClientIds } = select( blockEditorStore );
const selectedBlockClientIds = getSelectedBlockClientIds();

return {
clientIds: selectedBlockClientIds,
};
}, [] );
const blocks = useSelect(
( select ) =>
select( blockEditorStore ).getBlocksByClientId( clientIds ),
[ clientIds ]
);
const { replaceBlocks, multiSelect } = useDispatch( blockEditorStore );
const { possibleBlockTransformations, canRemove } = useSelect(
( select ) => {
const { blocks, clientIds, canRemove, possibleBlockTransformations } =
useSelect( ( select ) => {
const {
getBlockRootClientId,
getBlockTransformItems,
getSelectedBlockClientIds,
getBlocksByClientId,
canRemoveBlocks,
} = select( blockEditorStore );

const selectedBlockClientIds = getSelectedBlockClientIds();
const selectedBlocks = getBlocksByClientId(
selectedBlockClientIds
);
const rootClientId = getBlockRootClientId(
Array.isArray( clientIds ) ? clientIds[ 0 ] : clientIds
Copy link
Member

Choose a reason for hiding this comment

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

getSelectedBlockClientIds() will always be an array. Nice 👍

selectedBlockClientIds[ 0 ]
);
return {
blocks: selectedBlocks,
clientIds: selectedBlockClientIds,
possibleBlockTransformations: getBlockTransformItems(
blocks,
selectedBlocks,
rootClientId
),
canRemove: canRemoveBlocks(
selectedBlockClientIds,
rootClientId
),
canRemove: canRemoveBlocks( clientIds, rootClientId ),
};
},
[ clientIds, blocks ]
);
}, [] );

const isTemplate = blocks.length === 1 && isTemplatePart( blocks[ 0 ] );

Expand Down