From f12b78b7c4e830032d7f9a4641df40236644683b Mon Sep 17 00:00:00 2001 From: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Date: Tue, 6 Aug 2024 17:22:39 +0900 Subject: [PATCH] Drag & Drop: Fix unexpected row/galley creation logic (#64241) * 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 Co-authored-by: ramonjd Co-authored-by: andrewserong --- .../components/use-block-drop-zone/index.js | 80 +++++++++++++++---- .../src/components/use-on-block-drop/index.js | 10 +-- 2 files changed, 67 insertions(+), 23 deletions(-) diff --git a/packages/block-editor/src/components/use-block-drop-zone/index.js b/packages/block-editor/src/components/use-block-drop-zone/index.js index 857a132f1f9fa4..415ed5070d28da 100644 --- a/packages/block-editor/src/components/use-block-drop-zone/index.js +++ b/packages/block-editor/src/components/use-block-drop-zone/index.js @@ -301,8 +301,10 @@ export default function useBlockDropZone( { operation: 'insert', } ); - const { getBlockType } = useSelect( blocksStore ); + const { getBlockType, getBlockVariations, getGroupingBlockName } = + useSelect( blocksStore ); const { + canInsertBlockType, getBlockListSettings, getBlocks, getBlockIndex, @@ -310,6 +312,7 @@ export default function useBlockDropZone( { getBlockNamesByClientId, getAllowedBlocks, isDragging, + isGroupable, } = unlock( useSelect( blockEditorStore ) ); const { showInsertionPoint, @@ -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( { @@ -436,6 +484,10 @@ export default function useBlockDropZone( { showInsertionPoint, isDragging, startDragging, + canInsertBlockType, + getBlockVariations, + getGroupingBlockName, + isGroupable, ] ), 200 diff --git a/packages/block-editor/src/components/use-on-block-drop/index.js b/packages/block-editor/src/components/use-on-block-drop/index.js index 420cd398edfa39..75b9a44e166f5c 100644 --- a/packages/block-editor/src/components/use-on-block-drop/index.js +++ b/packages/block-editor/src/components/use-on-block-drop/index.js @@ -232,7 +232,6 @@ export default function useOnBlockDrop( getBlocksByClientId, getSettings, getBlock, - isGroupable, } = useSelect( blockEditorStore ); const { getGroupingBlockName } = useSelect( blocksStore ); const { @@ -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 ); @@ -325,7 +318,6 @@ export default function useOnBlockDrop( getBlockOrder, targetRootClientId, targetBlockIndex, - isGroupable, operation, replaceBlocks, getBlock,