Merge pull request #395 from mirumee/fix/rich-text-error

Handle rich text editor content error
This commit is contained in:
Dominik Żegleń 2020-03-03 11:33:59 +01:00 committed by GitHub
commit 4c8cce4f23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 213 additions and 192 deletions

View file

@ -36,6 +36,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Remove PO files from repo and update translations #409 by @dominik-zeglen - Remove PO files from repo and update translations #409 by @dominik-zeglen
- Add optional chaining and explicitely return "Not found" page - #408 by @dominik-zeglen - Add optional chaining and explicitely return "Not found" page - #408 by @dominik-zeglen
- Do not store errors in form component - #410 by @dominik-zeglen - Do not store errors in form component - #410 by @dominik-zeglen
- Handle rich text editor content error - #395 by @dominik-zeglen
## 2.0.0 ## 2.0.0

View file

@ -1267,6 +1267,10 @@
"context": "button", "context": "button",
"string": "Add or Edit Link" "string": "Add or Edit Link"
}, },
"src_dot_components_dot_RichTextEditor_dot_286109898": {
"context": "rich text error",
"string": "Invalid content"
},
"src_dot_components_dot_RichTextEditor_dot_2925475978": { "src_dot_components_dot_RichTextEditor_dot_2925475978": {
"string": "URL Linked" "string": "URL Linked"
}, },

View file

@ -1,6 +1,7 @@
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
import { fade } from "@material-ui/core/styles/colorManipulator"; import { fade } from "@material-ui/core/styles/colorManipulator";
import Typography from "@material-ui/core/Typography"; import Typography from "@material-ui/core/Typography";
import { FormattedMessage } from "react-intl";
import classNames from "classnames"; import classNames from "classnames";
import { RawDraftContentState } from "draft-js"; import { RawDraftContentState } from "draft-js";
import { import {
@ -13,6 +14,8 @@ import isEqual from "lodash-es/isEqual";
import React from "react"; import React from "react";
import { ChangeEvent } from "@saleor/hooks/useForm"; import { ChangeEvent } from "@saleor/hooks/useForm";
import ErrorBoundary from "react-error-boundary";
import { CreateCSSProperties } from "@material-ui/styles/withStyles";
import BoldIcon from "../../icons/BoldIcon"; import BoldIcon from "../../icons/BoldIcon";
import HeaderTwo from "../../icons/HeaderTwo"; import HeaderTwo from "../../icons/HeaderTwo";
import HeaderThree from "../../icons/HeaderThree"; import HeaderThree from "../../icons/HeaderThree";
@ -38,7 +41,17 @@ export interface RichTextEditorProps {
} }
const useStyles = makeStyles( const useStyles = makeStyles(
theme => ({ theme => {
const editorContainer: CreateCSSProperties = {
border: `1px ${theme.palette.divider} solid`,
borderRadius: 4,
padding: "27px 12px 10px",
position: "relative",
transition: theme.transitions.duration.shortest + "ms"
};
return {
editorContainer,
error: { error: {
color: theme.palette.error.main color: theme.palette.error.main
}, },
@ -93,11 +106,7 @@ const useStyles = makeStyles(
"&:hover": { "&:hover": {
borderColor: theme.palette.primary.main borderColor: theme.palette.primary.main
}, },
border: `1px ${theme.palette.divider} solid`, ...editorContainer
borderRadius: 4,
padding: "27px 12px 10px",
position: "relative",
transition: theme.transitions.duration.shortest + "ms"
}, },
"&-Toolbar": { "&-Toolbar": {
"&Button": { "&Button": {
@ -189,7 +198,8 @@ const useStyles = makeStyles(
smallIcon: { smallIcon: {
marginLeft: 10 marginLeft: 10
} }
}), };
},
{ name: "RichTextEditor" } { name: "RichTextEditor" }
); );
@ -226,6 +236,18 @@ const RichTextEditor: React.FC<RichTextEditorProps> = props => {
<Typography className={classes.label} variant="caption" color="primary"> <Typography className={classes.label} variant="caption" color="primary">
{label} {label}
</Typography> </Typography>
<ErrorBoundary
FallbackComponent={() => (
<div className={classes.editorContainer}>
<Typography color="error">
<FormattedMessage
defaultMessage="Invalid content"
description="rich text error"
/>
</Typography>
</div>
)}
>
<DraftailEditor <DraftailEditor
key={JSON.stringify(initial)} key={JSON.stringify(initial)}
rawContentState={ rawContentState={
@ -269,15 +291,9 @@ const RichTextEditor: React.FC<RichTextEditorProps> = props => {
source: LinkSource, source: LinkSource,
type: ENTITY_TYPE.LINK type: ENTITY_TYPE.LINK
} }
// {
// attributes: ["href"],
// decorator: ImageEntity,
// icon: <ImageIcon />,
// source: ImageSource,
// type: ENTITY_TYPE.IMAGE
// }
]} ]}
/> />
</ErrorBoundary>
</div> </div>
{helperText && ( {helperText && (
<Typography <Typography