-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Drag & Drop: Fix unexpected row/gallery creation logic #64241
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,6 +274,27 @@ export function isDropTargetValid( | |
return areBlocksAllowed && targetMatchesDraggedBlockParents; | ||
} | ||
|
||
/** | ||
* Check if the Row or the Gallery can be created with the dragged | ||
* blocks and the target block. | ||
* @param {boolean} areAllImages | ||
* @param {boolean} canInsertGalleryBlock | ||
* @param {boolean} areGroupableBlocks | ||
* @param {boolean} canInsertRow | ||
* @return {boolean} Whether Row block or Gallery can be created. | ||
*/ | ||
function canCreateRowOrGallery( | ||
areAllImages, | ||
canInsertGalleryBlock, | ||
areGroupableBlocks, | ||
canInsertRow | ||
) { | ||
if ( areAllImages ) { | ||
return canInsertGalleryBlock || ( areGroupableBlocks && canInsertRow ); | ||
} | ||
return areGroupableBlocks && canInsertRow; | ||
} | ||
|
||
/** | ||
* @typedef {Object} WPBlockDropZoneConfig | ||
* @property {?HTMLElement} dropZoneElement Optional element to be used as the drop zone. | ||
|
@@ -301,15 +322,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, | ||
|
@@ -385,21 +409,59 @@ 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; | ||
|
||
// Checks if it is creatable either a Row variation or a Gallery block from | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checking, so this condition checks if a Row (Group) block or a Gallery block can be created after drop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it checks if it can be created before the drop. This prevents unintended inserters from appearing. |
||
// the dragged block and the target block. | ||
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' | ||
); | ||
const _canCreateRowOrGallery = canCreateRowOrGallery( | ||
areAllImages, | ||
canInsertGalleryBlock, | ||
areGroupableBlocks, | ||
canInsertRow | ||
); | ||
if ( ! _canCreateRowOrGallery ) { | ||
return; | ||
} | ||
} | ||
|
||
registry.batch( () => { | ||
setDropTarget( { | ||
|
@@ -436,6 +498,10 @@ export default function useBlockDropZone( { | |
showInsertionPoint, | ||
isDragging, | ||
startDragging, | ||
canInsertBlockType, | ||
getBlockVariations, | ||
getGroupingBlockName, | ||
isGroupable, | ||
] | ||
), | ||
200 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, it is guaranteed that at least one of the Row block or Gallery block can be created, so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just double-checking, is that because this check is now happening in the It looks like that other code also calls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes. If the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarifying! |
||
} 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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This process might not even need to be made into a function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Either or" I think.
It doesn't abstract much and could be inline and just as neat. Unless it needs unit testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same. Since it's only used in one place, I'd lean slightly toward inlining unless it needs unit testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the function and inlined it here 👍