-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathTinyMCE.js
More file actions
163 lines (141 loc) · 4.67 KB
/
TinyMCE.js
File metadata and controls
163 lines (141 loc) · 4.67 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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';
// Include all of the Native DOM and custom events from:
// https://github.com/tinymce/tinymce/blob/master/tools/docs/tinymce.Editor.js#L5-L12
const EVENTS = [
'focusin', 'focusout', 'click', 'dblclick', 'mousedown', 'mouseup',
'mousemove', 'mouseover', 'beforepaste', 'paste', 'cut', 'copy',
'selectionchange', 'mouseout', 'mouseenter', 'mouseleave', 'keydown',
'keypress', 'keyup', 'contextmenu', 'dragend', 'dragover', 'draggesture',
'dragdrop', 'drop', 'drag', 'BeforeRenderUI', 'SetAttrib', 'PreInit',
'PostRender', 'init', 'deactivate', 'activate', 'NodeChange',
'BeforeExecCommand', 'ExecCommand', 'show', 'hide', 'ProgressState',
'LoadContent', 'SaveContent', 'BeforeSetContent', 'SetContent',
'BeforeGetContent', 'GetContent', 'VisualAid', 'remove', 'submit', 'reset',
'BeforeAddUndo', 'AddUndo', 'change', 'undo', 'redo', 'ClearUndos',
'ObjectSelected', 'ObjectResizeStart', 'ObjectResized', 'PreProcess',
'PostProcess', 'focus', 'blur', 'dirty'
];
// Note: because the capitalization of the events is weird, we're going to get
// some inconsistently-named handlers, for example compare:
// 'onMouseleave' and 'onNodeChange'
const HANDLER_NAMES = EVENTS.map((event) => {
return 'on' + ucFirst(event);
});
const TinyMCE = React.createClass({
displayName: 'TinyMCE',
propTypes: {
config: React.PropTypes.object,
content: React.PropTypes.string,
id: React.PropTypes.string,
className: React.PropTypes.string
},
getDefaultProps() {
return {
config: {},
content: ''
};
},
componentWillMount() {
this.id = this.id || this.props.id || uuid();
},
componentDidMount() {
this._init(this.props.config, this.props.content);
},
componentWillReceiveProps(nextProps) {
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')) {
return c1.length == c2.length;
}
}))
{
this._init(nextProps.config, nextProps.content);
}
if (!isEqual(this.props.id, nextProps.id)) {
this.id = nextProps.id;
}
},
shouldComponentUpdate(nextProps) {
return (
!isEqual(this.props.content, nextProps.content) ||
!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;
}
})
);
},
componentWillUnmount() {
this._remove();
},
render() {
return this.props.config.inline ? (
<div
id={this.id}
className={this.props.className}
dangerouslySetInnerHTML={{__html: this.props.content}}
/>
) : (
<textarea
id={this.id}
className={this.props.className}
defaultValue={this.props.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';
const setupCallback = config.setup;
const hasSetupCallback = (typeof setupCallback === 'function');
config.selector = '#' + this.id;
config.setup = (editor) => {
EVENTS.forEach((event, index) => {
const handler = this.props[HANDLER_NAMES[index]];
if (typeof handler !== 'function') return;
editor.on(event, (e) => {
// native DOM events don't have access to the editor so we pass it here
handler(e, editor);
});
});
// need to set content here because the textarea will still have the
// old `this.props.content`
if (content) {
editor.on('init', () => {
editor.setContent(content);
});
}
if (hasSetupCallback) {
setupCallback(editor);
}
};
tinymce.init(config);
findDOMNode(this).style.hidden = '';
this._isInit = true;
},
_remove() {
tinymce.EditorManager.execCommand('mceRemoveEditor', true, this.id);
this._isInit = false;
}
});
// add handler propTypes
HANDLER_NAMES.forEach((name) => {
TinyMCE.propTypes[name] = React.PropTypes.func;
});
module.exports = TinyMCE;