2021-08-24 11:38:20 +00:00
|
|
|
import {
|
|
|
|
|
ReduxAction,
|
|
|
|
|
ReduxActionErrorTypes,
|
|
|
|
|
ReduxActionTypes,
|
2022-04-12 10:50:01 +00:00
|
|
|
} from "@appsmith/constants/ReduxActionConstants";
|
2023-02-21 13:38:16 +00:00
|
|
|
import {
|
|
|
|
|
all,
|
|
|
|
|
call,
|
|
|
|
|
fork,
|
|
|
|
|
put,
|
|
|
|
|
select,
|
|
|
|
|
take,
|
|
|
|
|
takeLatest,
|
|
|
|
|
} from "redux-saga/effects";
|
2021-08-24 11:38:20 +00:00
|
|
|
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
|
|
|
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";
|
2023-02-21 13:38:16 +00:00
|
|
|
import history, { NavigationMethod } from "utils/history";
|
|
|
|
|
import {
|
|
|
|
|
getCurrentPageId,
|
|
|
|
|
getIsEditorInitialized,
|
|
|
|
|
snipingModeSelector,
|
|
|
|
|
} from "selectors/editorSelectors";
|
|
|
|
|
import { builderURL, widgetURL } from "RouteBuilder";
|
|
|
|
|
import {
|
|
|
|
|
getAppMode,
|
|
|
|
|
getCanvasWidgets,
|
|
|
|
|
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";
|
2023-03-03 08:13:17 +00:00
|
|
|
import { quickScrollToWidget } from "utils/helpers";
|
2023-01-28 02:17:06 +00:00
|
|
|
import { areArraysEqual } from "utils/AppsmithUtils";
|
2023-02-21 13:38:16 +00:00
|
|
|
import { APP_MODE } from "entities/App";
|
2021-06-17 13:26:54 +00:00
|
|
|
|
2023-01-28 02:17:06 +00:00
|
|
|
function* selectWidgetSaga(action: ReduxAction<WidgetSelectionRequestPayload>) {
|
|
|
|
|
try {
|
2023-02-21 13:38:16 +00:00
|
|
|
const {
|
|
|
|
|
payload = [],
|
|
|
|
|
selectionRequestType,
|
|
|
|
|
invokedBy,
|
|
|
|
|
pageId,
|
|
|
|
|
} = 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: {
|
2023-02-21 13:38:16 +00:00
|
|
|
newSelection = [];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case SelectionRequestType.UnsafeSelect: {
|
|
|
|
|
newSelection = payload;
|
2023-01-28 02:17:06 +00:00
|
|
|
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,
|
|
|
|
|
) &&
|
2023-02-21 13:38:16 +00:00
|
|
|
newSelection[0] in allWidgets
|
2023-01-28 02:17:06 +00:00
|
|
|
) {
|
2023-02-21 13:38:16 +00:00
|
|
|
const selectionWidgetId = newSelection[0];
|
2023-01-28 02:17:06 +00:00
|
|
|
const parentId = allWidgets[selectionWidgetId].parentId;
|
|
|
|
|
if (parentId) {
|
|
|
|
|
const selectionSiblingWidgets: string[] = yield select(
|
|
|
|
|
getWidgetImmediateChildren,
|
|
|
|
|
parentId,
|
|
|
|
|
);
|
2023-02-21 13:38:16 +00:00
|
|
|
newSelection = newSelection.filter((each) =>
|
2023-01-28 02:17:06 +00:00
|
|
|
selectionSiblingWidgets.includes(each),
|
|
|
|
|
);
|
2021-08-24 11:38:20 +00:00
|
|
|
}
|
2021-06-17 13:26:54 +00:00
|
|
|
}
|
2023-02-21 13:38:16 +00:00
|
|
|
|
|
|
|
|
if (parentId && newSelection.length === 1) {
|
2023-01-28 02:17:06 +00:00
|
|
|
yield call(setWidgetAncestry, parentId, allWidgets);
|
|
|
|
|
}
|
2023-02-21 13:38:16 +00:00
|
|
|
if (areArraysEqual([...newSelection], [...selectedWidgets])) {
|
|
|
|
|
yield call(focusOnWidgetSaga, setSelectedWidgets(newSelection));
|
|
|
|
|
return;
|
2021-08-24 11:38:20 +00:00
|
|
|
}
|
2023-02-21 13:38:16 +00:00
|
|
|
yield call(appendSelectedWidgetToUrlSaga, newSelection, pageId, invokedBy);
|
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
|
2023-02-21 13:38:16 +00:00
|
|
|
* @param selectedWidgets
|
|
|
|
|
* @param pageId
|
|
|
|
|
* @param invokedBy
|
2022-10-17 15:16:38 +00:00
|
|
|
*/
|
|
|
|
|
function* appendSelectedWidgetToUrlSaga(
|
2023-02-21 13:38:16 +00:00
|
|
|
selectedWidgets: string[],
|
|
|
|
|
pageId?: string,
|
|
|
|
|
invokedBy?: NavigationMethod,
|
2022-10-17 15:16:38 +00:00
|
|
|
) {
|
2023-02-21 13:38:16 +00:00
|
|
|
const isSnipingMode: boolean = yield select(snipingModeSelector);
|
|
|
|
|
const appMode: APP_MODE = yield select(getAppMode);
|
|
|
|
|
const viewMode = appMode === APP_MODE.PUBLISHED;
|
2023-03-01 04:18:58 +00:00
|
|
|
if (isSnipingMode || viewMode) return;
|
2023-02-21 13:38:16 +00:00
|
|
|
const { pathname } = window.location;
|
2022-10-17 15:16:38 +00:00
|
|
|
const currentPageId: string = yield select(getCurrentPageId);
|
2023-02-21 13:38:16 +00:00
|
|
|
const currentURL = pathname;
|
|
|
|
|
const newUrl = selectedWidgets.length
|
|
|
|
|
? widgetURL({
|
|
|
|
|
pageId: pageId ?? currentPageId,
|
|
|
|
|
persistExistingParams: true,
|
|
|
|
|
selectedWidgets,
|
|
|
|
|
})
|
|
|
|
|
: builderURL({
|
|
|
|
|
pageId: pageId ?? currentPageId,
|
|
|
|
|
persistExistingParams: true,
|
|
|
|
|
});
|
|
|
|
|
if (currentURL !== newUrl) {
|
|
|
|
|
history.push(newUrl, { invokedBy });
|
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-02-21 13:38:16 +00:00
|
|
|
const isEditorInitialized: boolean = yield select(getIsEditorInitialized);
|
|
|
|
|
if (!isEditorInitialized) {
|
|
|
|
|
yield take(ReduxActionTypes.INITIALIZE_EDITOR_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 13:38:16 +00:00
|
|
|
function* focusOnWidgetSaga(action: ReduxAction<{ widgetIds: string[] }>) {
|
|
|
|
|
if (action.payload.widgetIds.length > 1) return;
|
|
|
|
|
const widgetId = action.payload.widgetIds[0];
|
|
|
|
|
const isEditorInitialized: boolean = yield select(getIsEditorInitialized);
|
|
|
|
|
if (!isEditorInitialized) {
|
|
|
|
|
yield take(ReduxActionTypes.INITIALIZE_EDITOR_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
const allWidgets: CanvasWidgetsReduxState = yield select(getCanvasWidgets);
|
|
|
|
|
if (widgetId) {
|
2023-03-03 08:13:17 +00:00
|
|
|
quickScrollToWidget(widgetId, allWidgets);
|
2023-02-21 13:38:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
),
|
2023-02-21 13:38:16 +00:00
|
|
|
takeLatest(ReduxActionTypes.SET_SELECTED_WIDGETS, focusOnWidgetSaga),
|
2021-06-17 13:26:54 +00:00
|
|
|
]);
|
|
|
|
|
}
|