@ckeditor/ckeditor5-core 35.2.1 → 35.3.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/src/context.js CHANGED
@@ -2,17 +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
-
6
5
  /**
7
6
  * @module core/context
8
7
  */
9
-
10
8
  import Config from '@ckeditor/ckeditor5-utils/src/config';
11
9
  import Collection from '@ckeditor/ckeditor5-utils/src/collection';
12
10
  import PluginCollection from './plugincollection';
13
11
  import Locale from '@ckeditor/ckeditor5-utils/src/locale';
14
12
  import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
15
-
16
13
  /**
17
14
  * Provides a common, higher-level environment for solutions that use multiple {@link module:core/editor/editor~Editor editors}
18
15
  * or plugins that work outside the editor. Use it instead of {@link module:core/editor/editor~Editor.create `Editor.create()`}
@@ -41,315 +38,223 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
41
38
  * See {@link module:core/context~Context.create `Context.create()`} for usage examples.
42
39
  */
43
40
  export default class Context {
44
- /**
45
- * Creates a context instance with a given configuration.
46
- *
47
- * Usually not to be used directly. See the static {@link module:core/context~Context.create `create()`} method.
48
- *
49
- * @param {Object} [config={}] The context configuration.
50
- */
51
- constructor( config ) {
52
- /**
53
- * Stores all the configurations specific to this context instance.
54
- *
55
- * @readonly
56
- * @type {module:utils/config~Config}
57
- */
58
- this.config = new Config( config, this.constructor.defaultConfig );
59
-
60
- const availablePlugins = this.constructor.builtinPlugins;
61
-
62
- this.config.define( 'plugins', availablePlugins );
63
-
64
- /**
65
- * The plugins loaded and in use by this context instance.
66
- *
67
- * @readonly
68
- * @type {module:core/plugincollection~PluginCollection}
69
- */
70
- this.plugins = new PluginCollection( this, availablePlugins );
71
-
72
- const languageConfig = this.config.get( 'language' ) || {};
73
-
74
- /**
75
- * @readonly
76
- * @type {module:utils/locale~Locale}
77
- */
78
- this.locale = new Locale( {
79
- uiLanguage: typeof languageConfig === 'string' ? languageConfig : languageConfig.ui,
80
- contentLanguage: this.config.get( 'language.content' )
81
- } );
82
-
83
- /**
84
- * Shorthand for {@link module:utils/locale~Locale#t}.
85
- *
86
- * @see module:utils/locale~Locale#t
87
- * @method #t
88
- */
89
- this.t = this.locale.t;
90
-
91
- /**
92
- * A list of editors that this context instance is injected to.
93
- *
94
- * @readonly
95
- * @type {module:utils/collection~Collection}
96
- */
97
- this.editors = new Collection();
98
-
99
- /**
100
- * Reference to the editor which created the context.
101
- * Null when the context was created outside of the editor.
102
- *
103
- * It is used to destroy the context when removing the editor that has created the context.
104
- *
105
- * @private
106
- * @type {module:core/editor/editor~Editor|null}
107
- */
108
- this._contextOwner = null;
109
- }
110
-
111
- /**
112
- * Loads and initializes plugins specified in the configuration.
113
- *
114
- * @returns {Promise.<module:core/plugin~LoadedPlugins>} A promise which resolves
115
- * once the initialization is completed, providing an array of loaded plugins.
116
- */
117
- initPlugins() {
118
- const plugins = this.config.get( 'plugins' ) || [];
119
- const substitutePlugins = this.config.get( 'substitutePlugins' ) || [];
120
-
121
- // Plugins for substitution should be checked as well.
122
- for ( const Plugin of plugins.concat( substitutePlugins ) ) {
123
- if ( typeof Plugin != 'function' ) {
124
- /**
125
- * Only a constructor function is allowed as a {@link module:core/contextplugin~ContextPlugin context plugin}.
126
- *
127
- * @error context-initplugins-constructor-only
128
- */
129
- throw new CKEditorError(
130
- 'context-initplugins-constructor-only',
131
- null,
132
- { Plugin }
133
- );
134
- }
135
-
136
- if ( Plugin.isContextPlugin !== true ) {
137
- /**
138
- * Only a plugin marked as a {@link module:core/contextplugin~ContextPlugin.isContextPlugin context plugin}
139
- * is allowed to be used with a context.
140
- *
141
- * @error context-initplugins-invalid-plugin
142
- */
143
- throw new CKEditorError(
144
- 'context-initplugins-invalid-plugin',
145
- null,
146
- { Plugin }
147
- );
148
- }
149
- }
150
-
151
- return this.plugins.init( plugins, [], substitutePlugins );
152
- }
153
-
154
- /**
155
- * Destroys the context instance and all editors used with the context,
156
- * releasing all resources used by the context.
157
- *
158
- * @returns {Promise} A promise that resolves once the context instance is fully destroyed.
159
- */
160
- destroy() {
161
- return Promise.all( Array.from( this.editors, editor => editor.destroy() ) )
162
- .then( () => this.plugins.destroy() );
163
- }
164
-
165
- /**
166
- * Adds a reference to the editor which is used with this context.
167
- *
168
- * When the given editor has created the context, the reference to this editor will be stored
169
- * as a {@link ~Context#_contextOwner}.
170
- *
171
- * This method should only be used by the editor.
172
- *
173
- * @protected
174
- * @param {module:core/editor/editor~Editor} editor
175
- * @param {Boolean} isContextOwner Stores the given editor as a context owner.
176
- */
177
- _addEditor( editor, isContextOwner ) {
178
- if ( this._contextOwner ) {
179
- /**
180
- * Cannot add multiple editors to the context which is created by the editor.
181
- *
182
- * @error context-addeditor-private-context
183
- */
184
- throw new CKEditorError( 'context-addeditor-private-context' );
185
- }
186
-
187
- this.editors.add( editor );
188
-
189
- if ( isContextOwner ) {
190
- this._contextOwner = editor;
191
- }
192
- }
193
-
194
- /**
195
- * Removes a reference to the editor which was used with this context.
196
- * When the context was created by the given editor, the context will be destroyed.
197
- *
198
- * This method should only be used by the editor.
199
- *
200
- * @protected
201
- * @param {module:core/editor/editor~Editor} editor
202
- * @return {Promise} A promise that resolves once the editor is removed from the context or when the context was destroyed.
203
- */
204
- _removeEditor( editor ) {
205
- if ( this.editors.has( editor ) ) {
206
- this.editors.remove( editor );
207
- }
208
-
209
- if ( this._contextOwner === editor ) {
210
- return this.destroy();
211
- }
212
-
213
- return Promise.resolve();
214
- }
215
-
216
- /**
217
- * Returns the context configuration which will be copied to the editors created using this context.
218
- *
219
- * The configuration returned by this method has the plugins configuration removed &mdash; plugins are shared with all editors
220
- * through another mechanism.
221
- *
222
- * This method should only be used by the editor.
223
- *
224
- * @protected
225
- * @returns {Object} Configuration as a plain object.
226
- */
227
- _getEditorConfig() {
228
- const result = {};
229
-
230
- for ( const name of this.config.names() ) {
231
- if ( ![ 'plugins', 'removePlugins', 'extraPlugins' ].includes( name ) ) {
232
- result[ name ] = this.config.get( name );
233
- }
234
- }
235
-
236
- return result;
237
- }
238
-
239
- /**
240
- * Creates and initializes a new context instance.
241
- *
242
- * const commonConfig = { ... }; // Configuration for all the plugins and editors.
243
- * const editorPlugins = [ ... ]; // Regular plugins here.
244
- *
245
- * Context
246
- * .create( {
247
- * // Only context plugins here.
248
- * plugins: [ ... ],
249
- *
250
- * // Configure the language for all the editors (it cannot be overwritten).
251
- * language: { ... },
252
- *
253
- * // Configuration for context plugins.
254
- * comments: { ... },
255
- * ...
256
- *
257
- * // Default configuration for editor plugins.
258
- * toolbar: { ... },
259
- * image: { ... },
260
- * ...
261
- * } )
262
- * .then( context => {
263
- * const promises = [];
264
- *
265
- * promises.push( ClassicEditor.create(
266
- * document.getElementById( 'editor1' ),
267
- * {
268
- * editorPlugins,
269
- * context
270
- * }
271
- * ) );
272
- *
273
- * promises.push( ClassicEditor.create(
274
- * document.getElementById( 'editor2' ),
275
- * {
276
- * editorPlugins,
277
- * context,
278
- * toolbar: { ... } // You can overwrite the configuration of the context.
279
- * }
280
- * ) );
281
- *
282
- * return Promise.all( promises );
283
- * } );
284
- *
285
- * @param {Object} [config] The context configuration.
286
- * @returns {Promise} A promise resolved once the context is ready. The promise resolves with the created context instance.
287
- */
288
- static create( config ) {
289
- return new Promise( resolve => {
290
- const context = new this( config );
291
-
292
- resolve( context.initPlugins().then( () => context ) );
293
- } );
294
- }
41
+ /**
42
+ * Creates a context instance with a given configuration.
43
+ *
44
+ * Usually not to be used directly. See the static {@link module:core/context~Context.create `create()`} method.
45
+ *
46
+ * @param {Object} [config={}] The context configuration.
47
+ */
48
+ constructor(config) {
49
+ /**
50
+ * Stores all the configurations specific to this context instance.
51
+ *
52
+ * @readonly
53
+ * @type {module:utils/config~Config}
54
+ */
55
+ this.config = new Config(config, this.constructor.defaultConfig);
56
+ const availablePlugins = this.constructor.builtinPlugins;
57
+ this.config.define('plugins', availablePlugins);
58
+ /**
59
+ * The plugins loaded and in use by this context instance.
60
+ *
61
+ * @readonly
62
+ * @type {module:core/plugincollection~PluginCollection}
63
+ */
64
+ this.plugins = new PluginCollection(this, availablePlugins);
65
+ const languageConfig = this.config.get('language') || {};
66
+ /**
67
+ * @readonly
68
+ * @type {module:utils/locale~Locale}
69
+ */
70
+ this.locale = new Locale({
71
+ uiLanguage: typeof languageConfig === 'string' ? languageConfig : languageConfig.ui,
72
+ contentLanguage: this.config.get('language.content')
73
+ });
74
+ /**
75
+ * Shorthand for {@link module:utils/locale~Locale#t}.
76
+ *
77
+ * @see module:utils/locale~Locale#t
78
+ * @method #t
79
+ */
80
+ this.t = this.locale.t;
81
+ /**
82
+ * A list of editors that this context instance is injected to.
83
+ *
84
+ * @readonly
85
+ * @type {module:utils/collection~Collection}
86
+ */
87
+ this.editors = new Collection();
88
+ /**
89
+ * Reference to the editor which created the context.
90
+ * Null when the context was created outside of the editor.
91
+ *
92
+ * It is used to destroy the context when removing the editor that has created the context.
93
+ *
94
+ * @private
95
+ * @type {module:core/editor/editor~Editor|null}
96
+ */
97
+ this._contextOwner = null;
98
+ }
99
+ /**
100
+ * Loads and initializes plugins specified in the configuration.
101
+ *
102
+ * @returns {Promise.<module:core/plugin~LoadedPlugins>} A promise which resolves
103
+ * once the initialization is completed, providing an array of loaded plugins.
104
+ */
105
+ initPlugins() {
106
+ const plugins = this.config.get('plugins') || [];
107
+ const substitutePlugins = this.config.get('substitutePlugins') || [];
108
+ // Plugins for substitution should be checked as well.
109
+ for (const Plugin of plugins.concat(substitutePlugins)) {
110
+ if (typeof Plugin != 'function') {
111
+ /**
112
+ * Only a constructor function is allowed as a {@link module:core/contextplugin~ContextPlugin context plugin}.
113
+ *
114
+ * @error context-initplugins-constructor-only
115
+ */
116
+ throw new CKEditorError('context-initplugins-constructor-only', null, { Plugin });
117
+ }
118
+ if (Plugin.isContextPlugin !== true) {
119
+ /**
120
+ * Only a plugin marked as a {@link module:core/contextplugin~ContextPlugin.isContextPlugin context plugin}
121
+ * is allowed to be used with a context.
122
+ *
123
+ * @error context-initplugins-invalid-plugin
124
+ */
125
+ throw new CKEditorError('context-initplugins-invalid-plugin', null, { Plugin });
126
+ }
127
+ }
128
+ return this.plugins.init(plugins, [], substitutePlugins);
129
+ }
130
+ /**
131
+ * Destroys the context instance and all editors used with the context,
132
+ * releasing all resources used by the context.
133
+ *
134
+ * @returns {Promise} A promise that resolves once the context instance is fully destroyed.
135
+ */
136
+ destroy() {
137
+ return Promise.all(Array.from(this.editors, editor => editor.destroy()))
138
+ .then(() => this.plugins.destroy());
139
+ }
140
+ /**
141
+ * Adds a reference to the editor which is used with this context.
142
+ *
143
+ * When the given editor has created the context, the reference to this editor will be stored
144
+ * as a {@link ~Context#_contextOwner}.
145
+ *
146
+ * This method should only be used by the editor.
147
+ *
148
+ * @protected
149
+ * @param {module:core/editor/editor~Editor} editor
150
+ * @param {Boolean} isContextOwner Stores the given editor as a context owner.
151
+ */
152
+ _addEditor(editor, isContextOwner) {
153
+ if (this._contextOwner) {
154
+ /**
155
+ * Cannot add multiple editors to the context which is created by the editor.
156
+ *
157
+ * @error context-addeditor-private-context
158
+ */
159
+ throw new CKEditorError('context-addeditor-private-context');
160
+ }
161
+ this.editors.add(editor);
162
+ if (isContextOwner) {
163
+ this._contextOwner = editor;
164
+ }
165
+ }
166
+ /**
167
+ * Removes a reference to the editor which was used with this context.
168
+ * When the context was created by the given editor, the context will be destroyed.
169
+ *
170
+ * This method should only be used by the editor.
171
+ *
172
+ * @protected
173
+ * @param {module:core/editor/editor~Editor} editor
174
+ * @return {Promise} A promise that resolves once the editor is removed from the context or when the context was destroyed.
175
+ */
176
+ _removeEditor(editor) {
177
+ if (this.editors.has(editor)) {
178
+ this.editors.remove(editor);
179
+ }
180
+ if (this._contextOwner === editor) {
181
+ return this.destroy();
182
+ }
183
+ return Promise.resolve();
184
+ }
185
+ /**
186
+ * Returns the context configuration which will be copied to the editors created using this context.
187
+ *
188
+ * The configuration returned by this method has the plugins configuration removed &mdash; plugins are shared with all editors
189
+ * through another mechanism.
190
+ *
191
+ * This method should only be used by the editor.
192
+ *
193
+ * @protected
194
+ * @returns {Object} Configuration as a plain object.
195
+ */
196
+ _getEditorConfig() {
197
+ const result = {};
198
+ for (const name of this.config.names()) {
199
+ if (!['plugins', 'removePlugins', 'extraPlugins'].includes(name)) {
200
+ result[name] = this.config.get(name);
201
+ }
202
+ }
203
+ return result;
204
+ }
205
+ /**
206
+ * Creates and initializes a new context instance.
207
+ *
208
+ * const commonConfig = { ... }; // Configuration for all the plugins and editors.
209
+ * const editorPlugins = [ ... ]; // Regular plugins here.
210
+ *
211
+ * Context
212
+ * .create( {
213
+ * // Only context plugins here.
214
+ * plugins: [ ... ],
215
+ *
216
+ * // Configure the language for all the editors (it cannot be overwritten).
217
+ * language: { ... },
218
+ *
219
+ * // Configuration for context plugins.
220
+ * comments: { ... },
221
+ * ...
222
+ *
223
+ * // Default configuration for editor plugins.
224
+ * toolbar: { ... },
225
+ * image: { ... },
226
+ * ...
227
+ * } )
228
+ * .then( context => {
229
+ * const promises = [];
230
+ *
231
+ * promises.push( ClassicEditor.create(
232
+ * document.getElementById( 'editor1' ),
233
+ * {
234
+ * editorPlugins,
235
+ * context
236
+ * }
237
+ * ) );
238
+ *
239
+ * promises.push( ClassicEditor.create(
240
+ * document.getElementById( 'editor2' ),
241
+ * {
242
+ * editorPlugins,
243
+ * context,
244
+ * toolbar: { ... } // You can overwrite the configuration of the context.
245
+ * }
246
+ * ) );
247
+ *
248
+ * return Promise.all( promises );
249
+ * } );
250
+ *
251
+ * @param {Object} [config] The context configuration.
252
+ * @returns {Promise} A promise resolved once the context is ready. The promise resolves with the created context instance.
253
+ */
254
+ static create(config) {
255
+ return new Promise(resolve => {
256
+ const context = new this(config);
257
+ resolve(context.initPlugins().then(() => context));
258
+ });
259
+ }
295
260
  }
