2021-08-24 11:38:20 +00:00
|
|
|
import {
|
|
|
|
|
ReduxAction,
|
|
|
|
|
ReduxActionErrorTypes,
|
|
|
|
|
ReduxActionTypes,
|
2022-04-12 10:50:01 +00:00
|
|
|
} from "@appsmith/constants/ReduxActionConstants";
|
2021-08-24 11:38:20 +00:00
|
|
|
import { all, call, fork, put, select, takeLatest } from "redux-saga/effects";
|
|
|
|
|
import {
|
2022-12-08 12:16:41 +00:00
|
|
|
getWidgetIdsByType,
|
2021-08-24 11:38:20 +00:00
|
|
|
getWidgetImmediateChildren,
|
|
|
|
|
getWidgets,
|
|
|
|
|
} from "./selectors";
|
2021-06-17 13:26:54 +00:00
|
|
|
import {
|
2023-01-28 02:17:06 +00:00
|
|
|
setLastSelectedWidget,
|
|
|
|
|
setSelectedWidgets,
|
|
|
|
|
WidgetSelectionRequestPayload,
|
2021-06-17 13:26:54 +00:00
|
|
|
} from "actions/widgetSelectionActions";
|
2022-09-15 05:44:11 +00:00
|
|
|
import { getLastSelectedWidget, getSelectedWidgets } from "selectors/ui";
|
2023-01-28 02:17:06 +00:00
|
|
|
import { CanvasWidgetsReduxState } from "reducers/entityReducers/canvasWidgetsReducer";
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2022-12-08 12:16:41 +00:00
|
|
|
import { closeAllModals, showModal } from "actions/widgetActions";
|
2022-10-17 15:16:38 +00:00
|
|
|
import history from "utils/history";
|
|
|
|
|
import { getCurrentPageId } from "selectors/editorSelectors";
|
|
|
|
|
import { builderURL } from "RouteBuilder";
|
2023-01-28 02:17:06 +00:00
|
|
|
import { getParentModalId } from "selectors/entitiesSelector";
|
2022-12-08 12:16:41 +00:00
|
|
|
import {
|
2023-01-28 02:17:06 +00:00
|
|
|
assertParentId,
|
|
|
|
|
isInvalidSelectionRequest,
|
|
|
|
|
pushPopWidgetSelection,
|
|
|
|
|
selectAllWidgetsInCanvasSaga,
|
|
|
|
|
SelectionRequestType,
|
|
|
|
|
selectMultipleWidgets,
|
|
|
|
|
selectOneWidget,
|
|
|
|
|
SetSelectionResult,
|
|
|
|
|
setWidgetAncestry,
|
|
|
|
|
shiftSelectWidgets,
|
|
|
|
|
unselectWidget,
|
|
|
|
|
} from "sagas/WidgetSelectUtils";
|
|
|
|
|
import { inGuidedTour } from "selectors/onboardingSelectors";
|
|
|
|
|
import { MAIN_CONTAINER_WIDGET_ID } from "constants/WidgetConstants";
|
|
|
|
|
import { areArraysEqual } from "utils/AppsmithUtils";
|
2021-06-17 13:26:54 +00:00
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
function* selectWidgetSaga(action: ReduxAction<WidgetSelectionRequestPayload>) {
|
|
|
|
|
try {
|
|
|
|
|
const { payload = [], selectionRequestType } = action.payload;
|
2021-06-17 13:26:54 +00:00
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
if (payload.some(isInvalidSelectionRequest)) {
|
|
|
|
|
// Throw error
|
|
|
|
|
return;
|
2021-08-24 11:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
let newSelection: SetSelectionResult;
|
2021-08-24 11:38:20 +00:00
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
const allWidgets: CanvasWidgetsReduxState = yield select(getWidgets);
|
|
|
|
|
const selectedWidgets: string[] = yield select(getSelectedWidgets);
|
|
|
|
|
const lastSelectedWidget: string = yield select(getLastSelectedWidget);
|
2021-08-24 11:38:20 +00:00
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
// It is possible that the payload is empty.
|
|
|
|
|
// These properties can be used for a finding sibling widgets for certain types of selections
|
|
|
|
|
const widgetId = payload[0];
|
|
|
|
|
const parentId: string | undefined =
|
|
|
|
|
widgetId in allWidgets ? allWidgets[widgetId].parentId : undefined;
|
|
|
|
|
|
2023-02-21 04:13:25 +00:00
|
|
|
if (
|
|
|
|
|
widgetId &&
|
|
|
|
|
!allWidgets[widgetId] &&
|
|
|
|
|
selectionRequestType === SelectionRequestType.One
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
switch (selectionRequestType) {
|
|
|
|
|
case SelectionRequestType.Empty: {
|
|
|
|
|
newSelection = {
|
|
|
|
|
widgets: [],
|
|
|
|
|
lastWidgetSelected: MAIN_CONTAINER_WIDGET_ID,
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SelectionRequestType.One: {
|
|
|
|
|
assertParentId(parentId);
|
|
|
|
|
newSelection = selectOneWidget(payload);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SelectionRequestType.Multiple: {
|
|
|
|
|
newSelection = selectMultipleWidgets(payload, allWidgets);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SelectionRequestType.ShiftSelect: {
|
|
|
|
|
assertParentId(parentId);
|
|
|
|
|
const siblingWidgets: string[] = yield select(
|
|
|
|
|
getWidgetImmediateChildren,
|
|
|
|
|
parentId,
|
|
|
|
|
);
|
|
|
|
|
newSelection = shiftSelectWidgets(
|
|
|
|
|
payload,
|
|
|
|
|
siblingWidgets,
|
|
|
|
|
selectedWidgets,
|
|
|
|
|
lastSelectedWidget,
|
2021-08-24 11:38:20 +00:00
|
|
|
);
|
2023-01-28 02:17:06 +00:00
|
|
|
break;
|
2021-08-24 11:38:20 +00:00
|
|
|
}
|
2023-01-28 02:17:06 +00:00
|
|
|
case SelectionRequestType.PushPop: {
|
|
|
|
|
assertParentId(parentId);
|
|
|
|
|
const siblingWidgets: string[] = yield select(
|
|
|
|
|
getWidgetImmediateChildren,
|
|
|
|
|
parentId,
|
|
|
|
|
);
|
|
|
|
|
newSelection = pushPopWidgetSelection(
|
|
|
|
|
payload,
|
|
|
|
|
selectedWidgets,
|
|
|
|
|
siblingWidgets,
|
2021-08-24 11:38:20 +00:00
|
|
|
);
|
2023-01-28 02:17:06 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SelectionRequestType.Unselect: {
|
|
|
|
|
newSelection = unselectWidget(payload, selectedWidgets);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SelectionRequestType.All: {
|
|
|
|
|
newSelection = yield call(selectAllWidgetsInCanvasSaga);
|
2021-08-24 11:38:20 +00:00
|
|
|
}
|
2021-06-17 13:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
if (!newSelection) return;
|
2021-06-17 13:26:54 +00:00
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
// When append selections happen, we want to ensure they all exist under the same parent
|
|
|
|
|
// Selections across parents is not possible.
|
|
|
|
|
if (
|
|
|
|
|
[SelectionRequestType.PushPop, SelectionRequestType.ShiftSelect].includes(
|
|
|
|
|
selectionRequestType,
|
|
|
|
|
) &&
|
|
|
|
|
newSelection.widgets[0] in allWidgets
|
|
|
|
|
) {
|
|
|
|
|
const selectionWidgetId = newSelection.widgets[0];
|
|
|
|
|
const parentId = allWidgets[selectionWidgetId].parentId;
|
|
|
|
|
if (parentId) {
|
|
|
|
|
const selectionSiblingWidgets: string[] = yield select(
|
|
|
|
|
getWidgetImmediateChildren,
|
|
|
|
|
parentId,
|
|
|
|
|
);
|
|
|
|
|
newSelection.widgets = newSelection.widgets.filter((each) =>
|
|
|
|
|
selectionSiblingWidgets.includes(each),
|
|
|
|
|
);
|
2021-08-24 11:38:20 +00:00
|
|
|
}
|
2021-06-17 13:26:54 +00:00
|
|
|
}
|
2023-01-28 02:17:06 +00:00
|
|
|
if (!areArraysEqual(newSelection.widgets, selectedWidgets)) {
|
|
|
|
|
yield put(setSelectedWidgets(newSelection.widgets));
|
2021-08-24 11:38:20 +00:00
|
|
|
}
|
2023-01-28 02:17:06 +00:00
|
|
|
if (parentId && newSelection.widgets.length === 1) {
|
|
|
|
|
yield call(setWidgetAncestry, parentId, allWidgets);
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
newSelection.lastWidgetSelected &&
|
|
|
|
|
newSelection.lastWidgetSelected !== lastSelectedWidget
|
|
|
|
|
) {
|
|
|
|
|
yield put(setLastSelectedWidget(newSelection.lastWidgetSelected));
|
2021-08-24 11:38:20 +00:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.WIDGET_SELECTION_ERROR,
|
|
|
|
|
payload: {
|
2023-01-28 02:17:06 +00:00
|
|
|
action: ReduxActionTypes.SELECT_WIDGET_INIT,
|
2021-08-24 11:38:20 +00:00
|
|
|
error,
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-06-28 07:11:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 15:16:38 +00:00
|
|
|
/**
|
|
|
|
|
* Append Selected widgetId as hash to the url path
|
|
|
|
|
* @param action
|
|
|
|
|
*/
|
|
|
|
|
function* appendSelectedWidgetToUrlSaga(
|
|
|
|
|
action: ReduxAction<{ selectedWidgets: string[] }>,
|
|
|
|
|
) {
|
2023-01-28 02:17:06 +00:00
|
|
|
const guidedTourEnabled: boolean = yield select(inGuidedTour);
|
|
|
|
|
if (guidedTourEnabled) return;
|
2022-10-17 15:16:38 +00:00
|
|
|
const { hash, pathname } = window.location;
|
|
|
|
|
const { selectedWidgets } = action.payload;
|
|
|
|
|
const currentPageId: string = yield select(getCurrentPageId);
|
|
|
|
|
|
|
|
|
|
const currentURL = hash ? `${pathname}${hash}` : pathname;
|
|
|
|
|
let canvasEditorURL;
|
|
|
|
|
if (selectedWidgets.length === 1) {
|
|
|
|
|
canvasEditorURL = `${builderURL({
|
|
|
|
|
pageId: currentPageId,
|
|
|
|
|
hash: selectedWidgets[0],
|
2022-11-28 05:44:31 +00:00
|
|
|
persistExistingParams: true,
|
2022-10-17 15:16:38 +00:00
|
|
|
})}`;
|
|
|
|
|
} else {
|
|
|
|
|
canvasEditorURL = `${builderURL({
|
|
|
|
|
pageId: currentPageId,
|
2022-11-28 05:44:31 +00:00
|
|
|
persistExistingParams: true,
|
2022-10-17 15:16:38 +00:00
|
|
|
})}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentURL !== canvasEditorURL) {
|
2022-11-28 05:44:31 +00:00
|
|
|
history.replace(canvasEditorURL);
|
2022-10-17 15:16:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-16 09:24:42 +00:00
|
|
|
function* canPerformSelectionSaga(saga: any, action: any) {
|
|
|
|
|
const isDragging: boolean = yield select(
|
|
|
|
|
(state: AppState) => state.ui.widgetDragResize.isDragging,
|
|
|
|
|
);
|
|
|
|
|
if (!isDragging) {
|
|
|
|
|
yield fork(saga, action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
function* openOrCloseModalSaga(action: ReduxAction<{ widgetIds: string[] }>) {
|
|
|
|
|
if (action.payload.widgetIds.length !== 1) return;
|
2021-08-16 09:24:42 +00:00
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
const selectedWidget = action.payload.widgetIds[0];
|
2022-12-08 12:16:41 +00:00
|
|
|
|
|
|
|
|
const modalWidgetIds: string[] = yield select(
|
|
|
|
|
getWidgetIdsByType,
|
|
|
|
|
"MODAL_WIDGET",
|
|
|
|
|
);
|
|
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
const widgetIsModal = modalWidgetIds.includes(selectedWidget);
|
2022-12-08 12:16:41 +00:00
|
|
|
|
|
|
|
|
if (widgetIsModal) {
|
2023-01-28 02:17:06 +00:00
|
|
|
yield put(showModal(selectedWidget));
|
2022-12-08 12:16:41 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 02:32:03 +00:00
|
|
|
const widgetMap: CanvasWidgetsReduxState = yield select(getWidgets);
|
2023-01-28 02:17:06 +00:00
|
|
|
const widget = widgetMap[selectedWidget];
|
2022-12-08 12:16:41 +00:00
|
|
|
|
2022-12-13 10:04:32 +00:00
|
|
|
if (widget && widget.parentId) {
|
2022-12-15 02:32:03 +00:00
|
|
|
const parentModalId = getParentModalId(widget, widgetMap);
|
|
|
|
|
const widgetInModal = modalWidgetIds.includes(parentModalId);
|
2022-12-08 12:16:41 +00:00
|
|
|
if (widgetInModal) {
|
2022-12-15 02:32:03 +00:00
|
|
|
yield put(showModal(parentModalId));
|
2022-12-08 12:16:41 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield put(closeAllModals());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 13:26:54 +00:00
|
|
|
export function* widgetSelectionSagas() {
|
|
|
|
|
yield all([
|
2023-01-28 02:17:06 +00:00
|
|
|
takeLatest(ReduxActionTypes.SELECT_WIDGET_INIT, selectWidgetSaga),
|
2021-06-17 13:26:54 +00:00
|
|
|
takeLatest(
|
2023-01-28 02:17:06 +00:00
|
|
|
ReduxActionTypes.SET_SELECTED_WIDGETS,
|
2022-12-08 12:16:41 +00:00
|
|
|
canPerformSelectionSaga,
|
|
|
|
|
openOrCloseModalSaga,
|
|
|
|
|
),
|
2022-10-17 15:16:38 +00:00
|
|
|
takeLatest(
|
|
|
|
|
ReduxActionTypes.APPEND_SELECTED_WIDGET_TO_URL,
|
2023-01-28 02:17:06 +00:00
|
|
|
canPerformSelectionSaga,
|
2022-10-17 15:16:38 +00:00
|
|
|
appendSelectedWidgetToUrlSaga,
|
|
|
|
|
),
|
2021-06-17 13:26:54 +00:00
|
|
|
]);
|
|
|
|
|
}
|