@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.
@@ -2,11 +2,9 @@
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/editor/editor
8
7
  */
9
-
10
8
  import Context from '../context';
11
9
  import Config from '@ckeditor/ckeditor5-utils/src/config';
12
10
  import EditingController from '@ckeditor/ckeditor5-engine/src/controller/editingcontroller';
@@ -16,12 +14,9 @@ import DataController from '@ckeditor/ckeditor5-engine/src/controller/datacontro
16
14
  import Conversion from '@ckeditor/ckeditor5-engine/src/conversion/conversion';
17
15
  import Model from '@ckeditor/ckeditor5-engine/src/model/model';
18
16
  import EditingKeystrokeHandler from '../editingkeystrokehandler';
19
-
20
- import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
21
- import mix from '@ckeditor/ckeditor5-utils/src/mix';
17
+ import { Observable } from '@ckeditor/ckeditor5-utils/src/observablemixin';
22
18
  import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
23
19
  import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
24
-
25
20
  /**
26
21
  * The class representing a basic, generic editor.
27
22
  *
@@ -43,440 +38,367 @@ import { StylesProcessor } from '@ckeditor/ckeditor5-engine/src/view/stylesmap';
43
38
  * @abstract
44
39
  * @mixes module:utils/observablemixin~ObservableMixin
45
40
  */
