-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathmain.jsx
326 lines (283 loc) · 11.3 KB
/
main.jsx
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
(function () {
'use strict';
/**
* The main editor UI class manages a hierarchy of widgets (toolbars and buttons).
*
* @uses WidgetExclusive
* @uses WidgetFocusManager
*
* @class UI
*/
var UI = React.createClass({
mixins: [AlloyEditor.WidgetExclusive, AlloyEditor.WidgetFocusManager],
// Allows validating props being passed to the component.
propTypes: {
/**
* Localized messages for live aria updates. Should include the following messages:
* - noToolbar: Notification for no available toolbar in the editor.
* - oneToolbar: Notification for just one available toolbar in the editor.
* - manyToolbars: Notification for more than one available toolbar in the editor.
*
* @property {Object} ariaUpdates
*/
ariaUpdates: React.PropTypes.object,
/**
* The editor instance where the component is being used.
*
* @property {Object} editor
*/
editor: React.PropTypes.object.isRequired,
/**
* The delay (ms), after which key or mouse events will be processed.
*
* @property {Number} eventsDelay
*/
eventsDelay: React.PropTypes.number,
/**
* The toolbars configuration for this editor instance
*
* @property {Object} toolbars
*/
toolbars: React.PropTypes.object.isRequired
},
/**
* Lifecycle. Invoked once before the component is mounted.
*
* @method getInitialState
*/
getInitialState: function() {
return {
hidden: false
};
},
/**
* Lifecycle. Returns the default values of the properties used in the widget.
*
* @method getDefaultProps
* @return {Object} The default properties.
*/
getDefaultProps: function() {
return {
circular: true,
descendants: '[class*=toolbar-]',
eventsDelay: 0,
keys: {
next: 9
}
};
},
/**
* Lifecycle. Invoked once, only on the client, immediately after the initial rendering occurs.
*
* @method componentDidMount
*/
componentDidMount: function () {
var editor = this.props.editor.get('nativeEditor');
editor.on('editorInteraction', this._onEditorInteraction, this);
editor.on('actionPerformed', this._onActionPerformed, this);
editor.on('key', this._onEditorKey, this);
// Set up events for hiding the UI when user stops interacting with the editor.
// This may happen when he just clicks outside of the editor. However,
// this does not include a situation when he clicks on some button, part of
// editor's UI.
// It is not easy to debounce _setUIHidden on click, because if we
// debounce it, when the handler is being invoked, the target may be no more part
// of the editor's UI - onActionPerformed causes re-render.
this._clickListener = function (event) {
this._setUIHidden(event.target);
}.bind(this);
this._keyDownListener = CKEDITOR.tools.debounce(function(event) {
this._setUIHidden(document.activeElement);
}, this.props.eventsDelay, this);
editor.once('contentDom', function() {
document.addEventListener('click', this._clickListener);
document.addEventListener('keydown', this._keyDownListener);
}.bind(this));
},
/**
* Lifecycle. Invoked immediately after the component's updates are flushed to the DOM.
* Fires 'ariaUpdate' event passing ARIA related messages.
*
* @method componentDidUpdate
*/
componentDidUpdate: function (prevProps, prevState) {
var domNode = React.findDOMNode(this);
if (domNode) {
this.props.editor.get('nativeEditor').fire('ariaUpdate', {
message: this._getAvailableToolbarsMessage(domNode)
});
}
},
_getAriaUpdateTemplate: function(ariaUpdate) {
if (!this._ariaUpdateTemplates) {
this._ariaUpdateTemplates = {};
}
if (!this._ariaUpdateTemplates[ariaUpdate]) {
this._ariaUpdateTemplates[ariaUpdate] = new CKEDITOR.template(this._getAriaUpdates()[ariaUpdate]);
}
return this._ariaUpdateTemplates[ariaUpdate];
},
/**
* Returns the templates for ARIA messages.
*
* @protected
* @method _getAriaUpdates
* @return {Object} ARIA relates messages. Default:
* {
* noToolbar: AlloyEditor.Strings.ariaUpdateNoToolbar,
* oneToolbar: AlloyEditor.Strings.ariaUpdateOneToolbar,
* manyToolbars: AlloyEditor.Strings.ariaUpdateManyToolbars
* }
*/
_getAriaUpdates: function() {
return this.props.ariaUpdates || {
noToolbar: AlloyEditor.Strings.ariaUpdateNoToolbar,
oneToolbar: AlloyEditor.Strings.ariaUpdateOneToolbar,
manyToolbars: AlloyEditor.Strings.ariaUpdateManyToolbars
};
},
/**
* Returns an ARIA message which represents the number of currently available toolbars.
*
* @method _getAvailableToolbarsMessage
* @protected
* @param {CKEDITOR.dom.element} domNode The DOM node from which the available toolbars will be retrieved.
* @return {String} The ARIA message for the number of available toolbars
*/
_getAvailableToolbarsMessage: function(domNode) {
var toolbarsNodeList = domNode.querySelectorAll('[role="toolbar"]');
if (!toolbarsNodeList.length) {
return this._getAriaUpdates().noToolbar;
} else {
var toolbarNames = Array.prototype.slice.call(toolbarsNodeList).map(function(toolbar) {
return toolbar.getAttribute('aria-label');
});
var ariaUpdate = toolbarNames.length === 1 ? 'oneToolbar' : 'manyToolbars';
return this._getAriaUpdateTemplate(ariaUpdate).output({
toolbars: toolbarNames.join(',').replace(/,([^,]*)$/, ' and ' + '$1')
});
}
},
/**
* Lifecycle. Invoked immediately before a component is unmounted from the DOM.
*
* @method componentWillUnmount
*/
componentWillUnmount: function() {
if (this._clickListener) {
document.removeEventListener('click', this._clickListener);
}
if (this._keyDownListener) {
this._keyDownListener.detach();
document.removeEventListener('keydown', this._keyDownListener);
}
},
/**
* Lifecycle. Renders the UI of the editor. This may include several toolbars and buttons.
* The editor's UI also takes care of rendering the items in exclusive mode.
*
* @method render
* @return {Object} The content which should be rendered.
*/
render: function() {
if (this.state.hidden) {
return null;
}
var toolbars = Object.keys(this.props.toolbars).map(function(toolbar) {
return AlloyEditor.Toolbars[toolbar] || window[toolbar];
});
toolbars = this.filterExclusive(toolbars).map(function(toolbar) {
var props = this.mergeExclusiveProps({
config: this.props.toolbars[toolbar.key],
editor: this.props.editor,
editorEvent: this.state.editorEvent,
key: toolbar.key,
onDismiss: this._onDismissToolbarFocus,
selectionData: this.state.selectionData
}, toolbar.key);
return React.createElement(toolbar, props);
}.bind(this));
return (
<div className="toolbars" onKeyDown={this.handleKey}>
{toolbars}
</div>
);
},
/**
* Listener to the editor's `actionPerformed` event. Sets state and redraws the UI of the editor.
*
* @protected
* @method _onActionPerformed
* @param {SynteticEvent} event The provided event
*/
_onActionPerformed: function(event) {
var editor = this.props.editor.get('nativeEditor');
editor.focus();
this.setState({
itemExclusive: null,
selectionData: editor.getSelectionData()
});
},
/**
* Executed when a dismiss key is pressed over a toolbar to return the focus to the editor.
*
* @protected
* @method _onDismissToolbarFocus
*/
_onDismissToolbarFocus: function() {
var editor = this.props.editor.get('nativeEditor');
editor.focus();
},
/**
* Listener to the editor's `userInteraction` event. Retrieves the data about the user selection and
* provides it via component's state property.
*
* @protected
* @method _onEditorInteraction
* @param {SynteticEvent} event The provided event
*/
_onEditorInteraction: function(event) {
this.setState({
editorEvent: event,
hidden: false,
itemExclusive: null,
selectionData: event.data.selectionData
});
},
/**
* Focuses on the active toolbar when the combination ALT+F10 is pressed inside the editor.
*
* @protected
* @method _onEditorKey
*/
_onEditorKey: function(event) {
var nativeEvent = event.data.domEvent.$;
if (nativeEvent.altKey && nativeEvent.keyCode === 121) {
this.focus();
}
},
/**
* Checks if the target with which the user interacted is part of editor's UI or it is
* the editable area. If none of these, sets the state of editor's UI to be hidden.
*
* @protected
* @method _setUIHidden
* @param {DOMElement} target The DOM element with which user interacted lastly.
*/
_setUIHidden: function(target) {
var domNode = React.findDOMNode(this);
if (domNode) {
var editable = this.props.editor.get('nativeEditor').editable();
var targetNode = new CKEDITOR.dom.node(target);
var res = (editable.$ === target) || editable.contains(targetNode) ||
(new CKEDITOR.dom.element(domNode)).contains(targetNode);
if (!res) {
this.setState({
hidden: true
});
}
}
}
});
AlloyEditor.UI = UI;
}());