PromucFlow_constructor/app/client/src/layoutSystems/common/utils/commonUtils.ts
Preet Sidhu 69f4a412bf
chore: add highlight calculation logic for layouts. (#27980)
## Description

1. Add LayoutComponent functionality.
2. Create Basic LayoutComponents.
3. Create LayoutPresets needed for Container-like widgets.
4. Add highlight calculation logic for all basic Layout Components.
5. Create dragging sagas for Anvil.
6. Create DraggingArena associated functionality to handle DnD in Anvil.

#### PR fixes following issue(s)
Fixes #27004 


#### Type of change
> Please delete options that are not relevant.
- New feature (non-breaking change which adds functionality)

## Testing

#### How Has This Been Tested?
- [ ] Manual
- [ ] JUnit
- [x] Jest
- [ ] Cypress


## Checklist:
#### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed

---------

Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com>
Co-authored-by: Aswath K <aswath.sana@gmail.com>
Co-authored-by: rahulramesha <rahul@appsmith.com>
Co-authored-by: rahulramesha <71900764+rahulramesha@users.noreply.github.com>
2023-10-19 16:27:40 -04:00

132 lines
4.1 KiB
TypeScript

import type {
AutoDimensionOptions,
AutoLayoutConfig,
WidgetSizeConfig,
} from "WidgetProvider/constants";
import WidgetFactory from "WidgetProvider/factory";
import { isFunction } from "lodash";
import type { BaseWidgetProps } from "widgets/BaseWidgetHOC/withBaseWidgetHOC";
export const getAutoDimensionsConfig = (
config: AutoLayoutConfig,
props: BaseWidgetProps,
): AutoDimensionOptions | undefined => {
let autoDimensionConfig = config.autoDimension;
if (isFunction(autoDimensionConfig)) {
autoDimensionConfig = autoDimensionConfig(props);
}
if (props.isListItemContainer && autoDimensionConfig) {
autoDimensionConfig.height = false;
}
return autoDimensionConfig;
};
export const getAutoLayoutWidgetConfig = (
props: BaseWidgetProps,
): AutoLayoutConfig => {
return WidgetFactory.getWidgetAutoLayoutConfig(props.type);
};
// TODO: update sizeConfig structure and get rid of this method.
export const restructureWidgetSizeConfig = (
sizeConfig: Array<WidgetSizeConfig> | undefined,
props: BaseWidgetProps,
): {
maxHeight: Record<string, string>;
maxWidth: Record<string, string>;
minHeight: Record<string, string>;
minWidth: Record<string, string>;
} => {
/**
* Size config is stored as an array of objects.
* Each object has a viewportMinWidth and a configuration function that returns the minMax sizes at the viewport.
* e.g [{ viewportMinWidth: 0, configuration: (props) => ({ maxHeight: 400, minWidth: 100 })}]
*
* WDS flex component requires the same information in a different structure (Responsive<T>):
* minWidth: { base: '100px', '480px': '200px' }, // default min width is 100px. However, above container width of 480px, min width changes to 200px.
* maxHeight: { base: 400 },
*/
// TODO: We should look into how size config is stored. Both structure and values can be updated.
const res: {
maxHeight: Record<string, string>;
maxWidth: Record<string, string>;
minHeight: Record<string, string>;
minWidth: Record<string, string>;
} = {
maxHeight: {},
maxWidth: {},
minHeight: {},
minWidth: {},
};
if (!sizeConfig || !sizeConfig.length) return res;
return sizeConfig.reduce(
(acc: any, size: WidgetSizeConfig) => {
const data = size.configuration(props);
if (size.viewportMinWidth === 0) {
// WDS flex component doesn't handle null || undefined values. Hence, add in properties only if they are defined.
const res = {
maxHeight: {},
maxWidth: {},
minHeight: {},
minWidth: {},
};
if (data?.maxHeight)
res.maxHeight = { base: addPixelToSize(data.maxHeight) };
if (data?.maxWidth)
res.maxWidth = { base: addPixelToSize(data.maxWidth) };
if (data?.minHeight)
res.minHeight = { base: addPixelToSize(data.minHeight) };
if (data?.minWidth)
res.minWidth = { base: addPixelToSize(data.minWidth) };
return res;
}
return {
maxHeight: data?.maxHeight
? {
...acc.maxHeight,
[addPixelToSize(size.viewportMinWidth)]: addPixelToSize(
data?.maxHeight,
),
}
: acc,
maxWidth: data?.maxWidth
? {
...acc.maxWidth,
[addPixelToSize(size.viewportMinWidth)]: addPixelToSize(
data?.maxWidth,
),
}
: acc,
minHeight: data?.minHeight
? {
...acc.minHeight,
[addPixelToSize(size.viewportMinWidth)]: addPixelToSize(
data?.minHeight,
),
}
: acc,
minWidth: data?.minWidth
? {
...acc.minWidth,
[addPixelToSize(size.viewportMinWidth)]: addPixelToSize(
data?.minWidth,
),
}
: acc,
};
},
{
maxHeight: {},
maxWidth: {},
minHeight: {},
minWidth: {},
},
);
};
export const addPixelToSize = (size: number | string): string => {
if (!size) return "";
return typeof size === "string" ? size : `${size}px`;
};