-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy patheditorwatchdog.ts
340 lines (285 loc) · 8.85 KB
/
editorwatchdog.ts
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
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module watchdog/editorwatchdog
*/
/* globals console */
// eslint-disable-next-line ckeditor5-rules/no-cross-package-imports
import type { CKEditorError } from 'ckeditor5/src/utils';
// eslint-disable-next-line ckeditor5-rules/no-cross-package-imports
import type {
Editor,
EditorConfig,
Context
} from 'ckeditor5/src/core';
import areConnectedThroughProperties from './utils/areconnectedthroughproperties';
import Watchdog, { type WatchdogConfig } from './watchdog';
import { throttle, cloneDeepWith, isElement, type DebouncedFunc } from 'lodash-es';
/**
* A watchdog for CKEditor 5 editors.
*
* See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and
* how to use it.
*/
export default class EditorWatchdog<TEditor extends Editor = Editor> extends Watchdog {
/**
* The current editor instance.
*/
private _editor: TEditor | null = null;
/**
* Throttled save method. The `save()` method is called the specified `saveInterval` after `throttledSave()` is called,
* unless a new action happens in the meantime.
*/
private _throttledSave: DebouncedFunc<() => void>;
/**
* The latest saved editor data represented as a root name -> root data object.
*/
private _data?: Record<string, string>;
/**
* The last document version.
*/
private _lastDocumentVersion?: number;
/**
* The editor source element or data.
*/
private _elementOrData?: HTMLElement | string | Record<string, string>;
/**
* The editor configuration.
*/
private _config?: EditorConfig;
/**
* The creation method.
*
* @see #setCreator
*/
declare protected _creator: EditorCreatorFunction<TEditor>;
/**
* The destruction method.
*
* @see #setDestructor
*/
declare protected _destructor: ( editor: Editor ) => Promise<unknown>;
private _excludedProps?: Set<unknown>;
/**
* @param Editor The editor class.
* @param watchdogConfig The watchdog plugin configuration.
*/
constructor( Editor: { create( ...args: any ): Promise<TEditor> } | null, watchdogConfig: WatchdogConfig = {} ) {
super( watchdogConfig );
// this._editorClass = Editor;
this._throttledSave = throttle(
this._save.bind( this ),
typeof watchdogConfig.saveInterval === 'number' ? watchdogConfig.saveInterval : 5000
);
// Set default creator and destructor functions:
if ( Editor ) {
this._creator = ( ( elementOrData, config ) => Editor.create( elementOrData, config ) );
}
this._destructor = editor => editor.destroy();
}
/**
* The current editor instance.
*/
public get editor(): TEditor | null {
return this._editor;
}
/**
* @internal
*/
public get _item(): TEditor | null {
return this._editor;
}
/**
* Sets the function that is responsible for the editor creation.
* It expects a function that should return a promise.
*
* ```ts
* watchdog.setCreator( ( element, config ) => ClassicEditor.create( element, config ) );
* ```
*/
public setCreator( creator: EditorCreatorFunction<TEditor> ): void {
this._creator = creator;
}
/**
* Sets the function that is responsible for the editor destruction.
* Overrides the default destruction function, which destroys only the editor instance.
* It expects a function that should return a promise or `undefined`.
*
* ```ts
* watchdog.setDestructor( editor => {
* // Do something before the editor is destroyed.
*
* return editor
* .destroy()
* .then( () => {
* // Do something after the editor is destroyed.
* } );
* } );
* ```
*/
public setDestructor( destructor: ( editor: Editor ) => Promise<unknown> ): void {
this._destructor = destructor;
}
/**
* Restarts the editor instance. This method is called whenever an editor error occurs. It fires the `restart` event and changes
* the state to `initializing`.
*
* @fires restart
*/
protected override _restart(): Promise<unknown> {
return Promise.resolve()
.then( () => {
this.state = 'initializing';
this._fire( 'stateChange' );
return this._destroy();
} )
.catch( err => {
console.error( 'An error happened during the editor destroying.', err );
} )
.then( () => {
if ( typeof this._elementOrData === 'string' ) {
return this.create( this._data, this._config, this._config!.context );
} else {
const updatedConfig = Object.assign( {}, this._config, {
initialData: this._data
} );
return this.create( this._elementOrData, updatedConfig, updatedConfig.context );
}
} )
.then( () => {
this._fire( 'restart' );
} );
}
/**
* Creates the editor instance and keeps it running, using the defined creator and destructor.
*
* @param elementOrData The editor source element or the editor data.
* @param config The editor configuration.
* @param context A context for the editor.
*/
public create(
elementOrData: HTMLElement | string | Record<string, string> = this._elementOrData!,
config: EditorConfig = this._config!,
context?: Context
): Promise<unknown> {
return Promise.resolve()
.then( () => {
super._startErrorHandling();
this._elementOrData = elementOrData;
// Clone configuration because it might be shared within multiple watchdog instances. Otherwise,
// when an error occurs in one of these editors, the watchdog will restart all of them.
this._config = this._cloneEditorConfiguration( config ) || {};
this._config!.context = context;
return this._creator( elementOrData, this._config! );
} )
.then( editor => {
this._editor = editor;
editor.model.document.on( 'change:data', this._throttledSave );
this._lastDocumentVersion = editor.model.document.version;
this._data = this._getData();
this.state = 'ready';
this._fire( 'stateChange' );
} );
}
/**
* Destroys the watchdog and the current editor instance. It fires the callback
* registered in {@link #setDestructor `setDestructor()`} and uses it to destroy the editor instance.
* It also sets the state to `destroyed`.
*/
public override destroy(): Promise<unknown> {
return Promise.resolve()
.then( () => {
this.state = 'destroyed';
this._fire( 'stateChange' );
super.destroy();
return this._destroy();
} );
}
private _destroy(): Promise<unknown> {
return Promise.resolve()
.then( () => {
this._stopErrorHandling();
// Save data if there is a remaining editor data change.
this._throttledSave.flush();
const editor = this._editor;
this._editor = null;
// Remove the `change:data` listener before destroying the editor.
// Incorrectly written plugins may trigger firing `change:data` events during the editor destruction phase
// causing the watchdog to call `editor.getData()` when some parts of editor are already destroyed.
editor!.model.document.off( 'change:data', this._throttledSave );
return this._destructor( editor! );
} );
}
/**
* Saves the editor data, so it can be restored after the crash even if the data cannot be fetched at
* the moment of the crash.
*/
private _save(): void {
const version = this._editor!.model.document.version;
try {
this._data = this._getData();
this._lastDocumentVersion = version;
} catch ( err ) {
console.error(
err,
'An error happened during restoring editor data. ' +
'Editor will be restored from the previously saved data.'
);
}
}
/**
* @internal
*/
public _setExcludedProperties( props: Set<unknown> ): void {
this._excludedProps = props;
}
/**
* Returns the editor data.
*/
private _getData(): Record<string, string> {
const data: Record<string, string> = {};
for ( const rootName of this._editor!.model.document.getRootNames() ) {
data[ rootName ] = this._editor!.data.get( { rootName } );
}
return data;
}
/**
* Traverses the error context and the current editor to find out whether these structures are connected
* to each other via properties.
*
* @internal
*/
public _isErrorComingFromThisItem( error: CKEditorError ): boolean {
return areConnectedThroughProperties( this._editor, error.context, this._excludedProps );
}
/**
* Clones the editor configuration.
*/
private _cloneEditorConfiguration( config: EditorConfig ): EditorConfig {
return cloneDeepWith( config, ( value, key ) => {
// Leave DOM references.
if ( isElement( value ) ) {
return value;
}
if ( key === 'context' ) {
return value;
}
} );
}
}
/**
* Fired after the watchdog restarts the error in case of a crash.
*
* @eventName ~EditorWatchdog#restart
*/
export type EditorWatchdogRestartEvent = {
name: 'restart';
args: [];
return: undefined;
};
export type EditorCreatorFunction<TEditor = Editor> = (
elementOrData: HTMLElement | string | Record<string, string>,
config: EditorConfig
) => Promise<TEditor>;