PromucFlow_constructor/app/client/src/components/editorComponents/DragLayerComponent.tsx

115 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-01-16 11:46:21 +00:00
import React, { useContext } 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";
import { OccupiedSpace } from "constants/editorConstants";
import DropTargetMask from "./DropTargetMask";
2020-01-16 11:46:21 +00:00
import { MAIN_CONTAINER_WIDGET_ID } from "constants/WidgetConstants";
import { DropTargetContext } from "./DropTargetComponent";
2019-09-21 01:52:38 +00:00
const WrappedDragLayer = styled.div`
position: absolute;
pointer-events: none;
left: 0;
2020-01-16 11:46:21 +00:00
right: 0;
bottom: 0;
2019-09-21 01:52:38 +00:00
top: 0;
`;
type DragLayerProps = {
parentOffset: XYCoord;
parentRowHeight: number;
parentColumnWidth: number;
2019-09-21 01:52:38 +00:00
visible: boolean;
2019-09-25 21:21:04 +00:00
dropTargetOffset: XYCoord;
occupiedSpaces?: OccupiedSpace[];
onBoundsUpdate: Function;
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;
2019-09-21 01:52:38 +00:00
};
const DragLayerComponent = (props: DragLayerProps) => {
2020-01-16 11:46:21 +00:00
const { updateDropTargetRows } = useContext(DropTargetContext);
2019-09-25 21:21:04 +00:00
const { isDragging, currentOffset, widget, canDrop } = useDragLayer(
monitor => ({
isDragging: monitor.isDragging(),
currentOffset: monitor.getSourceClientOffset(),
2019-09-25 21:21:04 +00:00
widget: monitor.getItem(),
canDrop: noCollision(
monitor.getSourceClientOffset() as XYCoord,
2019-09-25 21:21:04 +00:00
props.parentColumnWidth,
props.parentRowHeight,
monitor.getItem(),
props.dropTargetOffset,
props.occupiedSpaces,
2019-10-08 06:19:10 +00:00
props.parentRows,
props.parentCols,
2019-09-25 21:21:04 +00:00
),
}),
);
2020-01-16 11:46:21 +00:00
if (
props.visible &&
props.parentWidgetId === MAIN_CONTAINER_WIDGET_ID &&
currentOffset &&
props.parentRows
) {
const row = currentDropRow(
props.parentRowHeight,
props.parentOffset.y,
currentOffset.y,
widget,
);
updateDropTargetRows && updateDropTargetRows(row);
}
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-10-08 12:31:58 +00:00
if ((!isDragging || !props.visible) && !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].
*/
const isParentOffsetCalculated = props.parentOffset.x !== 0;
2019-09-21 01:52:38 +00:00
return (
<WrappedDragLayer>
<DropTargetMask
rowHeight={props.parentRowHeight}
columnWidth={props.parentColumnWidth}
setBounds={props.onBoundsUpdate}
/>
2020-02-11 09:56:21 +00:00
{props.visible &&
props.isOver &&
currentOffset &&
isParentOffsetCalculated && (
<DropZone
parentOffset={props.parentOffset}
parentRowHeight={props.parentRowHeight}
parentColumnWidth={props.parentColumnWidth}
width={widgetWidth}
height={widgetHeight}
currentOffset={currentOffset as XYCoord}
canDrop={canDrop}
/>
)}
2019-09-21 01:52:38 +00:00
</WrappedDragLayer>
);
};
export default DragLayerComponent;