PromucFlow_constructor/app/client/src/selectors/widgetSelectors.ts

170 lines
4.9 KiB
TypeScript
Raw Normal View History

import { createSelector } from "reselect";
import { AppState } from "@appsmith/reducers";
import {
CanvasWidgetsReduxState,
FlattenedWidgetProps,
} from "reducers/entityReducers/canvasWidgetsReducer";
import { getExistingWidgetNames } from "sagas/selectors";
2020-04-03 09:32:13 +00:00
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 "selectors/applicationSelectors";
import { APP_MODE } from "entities/App";
import { getIsTableFilterPaneVisible } from "selectors/tableFilterSelectors";
export const getIsDraggingOrResizing = (state: AppState) =>
state.ui.widgetDragResize.isResizing || state.ui.widgetDragResize.isDragging;
perf: Widget re-rendering refactor (#14485) * initial commit * props hoc * changes * removed ignores and withWidgetProps * added extra props to canvasStructure * widget props changes * list widget changes * reintroduced widget props hook and other refactors * remove warnings * added deepequal for childWidgets selector * fix global hotkeys and tabs widget jest test * fix main container test fix * fixed view mode width * fix form widget values * minor fix * fix skeleton * form widget validity fix * jest test fix * fixed tests: GlobalHotkeys, Tabs, CanvasSelectectionArena and fixed main container rendering * minor fix * minor comments * reverted commented code * simplified structure, selective redux state updates and other inconsistencies * fix junit test cases * stop form widget from force rendering children * fix test case * random commit to re run tests * update isFormValid prop only if it exists * detangling circular dependency * fixing cypress tests * cleaned up code * clean up man cnavas props and fix jest cases * fix rendering order of child widgets for canvas * fix dropdown reset spec * adding comments * cleaning up unwanted code * fix multiselect widget on deploy * adressing review comments * addressing minor review comment changes * destructuring modal widget child and fix test case * fix communityIssues cypress spec * rewrite isVisible logic to match previous behaviour * merging widget props with component props before checking isVisible * adressing review comments for modal widget's isVisible Co-authored-by: rahulramesha <rahul@appsmith.com>
2022-08-19 10:10:36 +00:00
export const getIsResizing = (state: AppState) =>
state.ui.widgetDragResize.isResizing;
const getCanvasWidgets = (state: AppState) => state.entities.canvasWidgets;
export const getModalDropdownList = createSelector(
getCanvasWidgets,
2020-12-24 04:32:25 +00:00
(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,
2020-04-20 05:42:46 +00:00
value: `${widget.widgetName}`,
}));
},
);
2020-04-03 09:32:13 +00:00
export const getNextModalName = createSelector(
getExistingWidgetNames,
(names) => {
const prefix =
WidgetFactory.widgetConfigMap.get("MODAL_WIDGET")?.widgetName || "";
return getNextEntityName(prefix, names);
},
2020-04-03 09:32:13 +00:00
);
/**
* 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;
},
);
perf: Widget re-rendering refactor (#14485) * initial commit * props hoc * changes * removed ignores and withWidgetProps * added extra props to canvasStructure * widget props changes * list widget changes * reintroduced widget props hook and other refactors * remove warnings * added deepequal for childWidgets selector * fix global hotkeys and tabs widget jest test * fix main container test fix * fixed view mode width * fix form widget values * minor fix * fix skeleton * form widget validity fix * jest test fix * fixed tests: GlobalHotkeys, Tabs, CanvasSelectectionArena and fixed main container rendering * minor fix * minor comments * reverted commented code * simplified structure, selective redux state updates and other inconsistencies * fix junit test cases * stop form widget from force rendering children * fix test case * random commit to re run tests * update isFormValid prop only if it exists * detangling circular dependency * fixing cypress tests * cleaned up code * clean up man cnavas props and fix jest cases * fix rendering order of child widgets for canvas * fix dropdown reset spec * adding comments * cleaning up unwanted code * fix multiselect widget on deploy * adressing review comments * addressing minor review comment changes * destructuring modal widget child and fix test case * fix communityIssues cypress spec * rewrite isVisible logic to match previous behaviour * merging widget props with component props before checking isVisible * adressing review comments for modal widget's isVisible Co-authored-by: rahulramesha <rahul@appsmith.com>
2022-08-19 10:10:36 +00:00
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,
getAppMode,
(
focusedWidgetId,
isTableFilterPaneVisible,
isResizing,
isDragging,
appMode,
) => {
const isFocused = focusedWidgetId === widgetId;
return (
isResizing ||
isDragging ||
appMode !== APP_MODE.EDIT ||
!isFocused ||
isTableFilterPaneVisible
);
},
);
};