forked from zenoamaro/react-quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.js
299 lines (262 loc) · 7.24 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict';
var React = require('react'),
QuillToolbar = require('./toolbar'),
QuillMixin = require('./mixin'),
T = React.PropTypes;
// Support React 0.11 and 0.12
// FIXME: Remove with React 0.13
if (React.createFactory) {
QuillToolbar = React.createFactory(QuillToolbar);
}
// Support React 0.12 and 0.13+
// FIXME: Remove with React 0.13
if (React.cloneElement) {
var cloneElement = React.cloneElement;
} else if (React.addons && React.addons.cloneWithProps) {
var cloneElement = React.addons.cloneWithProps;
} else {
throw new Error('React addons are required when using React 0.12 or less.');
}
var QuillComponent = React.createClass({
displayName: 'Quill',
mixins: [ QuillMixin ],
propTypes: {
id: T.string,
className: T.string,
style: T.object,
value: T.string,
defaultValue: T.string,
readOnly: T.bool,
modules: T.object,
toolbar: T.oneOfType([ T.array, T.oneOf([false]), ]),
formats: T.array,
styles: T.oneOfType([ T.object, T.oneOf([false]) ]),
theme: T.string,
pollInterval: T.number,
onKeyPress: T.func,
onKeyDown: T.func,
onKeyUp: T.func,
onChange: T.func,
onChangeSelection: T.func
},
/*
Changing one of these props should cause a re-render.
*/
dirtyProps: [
'id',
'className',
'modules',
'toolbar',
'formats',
'styles',
'theme',
'pollInterval'
],
getDefaultProps: function() {
return {
className: '',
theme: 'base',
modules: {
'link-tooltip': true
}
};
},
/*
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.setCustomFormats(editor);
// NOTE: Custom formats will be stripped when creating
// the editor, since they are not present there yet.
// Therefore, we re-set the contents from state.
this.setState({ editor:editor }, function() {
this.setEditorContents(editor, this.state.value);
}.bind(this));
},
componentWillUnmount: function() {
this.destroyEditor(this.state.editor);
// NOTE: Don't set the state to null here
// as it would generate a loop.
},
shouldComponentUpdate: function(nextProps, nextState) {
// Check if one of the changes should trigger a re-render.
for (var i=0; i<this.dirtyProps.length; i++) {
var prop = this.dirtyProps[i];
if (nextProps[prop] !== this.props[prop]) {
return true;
}
}
// Never re-render otherwise.
return false;
},
/*
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();
},
setCustomFormats: function (editor) {
if (!this.props.formats) {
return;
}
for (var i = 0; i < this.props.formats.length; i++) {
var format = this.props.formats[i];
editor.addFormat(format.name || format, format);
}
},
getEditorConfig: function() {
var config = {
readOnly: this.props.readOnly,
theme: this.props.theme,
// Let Quill set the defaults, if no formats supplied
formats: this.props.formats ? [] : undefined,
styles: this.props.styles,
modules: this.props.modules,
pollInterval: this.props.pollInterval
};
// Unless we're redefining the toolbar, or it has been explicitly
// disabled, attach to the default one as a ref.
if (this.props.toolbar !== false && !config.modules.toolbar) {
// Don't mutate the original modules
// because it's shared between components.
config.modules = JSON.parse(JSON.stringify(config.modules));
config.modules.toolbar = {
container: this.refs.toolbar.getDOMNode()
};
}
return config;
},
getEditor: function() {
return this.state.editor;
},
getEditorElement: function() {
return this.refs.editor.getDOMNode();
},
getEditorContents: function() {
return this.state.value;
},
getEditorSelection: function() {
return this.state.selection;
},
/*
Renders either the specified contents, or a default
configuration of toolbar and contents area.
*/
renderContents: function() {
if (React.Children.count(this.props.children)) {
// Clone children to own their refs.
return React.Children.map(
this.props.children,
function(c) { return cloneElement(c, { ref: c.ref }) }
);
} else {
return [
// Quill modifies these elements in-place,
// so we need to re-render them every time.
// Render the toolbar unless explicitly disabled.
this.props.toolbar !== false? QuillToolbar({
key: 'toolbar-' + Math.random(),
ref: 'toolbar',
items: this.props.toolbar
}) : false,
React.DOM.div({
key: 'editor-' + Math.random(),
ref: 'editor',
className: 'quill-contents',
dangerouslySetInnerHTML: { __html:this.getEditorContents() }
})
];
}
},
render: function() {
return React.DOM.div({
id: this.props.id,
style: this.props.style,
className: 'quill ' + this.props.className,
onKeyPress: this.props.onKeyPress,
onKeyDown: this.props.onKeyDown,
onKeyUp: this.props.onKeyUp,
onChange: this.preventDefault },
this.renderContents()
);
},
onEditorChange: function(value, delta, source) {
if (value !== this.getEditorContents()) {
this.setState({ value: value });
if (this.props.onChange) {
this.props.onChange(value, delta, source);
}
}
},
onEditorChangeSelection: function(range, source) {
var s = this.getEditorSelection() || {};
var r = range || {};
if (r.start !== s.start || r.end !== s.end) {
this.setState({ selection: range });
if (this.props.onChangeSelection) {
this.props.onChangeSelection(range, source);
}
}
},
focus: function() {
this.state.editor.focus();
},
blur: function() {
this.setEditorSelection(this.state.editor, null);
},
/*
Stop change events from the toolbar from
bubbling up outside.
*/
preventDefault: function(event) {
event.preventDefault();
event.stopPropagation();
}
});
module.exports = QuillComponent;