2021-03-03 05:26:47 +00:00
|
|
|
import { theme } from "constants/DefaultTheme";
|
|
|
|
|
import { ReduxActionTypes } from "constants/ReduxActionConstants";
|
2021-03-11 02:21:48 +00:00
|
|
|
import {
|
|
|
|
|
DefaultLayoutType,
|
|
|
|
|
layoutConfigurations,
|
2021-03-16 05:01:37 +00:00
|
|
|
MAIN_CONTAINER_WIDGET_ID,
|
2021-03-11 02:21:48 +00:00
|
|
|
} from "constants/WidgetConstants";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { APP_MODE } from "entities/App";
|
2021-03-03 05:26:47 +00:00
|
|
|
import { debounce } from "lodash";
|
|
|
|
|
import { AppsmithDefaultLayout } from "pages/Editor/MainContainerLayoutControl";
|
|
|
|
|
import { useCallback, useEffect } from "react";
|
|
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
|
import { AppState } from "reducers";
|
2021-03-16 05:01:37 +00:00
|
|
|
import { getWidget, getWidgets } from "sagas/selectors";
|
2021-03-03 05:26:47 +00:00
|
|
|
import { getAppMode } from "selectors/applicationSelectors";
|
|
|
|
|
import {
|
|
|
|
|
getCurrentApplicationLayout,
|
|
|
|
|
getCurrentPageId,
|
|
|
|
|
} from "selectors/editorSelectors";
|
2021-09-09 15:10:22 +00:00
|
|
|
import { calculateDynamicHeight } from "utils/DSLMigrations";
|
2021-03-03 05:26:47 +00:00
|
|
|
import { useWindowSizeHooks } from "./dragResizeHooks";
|
|
|
|
|
|
|
|
|
|
export const useDynamicAppLayout = () => {
|
2021-05-13 08:35:39 +00:00
|
|
|
const { height: screenHeight, width: screenWidth } = useWindowSizeHooks();
|
2021-03-16 05:01:37 +00:00
|
|
|
const mainContainer = useSelector((state: AppState) =>
|
|
|
|
|
getWidget(state, MAIN_CONTAINER_WIDGET_ID),
|
|
|
|
|
);
|
2021-03-03 05:26:47 +00:00
|
|
|
const currentPageId = useSelector(getCurrentPageId);
|
|
|
|
|
const appMode = useSelector(getAppMode);
|
2021-03-16 05:01:37 +00:00
|
|
|
const canvasWidgets = useSelector(getWidgets);
|
2021-03-03 05:26:47 +00:00
|
|
|
const appLayout = useSelector(getCurrentApplicationLayout);
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
|
|
const calculateFluidMaxWidth = (
|
|
|
|
|
screenWidth: number,
|
|
|
|
|
layoutMaxWidth: number,
|
|
|
|
|
) => {
|
|
|
|
|
const screenWidthWithBuffer = 0.95 * screenWidth;
|
|
|
|
|
const widthToFill =
|
2021-09-09 15:10:22 +00:00
|
|
|
appMode === APP_MODE.EDIT
|
2021-03-03 05:26:47 +00:00
|
|
|
? screenWidthWithBuffer - parseInt(theme.sidebarWidth)
|
|
|
|
|
: screenWidth;
|
|
|
|
|
if (layoutMaxWidth < 0) {
|
|
|
|
|
return widthToFill;
|
|
|
|
|
} else {
|
|
|
|
|
return widthToFill < layoutMaxWidth ? widthToFill : layoutMaxWidth;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resizeToLayout = (
|
|
|
|
|
screenWidth: number,
|
|
|
|
|
appLayout = AppsmithDefaultLayout,
|
|
|
|
|
) => {
|
2021-03-11 02:21:48 +00:00
|
|
|
const { type } = appLayout;
|
|
|
|
|
const { minWidth = -1, maxWidth = -1 } =
|
|
|
|
|
layoutConfigurations[type] || layoutConfigurations[DefaultLayoutType];
|
|
|
|
|
const calculatedMinWidth =
|
2021-09-09 15:10:22 +00:00
|
|
|
appMode === APP_MODE.EDIT
|
|
|
|
|
? minWidth - parseInt(theme.sidebarWidth)
|
|
|
|
|
: minWidth;
|
2021-03-11 02:21:48 +00:00
|
|
|
const layoutWidth = calculateFluidMaxWidth(screenWidth, maxWidth);
|
2021-10-18 14:03:44 +00:00
|
|
|
const { rightColumn } = mainContainer || {};
|
2021-03-11 02:21:48 +00:00
|
|
|
if (
|
|
|
|
|
(type === "FLUID" || calculatedMinWidth <= layoutWidth) &&
|
|
|
|
|
rightColumn !== layoutWidth
|
|
|
|
|
) {
|
2021-03-03 05:26:47 +00:00
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.UPDATE_CANVAS_LAYOUT,
|
|
|
|
|
payload: {
|
|
|
|
|
width: layoutWidth,
|
2021-10-18 14:03:44 +00:00
|
|
|
height: mainContainer?.minHeight,
|
2021-03-03 05:26:47 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const debouncedResize = useCallback(debounce(resizeToLayout, 250), [
|
|
|
|
|
mainContainer,
|
|
|
|
|
]);
|
|
|
|
|
|
2021-03-16 05:01:37 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
const calculatedMinHeight = calculateDynamicHeight(
|
|
|
|
|
canvasWidgets,
|
2021-10-18 14:03:44 +00:00
|
|
|
mainContainer?.minHeight,
|
2021-03-16 05:01:37 +00:00
|
|
|
);
|
2021-10-18 14:03:44 +00:00
|
|
|
if (calculatedMinHeight !== mainContainer?.minHeight) {
|
2021-03-16 05:01:37 +00:00
|
|
|
dispatch({
|
|
|
|
|
type: ReduxActionTypes.UPDATE_CANVAS_LAYOUT,
|
|
|
|
|
payload: {
|
|
|
|
|
height: calculatedMinHeight,
|
2021-10-18 14:03:44 +00:00
|
|
|
width: mainContainer?.rightColumn,
|
2021-03-16 05:01:37 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-10-18 14:03:44 +00:00
|
|
|
}, [screenHeight, mainContainer?.minHeight]);
|
2021-03-16 05:01:37 +00:00
|
|
|
|
2021-03-03 05:26:47 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
debouncedResize(screenWidth, appLayout);
|
|
|
|
|
}, [screenWidth]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
resizeToLayout(screenWidth, appLayout);
|
2021-10-18 14:03:44 +00:00
|
|
|
}, [appLayout, currentPageId, mainContainer?.rightColumn]);
|
2021-03-03 05:26:47 +00:00
|
|
|
};
|