-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathckeditor.js
More file actions
97 lines (79 loc) · 2.12 KB
/
ckeditor.js
File metadata and controls
97 lines (79 loc) · 2.12 KB
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
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";
/**
* @author codeslayer1
* @description CKEditor component to render a CKEditor textarea with defined configs and all CKEditor events handler
*/
class CKEditor extends React.Component {
constructor(props) {
super(props);
//Bindings
this.onLoad = this.onLoad.bind(this);
//State initialization
this.state = {
isScriptLoaded: this.props.isScriptLoaded,
config: this.props.config
};
}
//load ckeditor script as soon as component mounts if not already loaded
componentDidMount() {
if(!this.props.isScriptLoaded){
loadScript(this.props.scriptUrl, this.onLoad);
}else{
this.onLoad();
}
}
componentWillUnmount() {
this.unmounting = true;
}
reloadEditor() {
clearTimeout(this.timeout);
if (!this.editorInstance.ui.editor.loaded) {
loadScript(this.props.scriptUrl, this.onLoad);
}
}
onLoad() {
if (this.unmounting) return;
this.setState({
isScriptLoaded: true
});
if (!window.CKEDITOR) {
console.error("CKEditor not found");
return;
}
this.editorInstance = window.CKEDITOR.appendTo(
ReactDOM.findDOMNode(this),
this.state.config,
this.props.content
);
//Register listener for custom events if any
for(var event in this.props.events){
var eventHandler = this.props.events[event];
this.editorInstance.on(event, eventHandler);
}
this.timeout = setTimeout(this.reloadEditor.bind(this), 3000);
}
render() {
return <div className={this.props.activeClass} />;
}
}
CKEditor.defaultProps = {
content: "",
config: {},
isScriptLoaded: false,
scriptUrl: defaultScriptUrl,
activeClass: "",
events: {}
};
CKEditor.propTypes = {
content: PropTypes.any,
config: PropTypes.object,
isScriptLoaded: PropTypes.bool,
scriptUrl: PropTypes.string,
activeClass: PropTypes.string,
events: PropTypes.object
};
export default CKEditor;