forked from sonyan/react-wysiwyg-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EditableDiv.js
356 lines (337 loc) · 10.5 KB
/
EditableDiv.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var $ = require('npm-zepto');
var React = require('react');
var ReactBootstrap = require('react-bootstrap');
var ButtonGroup = ReactBootstrap.ButtonGroup;
var DropdownButton = ReactBootstrap.DropdownButton;
var MenuItem = ReactBootstrap.MenuItem;
var Button = ReactBootstrap.Button;
var ButtonInput = ReactBootstrap.ButtonInput;
var Overlay = ReactBootstrap.Overlay;
var Popover = ReactBootstrap.Popover;
var Input = ReactBootstrap.Input;
module.exports = React.createClass({
displayName: 'EditableDiv',
propTypes: {
content: React.PropTypes.string.isRequired,
onChange: React.PropTypes.func.isRequired,
onImageUpload: React.PropTypes.func,
onVideoUpload: React.PropTypes.func,
tooltipPlacement: React.PropTypes.string
},
getDefaultProps: function getDefaultProps() {
return {
tooltipPlacement: 'auto'
};
},
getInitialState: function getInitialState() {
// this is anti-pattern but we treat this.props.content as initial content
return {
html: this.props.content,
showImageTooltip: false,
showVideoTooltip: false
};
},
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
this.setState({
html: nextProps.content
});
},
shouldComponentUpdate: function shouldComponentUpdate(nextProps, nextState) {
return nextProps.content !== this.state.html || nextState.showImageTooltip !== this.state.showImageTooltip || nextState.showVideoTooltip !== this.state.showVideoTooltip;
},
_toggleImageTooltip: function _toggleImageTooltip() {
this.setState({
showImageTooltip: !this.state.showImageTooltip
});
},
_toggleVideoTooltip: function _toggleVideoTooltip() {
this.setState({
showVideoTooltip: !this.state.showVideoTooltip
});
},
_execCommand: function _execCommand(command, arg) {
document.execCommand(command, false, arg);
},
_emitChange: function _emitChange() {
var editor = this.refs.editor.getDOMNode();
var newHtml = editor.innerHTML;
this.setState({ html: newHtml }, (function () {
this.props.onChange({
target: {
value: newHtml
}
});
}).bind(this));
},
_onImageSubmit: function _onImageSubmit() {
var _this = this;
var files = this.refs.imageInput.getInputDOMNode().files;
this.props.onImageUpload(files, function (url) {
_this.refs.editor.getDOMNode().focus();
_this._execCommand('insertImage', url);
});
this._toggleImageTooltip();
},
_onVideoSubmit: function _onVideoSubmit() {
var _this2 = this;
var files = this.refs.videoInput.getInputDOMNode().files;
this.props.onVideoUpload(files, function (url) {
_this2.refs.editor.getDOMNode().focus();
_this2._execCommand('insertHTML', '<figure><video controls width="400" src="' + url + '">Your browser does not support HTML5 video.</video></figure>');
});
this._toggleVideoTooltip();
},
render: function render() {
var _this3 = this;
// customize css rules here
var toolbarStyle = { marginBottom: 3 };
var imgTooltipPlacement = this.props.tooltipPlacement;
var videoTooltipPlacement = this.props.tooltipPlacement;
if (imgTooltipPlacement === 'auto') {
var imgBtn = $('#imgUploadBtn');
if (imgBtn.length) {
imgTooltipPlacement = imgBtn.offset().left < 350 ? 'right' : 'left';
}
}
if (videoTooltipPlacement === 'auto') {
var videoUploadBtn = $('#videoUploadBtn');
if (videoUploadBtn.length) {
videoTooltipPlacement = videoUploadBtn.offset().left < 350 ? 'right' : 'left';
}
}
var imageUpload = this.props.onImageUpload === undefined ? null : React.createElement(
Overlay,
{
show: this.state.showImageTooltip,
onHide: function () {
return _this3.setState({ showImageTooltip: false });
},
placement: imgTooltipPlacement,
container: this,
rootClose: true,
target: function () {
return _this3.refs.imgUploadBtn.getDOMNode();
} },
React.createElement(
Popover,
{ id: 'popover', title: 'Image Upload' },
React.createElement(Input, { type: 'file',
ref: 'imageInput',
name: 'file',
label: 'Select an image to upload'
}),
React.createElement(ButtonInput, { type: 'submit', value: 'Submit', onClick: this._onImageSubmit })
)
);
var videoUpload = this.props.onVideoUpload === undefined ? null : React.createElement(
Overlay,
{
show: this.state.showVideoTooltip,
onHide: function () {
return _this3.setState({ showVideoTooltip: false });
},
placement: videoTooltipPlacement,
container: this,
rootClose: true,
target: function () {
return _this3.refs.videoUploadBtn.getDOMNode();
} },
React.createElement(
Popover,
{ id: 'popover', title: 'Video Upload' },
React.createElement(Input, { type: 'file',
ref: 'videoInput',
name: 'file',
label: 'Select a video to upload'
}),
React.createElement(ButtonInput, { type: 'submit', value: 'Submit', onClick: this._onVideoSubmit })
)
);
return React.createElement(
'div',
null,
React.createElement(
'div',
{ style: toolbarStyle },
React.createElement(
ButtonGroup,
null,
React.createElement(
DropdownButton,
{ title: React.createElement('i', { className: 'fa fa-paragraph' }), id: 'bg-nested-dropdown' },
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'formatBlock', 'P') },
'Paragraph'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'formatBlock', 'BLOCKQUOTE') },
'Block Quote'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'formatBlock', 'H1') },
'Header 1'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'formatBlock', 'H2') },
'Header 2'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'formatBlock', 'H3') },
'Header 3'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'formatBlock', 'H4') },
'Header 4'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'formatBlock', 'H5') },
'Header 5'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'formatBlock', 'H6') },
'Header 6'
)
),
React.createElement(
Button,
{ onClick: this._execCommand.bind(this, 'bold') },
React.createElement('i', { className: 'fa fa-bold' })
),
React.createElement(
Button,
{ onClick: this._execCommand.bind(this, 'italic') },
React.createElement('i', { className: 'fa fa-italic' })
),
React.createElement(
Button,
{ onClick: this._execCommand.bind(this, 'underline') },
React.createElement('i', { className: 'fa fa-underline' })
),
React.createElement(
Button,
{ onClick: this._execCommand.bind(this, 'strikeThrough') },
React.createElement('i', { className: 'fa fa-strikethrough' })
),
React.createElement(
DropdownButton,
{ title: React.createElement('i', { className: 'fa fa-text-height' }), id: 'bg-nested-dropdown' },
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'fontSize', 1) },
'1'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'fontSize', 2) },
'2'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'fontSize', 3) },
'3'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'fontSize', 4) },
'4'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'fontSize', 5) },
'5'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'fontSize', 6) },
'6'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'fontSize', 7) },
'7'
)
),
React.createElement(
Button,
{ onClick: this._execCommand.bind(this, 'insertOrderedList') },
React.createElement('i', { className: 'fa fa-list-ol' })
),
React.createElement(
Button,
{ onClick: this._execCommand.bind(this, 'insertUnorderedList') },
React.createElement('i', { className: 'fa fa-list-ul' })
),
React.createElement(
DropdownButton,
{ title: React.createElement('i', { className: 'fa fa-align-left' }), id: 'bg-nested-dropdown' },
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'justifyLeft') },
'Align Left'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'justifyRight') },
'Align Right'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'justifyCenter') },
'Align Center'
),
React.createElement(
MenuItem,
{ onSelect: this._execCommand.bind(this, 'justifyFull') },
'Align Justify'
)
),
React.createElement(
Button,
{ onClick: this._execCommand.bind(this, 'removeFormat') },
React.createElement('i', { className: 'fa fa-eraser' })
),
React.createElement(
Button,
{ ref: 'imgUploadBtn', id: 'imgUploadBtn', onClick: this._toggleImageTooltip },
React.createElement('i', { className: 'fa fa-picture-o' })
),
imageUpload,
React.createElement(
Button,
{ ref: 'videoUploadBtn', id: 'videoUploadBtn', onClick: this._toggleVideoTooltip },
React.createElement('i', { className: 'fa fa-file-video-o' })
),
videoUpload,
React.createElement(
Button,
{ onClick: this._execCommand.bind(this, 'createLink') },
React.createElement('i', { className: 'fa fa-link' })
)
)
),
React.createElement('div', _extends({
ref: 'editor',
className: 'form-control'
}, this.props, {
contentEditable: 'true',
dangerouslySetInnerHTML: { __html: this.state.html },
onBlur: function (e) {
if (e.relatedTarget.id === 'imgUploadBtn' || e.relatedTarget.id === 'videoUploadBtn') {
e.preventDefault();
_this3.refs.editor.getDOMNode().focus();
}
},
onInput: this._emitChange }))
);
}
});