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 { FormChange } from "@saleor/hooks/useForm";
|
|
|
|
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
|
|
|
|
|
|
|
export interface RichTextEditorProps {
|
|
|
|
disabled: boolean;
|
|
|
|
error: boolean;
|
|
|
|
helperText: string;
|
2020-10-28 15:30:44 +00:00
|
|
|
// TODO: Remove any type
|
|
|
|
initial: OutputData | any;
|
2019-06-19 14:40:52 +00:00
|
|
|
label: string;
|
|
|
|
name: string;
|
2020-10-28 15:26:30 +00:00
|
|
|
onChange: FormChange;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 15:28:40 +00:00
|
|
|
const useStyles = makeStyles(
|
2020-02-03 11:16:07 +00:00
|
|
|
theme => {
|
2020-10-28 15:26:30 +00:00
|
|
|
const hover = {
|
|
|
|
"&:hover": {
|
|
|
|
background: fade(theme.palette.primary.main, 0.1)
|
|
|
|
}
|
2020-02-03 11:16:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
error: {
|
|
|
|
color: theme.palette.error.main
|
2019-06-19 14:40:52 +00:00
|
|
|
},
|
2020-02-03 11:16:07 +00:00
|
|
|
helperText: {
|
|
|
|
marginTop: theme.spacing(0.75)
|
|
|
|
},
|
|
|
|
label: {
|
2020-10-28 15:26:30 +00:00
|
|
|
color: theme.palette.text.secondary,
|
2020-02-03 11:16:07 +00:00
|
|
|
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-02-03 11:16:07 +00:00
|
|
|
},
|
2020-10-28 15:26:30 +00:00
|
|
|
labelActive: {
|
|
|
|
color: theme.palette.primary.main
|
2020-02-03 11:16:07 +00:00
|
|
|
},
|
|
|
|
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-02-03 11:16:07 +00:00
|
|
|
},
|
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
|
|
|
}
|
2020-02-03 11:16:07 +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
|
|
|
class NewEditor extends EditorJS {}
|
2019-08-09 11:14:35 +00:00
|
|
|
|
2020-10-28 15:26:30 +00:00
|
|
|
const RichTextEditor: React.FC<RichTextEditorProps> = ({
|
|
|
|
error,
|
|
|
|
helperText,
|
|
|
|
initial,
|
2020-10-28 15:30:44 +00:00
|
|
|
label
|
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>();
|
|
|
|
React.useEffect(() => {
|
|
|
|
editor.current = new NewEditor({
|
|
|
|
data: initial,
|
|
|
|
holder: editorContainer.current,
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
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;
|