2021-11-23 08:01:46 +00:00
|
|
|
import classNames from "classnames";
|
2022-11-28 08:13:17 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2021-11-23 08:01:46 +00:00
|
|
|
import React, { useMemo, useCallback } from "react";
|
|
|
|
|
|
2021-03-03 05:26:47 +00:00
|
|
|
import {
|
|
|
|
|
getCurrentApplicationId,
|
|
|
|
|
getCurrentApplicationLayout,
|
|
|
|
|
} from "selectors/editorSelectors";
|
2021-11-23 08:01:46 +00:00
|
|
|
import { Colors } from "constants/Colors";
|
|
|
|
|
import {
|
|
|
|
|
AppLayoutConfig,
|
|
|
|
|
SupportedLayouts,
|
|
|
|
|
} from "reducers/entityReducers/pageListReducer";
|
2022-08-22 05:09:39 +00:00
|
|
|
import { TooltipComponent, Icon, IconName, IconSize } from "design-system";
|
2021-11-23 08:01:46 +00:00
|
|
|
import { updateApplicationLayout } from "actions/applicationActions";
|
2022-01-13 13:21:57 +00:00
|
|
|
|
2021-03-03 05:26:47 +00:00
|
|
|
interface AppsmithLayoutConfigOption {
|
2021-03-11 02:21:48 +00:00
|
|
|
name: string;
|
|
|
|
|
type: SupportedLayouts;
|
2021-03-03 05:26:47 +00:00
|
|
|
icon?: IconName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const AppsmithDefaultLayout: AppLayoutConfig = {
|
2021-11-23 08:01:46 +00:00
|
|
|
type: "FLUID",
|
2021-03-03 05:26:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const AppsmithLayouts: AppsmithLayoutConfigOption[] = [
|
2021-11-23 08:01:46 +00:00
|
|
|
{
|
|
|
|
|
name: "Fluid Width",
|
|
|
|
|
type: "FLUID",
|
|
|
|
|
icon: "fluid",
|
|
|
|
|
},
|
2021-03-03 05:26:47 +00:00
|
|
|
{
|
|
|
|
|
name: "Desktop",
|
2021-11-23 08:01:46 +00:00
|
|
|
type: "DESKTOP",
|
2021-03-03 05:26:47 +00:00
|
|
|
icon: "desktop",
|
|
|
|
|
},
|
2021-03-11 02:21:48 +00:00
|
|
|
{
|
2022-09-03 15:07:31 +00:00
|
|
|
name: "Tablet (Landscape)",
|
2021-03-11 02:21:48 +00:00
|
|
|
type: "TABLET_LARGE",
|
2022-09-03 15:07:31 +00:00
|
|
|
icon: "tabletLandscape",
|
2021-03-11 02:21:48 +00:00
|
|
|
},
|
2021-03-03 05:26:47 +00:00
|
|
|
{
|
2022-09-03 15:07:31 +00:00
|
|
|
name: "Tablet (Portrait)",
|
2021-03-11 02:21:48 +00:00
|
|
|
type: "TABLET",
|
2021-03-03 05:26:47 +00:00
|
|
|
icon: "tablet",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Mobile Device",
|
2021-03-11 02:21:48 +00:00
|
|
|
type: "MOBILE",
|
2021-03-03 05:26:47 +00:00
|
|
|
icon: "mobile",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
export function MainContainerLayoutControl() {
|
2021-11-23 08:01:46 +00:00
|
|
|
const dispatch = useDispatch();
|
2021-03-03 05:26:47 +00:00
|
|
|
const appId = useSelector(getCurrentApplicationId);
|
|
|
|
|
const appLayout = useSelector(getCurrentApplicationLayout);
|
2022-01-20 09:29:09 +00:00
|
|
|
|
2022-03-09 18:40:21 +00:00
|
|
|
const buttonRefs: Array<HTMLButtonElement | null> = [];
|
|
|
|
|
|
2021-11-23 08:01:46 +00:00
|
|
|
/**
|
2022-03-09 18:40:21 +00:00
|
|
|
* return selected layout index. if there is no app
|
2021-11-23 08:01:46 +00:00
|
|
|
* layout, use the default one ( fluid )
|
|
|
|
|
*/
|
2022-03-09 18:40:21 +00:00
|
|
|
const selectedIndex = useMemo(() => {
|
|
|
|
|
return AppsmithLayouts.findIndex(
|
2021-11-23 08:01:46 +00:00
|
|
|
(each) => each.type === (appLayout?.type || AppsmithDefaultLayout.type),
|
2021-03-03 05:26:47 +00:00
|
|
|
);
|
2021-11-23 08:01:46 +00:00
|
|
|
}, [appLayout]);
|
|
|
|
|
|
2022-03-09 18:40:21 +00:00
|
|
|
const [focusedIndex, setFocusedIndex] = React.useState(selectedIndex);
|
|
|
|
|
|
2021-11-23 08:01:46 +00:00
|
|
|
/**
|
|
|
|
|
* updates the app layout
|
|
|
|
|
*
|
|
|
|
|
* @param layoutConfig
|
|
|
|
|
*/
|
|
|
|
|
const updateAppLayout = useCallback(
|
|
|
|
|
(layoutConfig: AppLayoutConfig) => {
|
|
|
|
|
const { type } = layoutConfig;
|
|
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
|
updateApplicationLayout(appId || "", {
|
|
|
|
|
appLayout: {
|
|
|
|
|
type,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
[dispatch, appLayout],
|
|
|
|
|
);
|
|
|
|
|
|
2022-03-09 18:40:21 +00:00
|
|
|
const handleKeyDown = (event: React.KeyboardEvent, index: number) => {
|
|
|
|
|
if (!buttonRefs.length) return;
|
|
|
|
|
|
|
|
|
|
switch (event.key) {
|
|
|
|
|
case "ArrowRight":
|
|
|
|
|
case "Right":
|
|
|
|
|
const rightIndex = index === buttonRefs.length - 1 ? 0 : index + 1;
|
|
|
|
|
buttonRefs[rightIndex]?.focus();
|
|
|
|
|
setFocusedIndex(rightIndex);
|
|
|
|
|
break;
|
|
|
|
|
case "ArrowLeft":
|
|
|
|
|
case "Left":
|
|
|
|
|
const leftIndex = index === 0 ? buttonRefs.length - 1 : index - 1;
|
|
|
|
|
buttonRefs[leftIndex]?.focus();
|
|
|
|
|
setFocusedIndex(leftIndex);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-03 05:26:47 +00:00
|
|
|
return (
|
2022-05-04 09:45:57 +00:00
|
|
|
<div className="space-y-2 t--layout-control-wrapper">
|
2022-03-09 18:40:21 +00:00
|
|
|
<div
|
|
|
|
|
className="flex justify-around"
|
|
|
|
|
onBlur={() => setFocusedIndex(selectedIndex)}
|
|
|
|
|
>
|
2021-11-23 08:01:46 +00:00
|
|
|
{AppsmithLayouts.map((layoutOption: any, index: number) => {
|
|
|
|
|
return (
|
|
|
|
|
<TooltipComponent
|
|
|
|
|
className="flex-grow"
|
|
|
|
|
content={layoutOption.name}
|
|
|
|
|
key={layoutOption.name}
|
|
|
|
|
position={
|
|
|
|
|
index === AppsmithLayouts.length - 1 ? "bottom-right" : "bottom"
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<button
|
|
|
|
|
className={classNames({
|
2022-03-09 18:40:21 +00:00
|
|
|
"border-transparent border flex items-center justify-center p-2 flex-grow focus:bg-gray-200": true,
|
|
|
|
|
"bg-white border-gray-300": selectedIndex === index,
|
|
|
|
|
"bg-gray-100 hover:bg-gray-200": selectedIndex !== index,
|
2021-11-23 08:01:46 +00:00
|
|
|
})}
|
2022-03-09 18:40:21 +00:00
|
|
|
onClick={() => {
|
|
|
|
|
updateAppLayout(layoutOption);
|
|
|
|
|
setFocusedIndex(index);
|
|
|
|
|
}}
|
|
|
|
|
onKeyDown={(event) => handleKeyDown(event, index)}
|
|
|
|
|
ref={(input) => buttonRefs.push(input)}
|
|
|
|
|
tabIndex={index === focusedIndex ? 0 : -1}
|
2021-10-08 07:05:34 +00:00
|
|
|
>
|
|
|
|
|
<Icon
|
|
|
|
|
fillColor={Colors.BLACK}
|
2021-11-23 08:01:46 +00:00
|
|
|
name={layoutOption.icon}
|
|
|
|
|
size={layoutOption.iconSize || IconSize.MEDIUM}
|
2021-10-08 07:05:34 +00:00
|
|
|
/>
|
2021-11-23 08:01:46 +00:00
|
|
|
</button>
|
|
|
|
|
</TooltipComponent>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2021-03-03 05:26:47 +00:00
|
|
|
</div>
|
2021-11-23 08:01:46 +00:00
|
|
|
</div>
|
2021-03-03 05:26:47 +00:00
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|