2021-09-21 07:55:56 +00:00
|
|
|
import {
|
|
|
|
|
MultipleWidgetDeletePayload,
|
|
|
|
|
updateAndSaveLayout,
|
|
|
|
|
WidgetDelete,
|
|
|
|
|
} from "actions/pageActions";
|
|
|
|
|
import { closePropertyPane, closeTableFilterPane } from "actions/widgetActions";
|
|
|
|
|
import { selectWidgetInitAction } from "actions/widgetSelectionActions";
|
|
|
|
|
import {
|
|
|
|
|
ReduxAction,
|
|
|
|
|
ReduxActionErrorTypes,
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
WidgetReduxActionTypes,
|
2022-04-12 10:50:01 +00:00
|
|
|
} from "@appsmith/constants/ReduxActionConstants";
|
2021-09-21 07:55:56 +00:00
|
|
|
import { ENTITY_TYPE } from "entities/AppsmithConsole";
|
|
|
|
|
import LOG_TYPE from "entities/AppsmithConsole/logtype";
|
2022-03-17 10:58:26 +00:00
|
|
|
import { flattenDeep, omit, orderBy } from "lodash";
|
2021-09-21 07:55:56 +00:00
|
|
|
import {
|
|
|
|
|
CanvasWidgetsReduxState,
|
|
|
|
|
FlattenedWidgetProps,
|
|
|
|
|
} from "reducers/entityReducers/canvasWidgetsReducer";
|
|
|
|
|
import { all, call, put, select, takeEvery } from "redux-saga/effects";
|
|
|
|
|
import { getSelectedWidgets } from "selectors/ui";
|
|
|
|
|
import AnalyticsUtil from "utils/AnalyticsUtil";
|
|
|
|
|
import AppsmithConsole from "utils/AppsmithConsole";
|
|
|
|
|
import { WidgetProps } from "widgets/BaseWidget";
|
|
|
|
|
import { getSelectedWidget, getWidget, getWidgets } from "./selectors";
|
2021-11-08 12:22:41 +00:00
|
|
|
import {
|
|
|
|
|
getAllWidgetsInTree,
|
|
|
|
|
updateListWidgetPropertiesOnChildDelete,
|
2022-06-21 13:57:34 +00:00
|
|
|
WidgetsInTree,
|
2021-11-08 12:22:41 +00:00
|
|
|
} from "./WidgetOperationUtils";
|
2021-09-21 07:55:56 +00:00
|
|
|
import { showUndoRedoToast } from "utils/replayHelpers";
|
|
|
|
|
import WidgetFactory from "utils/WidgetFactory";
|
2022-01-25 13:56:52 +00:00
|
|
|
import {
|
|
|
|
|
inGuidedTour,
|
|
|
|
|
isExploringSelector,
|
|
|
|
|
} from "selectors/onboardingSelectors";
|
|
|
|
|
import { toggleShowDeviationDialog } from "actions/onboardingActions";
|
2022-11-23 09:48:23 +00:00
|
|
|
import { generateAutoHeightLayoutTreeAction } from "actions/autoHeightActions";
|
2023-01-28 02:17:06 +00:00
|
|
|
import { SelectionRequestType } from "sagas/WidgetSelectUtils";
|
|
|
|
|
|
2021-09-21 07:55:56 +00:00
|
|
|
const WidgetTypes = WidgetFactory.widgetTypes;
|
|
|
|
|
|
|
|
|
|
type WidgetDeleteTabChild = {
|
|
|
|
|
id: string;
|
|
|
|
|
index: number;
|
|
|
|
|
isVisible: boolean;
|
|
|
|
|
label: string;
|
|
|
|
|
widgetId: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function* deleteTabChildSaga(
|
|
|
|
|
deleteChildTabAction: ReduxAction<WidgetDeleteTabChild>,
|
|
|
|
|
) {
|
|
|
|
|
const { index, label, widgetId } = deleteChildTabAction.payload;
|
|
|
|
|
const allWidgets: CanvasWidgetsReduxState = yield select(getWidgets);
|
|
|
|
|
const tabWidget = allWidgets[widgetId];
|
|
|
|
|
if (tabWidget && tabWidget.parentId) {
|
|
|
|
|
const tabParentWidget = allWidgets[tabWidget.parentId];
|
2022-03-17 10:58:26 +00:00
|
|
|
const tabsArray: any = orderBy(
|
|
|
|
|
Object.values(tabParentWidget.tabsObj),
|
|
|
|
|
"index",
|
|
|
|
|
"asc",
|
|
|
|
|
);
|
2021-09-21 07:55:56 +00:00
|
|
|
if (tabsArray && tabsArray.length === 1) return;
|
|
|
|
|
const updatedArray = tabsArray.filter((eachItem: any, i: number) => {
|
|
|
|
|
return i !== index;
|
|
|
|
|
});
|
|
|
|
|
const updatedObj = updatedArray.reduce(
|
|
|
|
|
(obj: any, each: any, index: number) => {
|
|
|
|
|
obj[each.id] = {
|
|
|
|
|
...each,
|
|
|
|
|
index,
|
|
|
|
|
};
|
|
|
|
|
return obj;
|
|
|
|
|
},
|
|
|
|
|
{},
|
|
|
|
|
);
|
|
|
|
|
const updatedDslObj: UpdatedDSLPostDelete = yield call(
|
|
|
|
|
getUpdatedDslAfterDeletingWidget,
|
|
|
|
|
widgetId,
|
|
|
|
|
tabWidget.parentId,
|
|
|
|
|
);
|
|
|
|
|
if (updatedDslObj) {
|
|
|
|
|
const { finalWidgets, otherWidgetsToDelete } = updatedDslObj;
|
|
|
|
|
const parentUpdatedWidgets = {
|
|
|
|
|
...finalWidgets,
|
|
|
|
|
[tabParentWidget.widgetId]: {
|
|
|
|
|
...finalWidgets[tabParentWidget.widgetId],
|
|
|
|
|
tabsObj: updatedObj,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
yield put(updateAndSaveLayout(parentUpdatedWidgets));
|
|
|
|
|
yield call(postDelete, widgetId, label, otherWidgetsToDelete);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* deleteSagaInit(deleteAction: ReduxAction<WidgetDelete>) {
|
|
|
|
|
const { widgetId } = deleteAction.payload;
|
2022-06-21 13:57:34 +00:00
|
|
|
const selectedWidget: FlattenedWidgetProps | undefined = yield select(
|
|
|
|
|
getSelectedWidget,
|
|
|
|
|
);
|
2021-09-21 07:55:56 +00:00
|
|
|
const selectedWidgets: string[] = yield select(getSelectedWidgets);
|
2022-06-21 13:57:34 +00:00
|
|
|
const guidedTourEnabled: boolean = yield select(inGuidedTour);
|
|
|
|
|
const isExploring: boolean = yield select(isExploringSelector);
|
2022-01-25 13:56:52 +00:00
|
|
|
|
|
|
|
|
if (guidedTourEnabled && !isExploring) {
|
|
|
|
|
yield put(toggleShowDeviationDialog(true));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 07:55:56 +00:00
|
|
|
if (selectedWidgets.length > 1) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: WidgetReduxActionTypes.WIDGET_BULK_DELETE,
|
|
|
|
|
payload: deleteAction.payload,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!!widgetId || !!selectedWidget) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: WidgetReduxActionTypes.WIDGET_SINGLE_DELETE,
|
|
|
|
|
payload: deleteAction.payload,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UpdatedDSLPostDelete =
|
|
|
|
|
| {
|
|
|
|
|
finalWidgets: CanvasWidgetsReduxState;
|
|
|
|
|
otherWidgetsToDelete: (WidgetProps & {
|
|
|
|
|
children?: string[] | undefined;
|
|
|
|
|
})[];
|
|
|
|
|
widgetName: string;
|
|
|
|
|
}
|
|
|
|
|
| undefined;
|
|
|
|
|
|
|
|
|
|
function* getUpdatedDslAfterDeletingWidget(widgetId: string, parentId: string) {
|
|
|
|
|
const stateWidgets: CanvasWidgetsReduxState = yield select(getWidgets);
|
|
|
|
|
if (widgetId && parentId) {
|
|
|
|
|
const widgets = { ...stateWidgets };
|
|
|
|
|
const stateWidget: WidgetProps = yield select(getWidget, widgetId);
|
|
|
|
|
const widget = { ...stateWidget };
|
|
|
|
|
|
|
|
|
|
const stateParent: FlattenedWidgetProps = yield select(getWidget, parentId);
|
|
|
|
|
let parent = { ...stateParent };
|
|
|
|
|
|
|
|
|
|
// Remove entry from parent's children
|
|
|
|
|
|
|
|
|
|
if (parent.children) {
|
|
|
|
|
parent = {
|
|
|
|
|
...parent,
|
|
|
|
|
children: parent.children.filter((c) => c !== widgetId),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widgets[parentId] = parent;
|
|
|
|
|
|
|
|
|
|
const otherWidgetsToDelete = getAllWidgetsInTree(widgetId, widgets);
|
|
|
|
|
let widgetName = widget.widgetName;
|
|
|
|
|
// SPECIAL HANDLING FOR TABS IN A TABS WIDGET
|
|
|
|
|
if (parent.type === WidgetTypes.TABS_WIDGET && widget.tabName) {
|
|
|
|
|
widgetName = widget.tabName;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 12:22:41 +00:00
|
|
|
let finalWidgets: CanvasWidgetsReduxState = updateListWidgetPropertiesOnChildDelete(
|
2021-09-21 07:55:56 +00:00
|
|
|
widgets,
|
|
|
|
|
widgetId,
|
|
|
|
|
widgetName,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
finalWidgets = omit(
|
|
|
|
|
finalWidgets,
|
|
|
|
|
otherWidgetsToDelete.map((widgets) => widgets.widgetId),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
finalWidgets,
|
|
|
|
|
otherWidgetsToDelete,
|
|
|
|
|
widgetName,
|
|
|
|
|
} as UpdatedDSLPostDelete;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* deleteSaga(deleteAction: ReduxAction<WidgetDelete>) {
|
|
|
|
|
try {
|
|
|
|
|
let { parentId, widgetId } = deleteAction.payload;
|
|
|
|
|
const { disallowUndo, isShortcut } = deleteAction.payload;
|
|
|
|
|
|
|
|
|
|
if (!widgetId) {
|
|
|
|
|
const selectedWidget: FlattenedWidgetProps | undefined = yield select(
|
|
|
|
|
getSelectedWidget,
|
|
|
|
|
);
|
|
|
|
|
if (!selectedWidget) return;
|
|
|
|
|
|
2022-01-25 13:56:52 +00:00
|
|
|
// if widget is not deletable, don't do anything
|
2021-09-21 07:55:56 +00:00
|
|
|
if (selectedWidget.isDeletable === false) return false;
|
|
|
|
|
|
|
|
|
|
widgetId = selectedWidget.widgetId;
|
|
|
|
|
parentId = selectedWidget.parentId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (widgetId && parentId) {
|
|
|
|
|
const stateWidget: WidgetProps = yield select(getWidget, widgetId);
|
|
|
|
|
const widget = { ...stateWidget };
|
|
|
|
|
const updatedObj: UpdatedDSLPostDelete = yield call(
|
|
|
|
|
getUpdatedDslAfterDeletingWidget,
|
|
|
|
|
widgetId,
|
|
|
|
|
parentId,
|
|
|
|
|
);
|
|
|
|
|
if (updatedObj) {
|
|
|
|
|
const { finalWidgets, otherWidgetsToDelete, widgetName } = updatedObj;
|
|
|
|
|
yield put(updateAndSaveLayout(finalWidgets));
|
2022-11-23 09:48:23 +00:00
|
|
|
yield put(generateAutoHeightLayoutTreeAction(true, true));
|
2021-09-21 07:55:56 +00:00
|
|
|
const analyticsEvent = isShortcut
|
|
|
|
|
? "WIDGET_DELETE_VIA_SHORTCUT"
|
|
|
|
|
: "WIDGET_DELETE";
|
|
|
|
|
|
|
|
|
|
AnalyticsUtil.logEvent(analyticsEvent, {
|
|
|
|
|
widgetName: widget.widgetName,
|
|
|
|
|
widgetType: widget.type,
|
|
|
|
|
});
|
|
|
|
|
if (!disallowUndo) {
|
|
|
|
|
// close property pane after delete
|
|
|
|
|
yield put(closePropertyPane());
|
2023-01-28 02:17:06 +00:00
|
|
|
yield put(
|
|
|
|
|
selectWidgetInitAction(SelectionRequestType.Unselect, [widgetId]),
|
|
|
|
|
);
|
2021-09-21 07:55:56 +00:00
|
|
|
yield call(postDelete, widgetId, widgetName, otherWidgetsToDelete);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.WIDGET_OPERATION_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
action: WidgetReduxActionTypes.WIDGET_DELETE,
|
|
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* deleteAllSelectedWidgetsSaga(
|
|
|
|
|
deleteAction: ReduxAction<MultipleWidgetDeletePayload>,
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
const { disallowUndo = false } = deleteAction.payload;
|
2022-06-21 13:57:34 +00:00
|
|
|
const stateWidgets: CanvasWidgetsReduxState = yield select(getWidgets);
|
2021-09-21 07:55:56 +00:00
|
|
|
const widgets = { ...stateWidgets };
|
|
|
|
|
const selectedWidgets: string[] = yield select(getSelectedWidgets);
|
|
|
|
|
if (!(selectedWidgets && selectedWidgets.length !== 1)) return;
|
2022-06-21 13:57:34 +00:00
|
|
|
const widgetsToBeDeleted: WidgetsInTree = yield all(
|
2021-09-21 07:55:56 +00:00
|
|
|
selectedWidgets.map((eachId) => {
|
|
|
|
|
return call(getAllWidgetsInTree, eachId, widgets);
|
|
|
|
|
}),
|
|
|
|
|
);
|
2022-06-21 13:57:34 +00:00
|
|
|
const flattenedWidgets = flattenDeep(widgetsToBeDeleted);
|
2023-02-14 16:07:31 +00:00
|
|
|
|
2022-01-28 11:10:05 +00:00
|
|
|
const parentUpdatedWidgets = flattenedWidgets.reduce(
|
2021-09-21 07:55:56 +00:00
|
|
|
(allWidgets: any, eachWidget: any) => {
|
|
|
|
|
const { parentId, widgetId } = eachWidget;
|
|
|
|
|
const stateParent: FlattenedWidgetProps = allWidgets[parentId];
|
|
|
|
|
let parent = { ...stateParent };
|
|
|
|
|
if (parent.children) {
|
|
|
|
|
parent = {
|
|
|
|
|
...parent,
|
|
|
|
|
children: parent.children.filter((c) => c !== widgetId),
|
|
|
|
|
};
|
|
|
|
|
allWidgets[parentId] = parent;
|
|
|
|
|
}
|
|
|
|
|
return allWidgets;
|
|
|
|
|
},
|
|
|
|
|
widgets,
|
|
|
|
|
);
|
|
|
|
|
const finalWidgets: CanvasWidgetsReduxState = omit(
|
|
|
|
|
parentUpdatedWidgets,
|
2022-01-28 11:10:05 +00:00
|
|
|
flattenedWidgets.map((widgets: any) => widgets.widgetId),
|
2021-09-21 07:55:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
yield put(updateAndSaveLayout(finalWidgets));
|
2022-11-23 09:48:23 +00:00
|
|
|
yield put(generateAutoHeightLayoutTreeAction(true, true));
|
|
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
yield put(selectWidgetInitAction(SelectionRequestType.Empty));
|
2021-09-21 07:55:56 +00:00
|
|
|
const bulkDeleteKey = selectedWidgets.join(",");
|
|
|
|
|
if (!disallowUndo) {
|
|
|
|
|
// close property pane after delete
|
|
|
|
|
yield put(closePropertyPane());
|
|
|
|
|
yield put(closeTableFilterPane());
|
|
|
|
|
showUndoRedoToast(`${selectedWidgets.length}`, true, false, true);
|
|
|
|
|
if (bulkDeleteKey) {
|
2022-01-28 11:10:05 +00:00
|
|
|
flattenedWidgets.map((widget: any) => {
|
2021-09-21 07:55:56 +00:00
|
|
|
AppsmithConsole.info({
|
|
|
|
|
logType: LOG_TYPE.ENTITY_DELETED,
|
|
|
|
|
text: "Widget was deleted",
|
|
|
|
|
source: {
|
|
|
|
|
name: widget.widgetName,
|
|
|
|
|
type: ENTITY_TYPE.WIDGET,
|
|
|
|
|
id: widget.widgetId,
|
|
|
|
|
},
|
|
|
|
|
analytics: {
|
|
|
|
|
widgetType: widget.type,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.WIDGET_OPERATION_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
action: WidgetReduxActionTypes.WIDGET_DELETE,
|
|
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function* postDelete(
|
|
|
|
|
widgetId: string,
|
|
|
|
|
widgetName: string,
|
|
|
|
|
otherWidgetsToDelete: (WidgetProps & {
|
|
|
|
|
children?: string[] | undefined;
|
|
|
|
|
})[],
|
|
|
|
|
) {
|
|
|
|
|
showUndoRedoToast(widgetName, false, false, true);
|
|
|
|
|
|
|
|
|
|
if (widgetId) {
|
|
|
|
|
otherWidgetsToDelete.map((widget) => {
|
|
|
|
|
AppsmithConsole.info({
|
|
|
|
|
logType: LOG_TYPE.ENTITY_DELETED,
|
|
|
|
|
text: "Widget was deleted",
|
|
|
|
|
source: {
|
|
|
|
|
name: widget.widgetName,
|
|
|
|
|
type: ENTITY_TYPE.WIDGET,
|
|
|
|
|
id: widget.widgetId,
|
|
|
|
|
},
|
|
|
|
|
analytics: {
|
|
|
|
|
widgetType: widget.type,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function* widgetDeletionSagas() {
|
|
|
|
|
yield all([
|
|
|
|
|
takeEvery(WidgetReduxActionTypes.WIDGET_DELETE, deleteSagaInit),
|
|
|
|
|
takeEvery(WidgetReduxActionTypes.WIDGET_SINGLE_DELETE, deleteSaga),
|
|
|
|
|
takeEvery(
|
|
|
|
|
WidgetReduxActionTypes.WIDGET_BULK_DELETE,
|
|
|
|
|
deleteAllSelectedWidgetsSaga,
|
|
|
|
|
),
|
|
|
|
|
takeEvery(ReduxActionTypes.WIDGET_DELETE_TAB_CHILD, deleteTabChildSaga),
|
|
|
|
|
]);
|
|
|
|
|
}
|