2019-10-08 12:31:58 +00:00
|
|
|
import React, { useState, useContext, createContext, Context } from "react";
|
2019-09-25 17:24:23 +00:00
|
|
|
import { WidgetProps } from "../widgets/BaseWidget";
|
2019-10-08 06:19:10 +00:00
|
|
|
import { OccupiedSpaceContext } from "../widgets/ContainerWidget";
|
2019-09-25 17:24:23 +00:00
|
|
|
import { WidgetConfigProps } from "../reducers/entityReducers/widgetConfigReducer";
|
|
|
|
|
import { useDrop, XYCoord } from "react-dnd";
|
2019-09-17 10:09:00 +00:00
|
|
|
import { ContainerProps } from "./ContainerComponent";
|
|
|
|
|
import WidgetFactory from "../utils/WidgetFactory";
|
2019-09-25 21:21:04 +00:00
|
|
|
import { widgetOperationParams, noCollision } from "../utils/WidgetPropsUtils";
|
2019-09-25 17:24:23 +00:00
|
|
|
import DragLayerComponent from "./DragLayerComponent";
|
2019-10-18 08:16:26 +00:00
|
|
|
import { WidgetFunctionsContext } from "../pages/Editor/WidgetsEditor";
|
2019-10-08 10:16:07 +00:00
|
|
|
import { FocusContext } from "../pages/Editor/Canvas";
|
2019-09-22 20:25:05 +00:00
|
|
|
|
2019-09-17 15:09:55 +00:00
|
|
|
type DropTargetComponentProps = ContainerProps & {
|
2019-09-19 22:25:37 +00:00
|
|
|
updateWidget?: Function;
|
2019-09-25 17:24:23 +00:00
|
|
|
snapColumns?: number;
|
|
|
|
|
snapRows?: number;
|
|
|
|
|
snapColumnSpace: number;
|
|
|
|
|
snapRowSpace: number;
|
2019-09-17 15:09:55 +00:00
|
|
|
};
|
2019-09-19 22:25:37 +00:00
|
|
|
|
2019-09-25 17:24:23 +00:00
|
|
|
type DropTargetBounds = {
|
|
|
|
|
x: number;
|
|
|
|
|
y: number;
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
};
|
2019-09-19 22:25:37 +00:00
|
|
|
|
2019-10-08 12:31:58 +00:00
|
|
|
export const ResizingContext: Context<{
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
isResizing?: boolean | string;
|
2019-10-08 12:31:58 +00:00
|
|
|
setIsResizing?: Function;
|
|
|
|
|
}> = createContext({});
|
|
|
|
|
|
2019-09-17 10:09:00 +00:00
|
|
|
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
|
2019-09-25 17:24:23 +00:00
|
|
|
const [dropTargetOffset, setDropTargetOffset] = useState({ x: 0, y: 0 });
|
2019-10-08 12:31:58 +00:00
|
|
|
const [isResizing, setIsResizing] = useState(false);
|
2019-10-03 16:24:29 +00:00
|
|
|
const { updateWidget } = useContext(WidgetFunctionsContext);
|
2019-10-08 06:19:10 +00:00
|
|
|
const occupiedSpaces = useContext(OccupiedSpaceContext);
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
const { setFocus, showPropertyPane } = useContext(FocusContext);
|
2019-09-25 17:24:23 +00:00
|
|
|
// Make this component a drop target
|
2019-10-03 15:14:50 +00:00
|
|
|
const [{ isOver, isExactlyOver }, drop] = useDrop({
|
2019-09-17 10:09:00 +00:00
|
|
|
accept: Object.values(WidgetFactory.getWidgetTypes()),
|
2019-09-25 17:24:23 +00:00
|
|
|
drop(widget: WidgetProps & Partial<WidgetConfigProps>, monitor) {
|
|
|
|
|
// Make sure we're dropping in this container.
|
2019-10-02 21:14:58 +00:00
|
|
|
if (isOver) {
|
2019-10-03 16:24:29 +00:00
|
|
|
updateWidget &&
|
|
|
|
|
updateWidget(
|
2019-09-25 17:24:23 +00:00
|
|
|
...widgetOperationParams(
|
|
|
|
|
widget,
|
|
|
|
|
monitor.getClientOffset() as XYCoord,
|
|
|
|
|
dropTargetOffset,
|
|
|
|
|
props.snapColumnSpace,
|
|
|
|
|
props.snapRowSpace,
|
|
|
|
|
props.widgetId,
|
|
|
|
|
),
|
2019-09-19 22:25:37 +00:00
|
|
|
);
|
2019-09-17 10:09:00 +00:00
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
},
|
2019-09-25 17:24:23 +00:00
|
|
|
// Collect isOver for ui transforms when hovering over this component
|
2019-09-19 22:25:37 +00:00
|
|
|
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),
|
2019-10-03 15:14:50 +00:00
|
|
|
isExactlyOver: monitor.isOver({ shallow: true }),
|
2019-09-19 22:25:37 +00:00
|
|
|
}),
|
2019-09-25 17:24:23 +00:00
|
|
|
// 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;
|
2019-09-17 10:09:00 +00:00
|
|
|
},
|
|
|
|
|
});
|
2019-09-25 17:24:23 +00:00
|
|
|
|
|
|
|
|
const handleBoundsUpdate = (rect: DOMRect) => {
|
|
|
|
|
if (rect.x !== dropTargetOffset.x || rect.y !== dropTargetOffset.y) {
|
|
|
|
|
setDropTargetOffset({
|
|
|
|
|
x: rect.x,
|
|
|
|
|
y: rect.y,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-08 10:16:07 +00:00
|
|
|
const handleFocus = () => {
|
|
|
|
|
if (props.isRoot) {
|
|
|
|
|
setFocus && setFocus(props.widgetId);
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
showPropertyPane && showPropertyPane();
|
2019-10-08 10:16:07 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-17 10:09:00 +00:00
|
|
|
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>
|
2019-09-17 10:09:00 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DropTargetComponent;
|