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

122 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-10-08 12:31:58 +00:00
import React, { useContext, createContext, Context } from "react";
import styled from "styled-components";
import { WidgetProps, WidgetOperations } from "../widgets/BaseWidget";
import { useDrag, DragPreviewImage, DragSourceMonitor } from "react-dnd";
import blankImage from "../assets/images/blank.png";
2019-10-03 11:04:11 +00:00
import { ContainerProps } from "./ContainerComponent";
import { FocusContext } from "../pages/Editor/Canvas";
import { WidgetFunctionsContext } from "../pages/Editor";
import { ControlIcons } from "../icons/ControlIcons";
import { theme } from "../constants/DefaultTheme";
2019-10-08 12:31:58 +00:00
import { ResizingContext } from "./DropTargetComponent";
2019-10-01 20:15:02 +00:00
// FontSizes array in DefaultTheme.tsx
// Change this to toggle the size of delete and move handles.
const CONTROL_THEME_FONTSIZE_INDEX = 6;
const DraggableWrapper = styled.div<{ show: boolean }>`
2019-10-09 09:08:55 +00:00
pointer-events: auto !important;
& > div.control {
display: ${props => (props.show ? "block" : "none")};
}
display: block;
2019-10-08 06:19:10 +00:00
position: relative;
z-index: 1;
`;
const DragHandle = styled.div`
position: absolute;
2019-10-01 20:15:02 +00:00
left: -${props => props.theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX] / 2}px;
top: -${props => props.theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX] / 2}px;
cursor: move;
display: none;
cursor: grab;
`;
const DeleteControl = styled.div`
position: absolute;
2019-10-01 20:15:02 +00:00
right: -${props => props.theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX] / 2}px;
top: -${props => props.theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX] / 2}px;
display: none;
cursor: pointer;
`;
const moveControlIcon = ControlIcons.MOVE_CONTROL({
2019-10-01 20:15:02 +00:00
width: theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX],
height: theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX],
});
const deleteControlIcon = ControlIcons.DELETE_CONTROL({
2019-10-01 20:15:02 +00:00
width: theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX],
height: theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX],
});
type DraggableComponentProps = WidgetProps & ContainerProps;
2019-10-08 12:31:58 +00:00
export const DraggingContext: Context<{
isDragging?: boolean;
2019-10-03 15:38:46 +00:00
}> = createContext({});
const DraggableComponent = (props: DraggableComponentProps) => {
const { isFocused, setFocus } = useContext(FocusContext);
const { updateWidget } = useContext(WidgetFunctionsContext);
2019-10-08 12:31:58 +00:00
const { isResizing } = useContext(ResizingContext);
const deleteWidget = () => {
2019-10-03 17:35:13 +00:00
updateWidget &&
updateWidget(WidgetOperations.DELETE, props.widgetId, {
parentId: props.parentId,
});
};
const [{ isDragging }, drag, preview] = useDrag({
item: props,
collect: (monitor: DragSourceMonitor) => ({
isDragging: monitor.isDragging(),
}),
canDrag: () => {
return !isResizing && !!isFocused && isFocused === props.widgetId;
},
});
return (
2019-10-08 12:31:58 +00:00
<DraggingContext.Provider value={{ isDragging }}>
<DragPreviewImage src={blankImage} connect={preview} />
<DraggableWrapper
ref={drag}
onClick={(e: any) => {
if (setFocus) {
setFocus(props.widgetId);
e.stopPropagation();
}
}}
2019-10-03 15:38:46 +00:00
show={props.widgetId === isFocused && !isResizing}
style={{
display: isDragging ? "none" : "flex",
flexDirection: "column",
position: "absolute",
left: props.style
? props.style.xPosition + props.style.xPositionUnit
: 0,
top: props.style
? props.style.yPosition + props.style.yPositionUnit
2019-09-09 09:08:54 +00:00
: 0,
minWidth:
props.style.componentWidth + (props.style.widthUnit || "px"),
minHeight:
props.style.componentHeight + (props.style.heightUnit || "px"),
}}
>
2019-10-08 06:19:10 +00:00
{props.children}
<DragHandle className="control" ref={drag}>
{moveControlIcon}
</DragHandle>
<DeleteControl className="control" onClick={deleteWidget}>
{deleteControlIcon}
</DeleteControl>
</DraggableWrapper>
2019-10-08 12:31:58 +00:00
</DraggingContext.Provider>
);
2019-09-09 09:08:54 +00:00
};
export default DraggableComponent;