2019-09-21 01:52:38 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { useDragLayer, XYCoord } from "react-dnd";
|
|
|
|
|
import DropZone from "./Dropzone";
|
|
|
|
|
|
|
|
|
|
const WrappedDragLayer = styled.div`
|
|
|
|
|
position: absolute;
|
|
|
|
|
pointer-events: none;
|
|
|
|
|
z-index: 100;
|
|
|
|
|
left: 0;
|
|
|
|
|
top: 0;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
cursor: grab;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
type DragLayerProps = {
|
|
|
|
|
parentOffset: XYCoord;
|
2019-09-25 17:24:23 +00:00
|
|
|
parentRowHeight: number;
|
|
|
|
|
parentColumnWidth: number;
|
2019-09-21 01:52:38 +00:00
|
|
|
visible: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const DragLayerComponent = (props: DragLayerProps) => {
|
2019-09-25 17:24:23 +00:00
|
|
|
const { isDragging, currentOffset, widget } = useDragLayer(monitor => ({
|
2019-09-21 01:52:38 +00:00
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
|
currentOffset: monitor.getClientOffset(),
|
2019-09-25 17:24:23 +00:00
|
|
|
widget: monitor.getItem(),
|
2019-09-21 01:52:38 +00:00
|
|
|
}));
|
2019-09-25 17:24:23 +00:00
|
|
|
let widgetWidth = 0;
|
|
|
|
|
let widgetHeight = 0;
|
|
|
|
|
if (widget) {
|
|
|
|
|
widgetWidth = widget.columns
|
|
|
|
|
? widget.columns
|
|
|
|
|
: widget.rightColumn - widget.leftColumn;
|
|
|
|
|
widgetHeight = widget.rows ? widget.rows : widget.bottomRow - widget.topRow;
|
|
|
|
|
}
|
2019-09-21 01:52:38 +00:00
|
|
|
|
2019-09-25 17:24:23 +00:00
|
|
|
if (!isDragging || !props.visible) {
|
2019-09-21 01:52:38 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<WrappedDragLayer>
|
2019-09-25 17:24:23 +00:00
|
|
|
<DropZone
|
|
|
|
|
{...props}
|
|
|
|
|
width={widgetWidth}
|
|
|
|
|
height={widgetHeight}
|
|
|
|
|
currentOffset={currentOffset as XYCoord}
|
|
|
|
|
/>
|
2019-09-21 01:52:38 +00:00
|
|
|
</WrappedDragLayer>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
export default DragLayerComponent;
|