forked from zenoamaro/react-quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.js
301 lines (263 loc) · 7.32 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
300
301
'use strict';
var React = require('react'),
ReactDOM = require('react-dom'),
QuillToolbar = require('./toolbar'),
QuillMixin = require('./mixin'),
T = React.PropTypes;
// FIXME: Remove with the switch to JSX
QuillToolbar = React.createFactory(QuillToolbar);
var find = function(arr, predicate) {
if (!arr) {
return;
}
for (var i=0; i<arr.length; ++i) {
if (predicate(arr[i])) return arr[i];
}
}
var QuillComponent = React.createClass({
displayName: 'Quill',
mixins: [ QuillMixin ],
propTypes: {
id: T.string,
className: T.string,
style: T.object,
value: T.string,
defaultValue: T.string,
placeholder: T.string,
readOnly: T.bool,
modules: T.object,
toolbar: T.oneOfType([ T.array, T.oneOf([false]), ]), // deprecated for v1.0.0, use toolbar module
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: '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.setCustomFormats(editor); // deprecated in Quill v1.0
var fontOptions = document.querySelectorAll('.quill-toolbar .ql-font.ql-picker .ql-picker-item');
for (var i=0; i<fontOptions.length; ++i) {
fontOptions[i].style.fontFamily = fontOptions[i].dataset.value;
}
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) {
// 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();
},
/**
* @deprecated v1.0.0
*/
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,
formats: this.props.formats, // Let Quill set the defaults, if no formats supplied
styles: this.props.styles,
modules: this.props.modules,
pollInterval: this.props.pollInterval,
bounds: this.props.bounds,
placeholder: this.props.placeholder,
};
// Unless we're redefining the toolbar, or it has been explicitly
// disabled, attach to the default one as a ref.
// Note: Toolbar should be configured as a module for Quill v1.0.0 and above
// Pass toolbar={false} for versions >1.0
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: ReactDOM.findDOMNode(this.refs.toolbar)
}
}
return config;
},
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 either the specified contents, or a default
configuration of toolbar and contents area.
*/
renderContents: function() {
var contents = [];
var children = React.Children.map(
this.props.children,
function(c) { return React.cloneElement(c, {ref: c.ref}); }
);
if (this.props.toolbar !== false) {
var toolbar = find(children, function(child) {
return child.ref === 'toolbar';
})
contents.push(toolbar ? toolbar : QuillToolbar({
key: 'toolbar-' + Math.random(),
ref: 'toolbar',
items: this.props.toolbar
}))
}
var editor = find(children, function(child) {
return child.ref === 'editor';
})
contents.push(editor ? 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,
onChange: this.preventDefault },
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);
},
/*
Stop change events from the toolbar from
bubbling up outside.
*/
preventDefault: function(event) {
event.preventDefault();
event.stopPropagation();
}
});
module.exports = QuillComponent;