-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathckeditor.tsx
437 lines (371 loc) · 13.7 KB
/
ckeditor.tsx
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* globals window */
import React from 'react';
import PropTypes, { type InferProps, type Validator } from 'prop-types';
import uid from '@ckeditor/ckeditor5-utils/src/uid';
import type { EventInfo } from '@ckeditor/ckeditor5-utils';
import type { Editor, EditorConfig } from '@ckeditor/ckeditor5-core';
import type { DocumentChangeEvent } from '@ckeditor/ckeditor5-engine';
import { EditorWatchdog, ContextWatchdog } from '@ckeditor/ckeditor5-watchdog';
import type { WatchdogConfig } from '@ckeditor/ckeditor5-watchdog/src/watchdog';
import type { EditorCreatorFunction } from '@ckeditor/ckeditor5-watchdog/src/editorwatchdog';
import { ContextWatchdogContext } from './ckeditorcontext';
const REACT_INTEGRATION_READ_ONLY_LOCK_ID = 'Lock from React integration (@ckeditor/ckeditor5-react)';
// eslint-disable-next-line @typescript-eslint/ban-types
export default class CKEditor<TEditor extends Editor> extends React.Component<Props<TEditor>, {}> {
/**
* Contains a promise that resolves when the editor destruction is finished.
*/
private editorDestructionInProgress: Promise<void> | null = null;
/**
* 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
*/
private domContainer = React.createRef<HTMLDivElement>();
/**
* An instance of EditorWatchdog or an instance of EditorWatchdog-like adapter for ContextWatchdog.
* It holds the instance of the editor under `this.watchdog.editor` if `props.disableWatchdog` is set to false.
*/
private watchdog: EditorWatchdog<TEditor> | EditorWatchdogAdapter<TEditor> | null = null;
/**
* Holds the instance of the editor if `props.disableWatchdog` is set to true.
*/
private instance: Editor | undefined | null;
constructor( props: Props<TEditor> ) {
super( props );
const { CKEDITOR_VERSION } = window;
if ( CKEDITOR_VERSION ) {
const [ major ] = CKEDITOR_VERSION.split( '.' ).map( Number );
if ( major < 37 ) {
console.warn( 'The <CKEditor> component requires using CKEditor 5 in version 37 or higher.' );
}
} else {
console.warn( 'Cannot find the "CKEDITOR_VERSION" in the "window" scope.' );
}
}
/**
* An editor instance.
*/
public get editor(): Editor | null {
if ( this.props.disableWatchdog ) {
return this.instance!;
}
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.
*/
public override shouldComponentUpdate( nextProps: Readonly<Props<TEditor>> ): boolean {
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 ( nextProps.disableWatchdog !== this.props.disableWatchdog ) {
return true;
}
if ( this._shouldUpdateEditor( nextProps ) ) {
this.editor.data.set( nextProps.data! );
}
if ( 'disabled' in nextProps ) {
if ( nextProps.disabled ) {
this.editor.enableReadOnlyMode( REACT_INTEGRATION_READ_ONLY_LOCK_ID );
} else {
this.editor.disableReadOnlyMode( REACT_INTEGRATION_READ_ONLY_LOCK_ID );
}
}
return false;
}
/**
* Initialize the editor when the component is mounted.
*/
public override async componentDidMount(): Promise<void> {
await this._initializeEditor();
}
/**
* Re-render the entire component once again. The old editor will be destroyed and the new one will be created.
*/
public override async componentDidUpdate(): Promise<void> {
await this._destroyEditor();
await this._initializeEditor();
}
/**
* Destroy the editor before unmounting the component.
*/
public override async componentWillUnmount(): Promise<void> {
await this._destroyEditor();
}
/**
* Render a <div> element which will be replaced by CKEditor.
*/
public override render(): React.ReactNode {
return (
<div ref={ this.domContainer }></div>
);
}
/**
* Initializes the editor by creating a proper watchdog and initializing it with the editor's configuration.
*/
private async _initializeEditor(): Promise<unknown> {
await this.editorDestructionInProgress;
if ( this.props.disableWatchdog ) {
this.instance = await this._createEditor( this.domContainer.current!, this._getConfig() );
return;
}
/* istanbul ignore next */
if ( this.watchdog ) {
return;
}
if ( this.context instanceof ContextWatchdog ) {
this.watchdog = new EditorWatchdogAdapter( this.context );
} else {
this.watchdog = new CKEditor._EditorWatchdog( this.props.editor, this.props.watchdogConfig );
}
this.watchdog.setCreator( ( el, config ) => this._createEditor( el as any, config ) );
this.watchdog.on( 'error', ( _, { error, causesRestart } ) => {
const onError = this.props.onError || console.error;
onError( error, { phase: 'runtime', willEditorRestart: causesRestart } );
} );
await this.watchdog
.create( this.domContainer.current!, this._getConfig() )
.catch( error => {
const onError = this.props.onError || console.error;
onError( error, { phase: 'initialization', willEditorRestart: false } );
} );
}
/**
* Creates an editor from the element and configuration.
*
* @param element The source element.
* @param config CKEditor 5 editor configuration.
*/
private _createEditor( element: HTMLElement | string | Record<string, string>, config: EditorConfig ): Promise<TEditor> {
return this.props.editor.create( element as HTMLElement, config )
.then( editor => {
if ( 'disabled' in this.props ) {
// Switch to the read-only mode if the `[disabled]` attribute is specified.
/* istanbul ignore else */
if ( this.props.disabled ) {
editor.enableReadOnlyMode( REACT_INTEGRATION_READ_ONLY_LOCK_ID );
}
}
const modelDocument = editor.model.document;
const viewDocument = editor.editing.view.document;
modelDocument.on<DocumentChangeEvent>( '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( editor );
}
} );
return editor;
} );
}
/**
* Destroys the editor by destroying the watchdog.
*/
private async _destroyEditor(): Promise<void> {
this.editorDestructionInProgress = new Promise<void>( resolve => {
// It may happen during the tests that the watchdog instance is not assigned before destroying itself. See: #197.
//
// Additionally, we need to find a way to detect if the whole context has been destroyed. As `componentWillUnmount()`
// could be fired by <CKEditorContext /> and <CKEditor /> at the same time, this `setTimeout()` makes sure
// that <CKEditorContext /> component will be destroyed first, so during the code execution
// the `ContextWatchdog#state` would have a correct value. See `EditorWatchdogAdapter#destroy()` for more information.
/* istanbul ignore next */
setTimeout( async () => {
if ( this.watchdog ) {
await this.watchdog.destroy();
this.watchdog = null;
return resolve();
}
if ( this.instance ) {
await this.instance.destroy();
this.instance = null;
return resolve();
}
resolve();
} );
} );
}
/**
* Returns true when the editor should be updated.
*
* @param nextProps React's properties.
*/
private _shouldUpdateEditor( nextProps: Readonly<Props<TEditor>> ): boolean {
// 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!.data.get() === nextProps.data ) {
return false;
}
return true;
}
/**
* Returns the editor configuration.
*/
private _getConfig(): EditorConfig {
const config = this.props.config || {};
if ( this.props.data && config.initialData ) {
console.warn(
'Editor data should be provided either using `config.initialData` or `content` property. ' +
'The config value takes precedence over `content` property and will be used when both are specified.'
);
}
// Merge two possible ways of providing data into the `config.initialData` field.
return {
...config,
initialData: config.initialData || this.props.data || ''
};
}
public static override contextType = ContextWatchdogContext;
// Properties definition.
public static propTypes = {
editor: PropTypes.func.isRequired as unknown as Validator<{ create( ...args: any ): Promise<any> }>,
data: PropTypes.string,
config: PropTypes.object,
disableWatchdog: PropTypes.bool,
watchdogConfig: PropTypes.object,
onChange: PropTypes.func,
onReady: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
onError: PropTypes.func,
disabled: PropTypes.bool,
id: PropTypes.any
};
// 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.
public static _EditorWatchdog = EditorWatchdog;
}
/**
* TODO this is type space definition for props, the CKEditor.propTypes is a run-time props validation that should match.
*/
interface Props<TEditor extends Editor> extends InferProps<typeof CKEditor.propTypes> {
editor: { create( ...args: any ): Promise<TEditor> };
config?: EditorConfig;
watchdogConfig?: WatchdogConfig;
disableWatchdog?: boolean;
onReady?: ( editor: TEditor ) => void;
onError?: ( error: Error, details: ErrorDetails ) => void;
onChange?: ( event: EventInfo, editor: TEditor ) => void;
onFocus?: ( event: EventInfo, editor: TEditor ) => void;
onBlur?: ( event: EventInfo, editor: TEditor ) => void;
}
interface ErrorDetails {
phase: 'initialization' | 'runtime';
willEditorRestart?: boolean;
}
/**
* An adapter aligning the context watchdog API to the editor watchdog API for easier usage.
*/
export class EditorWatchdogAdapter<TEditor extends Editor> {
/**
* The context watchdog instance that will be wrapped into editor watchdog API.
*/
private readonly _contextWatchdog: ContextWatchdog;
/**
* A unique id for the adapter to distinguish editor items when using the context watchdog API.
*/
private readonly _id: string;
/**
* A watchdog's editor creator function.
*/
private _creator?: EditorCreatorFunction;
/**
* @param contextWatchdog The context watchdog instance that will be wrapped into editor watchdog API.
*/
constructor( contextWatchdog: ContextWatchdog ) {
this._contextWatchdog = contextWatchdog;
this._id = uid();
}
/**
* @param creator A watchdog's editor creator function.
*/
public setCreator( creator: EditorCreatorFunction ): void {
this._creator = creator;
}
/**
* Adds an editor configuration to the context watchdog registry. Creates an instance of it.
*
* @param sourceElementOrData A source element or data for the new editor.
* @param config CKEditor 5 editor config.
*/
public create( sourceElementOrData: HTMLElement | string, config: EditorConfig ): Promise<unknown> {
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.
*/
public on( _: string, callback: ( _: null, data: { error: Error; causesRestart?: boolean } ) => void ): void {
// Assume that the event name was error.
this._contextWatchdog.on( 'itemError', ( _, { itemId, error } ) => {
if ( itemId === this._id ) {
callback( null, { error, causesRestart: undefined } );
}
} );
}
public destroy(): Promise<unknown> {
// Destroying an editor instance after destroying the Context is handled in the `ContextWatchdog` class.
// As `EditorWatchdogAdapter` is an adapter, we should not destroy the editor manually.
// Otherwise, it causes that the editor is destroyed twice. However, there is a case, when the editor
// needs to be removed from the context, without destroying the context itself. We may assume the following
// relations with `ContextWatchdog#state`:
//
// a) `ContextWatchdog#state` === 'ready' - context is not destroyed; it's safe to destroy the editor manually.
// b) `ContextWatchdog#state` === 'destroyed' - context is destroyed; let `ContextWatchdog` handle the whole process.
//
// See #354 for more information.
if ( this._contextWatchdog.state === 'ready' ) {
return this._contextWatchdog.remove( this._id );
}
return Promise.resolve();
}
/**
* An editor instance.
*/
public get editor(): TEditor {
return this._contextWatchdog.getItem( this._id ) as TEditor;
}
}