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

check functions for equality and clone props and content properties #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 23 additions & 5 deletions lib/components/TinyMCE.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { findDOMNode } from 'react-dom';
import isEqual from 'lodash/lang/isEqual';
import clone from 'lodash/lang/clone';
import cloneDeep from 'lodash/lang/cloneDeep';
import uuid from '../helpers/uuid';
import ucFirst from '../helpers/ucFirst';

Expand Down Expand Up @@ -51,12 +52,19 @@ const TinyMCE = React.createClass({
},

componentDidMount() {
const config = clone(this.props.config);
this._init(config);
this._init(this.props.config, this.props.content);
},

componentWillReceiveProps(nextProps) {
if (!isEqual(this.props.config, nextProps.config)) {
if (!isEqual(this.props.config, nextProps.config, function(c1, c2) {
if(typeof c1 === 'function' && typeof c2 === 'function') {
return c1.toString() === c2.toString();
}
if(c1.hasOwnProperty('items') && c2.hasOwnProperty('items')) {

Choose a reason for hiding this comment

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

the check for items is possibly unnecessary as the config is cloned before given to tinymce so the items properties are not modified anymore

return c1.length == c2.length;
}
}))
{
this._init(nextProps.config, nextProps.content);
}
if (!isEqual(this.props.id, nextProps.id)) {
Expand All @@ -67,7 +75,14 @@ const TinyMCE = React.createClass({
shouldComponentUpdate(nextProps) {
return (
!isEqual(this.props.content, nextProps.content) ||
!isEqual(this.props.config, nextProps.config)
!isEqual(this.props.config, nextProps.config, function(c1, c2) {
if(typeof c1 === 'function' && typeof c2 === 'function') {
return c1.toString() === c2.toString();
}
if(c1.hasOwnProperty('items') && c2.hasOwnProperty('items')) {
return c1.length == c2.length;
}
})
);
},

Expand All @@ -91,11 +106,14 @@ const TinyMCE = React.createClass({
);
},

_init(config, content) {
_init(configProp, contentProp) {
if (this._isInit) {
this._remove();
}

var config = cloneDeep(configProp);
var content = clone(contentProp);

// hide the textarea that is me so that no one sees it
findDOMNode(this).style.hidden = 'hidden';

Expand Down