PromucFlow_constructor/app/client/src/selectors/widgetSelectors.ts
Hetu Nandu c2dbebf9b7
fix: Do not set widget ancestry if selection happens via canvas (#22069)
## Description

Implemented in #21317. A problem arose where if a widget that exists
inside a tabs widget is setting the default value of the tab in order to
navigate change the tab, it would fail to switch it because the selected
widget logic takes over.

> Improve selected widget visibility by skipping feature when selection
happens via a canvas click

Fixes #22070


Media



https://user-images.githubusercontent.com/12022471/229714138-55f89cda-3c27-4953-91c0-46f5a9834adf.mov




## Type of change

> Please delete options that are not relevant.

- Bug fix (non-breaking change which fixes an issue)


## How Has This Been Tested?

- Manual
- Cypress

### Test Plan
> Add Testsmith test cases links that relate to this PR

### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)


## Checklist:
### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
2023-04-10 12:55:14 +05:30

231 lines
6.9 KiB
TypeScript

import { createSelector } from "reselect";
import type { AppState } from "@appsmith/reducers";
import type {
CanvasWidgetsReduxState,
FlattenedWidgetProps,
} from "reducers/entityReducers/canvasWidgetsReducer";
import { getExistingWidgetNames } from "sagas/selectors";
import { getNextEntityName } from "utils/AppsmithUtils";
import WidgetFactory from "utils/WidgetFactory";
import {
getFocusedWidget,
getLastSelectedWidget,
getSelectedWidgets,
} from "./ui";
import { MAIN_CONTAINER_WIDGET_ID } from "constants/WidgetConstants";
import { get } from "lodash";
import { getAppMode } from "@appsmith/selectors/applicationSelectors";
import { APP_MODE } from "entities/App";
import { getIsTableFilterPaneVisible } from "selectors/tableFilterSelectors";
import { getIsAutoHeightWithLimitsChanging } from "utils/hooks/autoHeightUIHooks";
import { getIsPropertyPaneVisible } from "./propertyPaneSelectors";
import { previewModeSelector } from "./editorSelectors";
export const getIsDraggingOrResizing = (state: AppState) =>
state.ui.widgetDragResize.isResizing || state.ui.widgetDragResize.isDragging;
export const getIsResizing = (state: AppState) =>
state.ui.widgetDragResize.isResizing;
const getCanvasWidgets = (state: AppState) => state.entities.canvasWidgets;
export const getModalDropdownList = createSelector(
getCanvasWidgets,
(widgets) => {
const modalWidgets = Object.values(widgets).filter(
(widget: FlattenedWidgetProps) => widget.type === "MODAL_WIDGET",
);
if (modalWidgets.length === 0) return undefined;
return modalWidgets.map((widget: FlattenedWidgetProps) => ({
id: widget.widgetId,
label: widget.widgetName,
value: `${widget.widgetName}`,
}));
},
);
export const getNextModalName = createSelector(
getExistingWidgetNames,
(names) => {
const prefix =
WidgetFactory.widgetConfigMap.get("MODAL_WIDGET")?.widgetName || "";
return getNextEntityName(prefix, names);
},
);
/**
* Selector to get the parent widget of a particaular widget with id as a prop
*/
export const getParentWidget = createSelector(
getCanvasWidgets,
(state: AppState, widgetId: string) => widgetId,
(canvasWidgets, widgetId: string): FlattenedWidgetProps | undefined => {
if (canvasWidgets.hasOwnProperty(widgetId)) {
const widget = canvasWidgets[widgetId];
if (widget.parentId && canvasWidgets.hasOwnProperty(widget.parentId)) {
const parent = canvasWidgets[widget.parentId];
return parent;
}
}
return;
},
);
export const getFocusedParentToOpen = createSelector(
getCanvasWidgets,
(state: AppState) => state.ui.widgetDragResize.focusedWidget,
(canvasWidgets, focusedWidgetId) => {
return getParentToOpenIfAny(focusedWidgetId, canvasWidgets);
},
);
export const getParentToOpenSelector = (widgetId: string) => {
return createSelector(getCanvasWidgets, (canvasWidgets) => {
return getParentToOpenIfAny(widgetId, canvasWidgets);
});
};
// Check if widget is in the list of selected widgets
export const isWidgetSelected = (widgetId: string) => {
return createSelector(getSelectedWidgets, (widgets): boolean =>
widgets.includes(widgetId),
);
};
export const isCurrentWidgetFocused = (widgetId: string) => {
return createSelector(
getFocusedWidget,
(widget): boolean => widget === widgetId,
);
};
// Check if current widget is the last selected widget
export const isCurrentWidgetLastSelected = (widgetId: string) => {
return createSelector(
getLastSelectedWidget,
(widget): boolean => widget === widgetId,
);
};
// Check if current widget is one of multiple selected widgets
export const isMultiSelectedWidget = (widgetId: string) => {
return createSelector(
getSelectedWidgets,
(widgets): boolean => widgets.length > 1 && widgets.includes(widgetId),
);
};
export function getParentToOpenIfAny(
widgetId: string | undefined,
widgets: CanvasWidgetsReduxState,
) {
if (widgetId) {
let widget = get(widgets, widgetId, undefined);
// While this widget has a openParentPropertyPane equal to true
while (widget?.openParentPropertyPane) {
// Get parent widget props
const parent = get(widgets, `${widget.parentId}`, undefined);
// If parent has openParentPropertyPane = false, return the current parent
if (!parent?.openParentPropertyPane) {
return parent;
}
if (parent?.parentId && parent.parentId !== MAIN_CONTAINER_WIDGET_ID) {
widget = get(widgets, `${widget.parentId}`, undefined);
continue;
}
}
}
return;
}
export const shouldWidgetIgnoreClicksSelector = (widgetId: string) => {
return createSelector(
getFocusedWidget,
getIsTableFilterPaneVisible,
(state: AppState) => state.ui.widgetDragResize.isResizing,
(state: AppState) => state.ui.widgetDragResize.isDragging,
(state: AppState) => state.ui.canvasSelection.isDraggingForSelection,
getAppMode,
previewModeSelector,
getIsAutoHeightWithLimitsChanging,
(
focusedWidgetId,
isTableFilterPaneVisible,
isResizing,
isDragging,
isDraggingForSelection,
appMode,
isPreviewMode,
isAutoHeightWithLimitsChanging,
) => {
const isFocused = focusedWidgetId === widgetId;
return (
isDraggingForSelection ||
isResizing ||
isDragging ||
isPreviewMode ||
appMode !== APP_MODE.EDIT ||
!isFocused ||
isTableFilterPaneVisible ||
isAutoHeightWithLimitsChanging
);
},
);
};
export const getSelectedWidgetAncestry = (state: AppState) =>
state.ui.widgetDragResize.selectedWidgetAncestry;
export const getEntityExplorerWidgetAncestry = (state: AppState) =>
state.ui.widgetDragResize.entityExplorerAncestry;
export const getEntityExplorerWidgetsToExpand = createSelector(
getEntityExplorerWidgetAncestry,
(selectedWidgetAncestry: string[]) => {
return selectedWidgetAncestry.slice(1);
},
);
export const showWidgetAsSelected = (widgetId: string) => {
return createSelector(
getLastSelectedWidget,
getSelectedWidgets,
(lastSelectedWidgetId, selectedWidgets) => {
return (
lastSelectedWidgetId === widgetId ||
(selectedWidgets.length > 1 && selectedWidgets.includes(widgetId))
);
},
);
};
export const getFirstSelectedWidgetInList = createSelector(
getSelectedWidgets,
(selectedWidgets) => {
return selectedWidgets?.length ? selectedWidgets[0] : undefined;
},
);
export const isCurrentWidgetActiveInPropertyPane = (widgetId: string) => {
return createSelector(
getIsPropertyPaneVisible,
getFirstSelectedWidgetInList,
(isPaneVisible, firstSelectedWidgetId) => {
return isPaneVisible && firstSelectedWidgetId === widgetId;
},
);
};
export const isResizingOrDragging = createSelector(
(state: AppState) => state.ui.widgetDragResize.isResizing,
(state: AppState) => state.ui.widgetDragResize.isDragging,
(isResizing, isDragging) => !!isResizing || !!isDragging,
);