/
react-contenteditable.js
77 lines (65 loc) · 2.17 KB
/
react-contenteditable.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
import React from 'react';
import deepEqual from 'fast-deep-equal';
let stripNbsp = str => str && str.replace(/ |\u202F|\u00A0/g, ' ');
export default class ContentEditable extends React.Component {
constructor(props) {
super();
this.emitChange = this.emitChange.bind(this);
this.lastHtml = props.html;
}
render() {
var { tagName, html, ...props } = this.props;
return React.createElement(
tagName || 'div',
{
...props,
ref: (e) => this.htmlEl = e,
onInput: this.emitChange,
onBlur: this.props.onBlur || this.emitChange,
contentEditable: !this.props.disabled,
dangerouslySetInnerHTML: { __html: html }
},
this.props.children);
}
shouldComponentUpdate(nextProps) {
let { props, htmlEl } = this;
// We need not rerender if the change of props simply reflects the user's edits.
// Rerendering in this case would make the cursor/caret jump
// Rerender if there is no element yet... (somehow?)
if (!htmlEl) {
return true;
}
// ...or if html really changed... (programmatically, not by user edit)
if (
stripNbsp(nextProps.html) !== stripNbsp(htmlEl.innerHTML) &&
nextProps.html !== props.html
) {
return true;
}
let propNames = ['style', 'className', 'disabled', 'tagName'];
// Handle additional properties
return !propNames.every(name => deepEqual(props[name], nextProps[name]));
}
componentDidUpdate() {
if (this.htmlEl && this.props.html !== this.htmlEl.innerHTML) {
// Perhaps React (whose VDOM gets outdated because we often prevent
// rerendering) did not update the DOM. So we update it manually now.
this.htmlEl.innerHTML = this.lastHtml = this.props.html;
}
}
emitChange(evt) {
if (!this.htmlEl) return;
var html = this.htmlEl.innerHTML;
if (this.props.onChange && html !== this.lastHtml) {
// Clone event with Object.assign to avoid
// "Cannot assign to read only property 'target' of object"
var evt = Object.assign({}, evt, {
target: {
value: html
}
});
this.props.onChange(evt);
}
this.lastHtml = html;
}
}