@ckeditor/ckeditor5-utils 35.0.1 → 35.1.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.
package/CHANGELOG.md CHANGED
@@ -166,7 +166,7 @@ Internal changes only (updated dependencies, documentation, etc.).
166
166
 
167
167
  ### Other changes
168
168
 
169
- * Vairous fixes in the API docs. Thanks to [@denisname](https://github.com/denisname)!
169
+ * Various fixes in the API docs. Thanks to [@denisname](https://github.com/denisname)!
170
170
 
171
171
 
172
172
  ## [11.0.0](https://github.com/ckeditor/ckeditor5-utils/compare/v10.2.1...v11.0.0) (2018-10-08)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-utils",
3
- "version": "35.0.1",
3
+ "version": "35.1.0",
4
4
  "description": "Miscellaneous utilities used by CKEditor 5.",
5
5
  "keywords": [
6
6
  "ckeditor",
@@ -14,10 +14,10 @@
14
14
  "lodash-es": "^4.17.15"
15
15
  },
16
16
  "devDependencies": {
17
- "@ckeditor/ckeditor5-build-classic": "^35.0.1",
18
- "@ckeditor/ckeditor5-editor-classic": "^35.0.1",
19
- "@ckeditor/ckeditor5-core": "^35.0.1",
20
- "@ckeditor/ckeditor5-engine": "^35.0.1",
17
+ "@ckeditor/ckeditor5-build-classic": "^35.1.0",
18
+ "@ckeditor/ckeditor5-editor-classic": "^35.1.0",
19
+ "@ckeditor/ckeditor5-core": "^35.1.0",
20
+ "@ckeditor/ckeditor5-engine": "^35.1.0",
21
21
  "@types/lodash-es": "^4.17.6",
22
22
  "typescript": "^4.6.4"
23
23
  },
package/src/collection.js CHANGED
@@ -5,11 +5,10 @@
5
5
  /**
6
6
  * @module utils/collection
7
7
  */
8
- import EmitterMixin from './emittermixin';
8
+ import { Emitter } from './emittermixin';
9
9
  import CKEditorError from './ckeditorerror';
10
10
  import uid from './uid';
11
11
  import isIterable from './isiterable';
12
- import mix from './mix';
13
12
  /**
14
13
  * Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
15
14
  * in the collection (like in an array) or by their ids.
@@ -22,7 +21,7 @@ import mix from './mix';
22
21
  *
23
22
  * @mixes module:utils/emittermixin~EmitterMixin
24
23
  */
25
- class Collection {
24
+ export default class Collection extends Emitter {
26
25
  /**
27
26
  * Creates a new Collection instance.
28
27
  *
@@ -60,6 +59,7 @@ class Collection {
60
59
  * Items that do not have such a property will be assigned one when added to the collection.
61
60
  */
62
61
  constructor(initialItemsOrOptions = {}, options = {}) {
62
+ super();
63
63
  const hasInitialItems = isIterable(initialItemsOrOptions);
64
64
  if (!hasInitialItems) {
65
65
  options = initialItemsOrOptions;
@@ -615,5 +615,3 @@ class Collection {
615
615
  return this._items[Symbol.iterator]();
616
616
  }
617
617
  }
618
- mix(Collection, EmitterMixin);
619
- export default Collection;
@@ -2,15 +2,14 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
+ /* eslint-disable new-cap */
5
6
  /**
6
7
  * @module utils/dom/emittermixin
7
8
  */
8
- import { default as EmitterMixin, _getEmitterListenedTo, _setEmitterId } from '../emittermixin';
9
+ import { _getEmitterListenedTo, _setEmitterId, Emitter as BaseEmitter } from '../emittermixin';
9
10
  import uid from '../uid';
10
11
  import isNode from './isnode';
11
12
  import isWindow from './iswindow';
12
- import { extend } from 'lodash-es';
13
- import mix from '../mix';
14
13
  /**
15
14
  * Mixin that injects the DOM events API into its host. It provides the API
16
15
  * compatible with {@link module:utils/emittermixin~EmitterMixin}.
@@ -20,9 +19,9 @@ import mix from '../mix';
20
19
  *
21
20
  * import mix from '../utils/mix.js';
22
21
  * import DomEmitterMixin from '../utils/dom/emittermixin.js';
22
+ * import { Emitter } from '../utils/emittermixin.js';
23
23
  *
24
- * class SomeView {}
25
- * mix( SomeView, DomEmitterMixin );
24
+ * class SomeView extends DomEmitterMixin( Emitter ) {}
26
25
  *
27
26
  * const view = new SomeView();
28
27
  * view.listenTo( domElement, ( evt, domEvt ) => {
@@ -33,99 +32,79 @@ import mix from '../mix';
33
32
  * @mixes module:utils/emittermixin~EmitterMixin
34
33
  * @implements module:utils/dom/emittermixin~Emitter
35
34
  */
36
- const DomEmitterMixin = extend({}, EmitterMixin, {
37
- /**
38
- * Registers a callback function to be executed when an event is fired in a specific Emitter or DOM Node.
39
- * It is backwards compatible with {@link module:utils/emittermixin~EmitterMixin#listenTo}.
40
- *
41
- * @param {module:utils/emittermixin~Emitter|Node|Window} emitter The object that fires the event.
42
- * @param {String} event The name of the event.
43
- * @param {Function} callback The function to be called on event.
44
- * @param {Object} [options={}] Additional options.
45
- * @param {module:utils/priorities~PriorityString|Number} [options.priority='normal'] The priority of this event callback. The higher
46
- * the priority value the sooner the callback will be fired. Events having the same priority are called in the
47
- * order they were added.
48
- * @param {Boolean} [options.useCapture=false] Indicates that events of this type will be dispatched to the registered
49
- * listener before being dispatched to any EventTarget beneath it in the DOM tree.
50
- * @param {Boolean} [options.usePassive=false] Indicates that the function specified by listener will never call preventDefault()
51
- * and prevents blocking browser's main thread by this event handler.
52
- */
53
- listenTo(emitter, event, callback, options = {}) {
54
- // Check if emitter is an instance of DOM Node. If so, use corresponding ProxyEmitter (or create one if not existing).
55
- if (isNode(emitter) || isWindow(emitter)) {
56
- const proxyOptions = {
57
- capture: !!options.useCapture,
58
- passive: !!options.usePassive
59
- };
60
- const proxyEmitter = this._getProxyEmitter(emitter, proxyOptions) || new ProxyEmitter(emitter, proxyOptions);
61
- this.listenTo(proxyEmitter, event, callback, options);
62
- }
63
- else {
64
- // Execute parent class method with Emitter (or ProxyEmitter) instance.
65
- EmitterMixin.listenTo.call(this, emitter, event, callback, options);
35
+ export default function DomEmitterMixin(base) {
36
+ class Mixin extends base {
37
+ listenTo(emitter, event, callback, options = {}) {
38
+ // Check if emitter is an instance of DOM Node. If so, use corresponding ProxyEmitter (or create one if not existing).
39
+ if (isNode(emitter) || isWindow(emitter)) {
40
+ const proxyOptions = {
41
+ capture: !!options.useCapture,
42
+ passive: !!options.usePassive
43
+ };
44
+ const proxyEmitter = this._getProxyEmitter(emitter, proxyOptions) || new ProxyEmitter(emitter, proxyOptions);
45
+ this.listenTo(proxyEmitter, event, callback, options);
46
+ }
47
+ else {
48
+ // Execute parent class method with Emitter (or ProxyEmitter) instance.
49
+ BaseEmitter.prototype.listenTo.call(this, emitter, event, callback, options);
50
+ }
66
51
  }
67
- },
68
- /**
69
- * Stops listening for events. It can be used at different levels:
70
- * It is backwards compatible with {@link module:utils/emittermixin~EmitterMixin#listenTo}.
71
- *
72
- * * To stop listening to a specific callback.
73
- * * To stop listening to a specific event.
74
- * * To stop listening to all events fired by a specific object.
75
- * * To stop listening to all events fired by all object.
76
- *
77
- * @param {module:utils/emittermixin~Emitter|Node|Window} [emitter] The object to stop listening to.
78
- * If omitted, stops it for all objects.
79
- * @param {String} [event] (Requires the `emitter`) The name of the event to stop listening to. If omitted, stops it
80
- * for all events from `emitter`.
81
- * @param {Function} [callback] (Requires the `event`) The function to be removed from the call list for the given
82
- * `event`.
83
- */
84
- stopListening(emitter, event, callback) {
85
- // Check if the emitter is an instance of DOM Node. If so, forward the call to the corresponding ProxyEmitters.
86
- if (isNode(emitter) || isWindow(emitter)) {
87
- const proxyEmitters = this._getAllProxyEmitters(emitter);
88
- for (const proxy of proxyEmitters) {
89
- this.stopListening(proxy, event, callback);
52
+ stopListening(emitter, event, callback) {
53
+ // Check if the emitter is an instance of DOM Node. If so, forward the call to the corresponding ProxyEmitters.
54
+ if (isNode(emitter) || isWindow(emitter)) {
55
+ const proxyEmitters = this._getAllProxyEmitters(emitter);
56
+ for (const proxy of proxyEmitters) {
57
+ this.stopListening(proxy, event, callback);
58
+ }
59
+ }
60
+ else {
61
+ // Execute parent class method with Emitter (or ProxyEmitter) instance.
62
+ BaseEmitter.prototype.stopListening.call(this, emitter, event, callback);
90
63
  }
91
64
  }
92
- else {
93
- // Execute parent class method with Emitter (or ProxyEmitter) instance.
94
- EmitterMixin.stopListening.call(this, emitter, event, callback);
65
+ /**
66
+ * Retrieves ProxyEmitter instance for given DOM Node residing in this Host and given options.
67
+ *
68
+ * @private
69
+ * @param {Node|Window} node DOM Node of the ProxyEmitter.
70
+ * @param {Object} [options] Additional options.
71
+ * @param {Boolean} [options.useCapture=false] Indicates that events of this type will be dispatched to the registered
72
+ * listener before being dispatched to any EventTarget beneath it in the DOM tree.
73
+ * @param {Boolean} [options.usePassive=false] Indicates that the function specified by listener will never call preventDefault()
74
+ * and prevents blocking browser's main thread by this event handler.
75
+ * @returns {module:utils/dom/emittermixin~ProxyEmitter|null} ProxyEmitter instance bound to the DOM Node.
76
+ */
77
+ _getProxyEmitter(node, options) {
78
+ return _getEmitterListenedTo(this, getProxyEmitterId(node, options));
79
+ }
80
+ /**
81
+ * Retrieves all the ProxyEmitter instances for given DOM Node residing in this Host.
82
+ *
83
+ * @private
84
+ * @param {Node|Window} node DOM Node of the ProxyEmitter.
85
+ * @returns {Array.<module:utils/dom/emittermixin~ProxyEmitter>}
86
+ */
87
+ _getAllProxyEmitters(node) {
88
+ return [
89
+ { capture: false, passive: false },
90
+ { capture: false, passive: true },
91
+ { capture: true, passive: false },
92
+ { capture: true, passive: true }
93
+ ].map(options => this._getProxyEmitter(node, options)).filter(proxy => !!proxy);
95
94
  }
96
- },
97
- /**
98
- * Retrieves ProxyEmitter instance for given DOM Node residing in this Host and given options.
99
- *
100
- * @private
101
- * @param {Node|Window} node DOM Node of the ProxyEmitter.
102
- * @param {Object} [options] Additional options.
103
- * @param {Boolean} [options.useCapture=false] Indicates that events of this type will be dispatched to the registered
104
- * listener before being dispatched to any EventTarget beneath it in the DOM tree.
105
- * @param {Boolean} [options.usePassive=false] Indicates that the function specified by listener will never call preventDefault()
106
- * and prevents blocking browser's main thread by this event handler.
107
- * @returns {module:utils/dom/emittermixin~ProxyEmitter|null} ProxyEmitter instance bound to the DOM Node.
108
- */
109
- _getProxyEmitter(node, options) {
110
- return _getEmitterListenedTo(this, getProxyEmitterId(node, options));
111
- },
112
- /**
113
- * Retrieves all the ProxyEmitter instances for given DOM Node residing in this Host.
114
- *
115
- * @private
116
- * @param {Node|Window} node DOM Node of the ProxyEmitter.
117
- * @returns {Array.<module:utils/dom/emittermixin~ProxyEmitter>}
118
- */
119
- _getAllProxyEmitters(node) {
120
- return [
121
- { capture: false, passive: false },
122
- { capture: false, passive: true },
123
- { capture: true, passive: false },
124
- { capture: true, passive: true }
125
- ].map(options => this._getProxyEmitter(node, options)).filter(proxy => !!proxy);
126
95
  }
96
+ return Mixin;
97
+ }
98
+ export const Emitter = DomEmitterMixin(BaseEmitter);
99
+ // Backward compatibility with `mix`
100
+ ([
101
+ '_getProxyEmitter', '_getAllProxyEmitters',
102
+ 'on', 'once', 'off', 'listenTo',
103
+ 'stopListening', 'fire', 'delegate', 'stopDelegating',
104
+ '_addEventListener', '_removeEventListener'
105
+ ]).forEach(key => {
106
+ DomEmitterMixin[key] = Emitter.prototype[key];
127
107
  });
128
- export default DomEmitterMixin;
129
108
  /**
130
109
  * Creates a ProxyEmitter instance. Such an instance is a bridge between a DOM Node firing events
131
110
  * and any Host listening to them. It is backwards compatible with {@link module:utils/emittermixin~EmitterMixin#on}.
@@ -158,7 +137,7 @@ export default DomEmitterMixin;
158
137
  * @implements module:utils/dom/emittermixin~Emitter
159
138
  * @private
160
139
  */
161
- class ProxyEmitter {
140
+ class ProxyEmitter extends BaseEmitter {
162
141
  /**
163
142
  * @param {Node|Window} node DOM Node that fires events.
164
143
  * @param {Object} [options] Additional options.
@@ -168,6 +147,7 @@ class ProxyEmitter {
168
147
  * and prevents blocking browser's main thread by this event handler.
169
148
  */
170
149
  constructor(node, options) {
150
+ super();
171
151
  // Set emitter ID to match DOM Node "expando" property.
172
152
  _setEmitterId(this, getProxyEmitterId(node, options));
173
153
  // Remember the DOM Node this ProxyEmitter is bound to.
@@ -235,7 +215,7 @@ class ProxyEmitter {
235
215
  */
236
216
  _addEventListener(event, callback, options) {
237
217
  this.attach(event);
238
- EmitterMixin._addEventListener.call(this, event, callback, options);
218
+ BaseEmitter.prototype._addEventListener.call(this, event, callback, options);
239
219
  }
240
220
  /**
241
221
  * Removes callback from emitter for given event.
@@ -246,7 +226,7 @@ class ProxyEmitter {
246
226
  * @param {Function} callback The function to stop being called.
247
227
  */
248
228
  _removeEventListener(event, callback) {
249
- EmitterMixin._removeEventListener.call(this, event, callback);
229
+ BaseEmitter.prototype._removeEventListener.call(this, event, callback);
250
230
  this.detach(event);
251
231
  }
252
232
  /**
@@ -273,7 +253,6 @@ class ProxyEmitter {
273
253
  return domListener;
274
254
  }
275
255
  }
276
- mix(ProxyEmitter, EmitterMixin);
277
256
  // Gets an unique DOM Node identifier. The identifier will be set if not defined.
278
257
  //
279
258
  // @private
package/src/dom/global.js CHANGED
@@ -20,4 +20,18 @@
20
20
  *
21
21
  * console.log( global.window.innerWidth );
22
22
  */
23
- export default { window, document };
23
+ let global;
24
+ // In some environments window and document API might not be available.
25
+ try {
26
+ global = { window, document };
27
+ }
28
+ catch (e) {
29
+ // It's not possible to mock a window object to simulate lack of a window object without writing extremely convoluted code.
30
+ /* istanbul ignore next */
31
+ // Let's cast it to not change module's API.
32
+ // We only handle this so loading editor in environments without window and document doesn't fail.
33
+ // For better DX we shouldn't introduce mixed types and require developers to check the type manually.
34
+ // This module should not be used on purpose in any environment outside browser.
35
+ global = { window: {}, document: {} };
36
+ }
37
+ export default global;