-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathckeditor.jsx
342 lines (298 loc) · 8.95 KB
/
ckeditor.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/
import React from 'react';
import PropTypes from 'prop-types';
import EditorWatchdog from '@ckeditor/ckeditor5-watchdog/src/editorwatchdog';
import uid from '@ckeditor/ckeditor5-utils/src/uid';
import { ContextWatchdogContext } from './ckeditorcontext.jsx';
import ContextWatchdog from '@ckeditor/ckeditor5-watchdog/src/contextwatchdog';
export default class CKEditor extends React.Component {
constructor( props ) {
super( props );
// After mounting the editor, the variable will contain a reference to the created editor.
// @see: https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editor-Editor.html
this.domContainer = React.createRef();
/**
* An instance of EditorWatchdog or an instance of EditorWatchdog-like adapter for ContextWatchdog.
*
* @type {module:watchdog/watchdog~Watchdog|EditorWatchdogAdapter}
*/
this.watchdog = null;
}
/**
* An editor instance.
*
* @type {module:core/editor/editor~Editor|null}
*/
get editor() {
if ( !this.watchdog ) {
return null;
}
return this.watchdog.editor;
}
/**
* The CKEditor component should not be updated by React itself.
* However, if the component identifier changes, the whole structure should be created once again.
*
* @param {Object} nextProps
* @return {Boolean}
*/
shouldComponentUpdate( nextProps ) {
if ( !this.editor ) {
return false;
}
// Only when the component identifier changes the whole structure should be re-created once again.
if ( nextProps.id !== this.props.id ) {
return true;
}
if ( this._shouldUpdateEditor( nextProps ) ) {
this.editor.setData( nextProps.data );
}
if ( 'disabled' in nextProps ) {
this.editor.isReadOnly = nextProps.disabled;
}
return false;
}
/**
* Initialize the editor when the component is mounted.
*/
componentDidMount() {
this._initializeEditor();
}
/**
* Re-render the entire component once again. The old editor will be destroyed and the new one will be created.
*/
componentDidUpdate() {
this._destroyEditor();
this._initializeEditor();
}
/**
* Destroy the editor before unmounting the component.
*/
componentWillUnmount() {
this._destroyEditor();
}
/**
* Render a <div> element which will be replaced by CKEditor.
*
* @return {JSX.Element}
*/
render() {
return (
<div ref={ this.domContainer }></div>
);
}
/**
* Initializes the editor by creating a proper watchdog and initializing it with the editor's configuration.
*
* @private
*/
_initializeEditor() {
if ( this.context instanceof ContextWatchdog ) {
this.watchdog = new EditorWatchdogAdapter( this.context );
} else {
this.watchdog = new CKEditor._EditorWatchdog( this.props.editor );
}
this.watchdog.setCreator( ( el, config ) => this._createEditor( el, config ) );
this.watchdog.on( 'error', ( _, { error, causesRestart } ) => {
this.props.onError( error, { phase: 'runtime', willEditorRestart: causesRestart } );
} );
this.watchdog.create( this.domContainer.current, this._getConfig() )
.catch( error => this.props.onError( error, { phase: 'initialization', willEditorRestart: false } ) );
}
/**
* Creates an editor from the element and configuration.
*
* @private
* @param {HTMLElement} element The source element.
* @param {Object} config CKEditor 5 editor configuration.
* @returns {Promise}
*/
_createEditor( element, config ) {
return this.props.editor.create( element, config )
.then( editor => {
if ( 'disabled' in this.props ) {
editor.isReadOnly = this.props.disabled;
}
const modelDocument = editor.model.document;
const viewDocument = editor.editing.view.document;
modelDocument.on( 'change:data', event => {
/* istanbul ignore else */
if ( this.props.onChange ) {
this.props.onChange( event, editor );
}
} );
viewDocument.on( 'focus', event => {
/* istanbul ignore else */
if ( this.props.onFocus ) {
this.props.onFocus( event, editor );
}
} );
viewDocument.on( 'blur', event => {
/* istanbul ignore else */
if ( this.props.onBlur ) {
this.props.onBlur( event, editor );
}
} );
// The `onReady` callback should be fired once the `editor` property
// can be reached from the `<CKEditor>` component.
// Ideally this part should be moved to the watchdog item creator listeners.
setTimeout( () => {
if ( this.props.onReady ) {
this.props.onReady( this.editor );
}
} );
return editor;
} );
}
/**
* Destroys the editor by destroying the watchdog.
*
* @private
*/
_destroyEditor() {
// It may happen during the tests that the watchdog instance is not assigned before destroying itself. See: #197.
/* istanbul ignore next */
if ( !this.watchdog ) {
return;
}
this.watchdog.destroy();
this.watchdog = null;
}
/**
* Returns true when the editor should be updated.
*
* @private
* @param {Object} nextProps React's properties.
* @returns {Boolean}
*/
_shouldUpdateEditor( nextProps ) {
// Check whether `nextProps.data` is equal to `this.props.data` is required if somebody defined the `#data`
// property as a static string and updated a state of component when the editor's content has been changed.
// If we avoid checking those properties, the editor's content will back to the initial value because
// the state has been changed and React will call this method.
if ( this.props.data === nextProps.data ) {
return false;
}
// We should not change data if the editor's content is equal to the `#data` property.
if ( this.editor.getData() === nextProps.data ) {
return false;
}
return true;
}
/**
* Returns the editor configuration.
*
* @private
* @return {Object}
*/
_getConfig() {
if ( this.props.data && this.props.config.initialData ) {
console.warn(
'Editor data should be provided either using `config.initialData` or `data` properties. ' +
'The config property is over the data value and the first one will be used when specified both.'
);
}
// Merge two possible ways of providing data into the `config.initialData` field.
return {
...this.props.config,
initialData: this.props.config.initialData || this.props.data || ''
};
}
}
/**
* An adapter aligning the context watchdog API to the editor watchdog API for easier usage.
*/
class EditorWatchdogAdapter {
/**
* @param {ContextWatchdog} contextWatchdog The context watchdog instance that will be wrapped into wditor watchdog API.
*/
constructor( contextWatchdog ) {
this._contextWatchdog = contextWatchdog;
/**
* A unique id for the adapter to distinguish editor items when using the context watchdog API.
*
* @type {String}
*/
this._id = uid();
}
/**
* @param {Function} creator A watchdog's editor creator function.
*/
setCreator( creator ) {
this._creator = creator;
}
/**
* Adds an editor configuration to the context watchdog registry. Creates an instance of it.
*
* @param {HTMLElement | string} sourceElementOrData A source element or data for the new editor.
* @param {Object} config CKEditor 5 editor config.
* @returns {Promise}
*/
create( sourceElementOrData, config ) {
return this._contextWatchdog.add( {
sourceElementOrData,
config,
creator: this._creator,
id: this._id,
type: 'editor'
} );
}
/**
* Creates a listener that is attached to context watchdog's item and run when the context watchdog fires.
* Currently works only for the `error` event.
*
* @param {String} _
* @param {Function} callback
*/
on( _, callback ) {
// Assume that the event name was error.
this._contextWatchdog.on( 'itemError', ( _, { itemId, causesRestart, error } ) => {
if ( itemId === this._id ) {
callback( null, { error, causesRestart } );
}
} );
}
/**
* Removes the editor from registered editors and destroys it.
*/
destroy() {
this._contextWatchdog.remove( this._id );
}
/**
* An editor instance.
*/
get editor() {
return this._contextWatchdog.getItem( this._id );
}
}
CKEditor.contextType = ContextWatchdogContext;
// Properties definition.
CKEditor.propTypes = {
editor: PropTypes.func.isRequired,
data: PropTypes.string,
config: PropTypes.object,
onChange: PropTypes.func,
onReady: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
onError: PropTypes.func,
disabled: PropTypes.bool,
onInit: ( props, propName ) => {
if ( props[ propName ] ) {
return new Error(
'The "onInit" property is not supported anymore by the CKEditor component. Use the "onReady" property instead.'
);
}
}
};
// Default values for non-required properties.
CKEditor.defaultProps = {
config: {},
onError: ( error, details ) => console.error( error, details )
};
// Store the API in the static property to easily overwrite it in tests.
// Too bad dependency injection does not work in Webpack + ES 6 (const) + Babel.
CKEditor._EditorWatchdog = EditorWatchdog;