296
-
297
- /**
298
- * An array of plugins built into the `Context` class.
299
- *
300
- * It is used in CKEditor 5 builds featuring `Context` to provide a list of context plugins which are later automatically initialized
301
- * during the context initialization.
302
- *
303
- * They will be automatically initialized by `Context` unless `config.plugins` is passed.
304
- *
305
- * // Build some context plugins into the Context class first.
306
- * Context.builtinPlugins = [ FooPlugin, BarPlugin ];
307
- *
308
- * // Normally, you need to define config.plugins, but since Context.builtinPlugins was
309
- * // defined, now you can call create() without any configuration.
310
- * Context
311
- * .create()
312
- * .then( context => {
313
- * context.plugins.get( FooPlugin ); // -> An instance of the Foo plugin.
314
- * context.plugins.get( BarPlugin ); // -> An instance of the Bar plugin.
315
- * } );
316
- *
317
- * See also {@link module:core/context~Context.defaultConfig `Context.defaultConfig`}
318
- * and {@link module:core/editor/editor~Editor.builtinPlugins `Editor.builtinPlugins`}.
319
- *
320
- * @static
321
- * @member {Array.<Function>} module:core/context~Context.builtinPlugins
322
- */
323
-
324
- /**
325
- * The default configuration which is built into the `Context` class.
326
- *
327
- * It is used in CKEditor 5 builds featuring `Context` to provide the default configuration options which are later used during the
328
- * context initialization.
329
- *
330
- * Context.defaultConfig = {
331
- * foo: 1,
332
- * bar: 2
333
- * };
334
- *
335
- * Context
336
- * .create()
337
- * .then( context => {
338
- * context.config.get( 'foo' ); // -> 1
339
- * context.config.get( 'bar' ); // -> 2
340
- * } );
341
- *
342
- * // The default options can be overridden by the configuration passed to create().
343
- * Context
344
- * .create( { bar: 3 } )
345
- * .then( context => {
346
- * context.config.get( 'foo' ); // -> 1
347
- * context.config.get( 'bar' ); // -> 3
348
- * } );
349
- *
350
- * See also {@link module:core/context~Context.builtinPlugins `Context.builtinPlugins`}
351
- * and {@link module:core/editor/editor~Editor.defaultConfig `Editor.defaultConfig`}.
352
- *
353
- * @static
354
- * @member {Object} module:core/context~Context.defaultConfig
355
- */
@@ -2,14 +2,10 @@
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
-
6
5
  /**
7
6
  * @module core/contextplugin
8
7
  */