46
- export default class Editor {
47
- /**
48
- * Creates a new instance of the editor class.
49
- *
50
- * Usually, not to be used directly. See the static {@link module:core/editor/editor~Editor.create `create()`} method.
51
- *
52
- * @param {Object} [config={}] The editor configuration.
53
- */
54
- constructor( config = {} ) {
55
- // Prefer the language passed as the argument to the constructor instead of the constructor's `defaultConfig`, if both are set.
56
- const language = config.language || ( this.constructor.defaultConfig && this.constructor.defaultConfig.language );
57
-
58
- /**
59
- * The editor context.
60
- * When it is not provided through the configuration, the editor creates it.
61
- *
62
- * @protected
63
- * @type {module:core/context~Context}
64
- */
65
- this._context = config.context || new Context( { language } );
66
- this._context._addEditor( this, !config.context );
67
-
68
- // Clone the plugins to make sure that the plugin array will not be shared
69
- // between editors and make the watchdog feature work correctly.
70
- const availablePlugins = Array.from( this.constructor.builtinPlugins || [] );
71
-
72
- /**
73
- * Stores all configurations specific to this editor instance.
74
- *
75
- * editor.config.get( 'image.toolbar' );
76
- * // -> [ 'imageStyle:block', 'imageStyle:side', '|', 'toggleImageCaption', 'imageTextAlternative' ]
77
- *
78
- * @readonly
79
- * @member {module:utils/config~Config}
80
- */
81
- this.config = new Config( config, this.constructor.defaultConfig );
82
- this.config.define( 'plugins', availablePlugins );
83
- this.config.define( this._context._getEditorConfig() );
84
-
85
- /**
86
- * The plugins loaded and in use by this editor instance.
87
- *
88
- * editor.plugins.get( 'ClipboardPipeline' ); // -> An instance of the clipboard pipeline plugin.
89
- *
90
- * @readonly
91
- * @member {module:core/plugincollection~PluginCollection}
92
- */
93
- this.plugins = new PluginCollection( this, availablePlugins, this._context.plugins );
94
-
95
- /**
96
- * The locale instance.
97
- *
98
- * @readonly
99
- * @type {module:utils/locale~Locale}
100
- */
101
- this.locale = this._context.locale;
102
-
103
- /**
104
- * Shorthand for {@link module:utils/locale~Locale#t}.
105
- *
106
- * @see module:utils/locale~Locale#t
107
- * @method #t
108
- */
109
- this.t = this.locale.t;
110
-
111
- /**
112
- * A set of lock IDs for the {@link #isReadOnly} getter.
113
- *
114
- * @private
115
- * @type {Set.<String|Symbol>}
116
- */
117
- this._readOnlyLocks = new Set();
118
-
119
- /**
120
- * Commands registered to the editor.
121
- *
122
- * Use the shorthand {@link #execute `editor.execute()`} method to execute commands:
123
- *
124
- * // Execute the bold command:
125
- * editor.execute( 'bold' );
126
- *
127
- * // Check the state of the bold command:
128
- * editor.commands.get( 'bold' ).value;
129
- *
130
- * @readonly
131
- * @member {module:core/commandcollection~CommandCollection}
132
- */
133
- this.commands = new CommandCollection();
134
-
135
- /**
136
- * Indicates the editor life-cycle state.
137
- *
138
- * The editor is in one of the following states:
139
- *
140
- * * `initializing` &ndash; During the editor initialization (before
141
- * {@link module:core/editor/editor~Editor.create `Editor.create()`}) finished its job.
142
- * * `ready` &ndash; After the promise returned by the {@link module:core/editor/editor~Editor.create `Editor.create()`}
143
- * method is resolved.
144
- * * `destroyed` &ndash; Once the {@link #destroy `editor.destroy()`} method was called.
145
- *
146
- * @observable
147
- * @member {'initializing'|'ready'|'destroyed'} #state
148
- */
149
- this.set( 'state', 'initializing' );
150
- this.once( 'ready', () => ( this.state = 'ready' ), { priority: 'high' } );
151
- this.once( 'destroy', () => ( this.state = 'destroyed' ), { priority: 'high' } );
152
-
153
- /**
154
- * The editor's model.
155
- *
156
- * The central point of the editor's abstract data model.
157
- *
158
- * @readonly
159
- * @member {module:engine/model/model~Model}
160
- */
161
- this.model = new Model();
162
-
163
- const stylesProcessor = new StylesProcessor();
164
-
165
- /**
166
- * The {@link module:engine/controller/datacontroller~DataController data controller}.
167
- * Used e.g. for setting and retrieving the editor data.
168
- *
169
- * @readonly
170
- * @member {module:engine/controller/datacontroller~DataController}
171
- */
172
- this.data = new DataController( this.model, stylesProcessor );
173
-
174
- /**
175
- * The {@link module:engine/controller/editingcontroller~EditingController editing controller}.
176
- * Controls user input and rendering the content for editing.
177
- *
178
- * @readonly
179
- * @member {module:engine/controller/editingcontroller~EditingController}
180
- */
181
- this.editing = new EditingController( this.model, stylesProcessor );
182
- this.editing.view.document.bind( 'isReadOnly' ).to( this );
183
-
184
- /**
185
- * Conversion manager through which you can register model-to-view and view-to-model converters.
186
- *
187
- * See the {@link module:engine/conversion/conversion~Conversion} documentation to learn how to add converters.
188
- *
189
- * @readonly
190
- * @member {module:engine/conversion/conversion~Conversion}
191
- */
192
- this.conversion = new Conversion( [ this.editing.downcastDispatcher, this.data.downcastDispatcher ], this.data.upcastDispatcher );
193
- this.conversion.addAlias( 'dataDowncast', this.data.downcastDispatcher );
194
- this.conversion.addAlias( 'editingDowncast', this.editing.downcastDispatcher );
195
-
196
- /**
197
- * An instance of the {@link module:core/editingkeystrokehandler~EditingKeystrokeHandler}.
198
- *
199
- * It allows setting simple keystrokes:
200
- *
201
- * // Execute the bold command on Ctrl+E:
202
- * editor.keystrokes.set( 'Ctrl+E', 'bold' );
203
- *
204
- * // Execute your own callback:
205
- * editor.keystrokes.set( 'Ctrl+E', ( data, cancel ) => {
206
- * console.log( data.keyCode );
207
- *
208
- * // Prevent the default (native) action and stop the underlying keydown event
209
- * // so no other editor feature will interfere.
210
- * cancel();
211
- * } );
212
- *
213
- * Note: Certain typing-oriented keystrokes (like <kbd>Backspace</kbd> or <kbd>Enter</kbd>) are handled
214
- * by a low-level mechanism and trying to listen to them via the keystroke handler will not work reliably.
215
- * To handle these specific keystrokes, see the events fired by the
216
- * {@link module:engine/view/document~Document editing view document} (`editor.editing.view.document`).
217
- *
218
- * @readonly
219
- * @member {module:core/editingkeystrokehandler~EditingKeystrokeHandler}
220
- */
221
- this.keystrokes = new EditingKeystrokeHandler( this );
222
- this.keystrokes.listenTo( this.editing.view.document );
223
- }
224
-
225
- /**
226
- * Defines whether the editor is in the read-only mode.
227
- *
228
- * In read-only mode the editor {@link #commands commands} are disabled so it is not possible
229
- * to modify the document by using them. Also, the editable element(s) become non-editable.
230
- *
231
- * In order to make the editor read-only, you need to call the {@link #enableReadOnlyMode} method:
232
- *
233
- * editor.enableReadOnlyMode( 'feature-id' );
234
- *
41
+ export default class Editor extends Observable {
42
+ /**
43
+ * Creates a new instance of the editor class.
44
+ *
45
+ * Usually, not to be used directly. See the static {@link module:core/editor/editor~Editor.create `create()`} method.
46
+ *
47
+ * @param {Object} [config={}] The editor configuration.
48
+ */
49
+ constructor(config = {}) {
50
+ super();
51
+ const constructor = this.constructor;
52
+ // Prefer the language passed as the argument to the constructor instead of the constructor's `defaultConfig`, if both are set.
53
+ const language = config.language || (constructor.defaultConfig && constructor.defaultConfig.language);
54
+ /**
55
+ * The editor context.
56
+ * When it is not provided through the configuration, the editor creates it.
57
+ *
58
+ * @protected
59
+ * @type {module:core/context~Context}
60
+ */
61
+ this._context = config.context || new Context({ language });
62
+ this._context._addEditor(this, !config.context);
63
+ // Clone the plugins to make sure that the plugin array will not be shared
64
+ // between editors and make the watchdog feature work correctly.
65
+ const availablePlugins = Array.from(constructor.builtinPlugins || []);
66
+ /**
67
+ * Stores all configurations specific to this editor instance.
68
+ *
69
+ * editor.config.get( 'image.toolbar' );
70
+ * // -> [ 'imageStyle:block', 'imageStyle:side', '|', 'toggleImageCaption', 'imageTextAlternative' ]
71
+ *
72
+ * @readonly
73
+ * @member {module:utils/config~Config}
74
+ */
75
+ this.config = new Config(config, constructor.defaultConfig);
76
+ this.config.define('plugins', availablePlugins);
77
+ this.config.define(this._context._getEditorConfig());
78
+ /**
79
+ * The plugins loaded and in use by this editor instance.
80
+ *
81
+ * editor.plugins.get( 'ClipboardPipeline' ); // -> An instance of the clipboard pipeline plugin.
82
+ *
83
+ * @readonly
84
+ * @member {module:core/plugincollection~PluginCollection}
85
+ */
86
+ this.plugins = new PluginCollection(this, availablePlugins, this._context.plugins);
87
+ /**
88
+ * The locale instance.
89
+ *
90
+ * @readonly
91
+ * @type {module:utils/locale~Locale}
92
+ */
93
+ this.locale = this._context.locale;
94
+ /**
95
+ * Shorthand for {@link module:utils/locale~Locale#t}.
96
+ *
97
+ * @see module:utils/locale~Locale#t
98
+ * @method #t
99
+ */
100
+ this.t = this.locale.t;
101
+ /**
102
+ * A set of lock IDs for the {@link #isReadOnly} getter.
103
+ *
104
+ * @private
105
+ * @type {Set.<String|Symbol>}
106
+ */
107
+ this._readOnlyLocks = new Set();
108
+ /**
109
+ * Commands registered to the editor.
110
+ *
111
+ * Use the shorthand {@link #execute `editor.execute()`} method to execute commands:
112
+ *
113
+ * // Execute the bold command:
114
+ * editor.execute( 'bold' );
115
+ *
116
+ * // Check the state of the bold command:
117
+ * editor.commands.get( 'bold' ).value;
118
+ *
119
+ * @readonly
120
+ * @member {module:core/commandcollection~CommandCollection}
121
+ */
122
+ this.commands = new CommandCollection();
123
+ /**
124
+ * Indicates the editor life-cycle state.
125
+ *
126
+ * The editor is in one of the following states:
127
+ *
128
+ * * `initializing` &ndash; During the editor initialization (before
129
+ * {@link module:core/editor/editor~Editor.create `Editor.create()`}) finished its job.
130
+ * * `ready` &ndash; After the promise returned by the {@link module:core/editor/editor~Editor.create `Editor.create()`}
131
+ * method is resolved.
132
+ * * `destroyed` &ndash; Once the {@link #destroy `editor.destroy()`} method was called.
133
+ *
134
+ * @observable
135
+ * @member {'initializing'|'ready'|'destroyed'} #state
136
+ */
137
+ this.set('state', 'initializing');
138
+ this.once('ready', () => (this.state = 'ready'), { priority: 'high' });
139
+ this.once('destroy', () => (this.state = 'destroyed'), { priority: 'high' });
140
+ /**
141
+ * The editor's model.
142
+ *
143
+ * The central point of the editor's abstract data model.
144
+ *
145
+ * @readonly
146
+ * @member {module:engine/model/model~Model}
147
+ */
148
+ this.model = new Model();
149
+ const stylesProcessor = new StylesProcessor();
150
+ /**
151
+ * The {@link module:engine/controller/datacontroller~DataController data controller}.
152
+ * Used e.g. for setting and retrieving the editor data.
153
+ *
154
+ * @readonly
155
+ * @member {module:engine/controller/datacontroller~DataController}
156
+ */
157
+ this.data = new DataController(this.model, stylesProcessor);
158
+ /**
159
+ * The {@link module:engine/controller/editingcontroller~EditingController editing controller}.
160
+ * Controls user input and rendering the content for editing.
161
+ *
162
+ * @readonly
163
+ * @member {module:engine/controller/editingcontroller~EditingController}
164
+ */
165
+ this.editing = new EditingController(this.model, stylesProcessor);
166
+ this.editing.view.document.bind('isReadOnly').to(this);
167
+ /**
168
+ * Conversion manager through which you can register model-to-view and view-to-model converters.
169
+ *
170
+ * See the {@link module:engine/conversion/conversion~Conversion} documentation to learn how to add converters.
171
+ *
172
+ * @readonly
173
+ * @member {module:engine/conversion/conversion~Conversion}
174
+ */
175
+ this.conversion = new Conversion([this.editing.downcastDispatcher, this.data.downcastDispatcher], this.data.upcastDispatcher);
176
+ this.conversion.addAlias('dataDowncast', this.data.downcastDispatcher);
177
+ this.conversion.addAlias('editingDowncast', this.editing.downcastDispatcher);
178
+ /**
179
+ * An instance of the {@link module:core/editingkeystrokehandler~EditingKeystrokeHandler}.
180
+ *
181
+ * It allows setting simple keystrokes:
182
+ *
183
+ * // Execute the bold command on Ctrl+E:
184
+ * editor.keystrokes.set( 'Ctrl+E', 'bold' );
185
+ *
186
+ * // Execute your own callback:
187
+ * editor.keystrokes.set( 'Ctrl+E', ( data, cancel ) => {
188
+ * console.log( data.keyCode );
189
+ *
190
+ * // Prevent the default (native) action and stop the underlying keydown event
191
+ * // so no other editor feature will interfere.
192
+ * cancel();
193
+ * } );
194
+ *
195
+ * Note: Certain typing-oriented keystrokes (like <kbd>Backspace</kbd> or <kbd>Enter</kbd>) are handled
196
+ * by a low-level mechanism and trying to listen to them via the keystroke handler will not work reliably.
197
+ * To handle these specific keystrokes, see the events fired by the
198
+ * {@link module:engine/view/document~Document editing view document} (`editor.editing.view.document`).
199
+ *
200
+ * @readonly
201
+ * @member {module:core/editingkeystrokehandler~EditingKeystrokeHandler}
202
+ */
203
+ this.keystrokes = new EditingKeystrokeHandler(this);
204
+ this.keystrokes.listenTo(this.editing.view.document);
205
+ }
206
+ /**
207
+ * Defines whether the editor is in the read-only mode.
208
+ *
209
+ * In read-only mode the editor {@link #commands commands} are disabled so it is not possible
210
+ * to modify the document by using them. Also, the editable element(s) become non-editable.
211
+ *
212
+ * In order to make the editor read-only, you need to call the {@link #enableReadOnlyMode} method:
213
+ *
214
+ * editor.enableReadOnlyMode( 'feature-id' );
215
+ *
235
216
  * Later, to turn off the read-only mode, call {@link #disableReadOnlyMode}:
236
- *
237
- * editor.disableReadOnlyMode( 'feature-id' );
238
- *
239
- * @readonly
240
- * @observable
241
- * @member {Boolean} #isReadOnly
242
- */
243
- get isReadOnly() {
244
- return this._readOnlyLocks.size > 0;
245
- }
246
-
247
- set isReadOnly( value ) {
248
- /**
249
- * The {@link #isReadOnly Editor#isReadOnly} property is read-only since version `34.0.0` and can be set only using
250
- * {@link #enableReadOnlyMode `Editor#enableReadOnlyMode( lockId )`} and
251
- * {@link #disableReadOnlyMode `Editor#disableReadOnlyMode( lockId )`}.
252
- *
253
- * Usage before version `34.0.0`:
254
- *
255
- * editor.isReadOnly = true;
256
- * editor.isReadOnly = false;
257
- *
258
- * Usage since version `34.0.0`:
259
- *
260
- * editor.enableReadOnlyMode( 'my-feature-id' );
261
- * editor.disableReadOnlyMode( 'my-feature-id' );
262
- *
263
- * @error editor-isreadonly-has-no-setter
264
- */
265
- throw new CKEditorError( 'editor-isreadonly-has-no-setter' );
266
- }
267
-
268
- /**
269
- * Turns on the read-only mode in the editor.
270
- *
271
- * Editor can be switched to or out of the read-only mode by many features, under various circumstances. The editor supports locking
272
- * mechanism for the read-only mode. It enables easy control over the read-only mode when many features wants to turn it on or off at
273
- * the same time, without conflicting with each other. It guarantees that you will not make the editor editable accidentally (which
274
- * could lead to errors).
275
- *
276
- * Each read-only mode request is identified by a unique id (also called "lock"). If multiple plugins requested to turn on the
277
- * read-only mode, then, the editor will become editable only after all these plugins turn the read-only mode off (using the same ids).
278
- *
279
- * Note, that you cannot force the editor to disable the read-only mode if other plugins set it.
280
- *
281
- * After the first `enableReadOnlyMode()` call, the {@link #isReadOnly `isReadOnly` property} will be set to `true`:
282
- *
283
- * editor.isReadOnly; // `false`.
284
- * editor.enableReadOnlyMode( 'my-feature-id' );
285
- * editor.isReadOnly; // `true`.
286
- *
287
- * You can turn off the read-only mode ("clear the lock") using the {@link #disableReadOnlyMode `disableReadOnlyMode()`} method:
288
- *
289
- * editor.enableReadOnlyMode( 'my-feature-id' );
290
- * // ...
291
- * editor.disableReadOnlyMode( 'my-feature-id' );
292
- * editor.isReadOnly; // `false`.
293
- *
294
- * All "locks" need to be removed to enable editing:
295
- *
296
- * editor.enableReadOnlyMode( 'my-feature-id' );
297
- * editor.enableReadOnlyMode( 'my-other-feature-id' );
298
- * // ...
299
- * editor.disableReadOnlyMode( 'my-feature-id' );
300
- * editor.isReadOnly; // `true`.
301
- * editor.disableReadOnlyMode( 'my-other-feature-id' );
302
- * editor.isReadOnly; // `false`.
303
- *
304
- * @param {String|Symbol} lockId A unique ID for setting the editor to the read-only state.
305
- */
306
- enableReadOnlyMode( lockId ) {
307
- if ( typeof lockId !== 'string' && typeof lockId !== 'symbol' ) {
308
- /**
309
- * The lock ID is missing or it is not a string or symbol.
310
- *
311
- * @error editor-read-only-lock-id-invalid
312
- */
313
- throw new CKEditorError( 'editor-read-only-lock-id-invalid', null, { lockId } );
314
- }
315
-
316
- if ( this._readOnlyLocks.has( lockId ) ) {
317
- return;
318
- }
319
-
320
- this._readOnlyLocks.add( lockId );
321
-
322
- if ( this._readOnlyLocks.size === 1 ) {
323
- // Manually fire the `change:isReadOnly` event as only getter is provided.
324
- this.fire( 'change:isReadOnly', 'isReadOnly', true, false );
325
- }
326
- }
327
-
328
- /**
329
- * Removes the read-only lock from the editor with given lock ID.
330
- *
331
- * When no lock is present on the editor anymore, then the {@link #isReadOnly `isReadOnly` property} will be set to `false`.
332
- *
333
- * @param {String|Symbol} lockId The lock ID for setting the editor to the read-only state.
334
- */
335
- disableReadOnlyMode( lockId ) {
336
- if ( typeof lockId !== 'string' && typeof lockId !== 'symbol' ) {
337
- throw new CKEditorError( 'editor-read-only-lock-id-invalid', null, { lockId } );
338
- }
339
-
340
- if ( !this._readOnlyLocks.has( lockId ) ) {
341
- return;
342
- }
343
-
344
- this._readOnlyLocks.delete( lockId );
345
-
346
- if ( this._readOnlyLocks.size === 0 ) {
347
- // Manually fire the `change:isReadOnly` event as only getter is provided.
348
- this.fire( 'change:isReadOnly', 'isReadOnly', false, true );
349
- }
350
- }
351
-
352
- /**
353
- * Loads and initializes plugins specified in the configuration.
354
- *
355
- * @returns {Promise.<module:core/plugin~LoadedPlugins>} A promise which resolves
356
- * once the initialization is completed, providing an array of loaded plugins.
357
- */
358
- initPlugins() {
359
- const config = this.config;
360
- const plugins = config.get( 'plugins' );
361
- const removePlugins = config.get( 'removePlugins' ) || [];
362
- const extraPlugins = config.get( 'extraPlugins' ) || [];
363
- const substitutePlugins = config.get( 'substitutePlugins' ) || [];
364
-
365
- return this.plugins.init( plugins.concat( extraPlugins ), removePlugins, substitutePlugins );
366
- }
367
-
368
- /**
369
- * Destroys the editor instance, releasing all resources used by it.
370
- *
371
- * **Note** The editor cannot be destroyed during the initialization phase so if it is called
372
- * while the editor {@link #state is being initialized}, it will wait for the editor initialization before destroying it.
373
- *
374
- * @fires destroy
375
- * @returns {Promise} A promise that resolves once the editor instance is fully destroyed.
376
- */
377
- destroy() {
378
- let readyPromise = Promise.resolve();
379
-
380
- if ( this.state == 'initializing' ) {
381
- readyPromise = new Promise( resolve => this.once( 'ready', resolve ) );
382
- }
383
-
384
- return readyPromise
385
- .then( () => {
386
- this.fire( 'destroy' );
387
- this.stopListening();
388
- this.commands.destroy();
389
- } )
390
- .then( () => this.plugins.destroy() )
391
- .then( () => {
392
- this.model.destroy();
393
- this.data.destroy();
394
- this.editing.destroy();
395
- this.keystrokes.destroy();
396
- } )
397
- // Remove the editor from the context.
398
- // When the context was created by this editor, the context will be destroyed.
399
- .then( () => this._context._removeEditor( this ) );
400
- }
401
-
402
- /**
403
- * Executes the specified command with given parameters.
404
- *
405
- * Shorthand for:
406
- *
407
- * editor.commands.get( commandName ).execute( ... );
408
- *
409
- * @param {String} commandName The name of the command to execute.
410
- * @param {*} [...commandParams] Command parameters.
411
- * @returns {*} The value returned by the {@link module:core/commandcollection~CommandCollection#execute `commands.execute()`}.
412
- */
413
- execute( ...args ) {
414
- try {
415
- return this.commands.execute( ...args );
416
- } catch ( err ) {
417
- // @if CK_DEBUG // throw err;
418
- /* istanbul ignore next */
419
- CKEditorError.rethrowUnexpectedError( err, this );
420
- }
421
- }
422
-
423
- /**
424
- * Focuses the editor.
425
- *
426
- * **Note** To explicitly focus the editing area of the editor, use the
427
- * {@link module:engine/view/view~View#focus `editor.editing.view.focus()`} method of the editing view.
428
- *
429
- * Check out the {@glink framework/guides/deep-dive/ui/focus-tracking#focus-in-the-editor-ui Focus in the editor UI} section
430
- * of the {@glink framework/guides/deep-dive/ui/focus-tracking Deep dive into focus tracking} guide to learn more.
431
- */
432
- focus() {
433
- this.editing.view.focus();
434
- }
435
-
436
- /**
437
- * Creates and initializes a new editor instance.
438
- *
439
- * This is an abstract method. Every editor type needs to implement its own initialization logic.
440
- *
441
- * See the `create()` methods of the existing editor types to learn how to use them:
442
- *
443
- * * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}
444
- * * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}
445
- * * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}
446
- * * {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`}
447
- *
448
- * @abstract
449
- * @method module:core/editor/editor~Editor.create
450
- */
217
+ *
218
+ * editor.disableReadOnlyMode( 'feature-id' );
219
+ *
220
+ * @readonly
221
+ * @observable
222
+ * @member {Boolean} #isReadOnly
223
+ */
224
+ get isReadOnly() {
225
+ return this._readOnlyLocks.size > 0;
226
+ }
227
+ set isReadOnly(value) {
228
+ /**
229
+ * The {@link #isReadOnly Editor#isReadOnly} property is read-only since version `34.0.0` and can be set only using
230
+ * {@link #enableReadOnlyMode `Editor#enableReadOnlyMode( lockId )`} and
231
+ * {@link #disableReadOnlyMode `Editor#disableReadOnlyMode( lockId )`}.
232
+ *
233
+ * Usage before version `34.0.0`:
234
+ *
235
+ * editor.isReadOnly = true;
236
+ * editor.isReadOnly = false;
237
+ *
238
+ * Usage since version `34.0.0`:
239
+ *
240
+ * editor.enableReadOnlyMode( 'my-feature-id' );
241
+ * editor.disableReadOnlyMode( 'my-feature-id' );
242
+ *
243
+ * @error editor-isreadonly-has-no-setter
244
+ */
245
+ throw new CKEditorError('editor-isreadonly-has-no-setter');
246
+ }
247
+ /**
248
+ * Turns on the read-only mode in the editor.
249
+ *
250
+ * Editor can be switched to or out of the read-only mode by many features, under various circumstances. The editor supports locking
251
+ * mechanism for the read-only mode. It enables easy control over the read-only mode when many features wants to turn it on or off at
252
+ * the same time, without conflicting with each other. It guarantees that you will not make the editor editable accidentally (which
253
+ * could lead to errors).
254
+ *
255
+ * Each read-only mode request is identified by a unique id (also called "lock"). If multiple plugins requested to turn on the
256
+ * read-only mode, then, the editor will become editable only after all these plugins turn the read-only mode off (using the same ids).
257
+ *
258
+ * Note, that you cannot force the editor to disable the read-only mode if other plugins set it.
259
+ *
260
+ * After the first `enableReadOnlyMode()` call, the {@link #isReadOnly `isReadOnly` property} will be set to `true`:
261
+ *
262
+ * editor.isReadOnly; // `false`.
263
+ * editor.enableReadOnlyMode( 'my-feature-id' );
264
+ * editor.isReadOnly; // `true`.
265
+ *
266
+ * You can turn off the read-only mode ("clear the lock") using the {@link #disableReadOnlyMode `disableReadOnlyMode()`} method:
267
+ *
268
+ * editor.enableReadOnlyMode( 'my-feature-id' );
269
+ * // ...
270
+ * editor.disableReadOnlyMode( 'my-feature-id' );
271
+ * editor.isReadOnly; // `false`.
272
+ *
273
+ * All "locks" need to be removed to enable editing:
274
+ *
275
+ * editor.enableReadOnlyMode( 'my-feature-id' );
276
+ * editor.enableReadOnlyMode( 'my-other-feature-id' );
277
+ * // ...
278
+ * editor.disableReadOnlyMode( 'my-feature-id' );
279
+ * editor.isReadOnly; // `true`.
280
+ * editor.disableReadOnlyMode( 'my-other-feature-id' );
281
+ * editor.isReadOnly; // `false`.
282
+ *
283
+ * @param {String|Symbol} lockId A unique ID for setting the editor to the read-only state.
284
+ */
285
+ enableReadOnlyMode(lockId) {
286
+ if (typeof lockId !== 'string' && typeof lockId !== 'symbol') {
287
+ /**
288
+ * The lock ID is missing or it is not a string or symbol.
289
+ *
290
+ * @error editor-read-only-lock-id-invalid
291
+ */
292
+ throw new CKEditorError('editor-read-only-lock-id-invalid', null, { lockId });
293
+ }
294
+ if (this._readOnlyLocks.has(lockId)) {
295
+ return;
296
+ }
297
+ this._readOnlyLocks.add(lockId);
298
+ if (this._readOnlyLocks.size === 1) {
299
+ // Manually fire the `change:isReadOnly` event as only getter is provided.
300
+ this.fire('change:isReadOnly', 'isReadOnly', true, false);
301
+ }
302
+ }
303
+ /**
304
+ * Removes the read-only lock from the editor with given lock ID.
305
+ *
306
+ * When no lock is present on the editor anymore, then the {@link #isReadOnly `isReadOnly` property} will be set to `false`.
307
+ *
308
+ * @param {String|Symbol} lockId The lock ID for setting the editor to the read-only state.
309
+ */
310
+ disableReadOnlyMode(lockId) {
311
+ if (typeof lockId !== 'string' && typeof lockId !== 'symbol') {
312
+ throw new CKEditorError('editor-read-only-lock-id-invalid', null, { lockId });
313
+ }
314
+ if (!this._readOnlyLocks.has(lockId)) {
315
+ return;
316
+ }
317
+ this._readOnlyLocks.delete(lockId);
318
+ if (this._readOnlyLocks.size === 0) {
319
+ // Manually fire the `change:isReadOnly` event as only getter is provided.
320
+ this.fire('change:isReadOnly', 'isReadOnly', false, true);
321
+ }
322
+ }
323
+ /**
324
+ * Loads and initializes plugins specified in the configuration.
325
+ *
326
+ * @returns {Promise.<module:core/plugin~LoadedPlugins>} A promise which resolves
327
+ * once the initialization is completed, providing an array of loaded plugins.
328
+ */
329
+ initPlugins() {
330
+ const config = this.config;
331
+ const plugins = config.get('plugins');
332
+ const removePlugins = config.get('removePlugins') || [];
333
+ const extraPlugins = config.get('extraPlugins') || [];
334
+ const substitutePlugins = config.get('substitutePlugins') || [];
335
+ return this.plugins.init(plugins.concat(extraPlugins), removePlugins, substitutePlugins);
336
+ }
337
+ /**
338
+ * Destroys the editor instance, releasing all resources used by it.
339
+ *
340
+ * **Note** The editor cannot be destroyed during the initialization phase so if it is called
341
+ * while the editor {@link #state is being initialized}, it will wait for the editor initialization before destroying it.
342
+ *
343
+ * @fires destroy
344
+ * @returns {Promise} A promise that resolves once the editor instance is fully destroyed.
345
+ */
346
+ destroy() {
347
+ let readyPromise = Promise.resolve();
348
+ if (this.state == 'initializing') {
349
+ readyPromise = new Promise(resolve => this.once('ready', resolve));
350
+ }
351
+ return readyPromise
352
+ .then(() => {
353
+ this.fire('destroy');
354
+ this.stopListening();
355
+ this.commands.destroy();
356
+ })
357
+ .then(() => this.plugins.destroy())
358
+ .then(() => {
359
+ this.model.destroy();
360
+ this.data.destroy();
361
+ this.editing.destroy();
362
+ this.keystrokes.destroy();
363
+ })
364
+ // Remove the editor from the context.
365
+ // When the context was created by this editor, the context will be destroyed.
366
+ .then(() => this._context._removeEditor(this));
367
+ }
368
+ /**
369
+ * Executes the specified command with given parameters.
370
+ *
371
+ * Shorthand for:
372
+ *
373
+ * editor.commands.get( commandName ).execute( ... );
374
+ *
375
+ * @param {String} commandName The name of the command to execute.
376
+ * @param {*} [...commandParams] Command parameters.
377
+ * @returns {*} The value returned by the {@link module:core/commandcollection~CommandCollection#execute `commands.execute()`}.
378
+ */
379
+ execute(commandName, ...args) {
380
+ try {
381
+ return this.commands.execute(commandName, ...args);
382
+ }
383
+ catch (err) {
384
+ // @if CK_DEBUG // throw err;
385
+ /* istanbul ignore next */
386
+ CKEditorError.rethrowUnexpectedError(err, this);
387
+ }
388
+ }
389
+ /**
390
+ * Focuses the editor.
391
+ *
392
+ * **Note** To explicitly focus the editing area of the editor, use the
393
+ * {@link module:engine/view/view~View#focus `editor.editing.view.focus()`} method of the editing view.
394
+ *
395
+ * Check out the {@glink framework/guides/deep-dive/ui/focus-tracking#focus-in-the-editor-ui Focus in the editor UI} section
396
+ * of the {@glink framework/guides/deep-dive/ui/focus-tracking Deep dive into focus tracking} guide to learn more.
397
+ */
398
+ focus() {
399
+ this.editing.view.focus();
400
+ }
451
401
  }
