forked from marmelab/react-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
172 lines (155 loc) · 4.84 KB
/
index.tsx
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import debounce from 'lodash/debounce';
import React, {
useRef,
useEffect,
useCallback,
FunctionComponent,
ComponentProps,
} from 'react';
import Quill, { QuillOptionsStatic } from 'quill';
import { useInput, FieldTitle } from 'ra-core';
import { InputHelperText } from 'ra-ui-materialui';
import {
FormHelperText,
FormControl,
InputLabel,
PropTypes as MuiPropTypes,
} from '@material-ui/core';
import { makeStyles } from '@material-ui/core/styles';
import PropTypes from 'prop-types';
import styles from './styles';
const useStyles = makeStyles(styles, { name: 'RaRichTextInput' });
interface Props {
label?: string | false;
options?: QuillOptionsStatic;
source: string;
toolbar?:
| boolean
| string[]
| Array<any>[]
| string
| {
container: string | string[] | Array<any>[];
handlers?: Record<string, Function>;
};
fullWidth?: boolean;
configureQuill?: (instance: Quill) => void;
helperText?: ComponentProps<typeof InputHelperText>['helperText'];
record?: Record<any, any>;
resource?: string;
variant?: string;
margin?: MuiPropTypes.Margin;
[key: string]: any;
}
const RichTextInput: FunctionComponent<Props> = props => {
const {
options = {}, // Quill editor options
toolbar = true,
fullWidth = true,
classes: classesOverride,
configureQuill,
helperText,
label,
source,
resource,
variant,
margin = 'dense',
...rest
} = props;
const classes = useStyles(props);
const quillInstance = useRef<Quill>();
const divRef = useRef<HTMLDivElement>();
const editor = useRef<HTMLElement>();
const {
id,
isRequired,
input: { value, onChange },
meta: { touched, error },
} = useInput({ source, ...rest });
const lastValueChange = useRef(value);
// eslint-disable-next-line react-hooks/exhaustive-deps
const onTextChange = useCallback(
debounce(() => {
const value =
editor.current.innerHTML === '<p><br></p>'
? ''
: editor.current.innerHTML;
if (lastValueChange.current !== value) {
lastValueChange.current = value;
onChange(value);
}
}, 500),
[onChange]
);
useEffect(() => {
quillInstance.current = new Quill(divRef.current, {
modules: { toolbar, clipboard: { matchVisual: false } },
theme: 'snow',
...options,
});
if (configureQuill) {
configureQuill(quillInstance.current);
}
quillInstance.current.setContents(
quillInstance.current.clipboard.convert(value)
);
editor.current = divRef.current.querySelector('.ql-editor');
quillInstance.current.on('text-change', onTextChange);
return () => {
quillInstance.current.off('text-change', onTextChange);
if (onTextChange.cancel) {
onTextChange.cancel();
}
quillInstance.current = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (lastValueChange.current !== value) {
const selection = quillInstance.current.getSelection();
quillInstance.current.setContents(
quillInstance.current.clipboard.convert(value)
);
if (selection && quillInstance.current.hasFocus()) {
quillInstance.current.setSelection(selection);
}
}
}, [value]);
return (
<FormControl
error={!!(touched && error)}
fullWidth={fullWidth}
className="ra-rich-text-input"
margin={margin}
>
<InputLabel shrink htmlFor={id} className={classes.label}>
<FieldTitle
label={label}
source={source}
resource={resource}
isRequired={isRequired}
/>
</InputLabel>
<div data-testid="quill" ref={divRef} className={variant} />
<FormHelperText
error={!!error}
className={!!error ? 'ra-rich-text-input-error' : ''}
>
<InputHelperText
error={error}
helperText={helperText}
touched={touched}
/>
</FormHelperText>
</FormControl>
);
};
RichTextInput.propTypes = {
// @ts-ignore
label: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
options: PropTypes.object,
source: PropTypes.string,
fullWidth: PropTypes.bool,
configureQuill: PropTypes.func,
};
export default RichTextInput;