@ckeditor/ckeditor5-watchdog 41.3.1 → 41.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,337 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * @module watchdog/contextwatchdog
11
+ */
12
+ import type { Context, Editor, EditorConfig, ContextConfig } from 'ckeditor5/src/core.js';
13
+ import type { ArrayOrItem, CKEditorError } from 'ckeditor5/src/utils.js';
14
+ import Watchdog, { type WatchdogConfig, type WatchdogState } from './watchdog.js';
15
+ import EditorWatchdog, { type EditorCreatorFunction } from './editorwatchdog.js';
16
+ /**
17
+ * A watchdog for the {@link module:core/context~Context} class.
18
+ *
19
+ * See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and
20
+ * how to use it.
21
+ */
22
+ export default class ContextWatchdog<TContext extends Context = Context> extends Watchdog {
23
+ /**
24
+ * A map of internal watchdogs for added items.
25
+ */
26
+ protected _watchdogs: Map<string, EditorWatchdog<Editor>>;
27
+ /**
28
+ * The watchdog configuration.
29
+ */
30
+ private readonly _watchdogConfig;
31
+ /**
32
+ * The current context instance.
33
+ */
34
+ private _context;
35
+ /**
36
+ * Context properties (nodes/references) that are gathered during the initial context creation
37
+ * and are used to distinguish the origin of an error.
38
+ */
39
+ private _contextProps;
40
+ /**
41
+ * An action queue, which is used to handle async functions queuing.
42
+ */
43
+ private _actionQueues;
44
+ /**
45
+ * The configuration for the {@link module:core/context~Context}.
46
+ */
47
+ private _contextConfig?;
48
+ /**
49
+ * The creation method.
50
+ *
51
+ * @see #setCreator
52
+ */
53
+ protected _creator: (config: ContextConfig) => Promise<TContext>;
54
+ /**
55
+ * The destruction method.
56
+ *
57
+ * @see #setDestructor
58
+ */
59
+ protected _destructor: (context: Context) => Promise<unknown>;
60
+ /**
61
+ * The watched item.
62
+ */
63
+ _item: unknown;
64
+ /**
65
+ * The context watchdog class constructor.
66
+ *
67
+ * ```ts
68
+ * const watchdog = new ContextWatchdog( Context );
69
+ *
70
+ * await watchdog.create( contextConfiguration );
71
+ *
72
+ * await watchdog.add( item );
73
+ * ```
74
+ *
75
+ * See the {@glink features/watchdog Watchdog feature guide} to learn more how to use this feature.
76
+ *
77
+ * @param Context The {@link module:core/context~Context} class.
78
+ * @param watchdogConfig The watchdog configuration.
79
+ */
80
+ constructor(Context: {
81
+ create(...args: any): Promise<TContext>;
82
+ }, watchdogConfig?: WatchdogConfig);
83
+ /**
84
+ * Sets the function that is responsible for the context creation.
85
+ * It expects a function that should return a promise (or `undefined`).
86
+ *
87
+ * ```ts
88
+ * watchdog.setCreator( config => Context.create( config ) );
89
+ * ```
90
+ */
91
+ setCreator(creator: (config: ContextConfig) => Promise<TContext>): void;
92
+ /**
93
+ * Sets the function that is responsible for the context destruction.
94
+ * Overrides the default destruction function, which destroys only the context instance.
95
+ * It expects a function that should return a promise (or `undefined`).
96
+ *
97
+ * ```ts
98
+ * watchdog.setDestructor( context => {
99
+ * // Do something before the context is destroyed.
100
+ *
101
+ * return context
102
+ * .destroy()
103
+ * .then( () => {
104
+ * // Do something after the context is destroyed.
105
+ * } );
106
+ * } );
107
+ * ```
108
+ */
109
+ setDestructor(destructor: (context: Context) => Promise<unknown>): void;
110
+ /**
111
+ * The context instance. Keep in mind that this property might be changed when the context watchdog restarts,
112
+ * so do not keep this instance internally. Always operate on the `ContextWatchdog#context` property.
113
+ */
114
+ get context(): Context | null;
115
+ /**
116
+ * Initializes the context watchdog. Once it is created, the watchdog takes care about
117
+ * recreating the context and the provided items, and starts the error handling mechanism.
118
+ *
119
+ * ```ts
120
+ * await watchdog.create( {
121
+ * plugins: []
122
+ * } );
123
+ * ```
124
+ *
125
+ * @param contextConfig The context configuration. See {@link module:core/context~Context}.
126
+ */
127
+ create(contextConfig?: ContextConfig): Promise<unknown>;
128
+ /**
129
+ * Returns an item instance with the given `itemId`.
130
+ *
131
+ * ```ts
132
+ * const editor1 = watchdog.getItem( 'editor1' );
133
+ * ```
134
+ *
135
+ * @param itemId The item ID.
136
+ * @returns The item instance or `undefined` if an item with a given ID has not been found.
137
+ */
138
+ getItem(itemId: string): unknown;
139
+ /**
140
+ * Gets the state of the given item. See {@link #state} for a list of available states.
141
+ *
142
+ * ```ts
143
+ * const editor1State = watchdog.getItemState( 'editor1' );
144
+ * ```
145
+ *
146
+ * @param itemId Item ID.
147
+ * @returns The state of the item.
148
+ */
149
+ getItemState(itemId: string): WatchdogState;
150
+ /**
151
+ * Adds items to the watchdog. Once created, instances of these items will be available using the {@link #getItem} method.
152
+ *
153
+ * Items can be passed together as an array of objects:
154
+ *
155
+ * ```ts
156
+ * await watchdog.add( [ {
157
+ * id: 'editor1',
158
+ * type: 'editor',
159
+ * sourceElementOrData: document.querySelector( '#editor' ),
160
+ * config: {
161
+ * plugins: [ Essentials, Paragraph, Bold, Italic ],
162
+ * toolbar: [ 'bold', 'italic', 'alignment' ]
163
+ * },
164
+ * creator: ( element, config ) => ClassicEditor.create( element, config )
165
+ * } ] );
166
+ * ```
167
+ *
168
+ * Or one by one as objects:
169
+ *
170
+ * ```ts
171
+ * await watchdog.add( {
172
+ * id: 'editor1',
173
+ * type: 'editor',
174
+ * sourceElementOrData: document.querySelector( '#editor' ),
175
+ * config: {
176
+ * plugins: [ Essentials, Paragraph, Bold, Italic ],
177
+ * toolbar: [ 'bold', 'italic', 'alignment' ]
178
+ * },
179
+ * creator: ( element, config ) => ClassicEditor.create( element, config )
180
+ * ] );
181
+ * ```
182
+ *
183
+ * Then an instance can be retrieved using the {@link #getItem} method:
184
+ *
185
+ * ```ts
186
+ * const editor1 = watchdog.getItem( 'editor1' );
187
+ * ```
188
+ *
189
+ * Note that this method can be called multiple times, but for performance reasons it is better
190
+ * to pass all items together.
191
+ *
192
+ * @param itemConfigurationOrItemConfigurations An item configuration object or an array of item configurations.
193
+ */
194
+ add(itemConfigurationOrItemConfigurations: ArrayOrItem<WatchdogItemConfiguration>): Promise<unknown>;
195
+ /**
196
+ * Removes and destroys item(s) with given ID(s).
197
+ *
198
+ * ```ts
199
+ * await watchdog.remove( 'editor1' );
200
+ * ```
201
+ *
202
+ * Or
203
+ *
204
+ * ```ts
205
+ * await watchdog.remove( [ 'editor1', 'editor2' ] );
206
+ * ```
207
+ *
208
+ * @param itemIdOrItemIds Item ID or an array of item IDs.
209
+ */
210
+ remove(itemIdOrItemIds: ArrayOrItem<string>): Promise<unknown>;
211
+ /**
212
+ * Destroys the context watchdog and all added items.
213
+ * Once the context watchdog is destroyed, new items cannot be added.
214
+ *
215
+ * ```ts
216
+ * await watchdog.destroy();
217
+ * ```
218
+ */
219
+ destroy(): Promise<unknown>;
220
+ /**
221
+ * Restarts the context watchdog.
222
+ */
223
+ protected _restart(): Promise<unknown>;
224
+ /**
225
+ * Initializes the context watchdog.
226
+ */
227
+ private _create;
228
+ /**
229
+ * Destroys the context instance and all added items.
230
+ */
231
+ private _destroy;
232
+ /**
233
+ * Returns the watchdog for a given item ID.
234
+ *
235
+ * @param itemId Item ID.
236
+ */
237
+ protected _getWatchdog(itemId: string): Watchdog;
238
+ /**
239
+ * Checks whether an error comes from the context instance and not from the item instances.
240
+ *
241
+ * @internal
242
+ */
243
+ _isErrorComingFromThisItem(error: CKEditorError): boolean;
244
+ }
245
+ /**
246
+ * Fired after the watchdog restarts the context and the added items because of a crash.
247
+ *
248
+ * ```ts
249
+ * watchdog.on( 'restart', () => {
250
+ * console.log( 'The context has been restarted.' );
251
+ * } );
252
+ * ```
253
+ *
254
+ * @eventName ~ContextWatchdog#restart
255
+ */
256
+ export type ContextWatchdogRestartEvent = {
257
+ name: 'restart';
258
+ args: [];
259
+ return: undefined;
260
+ };
261
+ /**
262
+ * Fired when a new error occurred in one of the added items.
263
+ *
264
+ * ```ts
265
+ * watchdog.on( 'itemError', ( evt, { error, itemId } ) => {
266
+ * console.log( `An error occurred in an item with the '${ itemId }' ID.` );
267
+ * } );
268
+ * ```
269
+ *
270
+ * @eventName ~ContextWatchdog#itemError
271
+ */
272
+ export type ContextWatchdogItemErrorEvent = {
273
+ name: 'itemError';
274
+ args: [ContextWatchdogItemErrorEventData];
275
+ return: undefined;
276
+ };
277
+ /**
278
+ * The `itemError` event data.
279
+ */
280
+ export type ContextWatchdogItemErrorEventData = {
281
+ itemId: string;
282
+ error: Error;
283
+ };
284
+ /**
285
+ * Fired after an item has been restarted.
286
+ *
287
+ * ```ts
288
+ * watchdog.on( 'itemRestart', ( evt, { itemId } ) => {
289
+ * console.log( 'An item with with the '${ itemId }' ID has been restarted.' );
290
+ * } );
291
+ * ```
292
+ *
293
+ * @eventName ~ContextWatchdog#itemRestart
294
+ */
295
+ export type ContextWatchdogItemRestartEvent = {
296
+ name: 'itemRestart';
297
+ args: [ContextWatchdogItemRestartEventData];
298
+ return: undefined;
299
+ };
300
+ /**
301
+ * The `itemRestart` event data.
302
+ */
303
+ export type ContextWatchdogItemRestartEventData = {
304
+ itemId: string;
305
+ };
306
+ /**
307
+ * The watchdog item configuration interface.
308
+ */
309
+ export interface WatchdogItemConfiguration {
310
+ /**
311
+ * id A unique item identificator.
312
+ */
313
+ id: string;
314
+ /**
315
+ * The type of the item to create. At the moment, only `'editor'` is supported.
316
+ */
317
+ type: 'editor';
318
+ /**
319
+ * A function that initializes the item (the editor). The function takes editor initialization arguments
320
+ * and should return a promise. For example: `( el, config ) => ClassicEditor.create( el, config )`.
321
+ */
322
+ creator: EditorCreatorFunction;
323
+ /**
324
+ * A function that destroys the item instance (the editor). The function
325
+ * takes an item and should return a promise. For example: `editor => editor.destroy()`
326
+ */
327
+ destructor?: (editor: Editor) => Promise<unknown>;
328
+ /**
329
+ * The source element or data that will be passed
330
+ * as the first argument to the `Editor.create()` method.
331
+ */
332
+ sourceElementOrData: string | HTMLElement;
333
+ /**
334
+ * An editor configuration.
335
+ */
336
+ config: EditorConfig;
337
+ }
@@ -0,0 +1,195 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * @module watchdog/editorwatchdog
11
+ */
12
+ import type { CKEditorError } from 'ckeditor5/src/utils.js';
13
+ import type { Editor, EditorConfig, Context } from 'ckeditor5/src/core.js';
14
+ import Watchdog, { type WatchdogConfig } from './watchdog.js';
15
+ /**
16
+ * A watchdog for CKEditor 5 editors.
17
+ *
18
+ * See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and
19
+ * how to use it.
20
+ */
21
+ export default class EditorWatchdog<TEditor extends Editor = Editor> extends Watchdog {
22
+ /**
23
+ * The current editor instance.
24
+ */
25
+ private _editor;
26
+ /**
27
+ * A promise associated with the life cycle of the editor (creation or destruction processes).
28
+ *
29
+ * It is used to prevent the initialization of the editor if the previous instance has not been destroyed yet,
30
+ * and conversely, to prevent the destruction of the editor if it has not been initialized.
31
+ */
32
+ private _lifecyclePromise;
33
+ /**
34
+ * Throttled save method. The `save()` method is called the specified `saveInterval` after `throttledSave()` is called,
35
+ * unless a new action happens in the meantime.
36
+ */
37
+ private _throttledSave;
38
+ /**
39
+ * The latest saved editor data represented as a root name -> root data object.
40
+ */
41
+ private _data?;
42
+ /**
43
+ * The last document version.
44
+ */
45
+ private _lastDocumentVersion?;
46
+ /**
47
+ * The editor source element or data.
48
+ */
49
+ private _elementOrData?;
50
+ /**
51
+ * Specifies whether the editor was initialized using document data (`true`) or HTML elements (`false`).
52
+ */
53
+ private _initUsingData;
54
+ /**
55
+ * The latest record of the editor editable elements. Used to restart the editor.
56
+ */
57
+ private _editables;
58
+ /**
59
+ * The editor configuration.
60
+ */
61
+ private _config?;
62
+ /**
63
+ * The creation method.
64
+ *
65
+ * @see #setCreator
66
+ */
67
+ protected _creator: EditorCreatorFunction<TEditor>;
68
+ /**
69
+ * The destruction method.
70
+ *
71
+ * @see #setDestructor
72
+ */
73
+ protected _destructor: (editor: Editor) => Promise<unknown>;
74
+ private _excludedProps?;
75
+ /**
76
+ * @param Editor The editor class.
77
+ * @param watchdogConfig The watchdog plugin configuration.
78
+ */
79
+ constructor(Editor: {
80
+ create(...args: any): Promise<TEditor>;
81
+ } | null, watchdogConfig?: WatchdogConfig);
82
+ /**
83
+ * The current editor instance.
84
+ */
85
+ get editor(): TEditor | null;
86
+ /**
87
+ * @internal
88
+ */
89
+ get _item(): TEditor | null;
90
+ /**
91
+ * Sets the function that is responsible for the editor creation.
92
+ * It expects a function that should return a promise.
93
+ *
94
+ * ```ts
95
+ * watchdog.setCreator( ( element, config ) => ClassicEditor.create( element, config ) );
96
+ * ```
97
+ */
98
+ setCreator(creator: EditorCreatorFunction<TEditor>): void;
99
+ /**
100
+ * Sets the function that is responsible for the editor destruction.
101
+ * Overrides the default destruction function, which destroys only the editor instance.
102
+ * It expects a function that should return a promise or `undefined`.
103
+ *
104
+ * ```ts
105
+ * watchdog.setDestructor( editor => {
106
+ * // Do something before the editor is destroyed.
107
+ *
108
+ * return editor
109
+ * .destroy()
110
+ * .then( () => {
111
+ * // Do something after the editor is destroyed.
112
+ * } );
113
+ * } );
114
+ * ```
115
+ */
116
+ setDestructor(destructor: (editor: Editor) => Promise<unknown>): void;
117
+ /**
118
+ * Restarts the editor instance. This method is called whenever an editor error occurs. It fires the `restart` event and changes
119
+ * the state to `initializing`.
120
+ *
121
+ * @fires restart
122
+ */
123
+ protected _restart(): Promise<unknown>;
124
+ /**
125
+ * Creates the editor instance and keeps it running, using the defined creator and destructor.
126
+ *
127
+ * @param elementOrData The editor source element or the editor data.
128
+ * @param config The editor configuration.
129
+ * @param context A context for the editor.
130
+ */
131
+ create(elementOrData?: HTMLElement | string | Record<string, string> | Record<string, HTMLElement>, config?: EditorConfig, context?: Context): Promise<unknown>;
132
+ /**
133
+ * Destroys the watchdog and the current editor instance. It fires the callback
134
+ * registered in {@link #setDestructor `setDestructor()`} and uses it to destroy the editor instance.
135
+ * It also sets the state to `destroyed`.
136
+ */
137
+ destroy(): Promise<unknown>;
138
+ private _destroy;
139
+ /**
140
+ * Saves the editor data, so it can be restored after the crash even if the data cannot be fetched at
141
+ * the moment of the crash.
142
+ */
143
+ private _save;
144
+ /**
145
+ * @internal
146
+ */
147
+ _setExcludedProperties(props: Set<unknown>): void;
148
+ /**
149
+ * Gets all data that is required to reinitialize editor instance.
150
+ */
151
+ private _getData;
152
+ /**
153
+ * For each attached model root, returns its HTML editable element (if available).
154
+ */
155
+ private _getEditables;
156
+ /**
157
+ * Traverses the error context and the current editor to find out whether these structures are connected
158
+ * to each other via properties.
159
+ *
160
+ * @internal
161
+ */
162
+ _isErrorComingFromThisItem(error: CKEditorError): boolean;
163
+ /**
164
+ * Clones the editor configuration.
165
+ */
166
+ private _cloneEditorConfiguration;
167
+ }
168
+ export type EditorData = {
169
+ roots: Record<string, {
170
+ content: string;
171
+ attributes: string;
172
+ isLoaded: boolean;
173
+ }>;
174
+ markers: Record<string, {
175
+ rangeJSON: {
176
+ start: any;
177
+ end: any;
178
+ };
179
+ usingOperation: boolean;
180
+ affectsData: boolean;
181
+ }>;
182
+ commentThreads: string;
183
+ suggestions: string;
184
+ };
185
+ /**
186
+ * Fired after the watchdog restarts the error in case of a crash.
187
+ *
188
+ * @eventName ~EditorWatchdog#restart
189
+ */
190
+ export type EditorWatchdogRestartEvent = {
191
+ name: 'restart';
192
+ args: [];
193
+ return: undefined;
194
+ };
195
+ export type EditorCreatorFunction<TEditor = Editor> = (elementOrData: HTMLElement | string | Record<string, string> | Record<string, HTMLElement>, config: EditorConfig) => Promise<TEditor>;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * @module watchdog
11
+ */
12
+ export { default as ContextWatchdog } from './contextwatchdog.js';
13
+ export { default as EditorWatchdog } from './editorwatchdog.js';
14
+ export { default as Watchdog } from './watchdog.js';
15
+ import './augmentation.js';
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * Traverses both structures to find out whether there is a reference that is shared between both structures.
11
+ */
12
+ export default function areConnectedThroughProperties(target1: unknown, target2: unknown, excludedNodes?: Set<unknown>): boolean;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * @module watchdog/utils/getsubnodes
11
+ */
12
+ export default function getSubNodes(head: unknown, excludedProperties?: Set<unknown>): Set<unknown>;