Skip to content

Commit

Permalink
Try fixing list view drag-and-drop
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Apr 10, 2023
1 parent 8f29b49 commit b0ae1b3
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/block-editor/src/components/block-draggable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const BlockDraggable = ( {
[ clientIds ]
);
const isDragging = useRef( false );
const timerRef = useRef();
const [ startScrolling, scrollOnDragOver, stopScrolling ] =
useScrollWhenDragging();

Expand All @@ -44,12 +45,16 @@ const BlockDraggable = ( {

// Stop dragging blocks if the block draggable is unmounted.
useEffect( () => {
const timer = timerRef.current;
return () => {
if ( isDragging.current ) {
stopDraggingBlocks();
}
if ( timer ) {
window.cancelAnimationFrame( timer );
}
};
}, [] );
}, [ stopDraggingBlocks ] );

if ( ! isDraggable ) {
return children( { draggable: false } );
Expand All @@ -67,14 +72,18 @@ const BlockDraggable = ( {
__experimentalTransferDataType="wp-blocks"
transferData={ transferData }
onDragStart={ ( event ) => {
startDraggingBlocks( clientIds );
isDragging.current = true;
// Below code will hide the source element, which is triggered synchronously
// and will break the drag event. So we need to defer it to the next frame.
timerRef.current = window.requestAnimationFrame( () => {
startDraggingBlocks( clientIds );
isDragging.current = true;

startScrolling( event );
startScrolling( event );

if ( onDragStart ) {
onDragStart();
}
if ( onDragStart ) {
onDragStart();
}
} );
} }
onDragOver={ scrollOnDragOver }
onDragEnd={ () => {
Expand Down

0 comments on commit b0ae1b3

Please sign in to comment.