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

277 lines
8.4 KiB
TypeScript
Raw Normal View History

import React, { useContext } from "react";
import styled from "styled-components";
import { WidgetProps, WidgetOperations } from "widgets/BaseWidget";
import { ContainerWidgetProps } from "widgets/ContainerWidget";
2020-03-04 08:10:40 +00:00
import { useDrag, DragSourceMonitor } from "react-dnd";
import { WIDGET_PADDING } from "constants/WidgetConstants";
import { EditorContext } from "components/editorComponents/EditorContextProvider";
2019-11-25 05:07:27 +00:00
import { ControlIcons } from "icons/ControlIcons";
import { Tooltip } from "@blueprintjs/core";
2020-02-13 09:32:24 +00:00
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, getColorWithOpacity } from "constants/DefaultTheme";
import { Colors } from "constants/Colors";
2020-01-20 09:00:37 +00:00
import {
useWidgetSelection,
useShowPropertyPane,
useWidgetDragResize,
} from "utils/hooks/dragResizeHooks";
2020-03-03 07:02:53 +00:00
import AnalyticsUtil from "utils/AnalyticsUtil";
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 }>`
& > div.control {
display: ${props => (props.show ? "block" : "none")};
}
display: block;
flex-direction: column;
width: 100%;
height: 100%;
userselect: none;
cursor: grab;
`;
const WidgetBoundaries = styled.div`
transform: translate3d(-${WIDGET_PADDING + 1}px, -${WIDGET_PADDING + 1}px, 0);
2020-01-16 11:46:21 +00:00
z-index: 0;
width: calc(100% + ${WIDGET_PADDING - 2}px);
height: calc(100% + ${WIDGET_PADDING - 2}px);
position: absolute;
border: 1px dashed
${props => getColorWithOpacity(props.theme.colors.textAnchor, 0.5)};
2020-01-10 12:15:54 +00:00
pointer-events: none;
`;
const ClickCaptureMask = styled.div`
position: absolute;
left: 0;
top: 5%;
width: 100%;
height: 95%;
z-index: 2;
`;
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 deleteControlIcon = ControlIcons.DELETE_CONTROL({
width: CONTROL_ICON_SIZE,
height: CONTROL_ICON_SIZE,
});
type DraggableComponentProps = ContainerWidgetProps<WidgetProps>;
/* eslint-disable react/display-name */
const DraggableComponent = (props: DraggableComponentProps) => {
2020-01-20 09:00:37 +00:00
const showPropertyPane = useShowPropertyPane();
const { selectWidget, focusWidget } = useWidgetSelection();
const { setIsDragging } = useWidgetDragResize();
2020-01-06 11:02:22 +00:00
const propertyPaneState: PropertyPaneReduxState = useSelector(
(state: AppState) => state.ui.propertyPane,
);
2020-01-20 09:00:37 +00:00
const selectedWidget = useSelector(
(state: AppState) => state.ui.editor.selectedWidget,
);
const focusedWidget = useSelector(
(state: AppState) => state.ui.editor.focusedWidget,
);
const isResizing = useSelector(
(state: AppState) => state.ui.widgetDragResize.isResizing,
);
const isDragging = useSelector(
(state: AppState) => state.ui.widgetDragResize.isDragging,
);
2020-01-06 11:02:22 +00:00
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
2020-01-20 09:00:37 +00:00
const isDraggingDisabled: boolean = useSelector(
(state: AppState) => state.ui.widgetDragResize.isDraggingDisabled,
);
const deleteWidget = () => {
2020-03-03 07:02:53 +00:00
AnalyticsUtil.logEvent("WIDGET_DELETE", {
widgetName: props.widgetName,
widgetType: props.type,
});
showPropertyPane && showPropertyPane();
2019-10-03 17:35:13 +00:00
updateWidget &&
updateWidget(WidgetOperations.DELETE, props.widgetId, {
parentId: props.parentId,
});
};
const togglePropertyEditor = (e: any) => {
if (
(!propertyPaneState.isVisible &&
props.widgetId === propertyPaneState.widgetId) ||
props.widgetId !== propertyPaneState.widgetId
) {
2020-03-18 14:31:30 +00:00
AnalyticsUtil.logEvent("PROPERTY_PANE_OPEN_CLICK", {
widgetType: props.type,
widgetId: props.widgetId,
});
showPropertyPane && showPropertyPane(props.widgetId, undefined, true);
} else {
2020-03-18 14:31:30 +00:00
AnalyticsUtil.logEvent("PROPERTY_PANE_CLOSE_CLICK", {
widgetType: props.type,
widgetId: props.widgetId,
});
showPropertyPane && showPropertyPane();
}
2020-03-18 14:31:30 +00:00
e.preventDefault();
e.stopPropagation();
};
2020-03-04 08:10:40 +00:00
const [{ isCurrentWidgetDragging }, drag] = useDrag({
item: props as WidgetProps,
collect: (monitor: DragSourceMonitor) => ({
isCurrentWidgetDragging: monitor.isDragging(),
}),
begin: () => {
2020-03-03 07:02:53 +00:00
AnalyticsUtil.logEvent("WIDGET_DRAG", {
widgetName: props.widgetName,
widgetType: props.type,
});
showPropertyPane && showPropertyPane(undefined, true);
selectWidget && selectWidget(props.widgetId);
2020-01-20 09:00:37 +00:00
setIsDragging && setIsDragging(true);
},
end: (widget, monitor) => {
2020-03-11 13:59:46 +00:00
const didDrop = monitor.didDrop();
if (didDrop) {
showPropertyPane && showPropertyPane(props.widgetId, true);
}
2020-03-03 07:02:53 +00:00
AnalyticsUtil.logEvent("WIDGET_DROP", {
widgetName: props.widgetName,
widgetType: props.type,
2020-03-11 13:59:46 +00:00
didDrop: didDrop,
2020-03-03 07:02:53 +00:00
});
2020-03-04 08:10:40 +00:00
// Take this to the bottom of the stack. So that it runs last.
setTimeout(() => setIsDragging && setIsDragging(false), 0);
},
canDrag: () => {
2020-01-20 09:00:37 +00:00
return !isResizing && !isDraggingDisabled;
},
});
2020-03-04 08:10:40 +00:00
const isResizingOrDragging = !!isResizing || !!isDragging;
2020-02-27 03:56:30 +00:00
const className = `${WIDGET_CLASSNAME_PREFIX +
props.widgetId} t--draggable-${props.type
.split("_")
.join("")
.toLowerCase()}`;
return (
<React.Fragment>
<DraggableWrapper
2020-02-27 03:56:30 +00:00
className={className}
ref={drag}
onMouseOver={(e: any) => {
focusWidget &&
2020-03-04 08:10:40 +00:00
!isResizingOrDragging &&
focusedWidget !== props.widgetId &&
focusWidget(props.widgetId);
2020-01-06 11:02:22 +00:00
e.stopPropagation();
}}
2020-01-02 11:04:36 +00:00
onClick={(e: any) => {
2020-03-04 08:10:40 +00:00
if (!isResizingOrDragging) {
const shouldForceOpen = selectedWidget !== props.widgetId;
showPropertyPane &&
showPropertyPane(props.widgetId, undefined, shouldForceOpen);
2020-03-04 08:10:40 +00:00
selectWidget &&
selectedWidget !== props.widgetId &&
selectWidget(props.widgetId);
}
e.stopPropagation();
}}
2020-01-06 11:02:22 +00:00
show={
(props.widgetId === focusedWidget ||
props.widgetId === selectedWidget) &&
!isResizing
}
style={{
display: isCurrentWidgetDragging ? "none" : "flex",
}}
>
2020-02-13 09:32:24 +00:00
{selectedWidget !== props.widgetId && props.isDefaultClickDisabled && (
<ClickCaptureMask
onClick={(e: any) => {
const shouldForceOpen = selectedWidget !== props.widgetId;
showPropertyPane &&
showPropertyPane(props.widgetId, undefined, shouldForceOpen);
2020-02-13 09:32:24 +00:00
selectWidget && selectWidget(props.widgetId);
e.preventDefault();
e.stopPropagation();
}}
/>
)}
2019-10-08 06:19:10 +00:00
{props.children}
2020-02-27 03:56:30 +00:00
<DeleteControl
className="control t--widget-delete-control"
onClick={deleteWidget}
>
<Tooltip content="Delete" hoverOpenDelay={500}>
{deleteControlIcon}
</Tooltip>
</DeleteControl>
2020-02-27 03:56:30 +00:00
<EditControl
className="control t--widget-propertypane-toggle"
onClick={togglePropertyEditor}
>
2020-01-06 11:02:22 +00:00
<Tooltip content="Show props" hoverOpenDelay={500}>
{editControlIcon}
</Tooltip>
</EditControl>
2020-03-04 08:10:40 +00:00
<WidgetBoundaries
style={{
opacity:
isResizingOrDragging && selectedWidget !== props.widgetId ? 1 : 0,
}}
/>
</DraggableWrapper>
</React.Fragment>
);
2019-09-09 09:08:54 +00:00
};
export default DraggableComponent;