9
-
10
- import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
11
- import mix from '@ckeditor/ckeditor5-utils/src/mix';
12
-
8
+ import { Observable } from '@ckeditor/ckeditor5-utils/src/observablemixin';
13
9
  /**
14
10
  * The base class for {@link module:core/context~Context} plugin classes.
15
11
  *
@@ -27,35 +23,32 @@ import mix from '@ckeditor/ckeditor5-utils/src/mix';
27
23
  * @implements module:core/plugin~PluginInterface
28
24
  * @mixes module:utils/observablemixin~ObservableMixin
29
25
  */
30
- export default class ContextPlugin {
31
- /**
32
- * Creates a new plugin instance.
33
- *
34
- * @param {module:core/context~Context|module:core/editor/editor~Editor} context
35
- */
36
- constructor( context ) {
37
- /**
38
- * The context instance.
39
- *
40
- * @readonly
41
- * @type {module:core/context~Context|module:core/editor/editor~Editor}
42
- */
43
- this.context = context;
44
- }
45
-
46
- /**
47
- * @inheritDoc
48
- */
49
- destroy() {
50
- this.stopListening();
51
- }
52
-
53
- /**
54
- * @inheritDoc
55
- */
56
- static get isContextPlugin() {
57
- return true;
58
- }
26
+ export default class ContextPlugin extends Observable {
27
+ /**
28
+ * Creates a new plugin instance.
29
+ *
30
+ * @param {module:core/context~Context|module:core/editor/editor~Editor} context
31
+ */
32
+ constructor(context) {
33
+ super();
34
+ /**
35
+ * The context instance.
36
+ *
37
+ * @readonly
38
+ * @type {module:core/context~Context|module:core/editor/editor~Editor}
39
+ */
40
+ this.context = context;
41
+ }
42
+ /**
43
+ * @inheritDoc
44
+ */
45
+ destroy() {
46
+ this.stopListening();
47
+ }
48
+ /**
49
+ * @inheritDoc
50
+ */
51
+ static get isContextPlugin() {
52
+ return true;
53
+ }
59
54
  }
60
-
61
- mix( ContextPlugin, ObservableMixin );