-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathckeditorcontext.jsx
104 lines (84 loc) · 2.6 KB
/
ckeditorcontext.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
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/
import React from 'react';
import PropTypes from 'prop-types';
import ContextWatchdog from '@ckeditor/ckeditor5-watchdog/src/contextwatchdog';
export const ContextWatchdogContext = React.createContext( 'contextWatchdog' );
export default class CKEditorContext extends React.Component {
constructor( props, context ) {
super( props, context );
this.contextWatchdog = null;
if ( this.props.isLayoutReady ) {
this._initializeContextWatchdog( this.props.config );
}
}
async shouldComponentUpdate( nextProps ) {
// If the configuration changes then the ContextWatchdog needs to be destroyed and recreated
// On top of the new configuration.
if ( nextProps.id !== this.props.id ) {
/* istanbul ignore else */
if ( this.contextWatchdog ) {
await this.contextWatchdog.destroy();
}
await this._initializeContextWatchdog( nextProps.config );
}
if ( nextProps.isLayoutReady && !this.contextWatchdog ) {
await this._initializeContextWatchdog( nextProps.config );
return true;
}
// Rerender the component only when children has changed.
return this.props.children !== nextProps.children;
}
render() {
return (
<ContextWatchdogContext.Provider value={ this.contextWatchdog } >
{ this.props.children }
</ContextWatchdogContext.Provider>
);
}
async componentWillUnmount() {
await this._destroyContext();
}
async _initializeContextWatchdog( config ) {
this.contextWatchdog = new ContextWatchdog( this.props.context );
this.contextWatchdog.on( 'error', ( _, errorEvent ) => {
this.props.onError( errorEvent.error, {
phase: 'runtime',
willContextRestart: errorEvent.causesRestart
} );
} );
this.contextWatchdog.on( 'stateChange', () => {
if ( this.contextWatchdog.state === 'ready' && this.props.onReady ) {
this.props.onReady( this.contextWatchdog.context );
}
} );
await this.contextWatchdog.create( config )
.catch( error => {
this.props.onError( error, {
phase: 'initialization',
willContextRestart: false
} );
} );
}
async _destroyContext() {
if ( this.contextWatchdog ) {
await this.contextWatchdog.destroy();
this.contextWatchdog = null;
}
}
}
CKEditorContext.defaultProps = {
isLayoutReady: true,
onError: ( error, details ) => console.error( error, details )
};
// Properties definition.
CKEditorContext.propTypes = {
id: PropTypes.string,
isLayoutReady: PropTypes.bool,
context: PropTypes.func,
config: PropTypes.object,
onReady: PropTypes.func,
onError: PropTypes.func
};