2021-11-23 08:01:46 +00:00
|
|
|
import { debounce, get } from "lodash";
|
2022-05-26 04:52:55 +00:00
|
|
|
import { useCallback, useEffect, useMemo } from "react";
|
2023-03-04 07:25:54 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2021-11-23 08:01:46 +00:00
|
|
|
|
2023-03-04 07:25:54 +00:00
|
|
|
// import { updateLayoutForMobileBreakpointAction } from "actions/autoLayoutActions";
|
|
|
|
|
import { updateCanvasLayoutAction } from "actions/editorActions";
|
|
|
|
|
import { APP_SETTINGS_PANE_WIDTH } from "constants/AppConstants";
|
2021-03-11 02:21:48 +00:00
|
|
|
import {
|
|
|
|
|
DefaultLayoutType,
|
|
|
|
|
layoutConfigurations,
|
2023-03-04 07:25:54 +00:00
|
|
|
// MAIN_CONTAINER_WIDGET_ID,
|
2021-03-11 02:21:48 +00:00
|
|
|
} from "constants/WidgetConstants";
|
2023-03-04 07:25:54 +00:00
|
|
|
import { APP_MODE } from "entities/App";
|
|
|
|
|
// import { AppPositioningTypes } from "reducers/entityReducers/pageListReducer";
|
2021-03-03 05:26:47 +00:00
|
|
|
import {
|
|
|
|
|
getCurrentApplicationLayout,
|
2023-03-04 07:25:54 +00:00
|
|
|
// getCurrentAppPositioningType,
|
2021-03-03 05:26:47 +00:00
|
|
|
getCurrentPageId,
|
2022-04-14 04:36:20 +00:00
|
|
|
getMainCanvasProps,
|
2021-11-23 08:01:46 +00:00
|
|
|
previewModeSelector,
|
2021-03-03 05:26:47 +00:00
|
|
|
} from "selectors/editorSelectors";
|
2021-11-23 08:01:46 +00:00
|
|
|
import { getAppMode } from "selectors/entitiesSelector";
|
2023-03-04 07:25:54 +00:00
|
|
|
import {
|
|
|
|
|
getExplorerPinned,
|
|
|
|
|
getExplorerWidth,
|
|
|
|
|
} from "selectors/explorerSelector";
|
2022-05-26 04:52:55 +00:00
|
|
|
import { getIsCanvasInitialized } from "selectors/mainCanvasSelectors";
|
2023-03-23 11:41:58 +00:00
|
|
|
import {
|
|
|
|
|
getIsAppSettingsPaneOpen,
|
|
|
|
|
getIsAppSettingsPaneWithNavigationTabOpen,
|
|
|
|
|
} from "selectors/appSettingsPaneSelectors";
|
2023-01-09 05:24:41 +00:00
|
|
|
import {
|
|
|
|
|
getPaneCount,
|
|
|
|
|
getTabsPaneWidth,
|
|
|
|
|
isMultiPaneActive,
|
|
|
|
|
} from "selectors/multiPaneSelectors";
|
2023-03-23 11:41:58 +00:00
|
|
|
import { SIDE_NAV_WIDTH } from "pages/common/SideNav";
|
|
|
|
|
import {
|
|
|
|
|
getAppSidebarPinned,
|
|
|
|
|
getCurrentApplication,
|
|
|
|
|
getSidebarWidth,
|
|
|
|
|
} from "selectors/applicationSelectors";
|
|
|
|
|
import { useIsMobileDevice } from "./useDeviceDetect";
|
2023-03-04 07:25:54 +00:00
|
|
|
import { getPropertyPaneWidth } from "selectors/propertyPaneSelectors";
|
|
|
|
|
import { scrollbarWidth } from "utils/helpers";
|
|
|
|
|
import { useWindowSizeHooks } from "./dragResizeHooks";
|
2021-11-23 08:01:46 +00:00
|
|
|
|
|
|
|
|
const BORDERS_WIDTH = 2;
|
|
|
|
|
const GUTTER_WIDTH = 72;
|
2023-03-04 07:25:54 +00:00
|
|
|
export const AUTOLAYOUT_RESIZER_WIDTH_BUFFER = 40;
|
2021-03-03 05:26:47 +00:00
|
|
|
|
|
|
|
|
export const useDynamicAppLayout = () => {
|
2021-11-23 08:01:46 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const explorerWidth = useSelector(getExplorerWidth);
|
2022-12-02 05:49:51 +00:00
|
|
|
const propertyPaneWidth = useSelector(getPropertyPaneWidth);
|
2021-11-23 08:01:46 +00:00
|
|
|
const isExplorerPinned = useSelector(getExplorerPinned);
|
|
|
|
|
const appMode: APP_MODE | undefined = useSelector(getAppMode);
|
2022-11-23 09:48:23 +00:00
|
|
|
const { width: screenWidth } = useWindowSizeHooks();
|
2022-04-14 04:36:20 +00:00
|
|
|
const mainCanvasProps = useSelector(getMainCanvasProps);
|
2021-11-23 08:01:46 +00:00
|
|
|
const isPreviewMode = useSelector(previewModeSelector);
|
2021-03-03 05:26:47 +00:00
|
|
|
const currentPageId = useSelector(getCurrentPageId);
|
2022-05-26 04:52:55 +00:00
|
|
|
const isCanvasInitialized = useSelector(getIsCanvasInitialized);
|
2021-03-03 05:26:47 +00:00
|
|
|
const appLayout = useSelector(getCurrentApplicationLayout);
|
2022-12-02 05:49:51 +00:00
|
|
|
const isAppSettingsPaneOpen = useSelector(getIsAppSettingsPaneOpen);
|
2023-01-09 05:24:41 +00:00
|
|
|
const tabsPaneWidth = useSelector(getTabsPaneWidth);
|
|
|
|
|
const isMultiPane = useSelector(isMultiPaneActive);
|
|
|
|
|
const paneCount = useSelector(getPaneCount);
|
2023-03-23 11:41:58 +00:00
|
|
|
const isAppSidebarPinned = useSelector(getAppSidebarPinned);
|
|
|
|
|
const sidebarWidth = useSelector(getSidebarWidth);
|
|
|
|
|
const isAppSettingsPaneWithNavigationTabOpen = useSelector(
|
|
|
|
|
getIsAppSettingsPaneWithNavigationTabOpen,
|
|
|
|
|
);
|
|
|
|
|
const currentApplicationDetails = useSelector(getCurrentApplication);
|
|
|
|
|
const isMobile = useIsMobileDevice();
|
2023-03-04 07:25:54 +00:00
|
|
|
// const appPositioningType = useSelector(getCurrentAppPositioningType);
|
2021-03-03 05:26:47 +00:00
|
|
|
|
2022-11-23 09:48:23 +00:00
|
|
|
// /**
|
|
|
|
|
// * calculates min height
|
|
|
|
|
// */
|
|
|
|
|
// const calculatedMinHeight = useMemo(() => {
|
|
|
|
|
// return calculateDynamicHeight();
|
|
|
|
|
// }, [mainCanvasProps]);
|
2021-11-23 08:01:46 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* app layout range i.e minWidth and maxWidth for the current layout
|
|
|
|
|
* if there is no config for the current layout, use default layout i.e desktop
|
|
|
|
|
*/
|
|
|
|
|
const layoutWidthRange = useMemo(() => {
|
|
|
|
|
let minWidth = -1;
|
|
|
|
|
let maxWidth = -1;
|
|
|
|
|
|
|
|
|
|
if (appLayout) {
|
|
|
|
|
const { type } = appLayout;
|
|
|
|
|
const currentLayoutConfig = get(
|
|
|
|
|
layoutConfigurations,
|
|
|
|
|
type,
|
|
|
|
|
layoutConfigurations[DefaultLayoutType],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (currentLayoutConfig.minWidth) minWidth = currentLayoutConfig.minWidth;
|
|
|
|
|
if (currentLayoutConfig.maxWidth) maxWidth = currentLayoutConfig.maxWidth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { minWidth, maxWidth };
|
|
|
|
|
}, [appLayout]);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* calculate the width for the canvas
|
|
|
|
|
*
|
|
|
|
|
* cases:
|
|
|
|
|
* - if max width is negative, use calculated width
|
|
|
|
|
* - if calculated width is in range of min/max widths of layout, use calculated width
|
|
|
|
|
* - if calculated width is less then min width, use min Width
|
|
|
|
|
* - if calculated width is larger than max width, use max width
|
|
|
|
|
* - by default use min width
|
|
|
|
|
*
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
const calculateCanvasWidth = () => {
|
|
|
|
|
const { maxWidth, minWidth } = layoutWidthRange;
|
|
|
|
|
let calculatedWidth = screenWidth - scrollbarWidth();
|
|
|
|
|
|
2022-12-02 05:49:51 +00:00
|
|
|
// if preview mode is not on and the app setting pane is not opened, we need to subtract the width of the property pane
|
|
|
|
|
if (
|
|
|
|
|
isPreviewMode === false &&
|
|
|
|
|
!isAppSettingsPaneOpen &&
|
|
|
|
|
appMode === APP_MODE.EDIT
|
|
|
|
|
) {
|
2021-11-23 08:01:46 +00:00
|
|
|
calculatedWidth -= propertyPaneWidth;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 05:49:51 +00:00
|
|
|
// if app setting pane is open, we need to subtract the width of app setting page width
|
|
|
|
|
if (isAppSettingsPaneOpen === true && appMode === APP_MODE.EDIT) {
|
|
|
|
|
calculatedWidth -= APP_SETTINGS_PANE_WIDTH;
|
|
|
|
|
}
|
2021-11-23 08:01:46 +00:00
|
|
|
|
2022-12-02 05:49:51 +00:00
|
|
|
// if explorer is closed or its preview mode, we don't need to subtract the EE width
|
|
|
|
|
if (
|
|
|
|
|
isExplorerPinned === true &&
|
|
|
|
|
!isPreviewMode &&
|
|
|
|
|
appMode === APP_MODE.EDIT
|
|
|
|
|
) {
|
2021-11-23 08:01:46 +00:00
|
|
|
calculatedWidth -= explorerWidth;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 05:24:41 +00:00
|
|
|
if (isMultiPane) {
|
|
|
|
|
calculatedWidth = screenWidth - scrollbarWidth() - tabsPaneWidth - 100;
|
|
|
|
|
if (paneCount === 3) calculatedWidth -= propertyPaneWidth;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:41:58 +00:00
|
|
|
/**
|
|
|
|
|
* If there is
|
|
|
|
|
* 1. a sidebar for navigation,
|
|
|
|
|
* 2. it is pinned,
|
|
|
|
|
* 3. and device is not mobile
|
|
|
|
|
* we need to subtract the sidebar width as well in the following modes -
|
|
|
|
|
* 1. Preview
|
|
|
|
|
* 2. App settings open with navigation tab
|
|
|
|
|
* 3. Published
|
|
|
|
|
*/
|
|
|
|
|
if (
|
|
|
|
|
(appMode === APP_MODE.PUBLISHED ||
|
|
|
|
|
isPreviewMode ||
|
|
|
|
|
isAppSettingsPaneWithNavigationTabOpen) &&
|
|
|
|
|
!isMobile &&
|
|
|
|
|
sidebarWidth
|
|
|
|
|
) {
|
|
|
|
|
calculatedWidth -= sidebarWidth;
|
|
|
|
|
}
|
2023-03-04 07:25:54 +00:00
|
|
|
// const ele: any = document.getElementById("canvas-viewport");
|
|
|
|
|
// if (
|
|
|
|
|
// appMode === "EDIT" &&
|
|
|
|
|
// appLayout?.type === "FLUID" &&
|
|
|
|
|
// ele &&
|
|
|
|
|
// calculatedWidth > ele.clientWidth
|
|
|
|
|
// ) {
|
|
|
|
|
// calculatedWidth = ele.clientWidth;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (appPositioningType === AppPositioningTypes.AUTO && isPreviewMode) {
|
|
|
|
|
// calculatedWidth -= AUTOLAYOUT_RESIZER_WIDTH_BUFFER;
|
|
|
|
|
// }
|
|
|
|
|
|
2021-11-23 08:01:46 +00:00
|
|
|
switch (true) {
|
|
|
|
|
case maxWidth < 0:
|
|
|
|
|
case appLayout?.type === "FLUID":
|
|
|
|
|
case calculatedWidth < maxWidth && calculatedWidth > minWidth:
|
2022-05-04 09:45:57 +00:00
|
|
|
const totalWidthToSubtract = BORDERS_WIDTH + GUTTER_WIDTH;
|
|
|
|
|
// NOTE: gutter + border width will be only substracted when theme mode and preview mode are off
|
2021-11-23 08:01:46 +00:00
|
|
|
return (
|
|
|
|
|
calculatedWidth -
|
2023-03-23 11:41:58 +00:00
|
|
|
(appMode === APP_MODE.EDIT &&
|
|
|
|
|
!isPreviewMode &&
|
|
|
|
|
!isAppSettingsPaneWithNavigationTabOpen
|
2022-05-04 09:45:57 +00:00
|
|
|
? totalWidthToSubtract
|
2021-11-23 08:01:46 +00:00
|
|
|
: 0)
|
|
|
|
|
);
|
|
|
|
|
case calculatedWidth < minWidth:
|
|
|
|
|
return minWidth;
|
|
|
|
|
case calculatedWidth > maxWidth:
|
|
|
|
|
return maxWidth;
|
|
|
|
|
default:
|
|
|
|
|
return minWidth;
|
2021-03-03 05:26:47 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-23 08:01:46 +00:00
|
|
|
/**
|
|
|
|
|
* resizes the layout based on the layout type
|
|
|
|
|
*
|
|
|
|
|
* @param screenWidth
|
|
|
|
|
* @param appLayout
|
|
|
|
|
*/
|
|
|
|
|
const resizeToLayout = () => {
|
|
|
|
|
const calculatedWidth = calculateCanvasWidth();
|
2022-04-14 04:36:20 +00:00
|
|
|
const { width: rightColumn } = mainCanvasProps || {};
|
2023-01-09 05:24:41 +00:00
|
|
|
let scale = 1;
|
|
|
|
|
if (isMultiPane && appLayout?.type !== "FLUID") {
|
|
|
|
|
let canvasSpace =
|
|
|
|
|
screenWidth -
|
|
|
|
|
tabsPaneWidth -
|
|
|
|
|
SIDE_NAV_WIDTH -
|
|
|
|
|
GUTTER_WIDTH -
|
|
|
|
|
BORDERS_WIDTH;
|
|
|
|
|
if (paneCount === 3) canvasSpace -= propertyPaneWidth;
|
|
|
|
|
// Scale will always be between 0.5 to 1
|
|
|
|
|
scale = Math.max(
|
|
|
|
|
Math.min(+Math.abs(canvasSpace / calculatedWidth).toFixed(2), 1),
|
|
|
|
|
0.5,
|
|
|
|
|
);
|
|
|
|
|
dispatch(updateCanvasLayoutAction(calculatedWidth, scale));
|
|
|
|
|
} else if (rightColumn !== calculatedWidth || !isCanvasInitialized) {
|
|
|
|
|
dispatch(updateCanvasLayoutAction(calculatedWidth, scale));
|
2021-03-03 05:26:47 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const debouncedResize = useCallback(debounce(resizeToLayout, 250), [
|
2022-04-14 04:36:20 +00:00
|
|
|
mainCanvasProps,
|
2021-11-23 08:01:46 +00:00
|
|
|
screenWidth,
|
2023-01-09 05:24:41 +00:00
|
|
|
tabsPaneWidth,
|
|
|
|
|
paneCount,
|
2021-03-03 05:26:47 +00:00
|
|
|
]);
|
|
|
|
|
|
2023-03-04 07:25:54 +00:00
|
|
|
// const immediateDebouncedResize = useCallback(debounce(resizeToLayout), [
|
|
|
|
|
// mainCanvasProps,
|
|
|
|
|
// screenWidth,
|
|
|
|
|
// currentPageId,
|
|
|
|
|
// appMode,
|
|
|
|
|
// appLayout,
|
|
|
|
|
// isPreviewMode,
|
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
|
|
// const resizeObserver = new ResizeObserver(immediateDebouncedResize);
|
|
|
|
|
// useEffect(() => {
|
|
|
|
|
// const ele: any = document.getElementById("canvas-viewport");
|
|
|
|
|
// if (ele) {
|
|
|
|
|
// if (appLayout?.type === "FLUID") {
|
|
|
|
|
// resizeObserver.observe(ele);
|
|
|
|
|
// } else {
|
|
|
|
|
// resizeObserver.unobserve(ele);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return () => {
|
|
|
|
|
// ele && resizeObserver.unobserve(ele);
|
|
|
|
|
// };
|
|
|
|
|
// }, [appLayout, currentPageId, isPreviewMode]);
|
|
|
|
|
|
2021-11-23 08:01:46 +00:00
|
|
|
/**
|
|
|
|
|
* when screen height is changed, update canvas layout
|
|
|
|
|
*/
|
2022-11-23 09:48:23 +00:00
|
|
|
// useEffect(() => {
|
|
|
|
|
// if (calculatedMinHeight !== mainCanvasProps?.height) {
|
|
|
|
|
// // dispatch(updateCanvasLayoutAction(mainCanvasProps?.width));
|
|
|
|
|
// }
|
|
|
|
|
// }, [screenHeight, mainCanvasProps?.height]);
|
2021-03-16 05:01:37 +00:00
|
|
|
|
2021-03-03 05:26:47 +00:00
|
|
|
useEffect(() => {
|
2022-05-26 04:52:55 +00:00
|
|
|
if (isCanvasInitialized) debouncedResize();
|
2023-01-09 05:24:41 +00:00
|
|
|
}, [screenWidth, tabsPaneWidth, paneCount]);
|
2021-03-03 05:26:47 +00:00
|
|
|
|
2021-11-23 08:01:46 +00:00
|
|
|
/**
|
|
|
|
|
* resize the layout if any of the following thing changes:
|
|
|
|
|
* - app layout
|
|
|
|
|
* - page
|
|
|
|
|
* - container right column
|
|
|
|
|
* - preview mode
|
|
|
|
|
* - explorer width
|
|
|
|
|
* - explorer is pinned
|
2022-05-04 09:45:57 +00:00
|
|
|
* - theme mode is turned on
|
2023-03-23 11:41:58 +00:00
|
|
|
* - sidebar pin/unpin
|
|
|
|
|
* - app settings pane open with navigation tab
|
|
|
|
|
* - any of the following navigation settings changes
|
|
|
|
|
* - orientation
|
|
|
|
|
* - nav style
|
|
|
|
|
* - device changes to/from mobile
|
2021-11-23 08:01:46 +00:00
|
|
|
*/
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
resizeToLayout();
|
|
|
|
|
}, [
|
|
|
|
|
appLayout,
|
2022-04-14 04:36:20 +00:00
|
|
|
mainCanvasProps?.width,
|
2021-11-23 08:01:46 +00:00
|
|
|
isPreviewMode,
|
2023-03-23 11:41:58 +00:00
|
|
|
isAppSettingsPaneWithNavigationTabOpen,
|
2021-11-23 08:01:46 +00:00
|
|
|
explorerWidth,
|
2022-12-02 05:49:51 +00:00
|
|
|
propertyPaneWidth,
|
2021-11-23 08:01:46 +00:00
|
|
|
isExplorerPinned,
|
2022-12-08 07:21:58 +00:00
|
|
|
propertyPaneWidth,
|
2022-12-02 05:49:51 +00:00
|
|
|
isAppSettingsPaneOpen,
|
2023-03-23 11:41:58 +00:00
|
|
|
isAppSidebarPinned,
|
|
|
|
|
currentApplicationDetails?.applicationDetail?.navigationSetting
|
|
|
|
|
?.orientation,
|
|
|
|
|
currentApplicationDetails?.applicationDetail?.navigationSetting?.navStyle,
|
|
|
|
|
isMobile,
|
2023-03-04 07:25:54 +00:00
|
|
|
currentPageId, //TODO: preet - remove this after first merge.
|
2021-11-23 08:01:46 +00:00
|
|
|
]);
|
|
|
|
|
|
2023-03-04 07:25:54 +00:00
|
|
|
// useEffect(() => {
|
|
|
|
|
// dispatch(
|
|
|
|
|
// updateLayoutForMobileBreakpointAction(
|
|
|
|
|
// MAIN_CONTAINER_WIDGET_ID,
|
|
|
|
|
// appPositioningType === AppPositioningTypes.AUTO
|
|
|
|
|
// ? mainCanvasProps?.isMobile
|
|
|
|
|
// : false,
|
|
|
|
|
// calculateCanvasWidth(),
|
|
|
|
|
// ),
|
|
|
|
|
// );
|
|
|
|
|
// }, [mainCanvasProps?.isMobile, appPositioningType]);
|
|
|
|
|
|
2022-05-26 04:52:55 +00:00
|
|
|
return isCanvasInitialized;
|
2021-03-03 05:26:47 +00:00
|
|
|
};
|