mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-14 12:38:39 +00:00
59 lines
1.7 KiB
JavaScript
59 lines
1.7 KiB
JavaScript
import React from 'react';
|
|
import { useDragLayer } from 'react-dnd';
|
|
import { ItemTypes } from './ItemTypes';
|
|
import { BoxDragPreview } from './BoxDragPreview';
|
|
import { snapToGrid } from './snapToGrid';
|
|
const layerStyles = {
|
|
position: 'fixed',
|
|
pointerEvents: 'none',
|
|
zIndex: 100,
|
|
left: 0,
|
|
top: 0,
|
|
width: '100%',
|
|
height: '100%',
|
|
};
|
|
function getItemStyles(initialOffset, currentOffset, isSnapToGrid) {
|
|
if (!initialOffset || !currentOffset) {
|
|
return {
|
|
display: 'none',
|
|
};
|
|
}
|
|
let { x, y } = currentOffset;
|
|
if (isSnapToGrid) {
|
|
x -= initialOffset.x;
|
|
y -= initialOffset.y;
|
|
[x, y] = snapToGrid(x, y);
|
|
x += initialOffset.x;
|
|
y += initialOffset.y;
|
|
}
|
|
const transform = `translate(${x}px, ${y}px)`;
|
|
return {
|
|
transform,
|
|
WebkitTransform: transform,
|
|
};
|
|
}
|
|
export const CustomDragLayer = (props) => {
|
|
const { itemType, isDragging, item, initialOffset, currentOffset, } = useDragLayer((monitor) => ({
|
|
item: monitor.getItem(),
|
|
itemType: monitor.getItemType(),
|
|
initialOffset: monitor.getInitialSourceClientOffset(),
|
|
currentOffset: monitor.getSourceClientOffset(),
|
|
isDragging: monitor.isDragging(),
|
|
}));
|
|
function renderItem() {
|
|
switch (itemType) {
|
|
case ItemTypes.BOX:
|
|
return <BoxDragPreview component={item.component}/>;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
if (!isDragging) {
|
|
return null;
|
|
}
|
|
return (<div style={layerStyles}>
|
|
<div style={getItemStyles(initialOffset, currentOffset, props.snapToGrid)}>
|
|
{renderItem()}
|
|
</div>
|
|
</div>);
|
|
};
|