Skip to content

Commit

Permalink
Drag & Drop: Fix unexpected row/galley creation logic (WordPress#64241)
Browse files Browse the repository at this point in the history
* Drag & Drop: Fix unexpected row/galley creation logic

* Add comment

* Remove function that checks if a row or gallery can be created

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: andrewserong <[email protected]>
  • Loading branch information
4 people authored Aug 6, 2024
1 parent 8a9ce7f commit f12b78b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
80 changes: 66 additions & 14 deletions packages/block-editor/src/components/use-block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,18 @@ export default function useBlockDropZone( {
operation: 'insert',
} );

const { getBlockType } = useSelect( blocksStore );
const { getBlockType, getBlockVariations, getGroupingBlockName } =
useSelect( blocksStore );
const {
canInsertBlockType,
getBlockListSettings,
getBlocks,
getBlockIndex,
getDraggedBlockClientIds,
getBlockNamesByClientId,
getAllowedBlocks,
isDragging,
isGroupable,
} = unlock( useSelect( blockEditorStore ) );
const {
showInsertionPoint,
Expand Down Expand Up @@ -385,21 +388,66 @@ export default function useBlockDropZone( {
};
} );

const dropTargetPosition = getDropTargetPosition(
blocksData,
{ x: event.clientX, y: event.clientY },
getBlockListSettings( targetRootClientId )?.orientation,
{
dropZoneElement,
parentBlockClientId,
parentBlockOrientation: parentBlockClientId
? getBlockListSettings( parentBlockClientId )
?.orientation
: undefined,
rootBlockIndex: getBlockIndex( targetRootClientId ),
}
);

const [ targetIndex, operation, nearestSide ] =
getDropTargetPosition(
blocksData,
{ x: event.clientX, y: event.clientY },
getBlockListSettings( targetRootClientId )?.orientation,
{
dropZoneElement,
parentBlockClientId,
parentBlockOrientation: parentBlockClientId
? getBlockListSettings( parentBlockClientId )
?.orientation
: undefined,
rootBlockIndex: getBlockIndex( targetRootClientId ),
}
dropTargetPosition;

if ( operation === 'group' ) {
const targetBlock = blocks[ targetIndex ];
const areAllImages = [
targetBlock.name,
...draggedBlockNames,
].every( ( name ) => name === 'core/image' );
const canInsertGalleryBlock = canInsertBlockType(
'core/gallery',
targetRootClientId
);
const areGroupableBlocks = isGroupable( [
targetBlock.clientId,
getDraggedBlockClientIds(),
] );
const groupBlockVariations = getBlockVariations(
getGroupingBlockName(),
'block'
);
const canInsertRow =
groupBlockVariations &&
groupBlockVariations.find(
( { name } ) => name === 'group-row'
);

// If the dragged blocks and the target block are all images,
// check if it is creatable either a Row variation or a Gallery block.
if (
areAllImages &&
! canInsertGalleryBlock &&
( ! areGroupableBlocks || ! canInsertRow )
) {
return;
}
// If the dragged blocks and the target block are not all images,
// check if it is creatable a Row variation.
if (
! areAllImages &&
( ! areGroupableBlocks || ! canInsertRow )
) {
return;
}
}

registry.batch( () => {
setDropTarget( {
Expand Down Expand Up @@ -436,6 +484,10 @@ export default function useBlockDropZone( {
showInsertionPoint,
isDragging,
startDragging,
canInsertBlockType,
getBlockVariations,
getGroupingBlockName,
isGroupable,
]
),
200
Expand Down
10 changes: 1 addition & 9 deletions packages/block-editor/src/components/use-on-block-drop/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ export default function useOnBlockDrop(
getBlocksByClientId,
getSettings,
getBlock,
isGroupable,
} = useSelect( blockEditorStore );
const { getGroupingBlockName } = useSelect( blocksStore );
const {
Expand All @@ -255,17 +254,11 @@ export default function useOnBlockDrop(
if ( ! Array.isArray( blocks ) ) {
blocks = [ blocks ];
}

const clientIds = getBlockOrder( targetRootClientId );
const clientId = clientIds[ targetBlockIndex ];
const blocksClientIds = blocks.map( ( block ) => block.clientId );
const areGroupableBlocks = isGroupable( [
...blocksClientIds,
clientId,
] );
if ( operation === 'replace' ) {
replaceBlocks( clientId, blocks, undefined, initialPosition );
} else if ( operation === 'group' && areGroupableBlocks ) {
} else if ( operation === 'group' ) {
const targetBlock = getBlock( clientId );
if ( nearestSide === 'left' ) {
blocks.push( targetBlock );
Expand Down Expand Up @@ -325,7 +318,6 @@ export default function useOnBlockDrop(
getBlockOrder,
targetRootClientId,
targetBlockIndex,
isGroupable,
operation,
replaceBlocks,
getBlock,
Expand Down

0 comments on commit f12b78b

Please sign in to comment.