-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathckeditor.js
More file actions
121 lines (100 loc) · 2.96 KB
/
Copy pathckeditor.js
File metadata and controls
121 lines (100 loc) · 2.96 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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);
this.registerEventHandlers = this.registerEventHandlers.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;
if (this.editorInstance) {
this.editorInstance.removeAllListeners();
window.CKEDITOR.remove(this.editorInstance);
}
}
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 change event.
//PS - This prop is now deprecated since change event can now be directly listened via events prop.
/*
******** DEPRECATED ********
this.editorInstance.on("change", () => {
const content = this.editorInstance.getData();
//call onChange callback if present
if(this.props.onChange){
this.props.onChange(content);
}
});
*/
this.registerEventHandlers(this.props.events);
}
registerEventHandlers(events, prevEvents) {
prevEvents = prevEvents || {};
//Register listener for custom events if any
for (var event in events) {
if (events[event] !== prevEvents[event]) {
var prevEventHandler = prevEvents[event];
if (prevEventHandler) this.editorInstance.removeListener(event, prevEventHandler);
var eventHandler = events[event];
this.editorInstance.on(event, eventHandler);
}
}
}
componentWillReceiveProps(nextProps) {
if (this.editorInstance) this.registerEventHandlers(nextProps.events, this.props.events);
}
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;