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