2020-03-06 09:33:20 +00:00
|
|
|
import React, {
|
|
|
|
|
useContext,
|
2020-03-27 09:02:11 +00:00
|
|
|
useEffect,
|
2020-03-06 09:33:20 +00:00
|
|
|
useLayoutEffect,
|
2020-03-27 09:02:11 +00:00
|
|
|
RefObject,
|
2020-03-06 09:33:20 +00:00
|
|
|
useRef,
|
|
|
|
|
} from "react";
|
2019-09-21 01:52:38 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { useDragLayer, XYCoord } from "react-dnd";
|
|
|
|
|
import DropZone from "./Dropzone";
|
2020-01-16 11:46:21 +00:00
|
|
|
import { noCollision, currentDropRow } from "utils/WidgetPropsUtils";
|
2019-11-13 07:00:25 +00:00
|
|
|
import { OccupiedSpace } from "constants/editorConstants";
|
2020-03-27 09:02:11 +00:00
|
|
|
import { CONTAINER_GRID_PADDING } from "constants/WidgetConstants";
|
2020-01-16 11:46:21 +00:00
|
|
|
import { DropTargetContext } from "./DropTargetComponent";
|
2020-03-27 09:02:11 +00:00
|
|
|
import { scrollElementIntoParentCanvasView } from "utils/helpers";
|
|
|
|
|
import { getNearestParentCanvas } from "utils/generators";
|
|
|
|
|
|
|
|
|
|
const WrappedDragLayer = styled.div<{
|
|
|
|
|
columnWidth: number;
|
|
|
|
|
rowHeight: number;
|
|
|
|
|
ref: RefObject<HTMLDivElement>;
|
|
|
|
|
}>`
|
2019-09-21 01:52:38 +00:00
|
|
|
position: absolute;
|
|
|
|
|
pointer-events: none;
|
2020-03-27 09:02:11 +00:00
|
|
|
left: 0;
|
|
|
|
|
top: 0;
|
2020-03-06 09:33:20 +00:00
|
|
|
left: ${CONTAINER_GRID_PADDING}px;
|
|
|
|
|
top: ${CONTAINER_GRID_PADDING}px;
|
2020-03-27 09:02:11 +00:00
|
|
|
height: calc(100% - ${CONTAINER_GRID_PADDING}px);
|
|
|
|
|
width: calc(100% - ${CONTAINER_GRID_PADDING}px);
|
2020-03-06 09:33:20 +00:00
|
|
|
|
|
|
|
|
background-image: radial-gradient(
|
|
|
|
|
circle,
|
2020-06-10 17:31:20 +00:00
|
|
|
${props => props.theme.colors.grid} 1px,
|
2020-03-06 09:33:20 +00:00
|
|
|
transparent 0
|
|
|
|
|
);
|
|
|
|
|
background-size: ${props => props.columnWidth}px ${props => props.rowHeight}px;
|
|
|
|
|
background-position: -${props => props.columnWidth / 2}px -${props =>
|
|
|
|
|
props.rowHeight / 2}px;
|
2019-09-21 01:52:38 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
type DragLayerProps = {
|
2019-09-25 17:24:23 +00:00
|
|
|
parentRowHeight: number;
|
2020-03-27 09:02:11 +00:00
|
|
|
canDropTargetExtend: boolean;
|
2019-09-25 17:24:23 +00:00
|
|
|
parentColumnWidth: number;
|
2019-09-21 01:52:38 +00:00
|
|
|
visible: boolean;
|
2019-11-13 07:00:25 +00:00
|
|
|
occupiedSpaces?: OccupiedSpace[];
|
2020-11-03 13:05:40 +00:00
|
|
|
onBoundsUpdate: (rect: DOMRect) => void;
|
2019-10-03 15:14:50 +00:00
|
|
|
isOver: boolean;
|
2019-10-08 06:19:10 +00:00
|
|
|
parentRows?: number;
|
|
|
|
|
parentCols?: number;
|
2019-10-08 12:31:58 +00:00
|
|
|
isResizing?: boolean;
|
2020-01-16 11:46:21 +00:00
|
|
|
parentWidgetId: string;
|
2020-05-14 06:06:20 +00:00
|
|
|
force: boolean;
|
2019-09-21 01:52:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const DragLayerComponent = (props: DragLayerProps) => {
|
2020-01-16 11:46:21 +00:00
|
|
|
const { updateDropTargetRows } = useContext(DropTargetContext);
|
2020-03-27 09:02:11 +00:00
|
|
|
const dropTargetMask: RefObject<HTMLDivElement> = React.useRef(null);
|
|
|
|
|
const dropZoneRef = React.useRef<HTMLDivElement>(null);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const el = dropZoneRef.current;
|
|
|
|
|
const scrollParent: Element | null = getNearestParentCanvas(
|
|
|
|
|
dropTargetMask.current,
|
|
|
|
|
);
|
|
|
|
|
if (dropTargetMask.current) {
|
|
|
|
|
if (el && props.canDropTargetExtend) {
|
|
|
|
|
scrollElementIntoParentCanvasView(el, scrollParent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-03-06 09:33:20 +00:00
|
|
|
|
|
|
|
|
const dropTargetOffset = useRef({
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
});
|
2019-09-25 21:21:04 +00:00
|
|
|
const { isDragging, currentOffset, widget, canDrop } = useDragLayer(
|
|
|
|
|
monitor => ({
|
|
|
|
|
isDragging: monitor.isDragging(),
|
2019-12-27 10:10:04 +00:00
|
|
|
currentOffset: monitor.getSourceClientOffset(),
|
2019-09-25 21:21:04 +00:00
|
|
|
widget: monitor.getItem(),
|
|
|
|
|
canDrop: noCollision(
|
2019-12-27 10:10:04 +00:00
|
|
|
monitor.getSourceClientOffset() as XYCoord,
|
2019-09-25 21:21:04 +00:00
|
|
|
props.parentColumnWidth,
|
|
|
|
|
props.parentRowHeight,
|
|
|
|
|
monitor.getItem(),
|
2020-03-06 09:33:20 +00:00
|
|
|
dropTargetOffset.current,
|
2019-09-25 21:21:04 +00:00
|
|
|
props.occupiedSpaces,
|
2019-10-08 06:19:10 +00:00
|
|
|
props.parentRows,
|
|
|
|
|
props.parentCols,
|
2019-09-25 21:21:04 +00:00
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
);
|
2019-11-13 07:00:25 +00:00
|
|
|
|
2020-01-16 11:46:21 +00:00
|
|
|
if (
|
|
|
|
|
currentOffset &&
|
2020-03-27 09:02:11 +00:00
|
|
|
props.isOver &&
|
|
|
|
|
props.canDropTargetExtend &&
|
|
|
|
|
isDragging
|
2020-01-16 11:46:21 +00:00
|
|
|
) {
|
|
|
|
|
const row = currentDropRow(
|
|
|
|
|
props.parentRowHeight,
|
2020-03-06 09:33:20 +00:00
|
|
|
dropTargetOffset.current.y,
|
2020-01-16 11:46:21 +00:00
|
|
|
currentOffset.y,
|
|
|
|
|
widget,
|
|
|
|
|
);
|
|
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
updateDropTargetRows && updateDropTargetRows(widget.widgetId, row);
|
2020-01-16 11:46:21 +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;
|
|
|
|
|
}
|
2020-03-06 09:33:20 +00:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
const el = dropTargetMask.current;
|
|
|
|
|
if (el) {
|
|
|
|
|
const rect = el.getBoundingClientRect();
|
|
|
|
|
if (
|
|
|
|
|
rect.x !== dropTargetOffset.current.x ||
|
|
|
|
|
rect.y !== dropTargetOffset.current.y
|
|
|
|
|
) {
|
|
|
|
|
dropTargetOffset.current = { x: rect.x, y: rect.y };
|
|
|
|
|
props.onBoundsUpdate && props.onBoundsUpdate(rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-05-14 06:06:20 +00:00
|
|
|
if (
|
|
|
|
|
(!isDragging || !props.visible || !props.isOver) &&
|
|
|
|
|
!props.force &&
|
|
|
|
|
!props.isResizing
|
|
|
|
|
) {
|
2019-09-21 01:52:38 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2020-01-16 11:46:21 +00:00
|
|
|
|
2020-02-11 09:56:21 +00:00
|
|
|
/*
|
|
|
|
|
When the parent offsets are not updated, we don't need to show the dropzone, as the dropzone
|
|
|
|
|
will be rendered at an incorrect coordinates.
|
|
|
|
|
We can be sure that the parent offset has been calculated
|
|
|
|
|
when the coordiantes are not [0,0].
|
|
|
|
|
*/
|
2020-03-06 09:33:20 +00:00
|
|
|
const isParentOffsetCalculated = dropTargetOffset.current.x !== 0;
|
2020-02-11 09:56:21 +00:00
|
|
|
|
2019-09-21 01:52:38 +00:00
|
|
|
return (
|
2020-03-06 09:33:20 +00:00
|
|
|
<WrappedDragLayer
|
|
|
|
|
columnWidth={props.parentColumnWidth}
|
|
|
|
|
rowHeight={props.parentRowHeight}
|
|
|
|
|
ref={dropTargetMask}
|
|
|
|
|
>
|
2020-02-11 09:56:21 +00:00
|
|
|
{props.visible &&
|
|
|
|
|
props.isOver &&
|
|
|
|
|
currentOffset &&
|
|
|
|
|
isParentOffsetCalculated && (
|
|
|
|
|
<DropZone
|
2020-03-06 09:33:20 +00:00
|
|
|
parentOffset={dropTargetOffset.current}
|
2020-02-11 09:56:21 +00:00
|
|
|
parentRowHeight={props.parentRowHeight}
|
|
|
|
|
parentColumnWidth={props.parentColumnWidth}
|
|
|
|
|
width={widgetWidth}
|
|
|
|
|
height={widgetHeight}
|
|
|
|
|
currentOffset={currentOffset as XYCoord}
|
|
|
|
|
canDrop={canDrop}
|
2020-03-27 09:02:11 +00:00
|
|
|
ref={dropZoneRef}
|
2020-02-11 09:56:21 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
2019-09-21 01:52:38 +00:00
|
|
|
</WrappedDragLayer>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
export default DragLayerComponent;
|