PromucFlow_constructor/app/client/src/editorComponents/DropTargetComponent.tsx

142 lines
4.6 KiB
TypeScript
Raw Normal View History

2019-10-08 12:31:58 +00:00
import React, { useState, useContext, createContext, Context } from "react";
import { WidgetProps } from "../widgets/BaseWidget";
2019-10-08 06:19:10 +00:00
import { OccupiedSpaceContext } from "../widgets/ContainerWidget";
import { WidgetConfigProps } from "../reducers/entityReducers/widgetConfigReducer";
import { useDrop, XYCoord } from "react-dnd";
import { ContainerProps } from "./ContainerComponent";
import WidgetFactory from "../utils/WidgetFactory";
2019-09-25 21:21:04 +00:00
import { widgetOperationParams, noCollision } from "../utils/WidgetPropsUtils";
import DragLayerComponent from "./DragLayerComponent";
2019-10-18 08:16:26 +00:00
import { WidgetFunctionsContext } from "../pages/Editor/WidgetsEditor";
import { FocusContext } from "../pages/Editor/Canvas";
type DropTargetComponentProps = ContainerProps & {
updateWidget?: Function;
snapColumns?: number;
snapRows?: number;
snapColumnSpace: number;
snapRowSpace: number;
};
type DropTargetBounds = {
x: number;
y: number;
width: number;
height: number;
};
2019-10-08 12:31:58 +00:00
export const ResizingContext: Context<{
isResizing?: boolean | string;
2019-10-08 12:31:58 +00:00
setIsResizing?: Function;
}> = createContext({});
export const DropTargetComponent = (props: DropTargetComponentProps) => {
2019-09-25 21:21:04 +00:00
// Hook to keep the offset of the drop target container in state
const [dropTargetOffset, setDropTargetOffset] = useState({ x: 0, y: 0 });
2019-10-08 12:31:58 +00:00
const [isResizing, setIsResizing] = useState(false);
const { updateWidget } = useContext(WidgetFunctionsContext);
2019-10-08 06:19:10 +00:00
const occupiedSpaces = useContext(OccupiedSpaceContext);
const { setFocus, showPropertyPane } = useContext(FocusContext);
// Make this component a drop target
const [{ isOver, isExactlyOver }, drop] = useDrop({
accept: Object.values(WidgetFactory.getWidgetTypes()),
drop(widget: WidgetProps & Partial<WidgetConfigProps>, monitor) {
// Make sure we're dropping in this container.
2019-10-02 21:14:58 +00:00
if (isOver) {
updateWidget &&
updateWidget(
...widgetOperationParams(
widget,
monitor.getClientOffset() as XYCoord,
dropTargetOffset,
props.snapColumnSpace,
props.snapRowSpace,
props.widgetId,
),
);
}
return undefined;
},
// Collect isOver for ui transforms when hovering over this component
collect: monitor => ({
2019-10-02 21:14:58 +00:00
isOver:
(monitor.isOver({ shallow: true }) &&
props.widgetId !== monitor.getItem().widgetId) ||
(monitor.isOver() && props.widgetId !== monitor.getItem().widgetId),
isExactlyOver: monitor.isOver({ shallow: true }),
}),
// Only allow drop if the drag object is directly over this component
// As opposed to the drag object being over a child component, or outside the component bounds
2019-09-25 21:21:04 +00:00
// Also only if the dropzone does not overlap any existing children
2019-09-24 12:36:03 +00:00
canDrop: (widget, monitor) => {
2019-10-02 21:14:58 +00:00
// Check if the draggable is the same as the dropTarget
if (isOver) {
2019-09-25 21:21:04 +00:00
return noCollision(
monitor.getClientOffset() as XYCoord,
props.snapColumnSpace,
props.snapRowSpace,
widget,
dropTargetOffset,
2019-10-08 06:19:10 +00:00
occupiedSpaces,
props.snapRows,
props.snapColumns,
2019-09-25 21:21:04 +00:00
);
}
return false;
},
});
const handleBoundsUpdate = (rect: DOMRect) => {
if (rect.x !== dropTargetOffset.x || rect.y !== dropTargetOffset.y) {
setDropTargetOffset({
x: rect.x,
y: rect.y,
});
}
};
const handleFocus = () => {
if (props.isRoot) {
setFocus && setFocus(props.widgetId);
showPropertyPane && showPropertyPane();
}
};
return (
2019-10-08 12:31:58 +00:00
<ResizingContext.Provider value={{ isResizing, setIsResizing }}>
<div
onClick={handleFocus}
ref={drop}
style={{
position: "relative",
left: 0,
height: props.isRoot
? props.style.componentHeight + (props.style.heightUnit || "px")
: "100%",
width: props.isRoot
? props.style.componentWidth + (props.style.widthUnit || "px")
: "100%",
top: 0,
}}
>
{props.children}
<DragLayerComponent
parentOffset={dropTargetOffset}
parentRowHeight={props.snapRowSpace}
parentColumnWidth={props.snapColumnSpace}
visible={isOver || isResizing}
isOver={isExactlyOver}
dropTargetOffset={dropTargetOffset}
occupiedSpaces={occupiedSpaces}
onBoundsUpdate={handleBoundsUpdate}
parentRows={props.snapRows}
parentCols={props.snapColumns}
isResizing={isResizing}
/>
</div>
</ResizingContext.Provider>
);
};
export default DropTargetComponent;