()
| 377 | * Initializes the editor by creating a proper watchdog and initializing it with the editor's configuration. |
| 378 | */ |
| 379 | const _initializeEditor = async (): Promise<LifeCycleMountResult> => { |
| 380 | const supports = getInstalledCKBaseFeatures(); |
| 381 | |
| 382 | if ( props.disableWatchdog ) { |
| 383 | const instance = await _createEditor( props.data as any, _getConfig() ); |
| 384 | |
| 385 | return { |
| 386 | instance: instance as MultiRootEditor, |
| 387 | watchdog: null |
| 388 | }; |
| 389 | } |
| 390 | |
| 391 | const watchdog = ( () => { |
| 392 | if ( isContextWatchdogReadyToUse( context ) ) { |
| 393 | return new EditorWatchdogAdapter( context.watchdog ); |
| 394 | } |
| 395 | |
| 396 | return new props.editor.EditorWatchdog( props.editor, props.watchdogConfig ); |
| 397 | } )() as EditorWatchdogAdapter<MultiRootEditor>; |
| 398 | |
| 399 | const totalRestartsRef = { |
| 400 | current: 0 |
| 401 | }; |
| 402 | |
| 403 | // Keeping using `data` from creator function callback seems to be a good idea in theory, |
| 404 | // but in practice, it leads to instability. The `data` object can be changed during the editor |
| 405 | // initialization, which can lead to unexpected reset of value in the editor, that do not match |
| 406 | // with the current react state. To prevent this, we are using the `data` from the hook state. |
| 407 | // It's not super optimal, but it's the most stable solution at this moment. |
| 408 | // See more: https://github.com/ckeditor/ckeditor5-react/issues/542 |
| 409 | const watchdogEditorCreator = async ( config: EditorConfig ) => { |
| 410 | const { onAfterDestroy } = props; |
| 411 | |
| 412 | if ( totalRestartsRef.current > 0 && onAfterDestroy && editorRefs.instance.current ) { |
| 413 | onAfterDestroy( editorRefs.instance.current ); |
| 414 | } |
| 415 | |
| 416 | const instance = await _createEditor( data as any, config ); |
| 417 | |
| 418 | if ( totalRestartsRef.current > 0 ) { |
| 419 | semaphore.unsafeSetValue( { |
| 420 | instance, |
| 421 | watchdog |
| 422 | } ); |
| 423 | |
| 424 | setTimeout( () => { |
| 425 | /* istanbul ignore next -- @preserve */ |
| 426 | if ( props.onReady ) { |
| 427 | props.onReady( watchdog!.editor ); |
| 428 | } |
| 429 | } ); |
| 430 | } |
| 431 | |
| 432 | totalRestartsRef.current++; |
| 433 | return instance; |
| 434 | }; |
| 435 | |
| 436 | watchdog.on( 'error', ( _, { error, causesRestart } ) => { |
nothing calls this directly
no test coverage detected
searching dependent graphs…