forked from sonyan/react-wysiwyg-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EditableDiv.jsx
230 lines (216 loc) · 8.17 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
'use strict';
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() {
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
};
},
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;
},
_toggleImageTooltip: function() {
this.setState({
showImageTooltip: !this.state.showImageTooltip
});
},
_toggleVideoTooltip: function() {
this.setState({
showVideoTooltip: !this.state.showVideoTooltip
});
},
_execCommand: function(command, arg) {
document.execCommand(command, false, arg);
},
_emitChange: function() {
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() {
var files = this.refs.imageInput.getInputDOMNode().files;
this.props.onImageUpload(files, (url) => {
this.refs.editor.getDOMNode().focus();
this._execCommand('insertImage', url);
});
this._toggleImageTooltip();
},
_onVideoSubmit: function() {
var files = this.refs.videoInput.getInputDOMNode().files;
this.props.onVideoUpload(files, (url) => {
this.refs.editor.getDOMNode().focus();
this._execCommand('insertHTML', `<figure><video controls width="400" src="${url}">Your browser does not support HTML5 video.</video></figure>`);
});
this._toggleVideoTooltip();
},
render: function() {
// 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 : (
<Overlay
show={this.state.showImageTooltip}
onHide={() => this.setState({ showImageTooltip: false })}
placement={imgTooltipPlacement}
container={this}
rootClose={true}
target={() => this.refs.imgUploadBtn.getDOMNode()} >
<Popover id="popover" title="Image Upload" >
<Input type="file"
ref="imageInput"
name="file"
label="Select an image to upload"
/>
<ButtonInput type="submit" value="Submit" onClick={this._onImageSubmit}/>
</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={() => this.refs.videoUploadBtn.getDOMNode()} >
<Popover id="popover" title="Video Upload" >
<Input type="file"
ref="videoInput"
name="file"
label="Select a video to upload"
/>
<ButtonInput type="submit" value="Submit" onClick={this._onVideoSubmit}/>
</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 onClick={this._execCommand.bind(this, 'createLink')}>
<i className="fa fa-link"></i>
</Button>
</ButtonGroup>
</div>
<div
ref="editor"
className="form-control"
{...this.props}
contentEditable="true"
dangerouslySetInnerHTML={{__html: this.state.html}}
onBlur={(e) => {
if (e.relatedTarget.id === 'imgUploadBtn' || e.relatedTarget.id === 'videoUploadBtn') {
e.preventDefault();
this.refs.editor.getDOMNode().focus();
}
}}
onInput={this._emitChange}/>
</div>
);
}
});