2021-08-12 05:45:38 +00:00
|
|
|
import React, { CSSProperties, useMemo, useRef } from "react";
|
2019-09-22 20:25:05 +00:00
|
|
|
import styled from "styled-components";
|
2020-03-27 09:02:11 +00:00
|
|
|
import { WidgetProps } from "widgets/BaseWidget";
|
2020-03-06 09:33:20 +00:00
|
|
|
import { WIDGET_PADDING } from "constants/WidgetConstants";
|
2020-01-06 11:02:22 +00:00
|
|
|
import { useSelector } from "react-redux";
|
|
|
|
|
import { AppState } from "reducers";
|
2020-03-27 09:02:11 +00:00
|
|
|
import { getColorWithOpacity } from "constants/DefaultTheme";
|
2020-01-20 09:00:37 +00:00
|
|
|
import {
|
2021-07-20 05:18:58 +00:00
|
|
|
useShowTableFilterPane,
|
2020-01-20 09:00:37 +00:00
|
|
|
useWidgetDragResize,
|
|
|
|
|
} from "utils/hooks/dragResizeHooks";
|
2021-06-09 10:35:10 +00:00
|
|
|
import { commentModeSelector } from "selectors/commentsSelectors";
|
2021-07-26 16:44:10 +00:00
|
|
|
import { snipingModeSelector } from "selectors/editorSelectors";
|
2021-06-17 13:26:54 +00:00
|
|
|
import { useWidgetSelection } from "utils/hooks/useWidgetSelection";
|
2019-04-02 16:12:08 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
const DraggableWrapper = styled.div`
|
2019-10-02 19:42:25 +00:00
|
|
|
display: block;
|
2020-03-06 09:33:20 +00:00
|
|
|
flex-direction: column;
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
2020-03-27 09:02:11 +00:00
|
|
|
user-select: none;
|
2020-01-07 07:22:12 +00:00
|
|
|
cursor: grab;
|
2019-09-30 03:25:14 +00:00
|
|
|
`;
|
|
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// Widget Boundaries which is shown to indicate the boundaries of the widget
|
2020-01-08 05:19:01 +00:00
|
|
|
const WidgetBoundaries = styled.div`
|
2020-03-06 09:33:20 +00:00
|
|
|
transform: translate3d(-${WIDGET_PADDING + 1}px, -${WIDGET_PADDING + 1}px, 0);
|
2020-01-16 11:46:21 +00:00
|
|
|
z-index: 0;
|
2020-03-06 09:33:20 +00:00
|
|
|
width: calc(100% + ${WIDGET_PADDING - 2}px);
|
|
|
|
|
height: calc(100% + ${WIDGET_PADDING - 2}px);
|
|
|
|
|
position: absolute;
|
2020-01-08 05:19:01 +00:00
|
|
|
border: 1px dashed
|
2020-12-24 04:32:25 +00:00
|
|
|
${(props) => getColorWithOpacity(props.theme.colors.textAnchor, 0.5)};
|
2020-01-10 12:15:54 +00:00
|
|
|
pointer-events: none;
|
2020-01-08 05:19:01 +00:00
|
|
|
`;
|
|
|
|
|
|
2020-10-12 13:06:05 +00:00
|
|
|
type DraggableComponentProps = WidgetProps;
|
2019-04-02 16:12:08 +00:00
|
|
|
|
2019-10-21 11:40:24 +00:00
|
|
|
/* eslint-disable react/display-name */
|
|
|
|
|
|
2021-04-23 05:43:13 +00:00
|
|
|
/**
|
|
|
|
|
* can drag helper function for react-dnd hook
|
|
|
|
|
*
|
2021-08-12 05:45:38 +00:00
|
|
|
* @param isResizingOrDragging
|
2021-04-23 05:43:13 +00:00
|
|
|
* @param isDraggingDisabled
|
|
|
|
|
* @param props
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const canDrag = (
|
2021-08-12 05:45:38 +00:00
|
|
|
isResizingOrDragging: boolean,
|
2021-04-23 05:43:13 +00:00
|
|
|
isDraggingDisabled: boolean,
|
|
|
|
|
props: any,
|
2021-06-09 10:35:10 +00:00
|
|
|
isCommentMode: boolean,
|
2021-07-26 16:44:10 +00:00
|
|
|
isSnipingMode: boolean,
|
2021-04-23 05:43:13 +00:00
|
|
|
) => {
|
2021-06-09 10:35:10 +00:00
|
|
|
return (
|
2021-08-12 05:45:38 +00:00
|
|
|
!isResizingOrDragging &&
|
2021-07-26 16:44:10 +00:00
|
|
|
!isDraggingDisabled &&
|
|
|
|
|
!props?.dragDisabled &&
|
|
|
|
|
!isCommentMode &&
|
|
|
|
|
!isSnipingMode
|
2021-06-09 10:35:10 +00:00
|
|
|
);
|
2021-04-23 05:43:13 +00:00
|
|
|
};
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
function DraggableComponent(props: DraggableComponentProps) {
|
2020-03-27 09:02:11 +00:00
|
|
|
// Dispatch hook handy to set a widget as focused/selected
|
2021-05-13 08:35:39 +00:00
|
|
|
const { focusWidget, selectWidget } = useWidgetSelection();
|
2020-03-27 09:02:11 +00:00
|
|
|
|
2021-06-09 10:35:10 +00:00
|
|
|
const isCommentMode = useSelector(commentModeSelector);
|
2021-07-26 16:44:10 +00:00
|
|
|
const isSnipingMode = useSelector(snipingModeSelector);
|
2020-03-27 09:02:11 +00:00
|
|
|
// Dispatch hook handy to set any `DraggableComponent` as dragging/ not dragging
|
|
|
|
|
// The value is boolean
|
2021-08-12 05:45:38 +00:00
|
|
|
const { setDraggingCanvas, setDraggingState } = useWidgetDragResize();
|
|
|
|
|
const showTableFilterPane = useShowTableFilterPane();
|
2021-05-18 18:29:39 +00:00
|
|
|
const selectedWidgets = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.selectedWidgets,
|
|
|
|
|
);
|
2020-03-27 09:02:11 +00:00
|
|
|
// This state tels us which widget is focused
|
|
|
|
|
// The value is the widgetId of the focused widget.
|
2020-01-20 09:00:37 +00:00
|
|
|
const focusedWidget = useSelector(
|
Feature/entity browse (#220)
# New Feature: Entity Explorer
- Entities are actions (apis and queries), datasources, pages, and widgets
- With this new feature, all entities in the application will be available
to view in the new entity explorer sidebar
- All existing application features from the api sidebar, query sidebar, datasource sidebar and pages sidebar
now are avialable on the entity explorer sidebar
- Users are now able to quickly switch to any entity in the application from the entity explorer sidebar.
- Users can also search all entities in the application from the new sidebar. Use cmd + f or ctrl + f to focus on the search input
- Users can rename entities from the new sidebar
- Users can also perform contextual actions on these entities like set a page as home page, copy/move actions, delete entity, etc from the context menu available alongside the entities in the sidebar
- Users can view the properties of the entities in the sidebar, as well as copy bindings to use in the application.
2020-08-10 08:52:45 +00:00
|
|
|
(state: AppState) => state.ui.widgetDragResize.focusedWidget,
|
2020-01-20 09:00:37 +00:00
|
|
|
);
|
2021-09-22 07:18:46 +00:00
|
|
|
const isCurrentWidgetFocused = focusedWidget === props.widgetId;
|
2021-08-12 05:45:38 +00:00
|
|
|
const isCurrentWidgetSelected = selectedWidgets.includes(props.widgetId);
|
2020-01-20 09:00:37 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// This state tells us whether a `ResizableComponent` is resizing
|
2020-01-20 09:00:37 +00:00
|
|
|
const isResizing = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isResizing,
|
|
|
|
|
);
|
2020-03-27 09:02:11 +00:00
|
|
|
|
|
|
|
|
// This state tells us whether a `DraggableComponent` is dragging
|
2020-01-20 09:00:37 +00:00
|
|
|
const isDragging = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isDragging,
|
|
|
|
|
);
|
2020-01-06 11:02:22 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// This state tells us to disable dragging,
|
|
|
|
|
// This is usually true when widgets themselves implement drag/drop
|
|
|
|
|
// This flag resolves conflicting drag/drop triggers.
|
2020-01-20 09:00:37 +00:00
|
|
|
const isDraggingDisabled: boolean = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isDraggingDisabled,
|
2020-01-09 11:39:26 +00:00
|
|
|
);
|
2019-10-21 11:40:24 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
// True when any widget is dragging or resizing, including this one
|
2020-03-04 08:10:40 +00:00
|
|
|
const isResizingOrDragging = !!isResizing || !!isDragging;
|
2021-08-12 05:45:38 +00:00
|
|
|
const isCurrentWidgetDragging = isDragging && isCurrentWidgetSelected;
|
|
|
|
|
const isCurrentWidgetResizing = isResizing && isCurrentWidgetSelected;
|
2020-03-27 09:02:11 +00:00
|
|
|
// When mouse is over this draggable
|
|
|
|
|
const handleMouseOver = (e: any) => {
|
|
|
|
|
focusWidget &&
|
|
|
|
|
!isResizingOrDragging &&
|
2021-09-22 07:18:46 +00:00
|
|
|
!isCurrentWidgetFocused &&
|
2021-08-09 05:35:01 +00:00
|
|
|
!props.resizeDisabled &&
|
2020-03-27 09:02:11 +00:00
|
|
|
focusWidget(props.widgetId);
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
};
|
2021-08-12 05:45:38 +00:00
|
|
|
const shouldRenderComponent = !(isCurrentWidgetSelected && isDragging);
|
2020-03-27 09:02:11 +00:00
|
|
|
// Display this draggable based on the current drag state
|
2021-08-12 05:45:38 +00:00
|
|
|
const dragWrapperStyle: CSSProperties = {
|
2021-05-11 14:39:33 +00:00
|
|
|
display: isCurrentWidgetDragging ? "none" : "block",
|
2020-03-27 09:02:11 +00:00
|
|
|
};
|
2021-08-12 05:45:38 +00:00
|
|
|
const dragBoundariesStyle: React.CSSProperties = useMemo(() => {
|
|
|
|
|
return {
|
|
|
|
|
opacity: !isResizingOrDragging || isCurrentWidgetResizing ? 0 : 1,
|
|
|
|
|
position: "absolute",
|
|
|
|
|
transform: `translate(-50%, -50%)`,
|
|
|
|
|
top: "50%",
|
|
|
|
|
left: "50%",
|
|
|
|
|
};
|
|
|
|
|
}, [isResizingOrDragging, isCurrentWidgetResizing]);
|
|
|
|
|
|
|
|
|
|
const widgetBoundaries = <WidgetBoundaries style={dragBoundariesStyle} />;
|
2020-03-27 09:02:11 +00:00
|
|
|
|
|
|
|
|
const classNameForTesting = `t--draggable-${props.type
|
2020-02-27 03:56:30 +00:00
|
|
|
.split("_")
|
|
|
|
|
.join("")
|
|
|
|
|
.toLowerCase()}`;
|
2020-03-06 09:33:20 +00:00
|
|
|
|
2021-08-12 05:45:38 +00:00
|
|
|
const allowDrag = canDrag(
|
|
|
|
|
isResizingOrDragging,
|
|
|
|
|
isDraggingDisabled,
|
|
|
|
|
props,
|
|
|
|
|
isCommentMode,
|
|
|
|
|
isSnipingMode,
|
|
|
|
|
);
|
2020-03-27 09:02:11 +00:00
|
|
|
const className = `${classNameForTesting}`;
|
2021-08-12 05:45:38 +00:00
|
|
|
const draggableRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
const onDragStart = (e: any) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
2021-09-22 07:18:46 +00:00
|
|
|
// allowDrag check is added as react jest test simulation is not respecting default behaviour
|
|
|
|
|
// of draggable=false and triggering onDragStart. allowDrag condition check is purely for the test cases.
|
|
|
|
|
if (allowDrag && draggableRef.current && !(e.metaKey || e.ctrlKey)) {
|
|
|
|
|
if (!isCurrentWidgetFocused) return;
|
|
|
|
|
|
2021-08-12 05:45:38 +00:00
|
|
|
if (!isCurrentWidgetSelected) {
|
|
|
|
|
selectWidget(props.widgetId);
|
|
|
|
|
}
|
|
|
|
|
const widgetHeight = props.bottomRow - props.topRow;
|
|
|
|
|
const widgetWidth = props.rightColumn - props.leftColumn;
|
|
|
|
|
const bounds = draggableRef.current.getBoundingClientRect();
|
|
|
|
|
const startPoints = {
|
|
|
|
|
top: Math.min(
|
|
|
|
|
Math.max((e.clientY - bounds.top) / props.parentRowSpace, 0),
|
|
|
|
|
widgetHeight - 1,
|
|
|
|
|
),
|
|
|
|
|
left: Math.min(
|
|
|
|
|
Math.max((e.clientX - bounds.left) / props.parentColumnSpace, 0),
|
|
|
|
|
widgetWidth - 1,
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
showTableFilterPane();
|
|
|
|
|
setDraggingCanvas(props.parentId);
|
|
|
|
|
|
|
|
|
|
setDraggingState({
|
|
|
|
|
isDragging: true,
|
|
|
|
|
dragGroupActualParent: props.parentId || "",
|
|
|
|
|
draggingGroupCenter: { widgetId: props.widgetId },
|
|
|
|
|
startPoints,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-01-24 10:44:15 +00:00
|
|
|
|
2020-03-27 09:02:11 +00:00
|
|
|
return (
|
|
|
|
|
<DraggableWrapper
|
|
|
|
|
className={className}
|
2021-08-12 05:45:38 +00:00
|
|
|
data-testid={isCurrentWidgetSelected ? "t--selected" : ""}
|
|
|
|
|
draggable={allowDrag}
|
|
|
|
|
onDragStart={onDragStart}
|
2021-04-28 10:28:39 +00:00
|
|
|
onMouseOver={handleMouseOver}
|
2021-08-12 05:45:38 +00:00
|
|
|
ref={draggableRef}
|
|
|
|
|
style={dragWrapperStyle}
|
2020-03-27 09:02:11 +00:00
|
|
|
>
|
2020-12-14 07:08:00 +00:00
|
|
|
{shouldRenderComponent && props.children}
|
2020-03-27 09:02:11 +00:00
|
|
|
{widgetBoundaries}
|
|
|
|
|
</DraggableWrapper>
|
2019-09-17 10:09:00 +00:00
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2019-04-02 16:12:08 +00:00
|
|
|
|
2019-09-17 10:09:00 +00:00
|
|
|
export default DraggableComponent;
|