Skip to content

update ckeditor when prop changes #37

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

Merged
merged 1 commit into from
Aug 6, 2018
Merged
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
34 changes: 18 additions & 16 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ class Example extends React.Component {
this.state = {
content: "Hello World"
};

//setInterval(this.setContent.bind(this), 1000)
this.setContent = this.setContent.bind(this)
}

//------ Test for race condition ------ //
// setContent(){
// console.log("Setting content");
// this.setState({
// content: "Hello World " + Math.random()
// })
// }
setContent(){
console.log("Setting content");
this.setState({
content: "Hello World " + Math.random()
})
}

onChange(evt){
console.log("onChange fired with event info: ",evt, "and data: ",evt.editor.getData());
Expand All @@ -36,14 +35,17 @@ class Example extends React.Component {

render() {
return (
<CKEditor
content={this.state.content}
events={{
blur: this.onBlur,
afterPaste: this.afterPaste,
change: this.onChange
}}
/>
<div>
<button onClick={() => this.setContent()} children='Set content' />
<CKEditor
content={this.state.content}
events={{
blur: this.onBlur,
afterPaste: this.afterPaste,
change: this.onChange
}}
/>
</div>
);
}
}
Expand Down
32 changes: 19 additions & 13 deletions src/ckeditor.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from "react";
import PropTypes from "prop-types";
import ReactDOM from "react-dom";
import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
const loadScript = require('load-script');

var defaultScriptUrl = "https://cdn.ckeditor.com/4.6.2/standard/ckeditor.js";
var defaultScriptUrl = 'https://cdn.ckeditor.com/4.6.2/standard/ckeditor.js';

/**
* @author codeslayer1
Expand All @@ -18,20 +18,26 @@ class CKEditor extends React.Component {

//State initialization
this.state = {
isScriptLoaded: this.props.isScriptLoaded,
config: this.props.config
isScriptLoaded: props.isScriptLoaded
};
}

//load ckeditor script as soon as component mounts if not already loaded
componentDidMount() {
if(!this.props.isScriptLoaded){
if (!this.state.isScriptLoaded) {
loadScript(this.props.scriptUrl, this.onLoad);
}else{
} else {
this.onLoad();
}
}

componentWillReceiveProps(props) {
const editor = this.editorInstance;
if (editor && editor.getData() !== props.content) {
editor.setData(props.content);
}
}

componentWillUnmount() {
this.unmounting = true;
}
Expand All @@ -44,18 +50,18 @@ class CKEditor extends React.Component {
});

if (!window.CKEDITOR) {
console.error("CKEditor not found");
console.error('CKEditor not found');
return;
}

this.editorInstance = window.CKEDITOR.appendTo(
ReactDOM.findDOMNode(this),
this.state.config,
this.props.config,
this.props.content
);

//Register listener for custom events if any
for(var event in this.props.events){
for (var event in this.props.events) {
var eventHandler = this.props.events[event];

this.editorInstance.on(event, eventHandler);
Expand All @@ -68,11 +74,11 @@ class CKEditor extends React.Component {
}

CKEditor.defaultProps = {
content: "",
content: '',
config: {},
isScriptLoaded: false,
scriptUrl: defaultScriptUrl,
activeClass: "",
activeClass: '',
events: {}
};

Expand Down