2019-10-21 11:40:24 +00:00
|
|
|
import React, {
|
|
|
|
|
useContext,
|
|
|
|
|
createContext,
|
|
|
|
|
useState,
|
|
|
|
|
Context,
|
|
|
|
|
useCallback,
|
|
|
|
|
} 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";
|
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;
|
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-10-21 11:40:24 +00:00
|
|
|
const DraggableMask = styled.div`
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
position: absolute;
|
|
|
|
|
left: 0;
|
|
|
|
|
top: 0;
|
|
|
|
|
z-index: -1;
|
|
|
|
|
`;
|
|
|
|
|
|
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-10-21 11:40:24 +00:00
|
|
|
const editControlIcon = ControlIcons.EDIT_CONTROL({
|
2019-12-19 06:44:31 +00:00
|
|
|
width: CONTROL_ICON_SIZE,
|
|
|
|
|
height: CONTROL_ICON_SIZE,
|
2019-10-21 11:40:24 +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-21 11:40:24 +00:00
|
|
|
widgetNode?: HTMLDivElement;
|
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) => {
|
2019-10-21 11:40:24 +00:00
|
|
|
const { isFocused, setFocus, showPropertyPane } = useContext(FocusContext);
|
2019-11-13 07:00:25 +00:00
|
|
|
const { updateWidget } = useContext(EditorContext);
|
2019-10-21 11:40:24 +00:00
|
|
|
const [currentNode, setCurrentNode] = useState<HTMLDivElement>();
|
|
|
|
|
const referenceRef = useCallback(
|
|
|
|
|
node => {
|
|
|
|
|
if (node !== null && node !== currentNode) {
|
|
|
|
|
setCurrentNode(node);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[setCurrentNode, currentNode],
|
|
|
|
|
);
|
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) => {
|
|
|
|
|
if (showPropertyPane && props.widgetId && currentNode) {
|
|
|
|
|
showPropertyPane(props.widgetId, currentNode, true);
|
|
|
|
|
}
|
|
|
|
|
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: () => {
|
|
|
|
|
if (isFocused === props.widgetId && showPropertyPane && currentNode) {
|
|
|
|
|
showPropertyPane(props.widgetId, undefined);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-10-21 11:40:24 +00:00
|
|
|
end: (widget, monitor) => {
|
|
|
|
|
if (monitor.didDrop()) {
|
|
|
|
|
if (isFocused === props.widgetId && showPropertyPane && currentNode) {
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
showPropertyPane(props.widgetId, currentNode);
|
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 (
|
2019-10-21 11:40:24 +00:00
|
|
|
<DraggableComponentContext.Provider
|
|
|
|
|
value={{ isDragging, widgetNode: currentNode }}
|
|
|
|
|
>
|
2019-12-27 10:10:04 +00:00
|
|
|
<DragPreviewImage connect={preview} src={blankImage} />
|
2019-09-30 03:25:14 +00:00
|
|
|
<DraggableWrapper
|
2019-10-04 10:22:47 +00:00
|
|
|
ref={drag}
|
2019-12-27 10:10:04 +00:00
|
|
|
onMouseOver={(e: any) => {
|
|
|
|
|
if (setFocus) {
|
2019-10-01 20:07:43 +00:00
|
|
|
setFocus(props.widgetId);
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
}}
|
2019-12-27 10:10:04 +00:00
|
|
|
onMouseLeave={(e: any) => {
|
|
|
|
|
setFocus && setFocus(null);
|
|
|
|
|
showPropertyPane && showPropertyPane(props.widgetId);
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}}
|
|
|
|
|
onDoubleClick={(e: any) => {
|
|
|
|
|
setFocus && setFocus(props.widgetId);
|
|
|
|
|
showPropertyPane && showPropertyPane(props.widgetId, currentNode);
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}}
|
2019-10-03 15:38:46 +00:00
|
|
|
show={props.widgetId === isFocused && !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",
|
2019-04-02 16:12:08 +00:00
|
|
|
}}
|
|
|
|
|
>
|
2019-10-21 11:40:24 +00:00
|
|
|
<DraggableMask ref={referenceRef} />
|
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}>
|
2019-10-31 05:28:11 +00:00
|
|
|
<Tooltip content="Toggle 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;
|