2020-01-16 11:46:21 +00:00
|
|
|
import React, {
|
|
|
|
|
useState,
|
|
|
|
|
useContext,
|
|
|
|
|
ReactNode,
|
|
|
|
|
Context,
|
|
|
|
|
createContext,
|
|
|
|
|
useEffect,
|
|
|
|
|
} from "react";
|
|
|
|
|
import styled from "styled-components";
|
2019-11-13 07:00:25 +00:00
|
|
|
import { useDrop, XYCoord, DropTargetMonitor } from "react-dnd";
|
|
|
|
|
import { WidgetProps } from "widgets/BaseWidget";
|
|
|
|
|
import { WidgetConfigProps } from "reducers/entityReducers/widgetConfigReducer";
|
2020-03-27 09:02:11 +00:00
|
|
|
import {
|
|
|
|
|
widgetOperationParams,
|
|
|
|
|
noCollision,
|
|
|
|
|
getCanvasSnapRows,
|
|
|
|
|
} from "utils/WidgetPropsUtils";
|
2019-11-13 07:00:25 +00:00
|
|
|
import { EditorContext } from "components/editorComponents/EditorContextProvider";
|
2020-01-16 11:46:21 +00:00
|
|
|
import {
|
|
|
|
|
MAIN_CONTAINER_WIDGET_ID,
|
2020-03-27 09:02:11 +00:00
|
|
|
GridDefaults,
|
2020-04-15 11:22:15 +00:00
|
|
|
WidgetTypes,
|
2020-01-16 11:46:21 +00:00
|
|
|
} from "constants/WidgetConstants";
|
|
|
|
|
import { calculateDropTargetRows } from "./DropTargetUtils";
|
2019-09-25 17:24:23 +00:00
|
|
|
import DragLayerComponent from "./DragLayerComponent";
|
2020-01-16 11:46:21 +00:00
|
|
|
import { AppState } from "reducers";
|
|
|
|
|
import { useSelector } from "react-redux";
|
2020-01-20 09:00:37 +00:00
|
|
|
import {
|
|
|
|
|
useShowPropertyPane,
|
|
|
|
|
useWidgetSelection,
|
2020-03-27 09:02:11 +00:00
|
|
|
useCanvasSnapRowsUpdateHook,
|
2020-01-20 09:00:37 +00:00
|
|
|
} from "utils/hooks/dragResizeHooks";
|
2019-09-22 20:25:05 +00:00
|
|
|
|
2019-11-13 07:00:25 +00:00
|
|
|
type DropTargetComponentProps = WidgetProps & {
|
|
|
|
|
children?: ReactNode;
|
2019-09-25 17:24:23 +00:00
|
|
|
snapColumnSpace: number;
|
|
|
|
|
snapRowSpace: number;
|
2020-03-27 09:02:11 +00:00
|
|
|
minHeight: 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
|
|
|
|
2020-01-16 11:46:21 +00:00
|
|
|
const StyledDropTarget = styled.div`
|
|
|
|
|
transition: height 100ms ease-in;
|
2020-03-27 09:02:11 +00:00
|
|
|
width: 100%;
|
|
|
|
|
position: relative;
|
|
|
|
|
background: none;
|
|
|
|
|
user-select: none;
|
2020-01-16 11:46:21 +00:00
|
|
|
`;
|
|
|
|
|
|
2020-05-14 06:06:20 +00:00
|
|
|
const Onboarding = () => {
|
|
|
|
|
return (
|
|
|
|
|
<div style={{ position: "fixed", left: "50%", top: "50vh" }}>
|
|
|
|
|
<h2 style={{ color: "#ccc" }}>Drag and drop a widget here</h2>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-06 09:45:21 +00:00
|
|
|
/*
|
2020-01-16 11:46:21 +00:00
|
|
|
This context will provide the function which will help the draglayer and resizablecomponents trigger
|
|
|
|
|
an update of the main container's rows
|
|
|
|
|
*/
|
|
|
|
|
export const DropTargetContext: Context<{
|
2020-03-27 09:02:11 +00:00
|
|
|
updateDropTargetRows?: (widgetId: string, row: number) => boolean;
|
2020-04-03 09:32:13 +00:00
|
|
|
persistDropTargetRows?: (widgetId: string, row: number) => void;
|
2020-01-16 11:46:21 +00:00
|
|
|
}> = createContext({});
|
|
|
|
|
|
2019-09-17 10:09:00 +00:00
|
|
|
export const DropTargetComponent = (props: DropTargetComponentProps) => {
|
2020-03-27 09:02:11 +00:00
|
|
|
const canDropTargetExtend = props.canExtend;
|
2020-01-20 09:00:37 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const snapRows = getCanvasSnapRows(props.bottomRow, props.canExtend);
|
|
|
|
|
|
|
|
|
|
const { updateWidget, occupiedSpaces } = useContext(EditorContext);
|
2020-01-20 09:00:37 +00:00
|
|
|
|
|
|
|
|
const selectedWidget = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.editor.selectedWidget,
|
|
|
|
|
);
|
|
|
|
|
const isResizing = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isResizing,
|
2020-01-16 11:46:21 +00:00
|
|
|
);
|
2020-02-18 19:56:58 +00:00
|
|
|
const isDragging = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isDragging,
|
|
|
|
|
);
|
2020-01-16 11:46:21 +00:00
|
|
|
|
2019-11-13 07:00:25 +00:00
|
|
|
const spacesOccupiedBySiblingWidgets =
|
|
|
|
|
occupiedSpaces && occupiedSpaces[props.widgetId]
|
|
|
|
|
? occupiedSpaces[props.widgetId]
|
|
|
|
|
: undefined;
|
2020-01-16 11:46:21 +00:00
|
|
|
|
|
|
|
|
const childWidgets = useSelector(
|
|
|
|
|
(state: AppState) => state.entities.canvasWidgets[props.widgetId].children,
|
|
|
|
|
);
|
|
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const occupiedSpacesByChildren =
|
|
|
|
|
occupiedSpaces && occupiedSpaces[props.widgetId];
|
2020-01-16 11:46:21 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const [dropTargetOffset, setDropTargetOffset] = useState({ x: 0, y: 0 });
|
|
|
|
|
const [rows, setRows] = useState(snapRows);
|
2020-01-16 11:46:21 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const showPropertyPane = useShowPropertyPane();
|
|
|
|
|
const { selectWidget, focusWidget } = useWidgetSelection();
|
|
|
|
|
const updateCanvasSnapRows = useCanvasSnapRowsUpdateHook();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const snapRows = getCanvasSnapRows(props.bottomRow, props.canExtend);
|
|
|
|
|
setRows(snapRows);
|
|
|
|
|
}, [props.bottomRow, props.canExtend]);
|
|
|
|
|
|
2020-04-03 09:32:13 +00:00
|
|
|
const persistDropTargetRows = (widgetId: string, widgetBottomRow: number) => {
|
|
|
|
|
const newRows = calculateDropTargetRows(
|
|
|
|
|
widgetId,
|
|
|
|
|
widgetBottomRow,
|
|
|
|
|
props.minHeight / GridDefaults.DEFAULT_GRID_ROW_HEIGHT - 1,
|
|
|
|
|
occupiedSpacesByChildren,
|
|
|
|
|
);
|
2020-03-27 09:02:11 +00:00
|
|
|
const rowsToPersist = Math.max(
|
|
|
|
|
props.minHeight / GridDefaults.DEFAULT_GRID_ROW_HEIGHT - 1,
|
2020-04-03 09:32:13 +00:00
|
|
|
newRows,
|
2020-03-27 09:02:11 +00:00
|
|
|
);
|
|
|
|
|
setRows(rowsToPersist);
|
|
|
|
|
if (canDropTargetExtend) {
|
|
|
|
|
updateCanvasSnapRows(props.widgetId, rowsToPersist);
|
2020-01-16 11:46:21 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Update the rows of the main container based on the current widget's (dragging/resizing) bottom row */
|
2020-03-27 09:02:11 +00:00
|
|
|
const updateDropTargetRows = (widgetId: string, widgetBottomRow: number) => {
|
|
|
|
|
if (canDropTargetExtend) {
|
|
|
|
|
const newRows = calculateDropTargetRows(
|
|
|
|
|
widgetId,
|
|
|
|
|
widgetBottomRow,
|
|
|
|
|
props.minHeight / GridDefaults.DEFAULT_GRID_ROW_HEIGHT - 1,
|
|
|
|
|
occupiedSpacesByChildren,
|
|
|
|
|
);
|
2020-04-03 09:32:13 +00:00
|
|
|
if (rows < newRows) {
|
2020-03-27 09:02:11 +00:00
|
|
|
setRows(newRows);
|
2020-01-16 11:46:21 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isChildFocused =
|
|
|
|
|
!!childWidgets &&
|
|
|
|
|
!!selectedWidget &&
|
|
|
|
|
childWidgets.length > 0 &&
|
|
|
|
|
childWidgets.indexOf(selectedWidget) > -1;
|
|
|
|
|
|
|
|
|
|
const isChildResizing = !!isResizing && isChildFocused;
|
2019-09-25 17:24:23 +00:00
|
|
|
// Make this component a drop target
|
2020-05-14 06:06:20 +00:00
|
|
|
const [{ isExactlyOver, isOver }, drop] = useDrop({
|
2020-04-15 11:22:15 +00:00
|
|
|
accept: Object.values(WidgetTypes),
|
2020-03-27 09:02:11 +00:00
|
|
|
options: {
|
|
|
|
|
arePropsEqual: () => {
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-09-25 17:24:23 +00:00
|
|
|
drop(widget: WidgetProps & Partial<WidgetConfigProps>, monitor) {
|
|
|
|
|
// Make sure we're dropping in this container.
|
2019-11-13 07:00:25 +00:00
|
|
|
if (isExactlyOver) {
|
|
|
|
|
const updateWidgetParams = widgetOperationParams(
|
|
|
|
|
widget,
|
2019-12-27 10:10:04 +00:00
|
|
|
monitor.getSourceClientOffset() as XYCoord,
|
2019-11-13 07:00:25 +00:00
|
|
|
dropTargetOffset,
|
|
|
|
|
props.snapColumnSpace,
|
|
|
|
|
props.snapRowSpace,
|
|
|
|
|
props.widgetId,
|
|
|
|
|
);
|
2020-01-16 11:46:21 +00:00
|
|
|
|
2020-04-03 09:32:13 +00:00
|
|
|
// const widgetBottomRow = getWidgetBottomRow(widget, updateWidgetParams);
|
|
|
|
|
const widgetBottomRow =
|
|
|
|
|
updateWidgetParams.payload.topRow +
|
|
|
|
|
(updateWidgetParams.payload.rows || widget.bottomRow - widget.topRow);
|
|
|
|
|
|
2020-01-07 12:28:58 +00:00
|
|
|
// Only show propertypane if this is a new widget.
|
|
|
|
|
// If it is not a new widget, then let the DraggableComponent handle it.
|
2020-01-02 12:42:02 +00:00
|
|
|
showPropertyPane &&
|
2020-01-07 12:28:58 +00:00
|
|
|
updateWidgetParams.payload.newWidgetId &&
|
2020-01-02 12:42:02 +00:00
|
|
|
showPropertyPane(updateWidgetParams.payload.newWidgetId);
|
|
|
|
|
|
2020-01-16 11:46:21 +00:00
|
|
|
// Select the widget if it is a new widget
|
2020-03-27 09:02:11 +00:00
|
|
|
selectWidget && selectWidget(widget.widgetId);
|
2020-04-03 09:32:13 +00:00
|
|
|
persistDropTargetRows(widget.widgetId, widgetBottomRow);
|
2020-01-16 11:46:21 +00:00
|
|
|
|
|
|
|
|
/* Finally update the widget */
|
2019-10-03 16:24:29 +00:00
|
|
|
updateWidget &&
|
|
|
|
|
updateWidget(
|
2019-11-13 07:00:25 +00:00
|
|
|
updateWidgetParams.operation,
|
|
|
|
|
updateWidgetParams.widgetId,
|
|
|
|
|
updateWidgetParams.payload,
|
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-11-13 07:00:25 +00:00
|
|
|
collect: (monitor: DropTargetMonitor) => ({
|
2019-10-03 15:14:50 +00:00
|
|
|
isExactlyOver: monitor.isOver({ shallow: true }),
|
2020-05-14 06:06:20 +00:00
|
|
|
isOver: monitor.isOver(),
|
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
|
2019-11-13 07:00:25 +00:00
|
|
|
if (isExactlyOver) {
|
|
|
|
|
const hasCollision = !noCollision(
|
2019-12-27 10:10:04 +00:00
|
|
|
monitor.getSourceClientOffset() as XYCoord,
|
2019-09-25 21:21:04 +00:00
|
|
|
props.snapColumnSpace,
|
|
|
|
|
props.snapRowSpace,
|
|
|
|
|
widget,
|
|
|
|
|
dropTargetOffset,
|
2019-11-13 07:00:25 +00:00
|
|
|
spacesOccupiedBySiblingWidgets,
|
2020-01-16 11:46:21 +00:00
|
|
|
rows,
|
2020-03-27 09:02:11 +00:00
|
|
|
GridDefaults.DEFAULT_GRID_COLUMNS,
|
2019-09-25 21:21:04 +00:00
|
|
|
);
|
2019-11-13 07:00:25 +00:00
|
|
|
return !hasCollision;
|
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 = () => {
|
2020-02-18 19:56:58 +00:00
|
|
|
if (!props.parentId && !isResizing && !isDragging) {
|
2020-01-06 11:02:22 +00:00
|
|
|
selectWidget && selectWidget(props.widgetId);
|
2020-03-04 08:10:40 +00:00
|
|
|
focusWidget && focusWidget(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
|
|
|
}
|
|
|
|
|
};
|
2020-03-27 09:02:11 +00:00
|
|
|
const height = canDropTargetExtend
|
|
|
|
|
? `${Math.max(rows * props.snapRowSpace, props.minHeight)}px`
|
|
|
|
|
: "100%";
|
2020-01-16 11:46:21 +00:00
|
|
|
|
2020-02-13 09:32:24 +00:00
|
|
|
const border =
|
2020-03-27 09:02:11 +00:00
|
|
|
(isResizing || isDragging) &&
|
|
|
|
|
isExactlyOver &&
|
|
|
|
|
props.widgetId === MAIN_CONTAINER_WIDGET_ID
|
|
|
|
|
? "1px solid #DDDDDD"
|
2020-02-19 10:00:03 +00:00
|
|
|
: "1px solid transparent";
|
2020-02-13 09:32:24 +00:00
|
|
|
|
2019-09-17 10:09:00 +00:00
|
|
|
return (
|
2020-01-16 11:46:21 +00:00
|
|
|
<DropTargetContext.Provider
|
|
|
|
|
value={{ updateDropTargetRows, persistDropTargetRows }}
|
2019-12-25 09:17:37 +00:00
|
|
|
>
|
2020-01-16 11:46:21 +00:00
|
|
|
<StyledDropTarget
|
|
|
|
|
onClick={handleFocus}
|
|
|
|
|
ref={drop}
|
|
|
|
|
style={{
|
|
|
|
|
height,
|
2020-02-13 09:32:24 +00:00
|
|
|
border,
|
2020-01-16 11:46:21 +00:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{props.children}
|
2020-05-14 06:06:20 +00:00
|
|
|
{!(childWidgets && childWidgets.length) &&
|
|
|
|
|
!isDragging &&
|
|
|
|
|
!props.parentId && <Onboarding />}
|
2020-01-16 11:46:21 +00:00
|
|
|
<DragLayerComponent
|
|
|
|
|
parentWidgetId={props.widgetId}
|
2020-03-27 09:02:11 +00:00
|
|
|
canDropTargetExtend={canDropTargetExtend}
|
2020-01-16 11:46:21 +00:00
|
|
|
parentRowHeight={props.snapRowSpace}
|
|
|
|
|
parentColumnWidth={props.snapColumnSpace}
|
|
|
|
|
visible={isExactlyOver || isChildResizing}
|
|
|
|
|
isOver={isExactlyOver}
|
|
|
|
|
occupiedSpaces={spacesOccupiedBySiblingWidgets}
|
|
|
|
|
onBoundsUpdate={handleBoundsUpdate}
|
|
|
|
|
parentRows={rows}
|
|
|
|
|
parentCols={props.snapColumns}
|
|
|
|
|
isResizing={isChildResizing}
|
2020-05-14 06:06:20 +00:00
|
|
|
force={isDragging && !isOver && !props.parentId}
|
2020-01-16 11:46:21 +00:00
|
|
|
/>
|
|
|
|
|
</StyledDropTarget>
|
|
|
|
|
</DropTargetContext.Provider>
|
2019-09-17 10:09:00 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DropTargetComponent;
|