2020-11-05 16:30:38 +00:00
|
|
|
import EditorJS, {
|
2020-11-20 09:44:50 +00:00
|
|
|
LogLevels,
|
2020-11-05 16:30:38 +00:00
|
|
|
OutputData,
|
|
|
|
ToolConstructable,
|
|
|
|
ToolSettings
|
|
|
|
} from "@editorjs/editorjs";
|
2021-05-18 13:21:33 +00:00
|
|
|
import Embed from "@editorjs/embed";
|
2020-11-05 16:30:38 +00:00
|
|
|
import Header from "@editorjs/header";
|
|
|
|
import List from "@editorjs/list";
|
2021-05-07 08:06:08 +00:00
|
|
|
import Paragraph from "@editorjs/paragraph";
|
2020-11-05 16:30:38 +00:00
|
|
|
import Quote from "@editorjs/quote";
|
|
|
|
import strikethroughIcon from "@saleor/icons/StrikethroughIcon";
|
|
|
|
import classNames from "classnames";
|
|
|
|
import createGenericInlineTool from "editorjs-inline-tool";
|
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
import useStyles from "./styles";
|
2022-03-16 10:09:12 +00:00
|
|
|
import { clean } from "./utils";
|
2020-11-05 16:30:38 +00:00
|
|
|
|
|
|
|
export interface RichTextEditorContentProps {
|
|
|
|
className?: string;
|
|
|
|
data: OutputData;
|
|
|
|
onReady?: () => void;
|
|
|
|
}
|
|
|
|
|
2021-05-07 08:06:08 +00:00
|
|
|
const inlineToolbar = ["link", "bold", "italic", "strikethrough"];
|
|
|
|
|
2020-11-05 16:30:38 +00:00
|
|
|
export const tools: Record<string, ToolConstructable | ToolSettings> = {
|
2021-05-18 13:21:33 +00:00
|
|
|
embed: Embed,
|
2020-11-05 16:30:38 +00:00
|
|
|
header: {
|
|
|
|
class: Header,
|
|
|
|
config: {
|
|
|
|
defaultLevel: 1,
|
|
|
|
levels: [1, 2, 3]
|
2021-05-07 08:06:08 +00:00
|
|
|
},
|
|
|
|
inlineToolbar
|
|
|
|
},
|
|
|
|
list: {
|
|
|
|
class: List,
|
|
|
|
inlineToolbar
|
|
|
|
},
|
|
|
|
quote: {
|
|
|
|
class: Quote,
|
|
|
|
inlineToolbar
|
|
|
|
},
|
|
|
|
paragraph: {
|
|
|
|
class: Paragraph,
|
|
|
|
inlineToolbar
|
2020-11-05 16:30:38 +00:00
|
|
|
},
|
|
|
|
strikethrough: createGenericInlineTool({
|
|
|
|
sanitize: {
|
|
|
|
s: {}
|
|
|
|
},
|
|
|
|
shortcut: "CMD+S",
|
|
|
|
tagName: "s",
|
|
|
|
toolboxIcon: strikethroughIcon
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
const RichTextEditorContent: React.FC<RichTextEditorContentProps> = ({
|
|
|
|
className,
|
|
|
|
data,
|
|
|
|
onReady
|
|
|
|
}) => {
|
|
|
|
const classes = useStyles({});
|
|
|
|
|
|
|
|
const editor = React.useRef<EditorJS>();
|
|
|
|
const editorContainer = React.useRef<HTMLDivElement>();
|
|
|
|
React.useEffect(
|
|
|
|
() => {
|
2022-02-02 22:12:58 +00:00
|
|
|
if (data !== undefined && !editor.current) {
|
|
|
|
const editorjs = new EditorJS({
|
2020-11-05 16:30:38 +00:00
|
|
|
data,
|
|
|
|
holder: editorContainer.current,
|
2020-11-20 09:44:50 +00:00
|
|
|
logLevel: "ERROR" as LogLevels,
|
2022-02-02 22:12:58 +00:00
|
|
|
onReady: () => {
|
|
|
|
editor.current = editorjs;
|
|
|
|
|
|
|
|
if (onReady) {
|
|
|
|
onReady();
|
|
|
|
}
|
|
|
|
},
|
2020-11-05 16:30:38 +00:00
|
|
|
readOnly: true,
|
|
|
|
tools
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-02 22:12:58 +00:00
|
|
|
return () => {
|
2022-03-16 10:09:12 +00:00
|
|
|
clean(editor.current);
|
2022-02-02 22:12:58 +00:00
|
|
|
editor.current = null;
|
|
|
|
};
|
2020-11-05 16:30:38 +00:00
|
|
|
},
|
|
|
|
// Rerender editor only if changed from undefined to defined state
|
|
|
|
[data === undefined]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2022-03-16 10:09:12 +00:00
|
|
|
className={classNames(classes.editor, classes.rootStatic, className)}
|
2020-11-05 16:30:38 +00:00
|
|
|
ref={editorContainer}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
RichTextEditorContent.displayName = "RichTextEditorContent";
|
|
|
|
export default RichTextEditorContent;
|