PromucFlow_constructor/app/client/src/entities/AppTheming/utils.ts
balajisoundar 2608e3dbd3
chore: Move the widget config to widget class (#26073)
## Description
- Remove the config objects from widget and config maps from the widget
factory.
- Introduce methods in widget development API to dynamically fetch this
items.
- freeze the widget configuration.

#### PR fixes following issue(s)
Fixes https://github.com/appsmithorg/appsmith/issues/26008
> if no issue exists, please create an issue and ask the maintainers
about this first
>
>
#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
> Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
- This change requires a documentation update
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [x] Manual
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] 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
- [x] 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
- [x] 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
2023-09-06 17:45:04 +05:30

170 lines
5.9 KiB
TypeScript

import { get, has } from "lodash";
import {
combineDynamicBindings,
getDynamicBindings,
isDynamicValue,
THEME_BINDING_REGEX,
} from "utils/DynamicBindingUtils";
import WidgetFactory from "WidgetProvider/factory";
import { parseSchemaItem } from "widgets/WidgetUtils";
import { ROOT_SCHEMA_KEY } from "widgets/JSONFormWidget/constants";
import { getFieldStylesheet } from "widgets/JSONFormWidget/helper";
import type { UpdateWidgetPropertyPayload } from "actions/controlActions";
import type { CanvasWidgetsReduxState } from "reducers/entityReducers/canvasWidgetsReducer";
/**
* get properties to update for reset
*/
export const getPropertiesToUpdateForReset = (
canvasWidgets: CanvasWidgetsReduxState,
) => {
const propertiesToUpdate: UpdateWidgetPropertyPayload[] = [];
// ignoring these properites as these are objects itself
// these are used in json form, table and button group
// to style the children fields/components/widgets
const propertiesToIgnore = [
"childStylesheet",
"submitButtonStyles",
"resetButtonStyles",
];
// iterating over canvas widgets and their properties
// so that we can compare them with the value in stylesheet
// and if they are different, reset the value to the one stored
// in stylesheet
Object.keys(canvasWidgets).map((widgetId) => {
const widget = canvasWidgets[widgetId];
const stylesheetValue = WidgetFactory.getWidgetStylesheetConfigMap(
widget.type,
);
const modifications: any = {};
if (stylesheetValue) {
Object.keys(stylesheetValue)
.filter((propertyKey) => !propertiesToIgnore.includes(propertyKey))
.map((propertyKey) => {
if (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
THEME_BINDING_REGEX.test(stylesheetValue[propertyKey]) &&
stylesheetValue[propertyKey] !== widget[propertyKey]
) {
modifications[propertyKey] = stylesheetValue[propertyKey];
}
});
if (widget.type === "TABLE_WIDGET") {
Object.keys(widget.primaryColumns).map((primaryColumnKey) => {
const primaryColumn = widget.primaryColumns[primaryColumnKey];
const childStylesheetValue =
widget.childStylesheet[primaryColumn.columnType];
if (childStylesheetValue) {
Object.keys(childStylesheetValue).map((childPropertyKey) => {
const { jsSnippets, stringSegments } = getDynamicBindings(
childStylesheetValue[childPropertyKey],
);
const js = combineDynamicBindings(jsSnippets, stringSegments);
const computedValue = `{{${widget.widgetName}.sanitizedTableData.map((currentRow) => ( ${js}))}}`;
if (computedValue !== primaryColumn[childPropertyKey]) {
modifications[
`primaryColumns.${primaryColumnKey}.${childPropertyKey}`
] = computedValue;
}
});
}
});
}
if (widget.type === "BUTTON_GROUP_WIDGET") {
Object.keys(widget.groupButtons).map((groupButtonName: string) => {
const groupButton = widget.groupButtons[groupButtonName];
const childStylesheetValue = stylesheetValue?.childStylesheet?.button;
childStylesheetValue &&
Object.keys(childStylesheetValue).map((childPropertyKey) => {
if (
get(childStylesheetValue, childPropertyKey) !==
groupButton[childPropertyKey]
) {
modifications[
`groupButtons.${groupButtonName}.${childPropertyKey}`
] = get(childStylesheetValue, childPropertyKey);
}
});
});
}
if (widget.type === "JSON_FORM_WIDGET") {
if (has(widget, "schema")) {
parseSchemaItem(
widget.schema[ROOT_SCHEMA_KEY],
`schema.${ROOT_SCHEMA_KEY}`,
(schemaItem, propertyPath) => {
const fieldStylesheet = getFieldStylesheet(
widget.widgetName,
schemaItem.fieldType,
(WidgetFactory.getWidgetStylesheetConfigMap(widget.type) || {})
.childStylesheet as any,
);
Object.keys(fieldStylesheet).map((fieldPropertyKey) => {
const fieldStylesheetValue = fieldStylesheet[fieldPropertyKey];
if (
isDynamicValue(fieldStylesheetValue) &&
fieldStylesheetValue !== get(schemaItem, fieldPropertyKey)
) {
modifications[`${[propertyPath]}.${fieldPropertyKey}`] =
fieldStylesheetValue;
}
});
},
);
}
// reset submit button
(["submitButtonStyles", "resetButtonStyles"] as const).map(
(buttonStyleKey) => {
Object.keys(get(stylesheetValue, buttonStyleKey, {})).map(
(propertyKey) => {
const buttonStylesheetValue = get(
stylesheetValue,
`${buttonStyleKey}.${propertyKey}`,
) as string;
if (
buttonStylesheetValue &&
typeof buttonStylesheetValue === "string" &&
THEME_BINDING_REGEX.test(buttonStylesheetValue) &&
buttonStylesheetValue !==
widget[buttonStyleKey][propertyKey] &&
buttonStylesheetValue !== widget[buttonStyleKey][propertyKey]
) {
modifications[`${buttonStyleKey}.${propertyKey}`] =
buttonStylesheetValue;
}
},
);
},
);
}
if (Object.keys(modifications).length > 0) {
propertiesToUpdate.push({
widgetId,
updates: {
modify: modifications,
},
});
}
}
});
return propertiesToUpdate;
};