forked from pmyers88/react-wysiwyg-editor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EditableDiv.jsx
308 lines (290 loc) · 10.6 KB
/
EditableDiv.jsx
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
'use strict';
var $ = require('npm-zepto');
var React = require('react');
var ReactDOM = require('react-dom');
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() {
return {
tooltipPlacement: 'auto'
};
},
getInitialState: function() {
// this is anti-pattern but we treat this.props.content as initial content
return {
html: this.props.content,
showImageTooltip: false,
showVideoTooltip: false,
showLinkTooltip: false,
textSelection: null
};
},
componentWillReceiveProps: function(nextProps) {
this.setState({
html: nextProps.content
});
},
shouldComponentUpdate: function(nextProps, nextState) {
return nextProps.content !== this.state.html || nextState.showImageTooltip !== this.state.showImageTooltip ||
nextState.showVideoTooltip !== this.state.showVideoTooltip || nextState.showLinkTooltip !== this.state.showLinkTooltip;
},
_toggleImageTooltip: function() {
this.setState({
showImageTooltip: !this.state.showImageTooltip
});
},
_toggleVideoTooltip: function() {
this.setState({
showVideoTooltip: !this.state.showVideoTooltip
});
},
_toggleLinkTooltip: function() {
this.setState({
showLinkTooltip: !this.state.showLinkTooltip,
textSelection: this._saveSelection()
});
},
_execCommand: function(command, arg) {
document.execCommand(command, false, arg);
},
_emitChange: function() {
var editor = ReactDOM.findDOMNode(this.refs.editor);
var newHtml = editor.innerHTML;
this.setState({html: newHtml}, function() {
this.props.onChange({
target: {
value: newHtml
}
});
}.bind(this));
},
_onImageSubmit: function() {
var files = this.refs.imageInput.getInputDOMNode().files;
this.props.onImageUpload(files, (url) => {
ReactDOM.findDOMNode(this.refs.editor).focus();
this._execCommand('insertImage', url);
});
this._toggleImageTooltip();
},
_onVideoSubmit: function() {
var files = this.refs.videoInput.getInputDOMNode().files;
this.props.onVideoUpload(files, (url) => {
ReactDOM.findDOMNode(this.refs.editor).focus();
this._execCommand('insertHTML', `<figure><video controls width="400" src="${url}">Your browser does not support HTML5 video.</video><br/></figure>`);
});
this._toggleVideoTooltip();
},
// source: http://stackoverflow.com/a/5614571
_saveSelection: function() {
if (window.getSelection) {
if (window.getSelection().getRangeAt && window.getSelection().rangeCount) {
var ranges = [];
for (var i = 0, len = window.getSelection().rangeCount; i < len; ++i) {
ranges.push(window.getSelection().getRangeAt(i));
}
return ranges;
}
} else if (document.selection && document.selection.createRange) {
return document.selection.createRange();
}
return null;
},
_restoreSelection: function(savedSel) {
if (savedSel) {
if (window.getSelection) {
window.getSelection().removeAllRanges();
for (var i = 0, len = savedSel.length; i < len; ++i) {
window.getSelection().addRange(savedSel[i]);
}
} else if (document.selection && savedSel.select) {
savedSel.select();
}
}
},
_onLinkSubmit: function() {
ReactDOM.findDOMNode(this.refs.editor).focus();
this._restoreSelection(this.state.textSelection);
this._execCommand('createLink', this.refs.linkInput.getInputDOMNode().value);
this._toggleLinkTooltip();
},
render: function() {
// customize css rules here
var toolbarStyle = {marginBottom: 3};
var imgTooltipPlacement = this.props.tooltipPlacement;
var videoTooltipPlacement = this.props.tooltipPlacement;
var linkTooltipPlacement = 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';
}
}
if (linkTooltipPlacement === 'auto') {
var linkCreateBtn = $('#linkCreateBtn');
if (linkCreateBtn.length) {
linkTooltipPlacement = linkCreateBtn.offset().left < 350 ? 'right' : 'left';
}
}
var imageUpload = this.props.onImageUpload === undefined ? null : (
<Overlay
show={this.state.showImageTooltip}
onHide={() => this.setState({ showImageTooltip: false })}
placement={imgTooltipPlacement}
container={this}
rootClose={true}
target={() => ReactDOM.findDOMNode(this.refs.imgUploadBtn)} >
<Popover id="popover" title="Image Upload" >
<Input type="file"
ref="imageInput"
name="file"
label="Select an image to upload"
/>
<Button type="submit" onClick={this._onImageSubmit}>Submit</Button>
</Popover>
</Overlay>
);
var videoUpload = this.props.onVideoUpload === undefined ? null : (
<Overlay
show={this.state.showVideoTooltip}
onHide={() => this.setState({ showVideoTooltip: false })}
placement={videoTooltipPlacement}
container={this}
rootClose={true}
target={() => ReactDOM.findDOMNode(this.refs.videoUploadBtn)} >
<Popover id="popover" title="Video Upload" >
<Input type="file"
ref="videoInput"
name="file"
label="Select a video to upload"
/>
<Button type="submit" onClick={this._onVideoSubmit}>Submit</Button>
</Popover>
</Overlay>
);
var linkCreate = (
<Overlay
show={this.state.showLinkTooltip}
onHide={() => this.setState({ showLinkTooltip: false })}
placement={linkTooltipPlacement}
container={this}
rootClose={true}
target={() => ReactDOM.findDOMNode(this.refs.linkCreateBtn)} >
<Popover id="popover" title="Create Link">
<Input type="text"
ref="linkInput"
name="url"
defaultValue="http://"
label="Link URL"
/>
<Button type="submit" onClick={this._onLinkSubmit}>Submit</Button>
</Popover>
</Overlay>
);
return (
<div>
<div style={toolbarStyle}>
<ButtonGroup>
<DropdownButton title={<i className="fa fa-paragraph"></i>} id="bg-nested-dropdown">
<MenuItem onSelect={this._execCommand.bind(this, 'formatBlock', 'P')}>Paragraph</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'formatBlock', 'BLOCKQUOTE')}>Block Quote</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'formatBlock', 'H1')}>Header 1</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'formatBlock', 'H2')}>Header 2</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'formatBlock', 'H3')}>Header 3</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'formatBlock', 'H4')}>Header 4</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'formatBlock', 'H5')}>Header 5</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'formatBlock', 'H6')}>Header 6</MenuItem>
</DropdownButton>
<Button onClick={this._execCommand.bind(this, 'bold')}>
<i className="fa fa-bold"></i>
</Button>
<Button onClick={this._execCommand.bind(this, 'italic')}>
<i className="fa fa-italic"></i>
</Button>
<Button onClick={this._execCommand.bind(this, 'underline')}>
<i className="fa fa-underline"></i>
</Button>
<Button onClick={this._execCommand.bind(this, 'strikeThrough')}>
<i className="fa fa-strikethrough"></i>
</Button>
<DropdownButton title={<i className="fa fa-text-height"></i>} id="bg-nested-dropdown">
<MenuItem onSelect={this._execCommand.bind(this, 'fontSize', 1)}>1</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'fontSize', 2)}>2</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'fontSize', 3)}>3</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'fontSize', 4)}>4</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'fontSize', 5)}>5</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'fontSize', 6)}>6</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'fontSize', 7)}>7</MenuItem>
</DropdownButton>
<Button onClick={this._execCommand.bind(this, 'insertOrderedList')}>
<i className="fa fa-list-ol"></i>
</Button>
<Button onClick={this._execCommand.bind(this, 'insertUnorderedList')}>
<i className="fa fa-list-ul"></i>
</Button>
<DropdownButton title={<i className="fa fa-align-left"></i>} id="bg-nested-dropdown">
<MenuItem onSelect={this._execCommand.bind(this, 'justifyLeft')}>Align Left</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'justifyRight')}>Align Right</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'justifyCenter')}>Align Center</MenuItem>
<MenuItem onSelect={this._execCommand.bind(this, 'justifyFull')}>Align Justify</MenuItem>
</DropdownButton>
<Button onClick={this._execCommand.bind(this, 'removeFormat')}>
<i className="fa fa-eraser"></i>
</Button>
<Button ref="imgUploadBtn" id="imgUploadBtn" onClick={this._toggleImageTooltip}>
<i className="fa fa-picture-o"></i>
</Button>
{imageUpload}
<Button ref="videoUploadBtn" id="videoUploadBtn" onClick={this._toggleVideoTooltip}>
<i className="fa fa-file-video-o"></i>
</Button>
{videoUpload}
<Button ref="linkCreateBtn" id="linkCreateBtn" onClick={this._toggleLinkTooltip}>
<i className="fa fa-link"></i>
</Button>
{linkCreate}
</ButtonGroup>
</div>
<div
ref="editor"
className="form-control"
{...this.props}
contentEditable="true"
dangerouslySetInnerHTML={{__html: this.state.html}}
onBlur={(e) => {
if (!e || !e.relatedTarget) {
return;
}
if (e.relatedTarget.id === 'imgUploadBtn' || e.relatedTarget.id === 'videoUploadBtn' || e.relatedTarget.id === 'linkCreateBtn') {
e.preventDefault();
ReactDOM.findDOMNode(this.refs.editor).focus();
}
}}
onInput={this._emitChange}
onKeyPress={this._emitChange}
/>
</div>
);
}
});