2023-03-04 07:25:54 +00:00
|
|
|
import log from "loglevel";
|
2024-05-23 09:32:34 +00:00
|
|
|
import React, { useCallback } from "react";
|
2022-05-04 09:45:57 +00:00
|
|
|
import styled from "styled-components";
|
2023-07-26 12:40:44 +00:00
|
|
|
import * as Sentry from "@sentry/react";
|
2024-05-23 09:32:34 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2023-09-06 12:15:04 +00:00
|
|
|
import type { CanvasWidgetStructure } from "WidgetProvider/constants";
|
2022-12-30 14:52:11 +00:00
|
|
|
import useWidgetFocus from "utils/hooks/useWidgetFocus";
|
2023-11-03 17:13:36 +00:00
|
|
|
import { combinedPreviewModeSelector } from "selectors/editorSelectors";
|
2023-07-26 12:40:44 +00:00
|
|
|
import { getSelectedAppTheme } from "selectors/appThemingSelectors";
|
2023-09-11 15:55:11 +00:00
|
|
|
import { getViewportClassName } from "layoutSystems/autolayout/utils/AutoLayoutUtils";
|
2023-07-26 12:40:44 +00:00
|
|
|
import {
|
|
|
|
|
ThemeProvider as WDSThemeProvider,
|
|
|
|
|
useTheme,
|
2024-08-16 05:49:42 +00:00
|
|
|
} from "@appsmith/wds-theming";
|
2023-03-23 11:41:58 +00:00
|
|
|
import { getIsAppSettingsPaneWithNavigationTabOpen } from "selectors/appSettingsPaneSelectors";
|
2023-10-06 10:07:43 +00:00
|
|
|
import { CANVAS_ART_BOARD } from "constants/componentClassNameConstants";
|
2023-10-04 11:53:29 +00:00
|
|
|
import { renderAppsmithCanvas } from "layoutSystems/CanvasFactory";
|
|
|
|
|
import type { WidgetProps } from "widgets/BaseWidget";
|
2024-08-06 14:52:22 +00:00
|
|
|
import { getAppThemeSettings } from "ee/selectors/applicationSelectors";
|
2024-03-18 15:44:11 +00:00
|
|
|
import CodeModeTooltip from "pages/Editor/WidgetsEditor/components/CodeModeTooltip";
|
2024-04-12 17:24:04 +00:00
|
|
|
import { getIsAnvilLayout } from "layoutSystems/anvil/integrations/selectors";
|
2024-05-23 09:32:34 +00:00
|
|
|
import { focusWidget } from "actions/widgetActions";
|
2019-03-21 17:42:23 +00:00
|
|
|
|
2019-08-26 12:41:21 +00:00
|
|
|
interface CanvasProps {
|
2022-08-19 10:10:36 +00:00
|
|
|
widgetsStructure: CanvasWidgetStructure;
|
|
|
|
|
canvasWidth: number;
|
2023-09-12 14:14:02 +00:00
|
|
|
enableMainCanvasResizer?: boolean;
|
2019-08-29 11:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-06 07:26:47 +00:00
|
|
|
const StyledWDSThemeProvider = styled(WDSThemeProvider)`
|
|
|
|
|
min-height: 100%;
|
|
|
|
|
display: flex;
|
|
|
|
|
`;
|
|
|
|
|
|
2023-09-12 14:14:02 +00:00
|
|
|
const Wrapper = styled.section<{
|
2022-05-04 09:45:57 +00:00
|
|
|
background: string;
|
2023-02-03 05:47:40 +00:00
|
|
|
width: number;
|
2023-09-12 14:14:02 +00:00
|
|
|
$enableMainCanvasResizer: boolean;
|
2022-05-04 09:45:57 +00:00
|
|
|
}>`
|
2024-02-06 07:26:47 +00:00
|
|
|
flex: 1;
|
2022-05-04 09:45:57 +00:00
|
|
|
background: ${({ background }) => background};
|
2023-09-12 14:14:02 +00:00
|
|
|
width: ${({ $enableMainCanvasResizer, width }) =>
|
|
|
|
|
$enableMainCanvasResizer ? `100%` : `${width}px`};
|
2022-05-04 09:45:57 +00:00
|
|
|
`;
|
2023-02-03 05:47:40 +00:00
|
|
|
const Canvas = (props: CanvasProps) => {
|
2023-02-17 08:42:54 +00:00
|
|
|
const { canvasWidth } = props;
|
2023-11-03 17:13:36 +00:00
|
|
|
const isPreviewMode = useSelector(combinedPreviewModeSelector);
|
2023-03-23 11:41:58 +00:00
|
|
|
const isAppSettingsPaneWithNavigationTabOpen = useSelector(
|
|
|
|
|
getIsAppSettingsPaneWithNavigationTabOpen,
|
|
|
|
|
);
|
2022-05-04 09:45:57 +00:00
|
|
|
const selectedTheme = useSelector(getSelectedAppTheme);
|
2024-04-12 17:24:04 +00:00
|
|
|
const isAnvilLayout = useSelector(getIsAnvilLayout);
|
2023-09-15 17:00:10 +00:00
|
|
|
|
2023-12-01 12:39:21 +00:00
|
|
|
const themeSetting = useSelector(getAppThemeSettings);
|
|
|
|
|
const wdsThemeProps = {
|
|
|
|
|
borderRadius: themeSetting.borderRadius,
|
|
|
|
|
seedColor: themeSetting.accentColor,
|
2023-12-28 08:28:33 +00:00
|
|
|
colorMode: themeSetting.colorMode.toLowerCase(),
|
2023-12-01 12:39:21 +00:00
|
|
|
userSizing: themeSetting.sizing,
|
|
|
|
|
userDensity: themeSetting.density,
|
2024-03-05 11:53:18 +00:00
|
|
|
} as Parameters<typeof useTheme>[0];
|
|
|
|
|
// in case of non-WDS theme, we will pass an empty object to useTheme hook
|
|
|
|
|
// so that fixedLayout theme does not break because of calculations done in useTheme
|
2024-04-12 17:24:04 +00:00
|
|
|
const { theme } = useTheme(isAnvilLayout ? wdsThemeProps : {});
|
2022-05-04 09:45:57 +00:00
|
|
|
|
2024-05-23 09:32:34 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const unfocusAllWidgets = useCallback(() => {
|
|
|
|
|
dispatch(focusWidget());
|
|
|
|
|
}, [dispatch]);
|
|
|
|
|
|
2022-05-04 09:45:57 +00:00
|
|
|
/**
|
|
|
|
|
* background for canvas
|
|
|
|
|
*/
|
2023-12-18 11:16:00 +00:00
|
|
|
let backgroundForCanvas: string;
|
2022-05-04 09:45:57 +00:00
|
|
|
|
2023-03-23 11:41:58 +00:00
|
|
|
if (isPreviewMode || isAppSettingsPaneWithNavigationTabOpen) {
|
2023-12-18 11:16:00 +00:00
|
|
|
backgroundForCanvas = "initial";
|
2022-05-04 09:45:57 +00:00
|
|
|
} else {
|
2023-12-18 11:16:00 +00:00
|
|
|
backgroundForCanvas = selectedTheme.properties.colors.backgroundColor;
|
2022-05-04 09:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-30 14:52:11 +00:00
|
|
|
const focusRef = useWidgetFocus();
|
|
|
|
|
|
2023-09-12 14:14:02 +00:00
|
|
|
const marginHorizontalClass = props.enableMainCanvasResizer
|
|
|
|
|
? `mx-0`
|
|
|
|
|
: `mx-auto`;
|
|
|
|
|
const paddingBottomClass = props.enableMainCanvasResizer ? "" : "pb-52";
|
2023-12-18 11:16:00 +00:00
|
|
|
|
|
|
|
|
const renderChildren = () => {
|
2019-11-26 10:45:46 +00:00
|
|
|
return (
|
2024-03-13 06:23:49 +00:00
|
|
|
<CodeModeTooltip>
|
|
|
|
|
<Wrapper
|
|
|
|
|
$enableMainCanvasResizer={!!props.enableMainCanvasResizer}
|
2024-04-12 17:24:04 +00:00
|
|
|
background={isAnvilLayout ? "" : backgroundForCanvas}
|
2024-09-09 10:55:50 +00:00
|
|
|
className={`relative t--canvas-artboard ${paddingBottomClass} ${marginHorizontalClass} ${getViewportClassName(
|
2024-03-13 06:23:49 +00:00
|
|
|
canvasWidth,
|
|
|
|
|
)}`}
|
|
|
|
|
data-testid={"t--canvas-artboard"}
|
|
|
|
|
id={CANVAS_ART_BOARD}
|
2024-05-23 09:32:34 +00:00
|
|
|
onMouseLeave={unfocusAllWidgets}
|
2024-04-12 17:24:04 +00:00
|
|
|
ref={isAnvilLayout ? undefined : focusRef}
|
2024-03-13 06:23:49 +00:00
|
|
|
width={canvasWidth}
|
|
|
|
|
>
|
|
|
|
|
{props.widgetsStructure.widgetId &&
|
|
|
|
|
renderAppsmithCanvas(props.widgetsStructure as WidgetProps)}
|
|
|
|
|
</Wrapper>
|
|
|
|
|
</CodeModeTooltip>
|
2019-11-26 10:45:46 +00:00
|
|
|
);
|
2023-12-18 11:16:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
2024-04-12 17:24:04 +00:00
|
|
|
if (isAnvilLayout) {
|
2023-12-18 11:16:00 +00:00
|
|
|
return (
|
2024-02-06 07:26:47 +00:00
|
|
|
<StyledWDSThemeProvider theme={theme}>
|
|
|
|
|
{renderChildren()}
|
|
|
|
|
</StyledWDSThemeProvider>
|
2023-12-18 11:16:00 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return renderChildren();
|
2019-11-26 10:45:46 +00:00
|
|
|
} catch (error) {
|
2021-07-05 05:49:43 +00:00
|
|
|
log.error("Error rendering DSL", error);
|
|
|
|
|
Sentry.captureException(error);
|
2024-09-18 16:35:28 +00:00
|
|
|
|
2019-11-26 10:45:46 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2023-02-03 05:47:40 +00:00
|
|
|
};
|
2019-02-10 13:06:05 +00:00
|
|
|
|
2019-09-09 10:30:22 +00:00
|
|
|
export default Canvas;
|