2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2023-01-13 18:49:21 +00:00
|
|
|
import equal from "fast-deep-equal/es6";
|
2021-08-12 05:45:38 +00:00
|
|
|
import React, { ReactNode, useCallback } from "react";
|
2023-01-13 18:49:21 +00:00
|
|
|
import { useSelector } from "react-redux";
|
|
|
|
|
import { getIsPropertyPaneVisible } from "selectors/propertyPaneSelectors";
|
2022-09-14 06:55:08 +00:00
|
|
|
import {
|
|
|
|
|
getFocusedParentToOpen,
|
|
|
|
|
isWidgetSelected,
|
|
|
|
|
shouldWidgetIgnoreClicksSelector,
|
|
|
|
|
} from "selectors/widgetSelectors";
|
2023-01-13 18:49:21 +00:00
|
|
|
import { stopEventPropagation } from "utils/AppsmithUtils";
|
|
|
|
|
import { useWidgetSelection } from "./useWidgetSelection";
|
2023-01-28 02:17:06 +00:00
|
|
|
import { SelectionRequestType } from "sagas/WidgetSelectUtils";
|
2021-03-29 15:47:22 +00:00
|
|
|
|
2021-08-12 05:45:38 +00:00
|
|
|
export function ClickContentToOpenPropPane({
|
|
|
|
|
children,
|
|
|
|
|
widgetId,
|
|
|
|
|
}: {
|
|
|
|
|
widgetId: string;
|
|
|
|
|
children?: ReactNode;
|
|
|
|
|
}) {
|
|
|
|
|
const { focusWidget } = useWidgetSelection();
|
|
|
|
|
|
2022-09-14 06:55:08 +00:00
|
|
|
const clickToSelectWidget = useClickToSelectWidget(widgetId);
|
|
|
|
|
|
2021-08-12 05:45:38 +00:00
|
|
|
const focusedWidget = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.focusedWidget,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const isResizing = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isResizing,
|
|
|
|
|
);
|
|
|
|
|
const isDragging = useSelector(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isDragging,
|
|
|
|
|
);
|
|
|
|
|
const isResizingOrDragging = !!isResizing || !!isDragging;
|
|
|
|
|
const handleMouseOver = (e: any) => {
|
|
|
|
|
focusWidget &&
|
|
|
|
|
!isResizingOrDragging &&
|
|
|
|
|
focusedWidget !== widgetId &&
|
|
|
|
|
focusWidget(widgetId);
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-23 09:48:23 +00:00
|
|
|
const styles = {
|
|
|
|
|
width: "100%",
|
|
|
|
|
height: "100%",
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-12 05:45:38 +00:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
onClick={stopEventPropagation}
|
2023-01-13 18:49:21 +00:00
|
|
|
onMouseDownCapture={clickToSelectWidget}
|
2021-08-12 05:45:38 +00:00
|
|
|
onMouseOver={handleMouseOver}
|
2022-11-23 09:48:23 +00:00
|
|
|
style={styles}
|
2021-08-12 05:45:38 +00:00
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 06:55:08 +00:00
|
|
|
export const useClickToSelectWidget = (widgetId: string) => {
|
2021-05-27 06:41:38 +00:00
|
|
|
const { focusWidget, selectWidget } = useWidgetSelection();
|
2021-03-29 15:47:22 +00:00
|
|
|
const isPropPaneVisible = useSelector(getIsPropertyPaneVisible);
|
2022-09-14 06:55:08 +00:00
|
|
|
const isSelected = useSelector(isWidgetSelected(widgetId));
|
|
|
|
|
const parentWidgetToOpen = useSelector(getFocusedParentToOpen, equal);
|
|
|
|
|
const shouldIgnoreClicks = useSelector(
|
|
|
|
|
shouldWidgetIgnoreClicksSelector(widgetId),
|
2021-03-29 15:47:22 +00:00
|
|
|
);
|
|
|
|
|
|
2022-09-14 06:55:08 +00:00
|
|
|
const clickToSelectWidget = useCallback(
|
|
|
|
|
(e: any) => {
|
|
|
|
|
// Ignore click captures
|
|
|
|
|
// 1. If the component is resizing or dragging because it is handled internally in draggable component.
|
|
|
|
|
// 2. If table filter property pane is open.
|
|
|
|
|
if (shouldIgnoreClicks) return;
|
|
|
|
|
if ((!isPropPaneVisible && isSelected) || !isSelected) {
|
2023-01-28 02:17:06 +00:00
|
|
|
let type: SelectionRequestType = SelectionRequestType.One;
|
|
|
|
|
if (e.metaKey || e.ctrlKey) {
|
|
|
|
|
type = SelectionRequestType.PushPop;
|
|
|
|
|
} else if (e.shiftKey) {
|
|
|
|
|
type = SelectionRequestType.ShiftSelect;
|
|
|
|
|
}
|
2021-05-27 06:41:38 +00:00
|
|
|
|
2022-09-14 06:55:08 +00:00
|
|
|
if (parentWidgetToOpen) {
|
2023-01-28 02:17:06 +00:00
|
|
|
selectWidget(type, [parentWidgetToOpen.widgetId]);
|
2022-09-14 06:55:08 +00:00
|
|
|
} else {
|
2023-01-28 02:17:06 +00:00
|
|
|
selectWidget(type, [widgetId]);
|
2022-09-14 06:55:08 +00:00
|
|
|
focusWidget(widgetId);
|
|
|
|
|
}
|
2021-11-23 08:01:46 +00:00
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
if (
|
|
|
|
|
type === SelectionRequestType.PushPop ||
|
|
|
|
|
type === SelectionRequestType.ShiftSelect
|
|
|
|
|
) {
|
2022-09-14 06:55:08 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
2021-05-18 18:29:39 +00:00
|
|
|
}
|
2022-09-14 06:55:08 +00:00
|
|
|
},
|
|
|
|
|
[shouldIgnoreClicks, isPropPaneVisible, isSelected, parentWidgetToOpen],
|
|
|
|
|
);
|
2021-08-23 14:12:17 +00:00
|
|
|
return clickToSelectWidget;
|
2021-03-29 15:47:22 +00:00
|
|
|
};
|