-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathindex.tsx
More file actions
106 lines (97 loc) · 4.39 KB
/
Copy pathindex.tsx
File metadata and controls
106 lines (97 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, { useEffect } from 'react';
import { EditorState, convertToRaw } from 'draft-js';
import { RichTextArea } from '../Input/RichTextArea';
import { FormControl, Paper } from '@mui/material';
import { getEditorStateFromHtml, getEditorStateFromRaw } from './utils';
import { ErrorMessage } from '../Typography';
/**
* A Rich Text Editor component that uses react-draft-wysiwyg to render a rich text editor.
* Its primary purpose is to allow users to create rich text content to display in the {@link RichTextArea} component.
* It allows for rich text editing with various toolbar options.
* @param {Object} props - The properties for the RichTextEditor component.
* @param {Function} props.setRawText - Function to set the raw text content of the editor.
* @param {Function} props.handleEditorStateChange - Function to handle changes in the editor state.
* @param {string} props.initialRawEditorState - Initial raw editor state in JSON format.
* @param {string} props.initialHTMLText - Initial HTML text to populate the editor.
* @param {boolean} props.error - Boolean indicating if there is an error in the editor.
* @param {string} props.helperText - Text to display as helper text below the editor, typically used for error messages.
* @param {Object} props.toolbar - Configuration for the editor toolbar, allowing customization of available options.
* Options include 'inline', 'list', 'link', 'blockType', and 'history'.
* Each option can have sub-options, such as 'bold', 'italic', 'underline', etc.
* @see {@link https://jpuri.github.io/react-draft-wysiwyg/#/docs} for more details on the editor and its options.
* @see {@link https://draftjs.org/docs/advanced-topics-decorators} for more information on decorators in Draft.js.
* @returns
*/
const RichTextEditor = ({
setRawText = (_rawText: string) => {
/* empty default method */
},
handleEditorStateChange = (_stringifiedEditorState: string) => {
/* empty default method */
},
initialRawEditorState = '',
initialHTMLText = '',
error = false,
helperText = '',
toolbar = {
options: ['inline', 'list', 'link', 'blockType', 'history'],
inline: {
options: ['bold', 'italic', 'underline', 'superscript', 'subscript'],
},
blockType: { options: ['Normal', 'H2', 'H3', 'Blockquote'] },
list: { options: ['unordered', 'ordered'] },
},
}) => {
const getStateFromInitialValue = () => {
if (initialRawEditorState) {
setEditorState(getEditorStateFromRaw(initialRawEditorState));
return;
}
if (initialHTMLText) {
const contentState = getEditorStateFromHtml(initialHTMLText);
setEditorState(contentState);
}
if (!initialRawEditorState && !initialHTMLText) {
setEditorState(getEditorStateFromRaw(''));
}
};
const [editorState, setEditorState] = React.useState(getEditorStateFromRaw(initialRawEditorState));
const handleChange = (newEditorState: EditorState) => {
const plainText = newEditorState.getCurrentContent().getPlainText();
setEditorState(newEditorState);
const stringifiedEditorState = JSON.stringify(convertToRaw(newEditorState.getCurrentContent()));
handleEditorStateChange(stringifiedEditorState);
setRawText(plainText);
};
useEffect(() => {
getStateFromInitialValue();
}, [initialRawEditorState, initialHTMLText]);
return (
<FormControl fullWidth>
<Paper
sx={{
borderColor: error ? 'error.main' : '#605E5C',
borderWidth: '1px',
borderStyle: 'solid',
borderRadius: '8px',
}}
>
<form>
<RichTextArea
spellCheck
editorState={editorState}
onEditorStateChange={handleChange}
handlePastedText={() => false}
editorStyle={{
padding: '1em',
resize: 'vertical',
}}
toolbar={toolbar}
/>
</form>
</Paper>
<ErrorMessage error={error ? helperText : undefined} />
</FormControl>
);
};
export default RichTextEditor;