saleor-dashboard/src/components/RichTextEditor/RichTextEditor.tsx

189 lines
4.9 KiB
TypeScript
Raw Normal View History

2020-10-28 15:26:30 +00:00
import EditorJS, { OutputData } from "@editorjs/editorjs";
import Header from "@editorjs/header";
import List from "@editorjs/list";
import Quote from "@editorjs/quote";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import { fade } from "@material-ui/core/styles/colorManipulator";
import Typography from "@material-ui/core/Typography";
2020-10-28 15:26:30 +00:00
import strikethroughIcon from "@saleor/icons/StrikethroughIcon";
2019-08-09 11:14:35 +00:00
import classNames from "classnames";
2020-10-28 15:30:44 +00:00
import createGenericInlineTool from "editorjs-inline-tool";
2019-08-09 10:26:22 +00:00
import React from "react";
2019-06-19 14:40:52 +00:00
2020-11-03 11:35:36 +00:00
export type RichTextEditorChange = (data: OutputData) => void;
2019-06-19 14:40:52 +00:00
export interface RichTextEditorProps {
2020-11-03 11:35:36 +00:00
data: OutputData;
2019-06-19 14:40:52 +00:00
disabled: boolean;
error: boolean;
helperText: string;
label: string;
name: string;
2020-11-03 11:35:36 +00:00
onChange: RichTextEditorChange;
2019-06-19 14:40:52 +00:00
}
2019-12-03 15:28:40 +00:00
const useStyles = makeStyles(
theme => {
2020-10-28 15:26:30 +00:00
const hover = {
"&:hover": {
background: fade(theme.palette.primary.main, 0.1)
}
};
return {
error: {
color: theme.palette.error.main
2019-06-19 14:40:52 +00:00
},
helperText: {
marginTop: theme.spacing(0.75)
},
label: {
2020-10-28 15:26:30 +00:00
color: theme.palette.text.secondary,
position: "absolute",
2020-10-29 12:14:05 +00:00
top: theme.spacing(1),
2020-10-28 15:26:30 +00:00
transition: theme.transitions.duration.short + "ms"
},
2020-10-28 15:26:30 +00:00
labelActive: {
color: theme.palette.primary.main
},
root: {
2020-10-28 15:26:30 +00:00
"& .cdx-quote__text": {
minHeight: 24
2019-12-03 15:28:40 +00:00
},
2020-10-29 12:14:05 +00:00
"& .ce-block__content": {
margin: 0,
maxWidth: "unset"
},
2020-10-28 15:26:30 +00:00
"& .ce-conversion-tool": {
...hover
2019-10-30 14:34:24 +00:00
},
2020-10-28 15:26:30 +00:00
"& .ce-conversion-tool--focused": {
background: `${fade(theme.palette.primary.main, 0.1)} !important`
},
"& .ce-inline-tool": {
...hover,
height: 32,
transition: theme.transitions.duration.short + "ms",
width: 32
},
"& .ce-inline-toolbar__dropdown": {
...hover,
height: 32,
marginRight: 0
},
"& .ce-inline-toolbar__toggler-and-button-wrapper": {
paddingRight: 0
},
"& .codex-editor__redactor": {
marginRight: `${theme.spacing(4)}px !important`,
paddingBottom: "0 !important"
},
"& a": {
color: theme.palette.primary.light
},
"&:hover": {
borderColor: theme.palette.primary.main
},
2020-10-29 12:14:05 +00:00
border: `1px solid ${fade(theme.palette.text.secondary, 0.4)}`,
borderRadius: 4,
2020-10-28 15:26:30 +00:00
boxShadow: `inset 0 0 0 0 ${theme.palette.primary.main}`,
padding: theme.spacing(3, 2),
2020-10-29 12:14:05 +00:00
position: "relative",
2020-10-28 15:26:30 +00:00
transition: theme.transitions.duration.short + "ms"
},
2020-10-28 15:26:30 +00:00
rootActive: {
boxShadow: `inset 0px 0px 0 2px ${theme.palette.primary.main}`
2019-06-19 14:40:52 +00:00
}
};
},
2019-12-03 15:28:40 +00:00
{ name: "RichTextEditor" }
);
2019-08-09 11:14:35 +00:00
2020-10-28 15:26:30 +00:00
const RichTextEditor: React.FC<RichTextEditorProps> = ({
2020-11-03 11:35:36 +00:00
data,
2020-10-28 15:26:30 +00:00
error,
helperText,
2020-11-03 11:35:36 +00:00
label,
onChange
2020-10-28 15:26:30 +00:00
}) => {
const classes = useStyles({});
2019-10-30 14:34:24 +00:00
2020-10-28 15:26:30 +00:00
const [isFocused, setFocus] = React.useState(false);
const editor = React.useRef<EditorJS>();
const editorContainer = React.useRef<HTMLDivElement>();
2020-11-03 11:35:36 +00:00
React.useEffect(
() => {
if (data) {
editor.current = new EditorJS({
data,
holder: editorContainer.current,
onChange: async api => {
const savedData = await api.saver.save();
onChange(savedData);
2020-10-28 15:26:30 +00:00
},
2020-11-03 11:35:36 +00:00
tools: {
header: {
class: Header,
config: {
defaultLevel: 1,
levels: [1, 2, 3]
}
},
list: List,
quote: Quote,
strikethrough: createGenericInlineTool({
sanitize: {
s: true
},
shortcut: "CMD+S",
tagName: "s",
toolboxIcon: strikethroughIcon
})
}
});
2020-10-28 15:26:30 +00:00
}
2020-11-03 11:35:36 +00:00
return editor.current?.destroy;
},
// Rerender editor only if changed from undefined to defined state
[data === undefined]
);
React.useEffect(() => editor.current?.destroy, []);
2019-10-30 14:34:24 +00:00
return (
2020-10-28 15:26:30 +00:00
<div>
<div
className={classNames(classes.root, {
[classes.rootActive]: isFocused
})}
ref={editorContainer}
data-test="richTextEditor"
onFocus={() => setFocus(true)}
onBlur={() => setFocus(false)}
>
<Typography
className={classNames(classes.label, {
[classes.labelActive]: isFocused
})}
variant="caption"
>
2019-06-19 14:40:52 +00:00
{label}
</Typography>
</div>
{helperText && (
<Typography
className={classNames({
[classes.error]: error,
[classes.helperText]: true
})}
variant="caption"
>
{helperText}
</Typography>
)}
</div>
2019-10-30 14:34:24 +00:00
);
};
2019-06-19 14:40:52 +00:00
RichTextEditor.displayName = "RichTextEditor";
export default RichTextEditor;