forked from zenoamaro/react-quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.js
272 lines (235 loc) · 6.76 KB
/
component.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
'use strict';
var React = require('react');
var ReactDOM = require('react-dom');
var QuillMixin = require('./mixin');
var find = require('lodash/find');
var some = require('lodash/some');
var isEqual = require('lodash/isEqual');
var T = React.PropTypes;
var QuillComponent = React.createClass({
displayName: 'Quill',
mixins: [ QuillMixin ],
propTypes: {
id: T.string,
className: T.string,
theme: T.string,
style: T.object,
readOnly: T.bool,
value: T.string,
defaultValue: T.string,
placeholder: T.string,
bounds: T.oneOfType([T.string, T.element]),
onKeyPress: T.func,
onKeyDown: T.func,
onKeyUp: T.func,
onChange: T.func,
onChangeSelection: T.func,
modules: function(props) {
var isNotObject = T.object.apply(this, arguments);
if (isNotObject) return isNotObject;
if (
props.modules &&
props.modules.toolbar &&
props.modules.toolbar[0] &&
props.modules.toolbar[0].type
) return new Error(
'Since v1.0.0, React Quill will not create a custom toolbar for you ' +
'anymore. Create a toolbar explictly, or let Quill create one. ' +
'See: https://github.com/zenoamaro/react-quill#upgrading-to-react-quill-v1-0-0'
);
},
toolbar: function(props) {
if ('toolbar' in props) return new Error(
'The `toolbar` prop has been deprecated. Use `modules.toolbar` instead. ' +
'See: https://github.com/zenoamaro/react-quill#upgrading-to-react-quill-v1-0-0'
);
},
formats: function(props) {
var isNotArrayOfString = T.arrayOf(T.string).apply(this, arguments);
if (isNotArrayOfString) return new Error(
'You cannot specify custom `formats` anymore. Use Parchment instead. ' +
'See: https://github.com/zenoamaro/react-quill#upgrading-to-react-quill-v1-0-0'
);
},
styles: function(props) {
if ('styles' in props) return new Error(
'The `styles` prop has been deprecated. Use custom stylesheets instead. ' +
'See: https://github.com/zenoamaro/react-quill#upgrading-to-react-quill-v1-0-0'
);
},
pollInterval: function(props) {
if ('pollInterval' in props) return new Error(
'The `pollInterval` property does not have any effect anymore. ' +
'You can safely remove it from your props.' +
'See: https://github.com/zenoamaro/react-quill#upgrading-to-react-quill-v1-0-0'
);
}
},
/*
Changing one of these props should cause a re-render.
*/
dirtyProps: [
'id',
'className',
'style',
'modules',
'formats',
'bounds',
'theme',
],
getDefaultProps: function() {
return {
className: '',
theme: 'snow',
modules: {},
};
},
/*
We consider the component to be controlled if
whenever `value` is bein sent in props.
*/
isControlled: function() {
return 'value' in this.props;
},
getInitialState: function() {
return {
value: this.isControlled()
? this.props.value
: this.props.defaultValue
};
},
componentWillReceiveProps: function(nextProps) {
var editor = this.state.editor;
// If the component is unmounted and mounted too quickly
// an error is thrown in setEditorContents since editor is
// still undefined. Must check if editor is undefined
// before performing this call.
if (editor) {
// Update only if we've been passed a new `value`.
// This leaves components using `defaultValue` alone.
if ('value' in nextProps) {
// NOTE: Seeing that Quill is missing a way to prevent
// edits, we have to settle for a hybrid between
// controlled and uncontrolled mode. We can't prevent
// the change, but we'll still override content
// whenever `value` differs from current state.
if (nextProps.value !== this.getEditorContents()) {
this.setEditorContents(editor, nextProps.value);
}
}
// We can update readOnly state in-place.
if ('readOnly' in nextProps) {
if (nextProps.readOnly !== this.props.readOnly) {
this.setEditorReadOnly(editor, nextProps.readOnly);
}
}
}
},
componentDidMount: function() {
var editor = this.createEditor(
this.getEditorElement(),
this.getEditorConfig()
);
this.setState({ editor:editor });
},
componentWillUnmount: function() {
// NOTE: Don't set the state to null here
// as it would generate a loop.
},
shouldComponentUpdate: function(nextProps, nextState) {
// Rerender whenever a "dirtyProp" changes
var props = this.props;
return some(this.dirtyProps, function(prop) {
return !isEqual(nextProps[prop], props[prop]);
});
},
/*
If for whatever reason we are rendering again,
we should tear down the editor and bring it up
again.
*/
componentWillUpdate: function() {
this.componentWillUnmount();
},
componentDidUpdate: function() {
this.componentDidMount();
},
getEditorConfig: function() {
return {
bounds: this.props.bounds,
formats: this.props.formats,
modules: this.props.modules,
placeholder: this.props.placeholder,
readOnly: this.props.readOnly,
theme: this.props.theme,
};
},
getEditor: function() {
return this.state.editor;
},
getEditorElement: function() {
return ReactDOM.findDOMNode(this.refs.editor);
},
getEditorContents: function() {
return this.state.value;
},
getEditorSelection: function() {
return this.state.selection;
},
/*
Renders an editor element, unless it has been provided one to clone.
*/
renderContents: function() {
var contents = [];
var children = React.Children.map(
this.props.children,
function(c) { return React.cloneElement(c, {ref: c.ref}); }
);
var editor = find(children, function(child) {
return child.ref === 'editor';
});
contents.push(editor || React.DOM.div({
key: 'editor-' + Math.random(),
ref: 'editor',
className: 'quill-contents',
dangerouslySetInnerHTML: { __html:this.getEditorContents() }
}));
return contents;
},
render: function() {
return React.DOM.div({
id: this.props.id,
style: this.props.style,
className: ['quill'].concat(this.props.className).join(' '),
onKeyPress: this.props.onKeyPress,
onKeyDown: this.props.onKeyDown,
onKeyUp: this.props.onKeyUp },
this.renderContents()
);
},
onEditorChange: function(value, delta, source, editor) {
if (value !== this.getEditorContents()) {
this.setState({ value: value });
if (this.props.onChange) {
this.props.onChange(value, delta, source, editor);
}
}
},
onEditorChangeSelection: function(range, source, editor) {
var s = this.getEditorSelection() || {};
var r = range || {};
if (r.length !== s.length || r.index !== s.index) {
this.setState({ selection: range });
if (this.props.onChangeSelection) {
this.props.onChangeSelection(range, source, editor);
}
}
},
focus: function() {
this.state.editor.focus();
},
blur: function() {
this.setEditorSelection(this.state.editor, null);
}
});
module.exports = QuillComponent;