452
-
453
- mix( Editor, ObservableMixin );
454
-
455
- /**
456
- * Fired when the {@link module:engine/controller/datacontroller~DataController#event:ready data} and all additional
457
- * editor components are ready.
458
- *
459
- * Note: This event is most useful for plugin developers. When integrating the editor with your website or
460
- * application, you do not have to listen to `editor#ready` because when the promise returned by the static
461
- * {@link module:core/editor/editor~Editor.create `Editor.create()`} event is resolved, the editor is already ready.
462
- * In fact, since the first moment when the editor instance is available to you is inside `then()`'s callback,
463
- * you cannot even add a listener to the `editor#ready` event.
464
- *
465
- * See also the {@link #state `editor.state`} property.
466
- *
467
- * @event ready
468
- */
469
-
470
- /**
471
- * Fired when this editor instance is destroyed. The editor at this point is not usable and this event should be used to
472
- * perform the clean-up in any plugin.
473
- *
474
- *
475
- * See also the {@link #state `editor.state`} property.
476
- *
477
- * @event destroy
478
- */
479
-
480
402
  /**
481
403
  * This error is thrown when trying to pass a `<textarea>` element to a `create()` function of an editor class.
482
404
  *
@@ -491,7 +413,6 @@ mix( Editor, ObservableMixin );
491
413
  *
492
414
  * @error editor-wrong-element
493
415
  */
494
-
495
416
  /**
496
417
  * An array of plugins built into this editor class.
497
418
  *
@@ -539,7 +460,6 @@ mix( Editor, ObservableMixin );
539
460
  * @static
540
461
  * @member {Array.<Function>} module:core/editor/editor~Editor.builtinPlugins
541
462
  */
542
-
543
463
  /**
544
464
  * The default configuration which is built into the editor class.
545
465
  *