@ckeditor/ckeditor5-watchdog 38.1.1 → 38.2.0-alpha.1

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.
@@ -1,217 +1,217 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- import areConnectedThroughProperties from './utils/areconnectedthroughproperties';
6
- import Watchdog from './watchdog';
7
- import { throttle, cloneDeepWith, isElement } from 'lodash-es';
8
- /**
9
- * A watchdog for CKEditor 5 editors.
10
- *
11
- * See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and
12
- * how to use it.
13
- */
14
- export default class EditorWatchdog extends Watchdog {
15
- /**
16
- * @param Editor The editor class.
17
- * @param watchdogConfig The watchdog plugin configuration.
18
- */
19
- constructor(Editor, watchdogConfig = {}) {
20
- super(watchdogConfig);
21
- /**
22
- * The current editor instance.
23
- */
24
- this._editor = null;
25
- // this._editorClass = Editor;
26
- this._throttledSave = throttle(this._save.bind(this), typeof watchdogConfig.saveInterval === 'number' ? watchdogConfig.saveInterval : 5000);
27
- // Set default creator and destructor functions:
28
- if (Editor) {
29
- this._creator = ((elementOrData, config) => Editor.create(elementOrData, config));
30
- }
31
- this._destructor = editor => editor.destroy();
32
- }
33
- /**
34
- * The current editor instance.
35
- */
36
- get editor() {
37
- return this._editor;
38
- }
39
- /**
40
- * @internal
41
- */
42
- get _item() {
43
- return this._editor;
44
- }
45
- /**
46
- * Sets the function that is responsible for the editor creation.
47
- * It expects a function that should return a promise.
48
- *
49
- * ```ts
50
- * watchdog.setCreator( ( element, config ) => ClassicEditor.create( element, config ) );
51
- * ```
52
- */
53
- setCreator(creator) {
54
- this._creator = creator;
55
- }
56
- /**
57
- * Sets the function that is responsible for the editor destruction.
58
- * Overrides the default destruction function, which destroys only the editor instance.
59
- * It expects a function that should return a promise or `undefined`.
60
- *
61
- * ```ts
62
- * watchdog.setDestructor( editor => {
63
- * // Do something before the editor is destroyed.
64
- *
65
- * return editor
66
- * .destroy()
67
- * .then( () => {
68
- * // Do something after the editor is destroyed.
69
- * } );
70
- * } );
71
- * ```
72
- */
73
- setDestructor(destructor) {
74
- this._destructor = destructor;
75
- }
76
- /**
77
- * Restarts the editor instance. This method is called whenever an editor error occurs. It fires the `restart` event and changes
78
- * the state to `initializing`.
79
- *
80
- * @fires restart
81
- */
82
- _restart() {
83
- return Promise.resolve()
84
- .then(() => {
85
- this.state = 'initializing';
86
- this._fire('stateChange');
87
- return this._destroy();
88
- })
89
- .catch(err => {
90
- console.error('An error happened during the editor destroying.', err);
91
- })
92
- .then(() => {
93
- if (typeof this._elementOrData === 'string') {
94
- return this.create(this._data, this._config, this._config.context);
95
- }
96
- else {
97
- const updatedConfig = Object.assign({}, this._config, {
98
- initialData: this._data
99
- });
100
- return this.create(this._elementOrData, updatedConfig, updatedConfig.context);
101
- }
102
- })
103
- .then(() => {
104
- this._fire('restart');
105
- });
106
- }
107
- /**
108
- * Creates the editor instance and keeps it running, using the defined creator and destructor.
109
- *
110
- * @param elementOrData The editor source element or the editor data.
111
- * @param config The editor configuration.
112
- * @param context A context for the editor.
113
- */
114
- create(elementOrData = this._elementOrData, config = this._config, context) {
115
- return Promise.resolve()
116
- .then(() => {
117
- super._startErrorHandling();
118
- this._elementOrData = elementOrData;
119
- // Clone configuration because it might be shared within multiple watchdog instances. Otherwise,
120
- // when an error occurs in one of these editors, the watchdog will restart all of them.
121
- this._config = this._cloneEditorConfiguration(config) || {};
122
- this._config.context = context;
123
- return this._creator(elementOrData, this._config);
124
- })
125
- .then(editor => {
126
- this._editor = editor;
127
- editor.model.document.on('change:data', this._throttledSave);
128
- this._lastDocumentVersion = editor.model.document.version;
129
- this._data = this._getData();
130
- this.state = 'ready';
131
- this._fire('stateChange');
132
- });
133
- }
134
- /**
135
- * Destroys the watchdog and the current editor instance. It fires the callback
136
- * registered in {@link #setDestructor `setDestructor()`} and uses it to destroy the editor instance.
137
- * It also sets the state to `destroyed`.
138
- */
139
- destroy() {
140
- return Promise.resolve()
141
- .then(() => {
142
- this.state = 'destroyed';
143
- this._fire('stateChange');
144
- super.destroy();
145
- return this._destroy();
146
- });
147
- }
148
- _destroy() {
149
- return Promise.resolve()
150
- .then(() => {
151
- this._stopErrorHandling();
152
- // Save data if there is a remaining editor data change.
153
- this._throttledSave.flush();
154
- const editor = this._editor;
155
- this._editor = null;
156
- // Remove the `change:data` listener before destroying the editor.
157
- // Incorrectly written plugins may trigger firing `change:data` events during the editor destruction phase
158
- // causing the watchdog to call `editor.getData()` when some parts of editor are already destroyed.
159
- editor.model.document.off('change:data', this._throttledSave);
160
- return this._destructor(editor);
161
- });
162
- }
163
- /**
164
- * Saves the editor data, so it can be restored after the crash even if the data cannot be fetched at
165
- * the moment of the crash.
166
- */
167
- _save() {
168
- const version = this._editor.model.document.version;
169
- try {
170
- this._data = this._getData();
171
- this._lastDocumentVersion = version;
172
- }
173
- catch (err) {
174
- console.error(err, 'An error happened during restoring editor data. ' +
175
- 'Editor will be restored from the previously saved data.');
176
- }
177
- }
178
- /**
179
- * @internal
180
- */
181
- _setExcludedProperties(props) {
182
- this._excludedProps = props;
183
- }
184
- /**
185
- * Returns the editor data.
186
- */
187
- _getData() {
188
- const data = {};
189
- for (const rootName of this._editor.model.document.getRootNames()) {
190
- data[rootName] = this._editor.data.get({ rootName });
191
- }
192
- return data;
193
- }
194
- /**
195
- * Traverses the error context and the current editor to find out whether these structures are connected
196
- * to each other via properties.
197
- *
198
- * @internal
199
- */
200
- _isErrorComingFromThisItem(error) {
201
- return areConnectedThroughProperties(this._editor, error.context, this._excludedProps);
202
- }
203
- /**
204
- * Clones the editor configuration.
205
- */
206
- _cloneEditorConfiguration(config) {
207
- return cloneDeepWith(config, (value, key) => {
208
- // Leave DOM references.
209
- if (isElement(value)) {
210
- return value;
211
- }
212
- if (key === 'context') {
213
- return value;
214
- }
215
- });
216
- }
217
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ import areConnectedThroughProperties from './utils/areconnectedthroughproperties.js';
6
+ import Watchdog from './watchdog.js';
7
+ import { throttle, cloneDeepWith, isElement } from 'lodash-es';
8
+ /**
9
+ * A watchdog for CKEditor 5 editors.
10
+ *
11
+ * See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and
12
+ * how to use it.
13
+ */
14
+ export default class EditorWatchdog extends Watchdog {
15
+ /**
16
+ * @param Editor The editor class.
17
+ * @param watchdogConfig The watchdog plugin configuration.
18
+ */
19
+ constructor(Editor, watchdogConfig = {}) {
20
+ super(watchdogConfig);
21
+ /**
22
+ * The current editor instance.
23
+ */
24
+ this._editor = null;
25
+ // this._editorClass = Editor;
26
+ this._throttledSave = throttle(this._save.bind(this), typeof watchdogConfig.saveInterval === 'number' ? watchdogConfig.saveInterval : 5000);
27
+ // Set default creator and destructor functions:
28
+ if (Editor) {
29
+ this._creator = ((elementOrData, config) => Editor.create(elementOrData, config));
30
+ }
31
+ this._destructor = editor => editor.destroy();
32
+ }
33
+ /**
34
+ * The current editor instance.
35
+ */
36
+ get editor() {
37
+ return this._editor;
38
+ }
39
+ /**
40
+ * @internal
41
+ */
42
+ get _item() {
43
+ return this._editor;
44
+ }
45
+ /**
46
+ * Sets the function that is responsible for the editor creation.
47
+ * It expects a function that should return a promise.
48
+ *
49
+ * ```ts
50
+ * watchdog.setCreator( ( element, config ) => ClassicEditor.create( element, config ) );
51
+ * ```
52
+ */
53
+ setCreator(creator) {
54
+ this._creator = creator;
55
+ }
56
+ /**
57
+ * Sets the function that is responsible for the editor destruction.
58
+ * Overrides the default destruction function, which destroys only the editor instance.
59
+ * It expects a function that should return a promise or `undefined`.
60
+ *
61
+ * ```ts
62
+ * watchdog.setDestructor( editor => {
63
+ * // Do something before the editor is destroyed.
64
+ *
65
+ * return editor
66
+ * .destroy()
67
+ * .then( () => {
68
+ * // Do something after the editor is destroyed.
69
+ * } );
70
+ * } );
71
+ * ```
72
+ */
73
+ setDestructor(destructor) {
74
+ this._destructor = destructor;
75
+ }
76
+ /**
77
+ * Restarts the editor instance. This method is called whenever an editor error occurs. It fires the `restart` event and changes
78
+ * the state to `initializing`.
79
+ *
80
+ * @fires restart
81
+ */
82
+ _restart() {
83
+ return Promise.resolve()
84
+ .then(() => {
85
+ this.state = 'initializing';
86
+ this._fire('stateChange');
87
+ return this._destroy();
88
+ })
89
+ .catch(err => {
90
+ console.error('An error happened during the editor destroying.', err);
91
+ })
92
+ .then(() => {
93
+ if (typeof this._elementOrData === 'string') {
94
+ return this.create(this._data, this._config, this._config.context);
95
+ }
96
+ else {
97
+ const updatedConfig = Object.assign({}, this._config, {
98
+ initialData: this._data
99
+ });
100
+ return this.create(this._elementOrData, updatedConfig, updatedConfig.context);
101
+ }
102
+ })
103
+ .then(() => {
104
+ this._fire('restart');
105
+ });
106
+ }
107
+ /**
108
+ * Creates the editor instance and keeps it running, using the defined creator and destructor.
109
+ *
110
+ * @param elementOrData The editor source element or the editor data.
111
+ * @param config The editor configuration.
112
+ * @param context A context for the editor.
113
+ */
114
+ create(elementOrData = this._elementOrData, config = this._config, context) {
115
+ return Promise.resolve()
116
+ .then(() => {
117
+ super._startErrorHandling();
118
+ this._elementOrData = elementOrData;
119
+ // Clone configuration because it might be shared within multiple watchdog instances. Otherwise,
120
+ // when an error occurs in one of these editors, the watchdog will restart all of them.
121
+ this._config = this._cloneEditorConfiguration(config) || {};
122
+ this._config.context = context;
123
+ return this._creator(elementOrData, this._config);
124
+ })
125
+ .then(editor => {
126
+ this._editor = editor;
127
+ editor.model.document.on('change:data', this._throttledSave);
128
+ this._lastDocumentVersion = editor.model.document.version;
129
+ this._data = this._getData();
130
+ this.state = 'ready';
131
+ this._fire('stateChange');
132
+ });
133
+ }
134
+ /**
135
+ * Destroys the watchdog and the current editor instance. It fires the callback
136
+ * registered in {@link #setDestructor `setDestructor()`} and uses it to destroy the editor instance.
137
+ * It also sets the state to `destroyed`.
138
+ */
139
+ destroy() {
140
+ return Promise.resolve()
141
+ .then(() => {
142
+ this.state = 'destroyed';
143
+ this._fire('stateChange');
144
+ super.destroy();
145
+ return this._destroy();
146
+ });
147
+ }
148
+ _destroy() {
149
+ return Promise.resolve()
150
+ .then(() => {
151
+ this._stopErrorHandling();
152
+ // Save data if there is a remaining editor data change.
153
+ this._throttledSave.flush();
154
+ const editor = this._editor;
155
+ this._editor = null;
156
+ // Remove the `change:data` listener before destroying the editor.
157
+ // Incorrectly written plugins may trigger firing `change:data` events during the editor destruction phase
158
+ // causing the watchdog to call `editor.getData()` when some parts of editor are already destroyed.
159
+ editor.model.document.off('change:data', this._throttledSave);
160
+ return this._destructor(editor);
161
+ });
162
+ }
163
+ /**
164
+ * Saves the editor data, so it can be restored after the crash even if the data cannot be fetched at
165
+ * the moment of the crash.
166
+ */
167
+ _save() {
168
+ const version = this._editor.model.document.version;
169
+ try {
170
+ this._data = this._getData();
171
+ this._lastDocumentVersion = version;
172
+ }
173
+ catch (err) {
174
+ console.error(err, 'An error happened during restoring editor data. ' +
175
+ 'Editor will be restored from the previously saved data.');
176
+ }
177
+ }
178
+ /**
179
+ * @internal
180
+ */
181
+ _setExcludedProperties(props) {
182
+ this._excludedProps = props;
183
+ }
184
+ /**
185
+ * Returns the editor data.
186
+ */
187
+ _getData() {
188
+ const data = {};
189
+ for (const rootName of this._editor.model.document.getRootNames()) {
190
+ data[rootName] = this._editor.data.get({ rootName });
191
+ }
192
+ return data;
193
+ }
194
+ /**
195
+ * Traverses the error context and the current editor to find out whether these structures are connected
196
+ * to each other via properties.
197
+ *
198
+ * @internal
199
+ */
200
+ _isErrorComingFromThisItem(error) {
201
+ return areConnectedThroughProperties(this._editor, error.context, this._excludedProps);
202
+ }
203
+ /**
204
+ * Clones the editor configuration.
205
+ */
206
+ _cloneEditorConfiguration(config) {
207
+ return cloneDeepWith(config, (value, key) => {
208
+ // Leave DOM references.
209
+ if (isElement(value)) {
210
+ return value;
211
+ }
212
+ if (key === 'context') {
213
+ return value;
214
+ }
215
+ });
216
+ }
217
+ }
package/src/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- * @module watchdog
7
- */
8
- export { default as ContextWatchdog } from './contextwatchdog';
9
- export { default as EditorWatchdog } from './editorwatchdog';
10
- export { default as Watchdog } from './watchdog';
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module watchdog
7
+ */
8
+ export { default as ContextWatchdog } from './contextwatchdog.js';
9
+ export { default as EditorWatchdog } from './editorwatchdog.js';
10
+ export { default as Watchdog } from './watchdog.js';
package/src/index.js CHANGED
@@ -1,10 +1,10 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- * @module watchdog
7
- */
8
- export { default as ContextWatchdog } from './contextwatchdog';
9
- export { default as EditorWatchdog } from './editorwatchdog';
10
- export { default as Watchdog } from './watchdog';
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module watchdog
7
+ */
8
+ export { default as ContextWatchdog } from './contextwatchdog.js';
9
+ export { default as EditorWatchdog } from './editorwatchdog.js';
10
+ export { default as Watchdog } from './watchdog.js';
@@ -1,8 +1,8 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- * Traverses both structures to find out whether there is a reference that is shared between both structures.
7
- */
8
- export default function areConnectedThroughProperties(target1: unknown, target2: unknown, excludedNodes?: Set<unknown>): boolean;
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * Traverses both structures to find out whether there is a reference that is shared between both structures.
7
+ */
8
+ export default function areConnectedThroughProperties(target1: unknown, target2: unknown, excludedNodes?: Set<unknown>): boolean;
@@ -1,58 +1,58 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- * @module watchdog/utils/areconnectedthroughproperties
7
- */
8
- /* globals console */
9
- import getSubNodes from './getsubnodes';
10
- /**
11
- * Traverses both structures to find out whether there is a reference that is shared between both structures.
12
- */
13
- export default function areConnectedThroughProperties(target1, target2, excludedNodes = new Set()) {
14
- if (target1 === target2 && isObject(target1)) {
15
- return true;
16
- }
17
- // @if CK_DEBUG_WATCHDOG // return checkConnectionBetweenProps( target1, target2, excludedNodes );
18
- const subNodes1 = getSubNodes(target1, excludedNodes);
19
- const subNodes2 = getSubNodes(target2, excludedNodes);
20
- for (const node of subNodes1) {
21
- if (subNodes2.has(node)) {
22
- return true;
23
- }
24
- }
25
- return false;
26
- }
27
- /* istanbul ignore next -- @preserve */
28
- // eslint-disable-next-line
29
- function checkConnectionBetweenProps(target1, target2, excludedNodes) {
30
- const { subNodes: subNodes1, prevNodeMap: prevNodeMap1 } = getSubNodes(target1, excludedNodes.subNodes);
31
- const { subNodes: subNodes2, prevNodeMap: prevNodeMap2 } = getSubNodes(target2, excludedNodes.subNodes);
32
- for (const sharedNode of subNodes1) {
33
- if (subNodes2.has(sharedNode)) {
34
- const connection = [];
35
- connection.push(sharedNode);
36
- let node = prevNodeMap1.get(sharedNode);
37
- while (node && node !== target1) {
38
- connection.push(node);
39
- node = prevNodeMap1.get(node);
40
- }
41
- node = prevNodeMap2.get(sharedNode);
42
- while (node && node !== target2) {
43
- connection.unshift(node);
44
- node = prevNodeMap2.get(node);
45
- }
46
- console.log('--------');
47
- console.log({ target1 });
48
- console.log({ sharedNode });
49
- console.log({ target2 });
50
- console.log({ connection });
51
- return true;
52
- }
53
- }
54
- return false;
55
- }
56
- function isObject(structure) {
57
- return typeof structure === 'object' && structure !== null;
58
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module watchdog/utils/areconnectedthroughproperties
7
+ */
8
+ /* globals console */
9
+ import getSubNodes from './getsubnodes.js';
10
+ /**
11
+ * Traverses both structures to find out whether there is a reference that is shared between both structures.
12
+ */
13
+ export default function areConnectedThroughProperties(target1, target2, excludedNodes = new Set()) {
14
+ if (target1 === target2 && isObject(target1)) {
15
+ return true;
16
+ }
17
+ // @if CK_DEBUG_WATCHDOG // return checkConnectionBetweenProps( target1, target2, excludedNodes );
18
+ const subNodes1 = getSubNodes(target1, excludedNodes);
19
+ const subNodes2 = getSubNodes(target2, excludedNodes);
20
+ for (const node of subNodes1) {
21
+ if (subNodes2.has(node)) {
22
+ return true;
23
+ }
24
+ }
25
+ return false;
26
+ }
27
+ /* istanbul ignore next -- @preserve */
28
+ // eslint-disable-next-line
29
+ function checkConnectionBetweenProps(target1, target2, excludedNodes) {
30
+ const { subNodes: subNodes1, prevNodeMap: prevNodeMap1 } = getSubNodes(target1, excludedNodes.subNodes);
31
+ const { subNodes: subNodes2, prevNodeMap: prevNodeMap2 } = getSubNodes(target2, excludedNodes.subNodes);
32
+ for (const sharedNode of subNodes1) {
33
+ if (subNodes2.has(sharedNode)) {
34
+ const connection = [];
35
+ connection.push(sharedNode);
36
+ let node = prevNodeMap1.get(sharedNode);
37
+ while (node && node !== target1) {
38
+ connection.push(node);
39
+ node = prevNodeMap1.get(node);
40
+ }
41
+ node = prevNodeMap2.get(sharedNode);
42
+ while (node && node !== target2) {
43
+ connection.unshift(node);
44
+ node = prevNodeMap2.get(node);
45
+ }
46
+ console.log('--------');
47
+ console.log({ target1 });
48
+ console.log({ sharedNode });
49
+ console.log({ target2 });
50
+ console.log({ connection });
51
+ return true;
52
+ }
53
+ }
54
+ return false;
55
+ }
56
+ function isObject(structure) {
57
+ return typeof structure === 'object' && structure !== null;
58
+ }
@@ -1,8 +1,8 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- * @module watchdog/utils/getsubnodes
7
- */
8
- export default function getSubNodes(head: unknown, excludedProperties?: Set<unknown>): Set<unknown>;
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ * @module watchdog/utils/getsubnodes
7
+ */
8
+ export default function getSubNodes(head: unknown, excludedProperties?: Set<unknown>): Set<unknown>;