@gechiui/block-editor
Version:
116 lines (102 loc) • 2.53 kB
JavaScript
/**
* GeChiUI dependencies
*/
import { getBlockType } from '@gechiui/blocks';
import { Draggable } from '@gechiui/components';
import { useSelect, useDispatch } from '@gechiui/data';
import { useEffect, useRef } from '@gechiui/element';
/**
* Internal dependencies
*/
import BlockDraggableChip from './draggable-chip';
import useScrollWhenDragging from './use-scroll-when-dragging';
import { store as blockEditorStore } from '../../store';
const BlockDraggable = ( {
children,
clientIds,
cloneClassname,
onDragStart,
onDragEnd,
} ) => {
const { srcRootClientId, isDraggable, icon } = useSelect(
( select ) => {
const {
getBlockRootClientId,
getTemplateLock,
getBlockName,
} = select( blockEditorStore );
const rootClientId = getBlockRootClientId( clientIds[ 0 ] );
const templateLock = rootClientId
? getTemplateLock( rootClientId )
: null;
const blockName = getBlockName( clientIds[ 0 ] );
return {
srcRootClientId: rootClientId,
isDraggable: 'all' !== templateLock,
icon: getBlockType( blockName )?.icon,
};
},
[ clientIds ]
);
const isDragging = useRef( false );
const [
startScrolling,
scrollOnDragOver,
stopScrolling,
] = useScrollWhenDragging();
const { startDraggingBlocks, stopDraggingBlocks } = useDispatch(
blockEditorStore
);
// Stop dragging blocks if the block draggable is unmounted
useEffect( () => {
return () => {
if ( isDragging.current ) {
stopDraggingBlocks();
}
};
}, [] );
if ( ! isDraggable ) {
return children( { isDraggable: false } );
}
const transferData = {
type: 'block',
srcClientIds: clientIds,
srcRootClientId,
};
return (
<Draggable
cloneClassname={ cloneClassname }
__experimentalTransferDataType="gc-blocks"
transferData={ transferData }
onDragStart={ ( event ) => {
startDraggingBlocks( clientIds );
isDragging.current = true;
startScrolling( event );
if ( onDragStart ) {
onDragStart();
}
} }
onDragOver={ scrollOnDragOver }
onDragEnd={ () => {
stopDraggingBlocks();
isDragging.current = false;
stopScrolling();
if ( onDragEnd ) {
onDragEnd();
}
} }
__experimentalDragComponent={
<BlockDraggableChip count={ clientIds.length } icon={ icon } />
}
>
{ ( { onDraggableStart, onDraggableEnd } ) => {
return children( {
draggable: true,
onDragStart: onDraggableStart,
onDragEnd: onDraggableEnd,
} );
} }
</Draggable>
);
};
export default BlockDraggable;