PromucFlow_constructor/app/client/src/widgets/RichTextEditorWidget.tsx

189 lines
5.3 KiB
TypeScript
Raw Normal View History

import React, { lazy, Suspense } from "react";
2020-03-20 11:17:30 +00:00
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
import { WidgetType } from "constants/WidgetConstants";
import { EventType } from "constants/ActionConstants";
import { WidgetPropertyValidationType } from "utils/WidgetValidation";
2020-03-20 11:17:30 +00:00
import { VALIDATION_TYPES } from "constants/WidgetValidation";
import {
TriggerPropertiesMap,
DerivedPropertiesMap,
} from "utils/WidgetFactory";
2020-04-16 08:32:58 +00:00
import Skeleton from "components/utils/Skeleton";
import * as Sentry from "@sentry/react";
import { retryPromise } from "utils/AppsmithUtils";
2020-10-06 09:01:51 +00:00
import withMeta, { WithMeta } from "./MetaHOC";
2020-03-20 11:17:30 +00:00
const RichTextEditorComponent = lazy(() =>
retryPromise(() =>
import(
/* webpackChunkName: "rte",webpackPrefetch: 2 */ "components/designSystems/appsmith/RichTextEditorComponent"
),
2020-04-17 04:59:43 +00:00
),
);
2020-03-20 11:17:30 +00:00
class RichTextEditorWidget extends BaseWidget<
RichTextEditorWidgetProps,
WidgetState
> {
static getPropertyPaneConfig() {
return [
{
sectionName: "General",
children: [
{
propertyName: "inputType",
helpText:
"Sets the input type of the default text property in widget.",
label: "Input Type",
controlType: "DROP_DOWN",
options: [
{
label: "Text",
value: "text",
},
{
label: "HTML",
value: "html",
},
],
isBindProperty: false,
isTriggerProperty: false,
},
{
propertyName: "defaultText",
helpText:
"Sets the default text of the widget. The text is updated if the default text changes",
label: "Default text",
controlType: "INPUT_TEXT",
placeholderText: "Enter HTML",
isBindProperty: true,
isTriggerProperty: false,
},
{
propertyName: "isVisible",
label: "Visible",
helpText: "Controls the visibility of the widget",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
},
{
propertyName: "isDisabled",
label: "Disable",
helpText: "Disables input to this widget",
controlType: "SWITCH",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: false,
},
],
},
{
sectionName: "Actions",
children: [
{
helpText: "Triggers an action when the text is changed",
propertyName: "onTextChange",
label: "onTextChange",
controlType: "ACTION_SELECTOR",
isJSConvertible: true,
isBindProperty: true,
isTriggerProperty: true,
},
],
},
];
}
2020-03-20 11:17:30 +00:00
static getPropertyValidationMap(): WidgetPropertyValidationType {
return {
text: VALIDATION_TYPES.TEXT,
2020-03-20 11:17:30 +00:00
placeholder: VALIDATION_TYPES.TEXT,
defaultValue: VALIDATION_TYPES.TEXT,
isDisabled: VALIDATION_TYPES.BOOLEAN,
isVisible: VALIDATION_TYPES.BOOLEAN,
// onTextChange: VALIDATION_TYPES.ACTION_SELECTOR,
2020-03-20 11:17:30 +00:00
};
}
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onTextChange: true,
};
}
2020-04-17 16:15:09 +00:00
static getMetaPropertiesMap(): Record<string, any> {
return {
text: undefined,
2020-04-17 16:15:09 +00:00
};
}
static getDefaultPropertiesMap(): Record<string, string> {
return {
text: "defaultText",
2020-04-17 16:15:09 +00:00
};
}
static getDerivedPropertiesMap(): DerivedPropertiesMap {
return {
value: `{{this.text}}`,
};
}
2020-04-15 11:42:11 +00:00
onValueChange = (text: string) => {
2021-02-19 05:48:16 +00:00
if (this.props.inputType === "text") {
text = text.replace(/\&lt\;/g, "<");
text = text.replace(/\&gt\;>/g, ">");
text = text.replace(/\<br\/\>/g, "\n");
}
2020-10-06 16:47:16 +00:00
this.props.updateWidgetMetaProperty("text", text, {
dynamicString: this.props.onTextChange,
event: {
type: EventType.ON_TEXT_CHANGE,
},
});
2020-03-20 11:17:30 +00:00
};
getPageView() {
let defaultValue = this.props.text || "";
if (this.props.inputType === "text") {
2021-02-19 05:48:16 +00:00
defaultValue = defaultValue.replace(/\</g, "&lt;");
defaultValue = defaultValue.replace(/\>/g, "&gt;");
defaultValue = defaultValue.replace(/\n/g, "<br/>");
}
2020-03-20 11:17:30 +00:00
return (
2020-04-16 08:32:58 +00:00
<Suspense fallback={<Skeleton />}>
<RichTextEditorComponent
onValueChange={this.onValueChange}
defaultValue={defaultValue}
widgetId={this.props.widgetId}
placeholder={this.props.placeholder}
key={this.props.widgetId}
isDisabled={this.props.isDisabled}
2021-02-19 05:48:16 +00:00
formatType={this.props.inputType}
isVisible={this.props.isVisible}
/>
</Suspense>
2020-03-20 11:17:30 +00:00
);
}
getWidgetType(): WidgetType {
return "RICH_TEXT_EDITOR_WIDGET";
}
}
2020-10-06 09:01:51 +00:00
export interface RichTextEditorWidgetProps extends WidgetProps, WithMeta {
2020-04-15 11:42:11 +00:00
defaultText?: string;
text: string;
inputType: string;
2020-03-20 11:17:30 +00:00
placeholder?: string;
onTextChange?: string;
isDisabled?: boolean;
isVisible?: boolean;
}
export default RichTextEditorWidget;
export const ProfiledRichTextEditorWidget = Sentry.withProfiler(
2020-10-06 09:01:51 +00:00
withMeta(RichTextEditorWidget),
);