Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IE11 support #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/react-contenteditable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var OBSERVER_CONFIG = { childList: true, subtree: true, characterData: true };

var ContentEditable = function (_React$Component) {
_inherits(ContentEditable, _React$Component);

Expand All @@ -48,12 +50,21 @@ var ContentEditable = function (_React$Component) {
ref: function ref(e) {
return _this2.htmlEl = e;
},
onInput: this.emitChange,
onBlur: this.props.onBlur || this.emitChange,
contentEditable: !this.props.disabled,
dangerouslySetInnerHTML: { __html: html }
}), this.props.children);
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var _this3 = this;

this.observer = new MutationObserver(function (mutations) {
mutations.forEach(_this3.emitChange);
});
this.observer.observe(this.htmlEl, OBSERVER_CONFIG);
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps) {
Expand All @@ -79,13 +90,18 @@ var ContentEditable = function (_React$Component) {
this.htmlEl.innerHTML = this.props.html;
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.observer.disconnect();
}
}, {
key: 'emitChange',
value: function emitChange(evt) {
if (!this.htmlEl) return;
var html = this.htmlEl.innerHTML;
if (this.props.onChange && html !== this.lastHtml) {
evt.target = { value: html };
evt.target.value = html;
this.props.onChange(evt);
}
this.lastHtml = html;
Expand Down
16 changes: 14 additions & 2 deletions src/react-contenteditable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';

const OBSERVER_CONFIG = { childList: true, subtree: true, characterData: true };

export default class ContentEditable extends React.Component {
constructor() {
super();
Expand All @@ -14,14 +16,20 @@ export default class ContentEditable extends React.Component {
{
...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);
}

componentDidMount() {
this.observer = new MutationObserver((mutations) => {
mutations.forEach(this.emitChange);
});
this.observer.observe(this.htmlEl, OBSERVER_CONFIG);
}

shouldComponentUpdate(nextProps) {
// 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.
Expand All @@ -46,11 +54,15 @@ export default class ContentEditable extends React.Component {
}
}

componentWillUnmount() {
this.observer.disconnect();
}

emitChange(evt) {
if (!this.htmlEl) return;
var html = this.htmlEl.innerHTML;
if (this.props.onChange && html !== this.lastHtml) {
evt.target = { value: html };
evt.target.value = html;
this.props.onChange(evt);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to somehow pass the html to onChange!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it enough to get the whole MutationRecord (=evt) in onChange? The element's innerHtml is then accessible via evt.target.innerHtml.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on the node you're getting. In my case I get a text node so evt.target.innerHTML isn't a thing. Drop document.createTextNode('') in your console to see what's there.

}
this.lastHtml = html;
Expand Down