import EditorJS, { OutputData } from "@editorjs/editorjs"; import FormControl from "@material-ui/core/FormControl"; import FormHelperText from "@material-ui/core/FormHelperText"; import InputLabel from "@material-ui/core/InputLabel"; import classNames from "classnames"; import React from "react"; import { RichTextEditorContentProps, tools } from "./RichTextEditorContent"; import useStyles from "./styles"; export type RichTextEditorChange = (data: OutputData) => void; export interface RichTextEditorProps extends RichTextEditorContentProps { disabled: boolean; error: boolean; helperText: string; label: string; name: string; onChange: RichTextEditorChange; } const RichTextEditor: React.FC = ({ data, disabled, error, helperText, label, name, onChange, onReady }) => { const classes = useStyles({}); const [isFocused, setFocus] = React.useState(false); const editor = React.useRef(); const editorContainer = React.useRef(); React.useEffect( () => { if (data) { editor.current = new EditorJS({ data, holder: editorContainer.current, onChange: async api => { const savedData = await api.saver.save(); onChange(savedData); }, onReady, readOnly: disabled, tools }); } return editor.current?.destroy; }, // Rerender editor only if changed from undefined to defined state [data === undefined] ); React.useEffect(() => editor.current?.destroy, []); React.useEffect(() => { if (editor.current?.readOnly) { editor.current.readOnly.toggle(disabled); } }, [disabled]); return ( {label}
setFocus(true)} onBlur={() => setFocus(false)} /> {helperText} ); }; RichTextEditor.displayName = "RichTextEditor"; export default RichTextEditor;