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

210 lines
6.2 KiB
TypeScript
Raw Normal View History

import React, { useContext, createContext, Context } from "react";
import styled from "styled-components";
import { WidgetProps, WidgetOperations } from "widgets/BaseWidget";
import { ContainerWidgetProps } from "widgets/ContainerWidget";
import { useDrag, DragPreviewImage, DragSourceMonitor } from "react-dnd";
import blankImage from "assets/images/blank.png";
import { FocusContext, ResizingContext } from "pages/Editor/CanvasContexts";
import { EditorContext } from "components/editorComponents/EditorContextProvider";
2019-11-25 05:07:27 +00:00
import { ControlIcons } from "icons/ControlIcons";
import { Tooltip } from "@blueprintjs/core";
import { WIDGET_CLASSNAME_PREFIX } from "constants/WidgetConstants";
2020-01-06 11:02:22 +00:00
import { useSelector } from "react-redux";
import { PropertyPaneReduxState } from "reducers/uiReducers/propertyPaneReducer";
import { AppState } from "reducers";
import { theme } from "constants/DefaultTheme";
import { Colors } from "constants/Colors";
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;
cursor: grab;
`;
const DragHandle = styled.div`
position: absolute;
left: 0px;
top: -${props => props.theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX]}px;
cursor: move;
display: none;
cursor: grab;
`;
const DeleteControl = styled.div`
position: absolute;
right: ${props => props.theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX]}px;
top: -${props => props.theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX]}px;
display: none;
cursor: pointer;
`;
const EditControl = styled.div`
position: absolute;
right: 0;
top: -${props => props.theme.fontSizes[CONTROL_THEME_FONTSIZE_INDEX]}px;
display: none;
cursor: pointer;
`;
const CONTROL_ICON_SIZE = 20;
const moveControlIcon = ControlIcons.MOVE_CONTROL({
width: CONTROL_ICON_SIZE,
height: CONTROL_ICON_SIZE,
});
const deleteControlIcon = ControlIcons.DELETE_CONTROL({
width: CONTROL_ICON_SIZE,
height: CONTROL_ICON_SIZE,
});
type DraggableComponentProps = ContainerWidgetProps<WidgetProps>;
export const DraggableComponentContext: Context<{
isDragging?: boolean;
2019-10-03 15:38:46 +00:00
}> = createContext({});
/* eslint-disable react/display-name */
const DraggableComponent = (props: DraggableComponentProps) => {
2020-01-02 11:04:36 +00:00
const {
2020-01-06 11:02:22 +00:00
focusedWidget,
selectedWidget,
focusWidget,
selectWidget,
2020-01-02 11:04:36 +00:00
showPropertyPane,
} = useContext(FocusContext);
2020-01-06 11:02:22 +00:00
const propertyPaneState: PropertyPaneReduxState = useSelector(
(state: AppState) => state.ui.propertyPane,
);
const editControlIcon = ControlIcons.EDIT_CONTROL({
width: CONTROL_ICON_SIZE,
height: CONTROL_ICON_SIZE,
color:
propertyPaneState.widgetId === props.widgetId &&
propertyPaneState.isVisible
? theme.colors.textDefault
: theme.colors.textOnDarkBG,
background:
2020-01-06 11:02:22 +00:00
propertyPaneState.widgetId === props.widgetId &&
propertyPaneState.isVisible
? Colors.HIT_GRAY
: Colors.SHARK,
});
const { updateWidget } = useContext(EditorContext);
2020-01-02 11:04:36 +00:00
2019-10-08 12:31:58 +00:00
const { isResizing } = useContext(ResizingContext);
const deleteWidget = () => {
showPropertyPane && showPropertyPane();
2019-10-03 17:35:13 +00:00
updateWidget &&
updateWidget(WidgetOperations.DELETE, props.widgetId, {
parentId: props.parentId,
});
};
const togglePropertyEditor = (e: any) => {
if (showPropertyPane && props.widgetId) {
showPropertyPane(props.widgetId, true);
}
e.stopPropagation();
};
const [{ isDragging }, drag, preview] = useDrag({
item: props as WidgetProps,
collect: (monitor: DragSourceMonitor) => ({
isDragging: monitor.isDragging(),
}),
begin: () => {
showPropertyPane && showPropertyPane(undefined, true);
2020-01-06 11:02:22 +00:00
selectWidget && selectWidget(props.widgetId);
},
end: (widget, monitor) => {
if (monitor.didDrop()) {
showPropertyPane && showPropertyPane(props.widgetId, true);
}
},
canDrag: () => {
2019-12-25 09:17:37 +00:00
return !isResizing;
},
});
return (
<DraggableComponentContext.Provider value={{ isDragging }}>
<DragPreviewImage connect={preview} src={blankImage} />
<DraggableWrapper
className={WIDGET_CLASSNAME_PREFIX + props.widgetId}
ref={drag}
onMouseOver={(e: any) => {
2020-01-06 11:02:22 +00:00
focusWidget && focusWidget(props.widgetId);
e.stopPropagation();
}}
onMouseLeave={(e: any) => {
2020-01-06 11:02:22 +00:00
focusWidget && focusWidget(null);
2020-01-02 11:04:36 +00:00
e.stopPropagation();
}}
onClick={(e: any) => {
2020-01-06 11:02:22 +00:00
selectWidget && selectWidget(props.widgetId);
if (
propertyPaneState.widgetId &&
propertyPaneState.widgetId !== props.widgetId
) {
2020-01-02 11:04:36 +00:00
showPropertyPane && showPropertyPane();
}
e.stopPropagation();
}}
onDoubleClick={(e: any) => {
showPropertyPane && showPropertyPane(props.widgetId);
e.stopPropagation();
}}
2020-01-06 11:02:22 +00:00
show={
(props.widgetId === focusedWidget ||
props.widgetId === selectedWidget) &&
!isResizing
}
style={{
display: isDragging ? "none" : "flex",
flexDirection: "column",
position: "absolute",
left: 0,
top: 0,
width: "100%",
height: "100%",
userSelect: "none",
cursor: "drag",
}}
>
2019-10-08 06:19:10 +00:00
{props.children}
<DragHandle className="control" ref={drag}>
<Tooltip content="Move" hoverOpenDelay={500}>
{moveControlIcon}
</Tooltip>
</DragHandle>
<DeleteControl className="control" onClick={deleteWidget}>
<Tooltip content="Delete" hoverOpenDelay={500}>
{deleteControlIcon}
</Tooltip>
</DeleteControl>
<EditControl className="control" onClick={togglePropertyEditor}>
2020-01-06 11:02:22 +00:00
<Tooltip content="Show props" hoverOpenDelay={500}>
{editControlIcon}
</Tooltip>
</EditControl>
</DraggableWrapper>
</DraggableComponentContext.Provider>
);
2019-09-09 09:08:54 +00:00
};
export default DraggableComponent;