1 | /**
|
2 | * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
3 | * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4 | */
|
5 | /**
|
6 | * @module core/editor/editor
|
7 | */
|
8 | import { Config, type Locale, type LocaleTranslate } from '@ckeditor/ckeditor5-utils';
|
9 | import { Conversion, DataController, EditingController, Model } from '@ckeditor/ckeditor5-engine';
|
10 | import type { EditorUI } from '@ckeditor/ckeditor5-ui';
|
11 | import { ContextWatchdog, EditorWatchdog } from '@ckeditor/ckeditor5-watchdog';
|
12 | import Context from '../context.js';
|
13 | import PluginCollection from '../plugincollection.js';
|
14 | import CommandCollection, { type CommandsMap } from '../commandcollection.js';
|
15 | import EditingKeystrokeHandler from '../editingkeystrokehandler.js';
|
16 | import Accessibility from '../accessibility.js';
|
17 | import type { LoadedPlugins, PluginConstructor } from '../plugin.js';
|
18 | import type { EditorConfig } from './editorconfig.js';
|
19 | declare const Editor_base: {
|
20 | new (): import("@ckeditor/ckeditor5-utils").Observable;
|
21 | prototype: import("@ckeditor/ckeditor5-utils").Observable;
|
22 | };
|
23 | /**
|
24 | * The class representing a basic, generic editor.
|
25 | *
|
26 | * Check out the list of its subclasses to learn about specific editor implementations.
|
27 | *
|
28 | * All editor implementations (like {@link module:editor-classic/classiceditor~ClassicEditor} or
|
29 | * {@link module:editor-inline/inlineeditor~InlineEditor}) should extend this class. They can add their
|
30 | * own methods and properties.
|
31 | *
|
32 | * When you are implementing a plugin, this editor represents the API
|
33 | * which your plugin can expect to get when using its {@link module:core/plugin~Plugin#editor} property.
|
34 | *
|
35 | * This API should be sufficient in order to implement the "editing" part of your feature
|
36 | * (schema definition, conversion, commands, keystrokes, etc.).
|
37 | * It does not define the editor UI, which is available only if
|
38 | * the specific editor implements also the {@link ~Editor#ui} property
|
39 | * (as most editor implementations do).
|
40 | */
|
41 | export default abstract class Editor extends /* #__PURE__ */ Editor_base {
|
42 | /**
|
43 | * A namespace for the accessibility features of the editor.
|
44 | */
|
45 | readonly accessibility: Accessibility;
|
46 | /**
|
47 | * Commands registered to the editor.
|
48 | *
|
49 | * Use the shorthand {@link #execute `editor.execute()`} method to execute commands:
|
50 | *
|
51 | * ```ts
|
52 | * // Execute the bold command:
|
53 | * editor.execute( 'bold' );
|
54 | *
|
55 | * // Check the state of the bold command:
|
56 | * editor.commands.get( 'bold' ).value;
|
57 | * ```
|
58 | */
|
59 | readonly commands: CommandCollection;
|
60 | /**
|
61 | * Stores all configurations specific to this editor instance.
|
62 | *
|
63 | * ```ts
|
64 | * editor.config.get( 'image.toolbar' );
|
65 | * // -> [ 'imageStyle:block', 'imageStyle:side', '|', 'toggleImageCaption', 'imageTextAlternative' ]
|
66 | * ```
|
67 | */
|
68 | readonly config: Config<EditorConfig>;
|
69 | /**
|
70 | * Conversion manager through which you can register model-to-view and view-to-model converters.
|
71 | *
|
72 | * See the {@link module:engine/conversion/conversion~Conversion} documentation to learn how to add converters.
|
73 | */
|
74 | readonly conversion: Conversion;
|
75 | /**
|
76 | * The {@link module:engine/controller/datacontroller~DataController data controller}.
|
77 | * Used e.g. for setting and retrieving the editor data.
|
78 | */
|
79 | readonly data: DataController;
|
80 | /**
|
81 | * The {@link module:engine/controller/editingcontroller~EditingController editing controller}.
|
82 | * Controls user input and rendering the content for editing.
|
83 | */
|
84 | readonly editing: EditingController;
|
85 | /**
|
86 | * The locale instance.
|
87 | */
|
88 | readonly locale: Locale;
|
89 | /**
|
90 | * The editor's model.
|
91 | *
|
92 | * The central point of the editor's abstract data model.
|
93 | */
|
94 | readonly model: Model;
|
95 | /**
|
96 | * The plugins loaded and in use by this editor instance.
|
97 | *
|
98 | * ```ts
|
99 | * editor.plugins.get( 'ClipboardPipeline' ); // -> An instance of the clipboard pipeline plugin.
|
100 | * ```
|
101 | */
|
102 | readonly plugins: PluginCollection<Editor>;
|
103 | /**
|
104 | * An instance of the {@link module:core/editingkeystrokehandler~EditingKeystrokeHandler}.
|
105 | *
|
106 | * It allows setting simple keystrokes:
|
107 | *
|
108 | * ```ts
|
109 | * // Execute the bold command on Ctrl+E:
|
110 | * editor.keystrokes.set( 'Ctrl+E', 'bold' );
|
111 | *
|
112 | * // Execute your own callback:
|
113 | * editor.keystrokes.set( 'Ctrl+E', ( data, cancel ) => {
|
114 | * console.log( data.keyCode );
|
115 | *
|
116 | * // Prevent the default (native) action and stop the underlying keydown event
|
117 | * // so no other editor feature will interfere.
|
118 | * cancel();
|
119 | * } );
|
120 | * ```
|
121 | *
|
122 | * Note: Certain typing-oriented keystrokes (like <kbd>Backspace</kbd> or <kbd>Enter</kbd>) are handled
|
123 | * by a low-level mechanism and trying to listen to them via the keystroke handler will not work reliably.
|
124 | * To handle these specific keystrokes, see the events fired by the
|
125 | * {@link module:engine/view/document~Document editing view document} (`editor.editing.view.document`).
|
126 | */
|
127 | readonly keystrokes: EditingKeystrokeHandler;
|
128 | /**
|
129 | * Shorthand for {@link module:utils/locale~Locale#t}.
|
130 | *
|
131 | * @see module:utils/locale~Locale#t
|
132 | */
|
133 | readonly t: LocaleTranslate;
|
134 | readonly id: string;
|
135 | /**
|
136 | * Indicates the editor life-cycle state.
|
137 | *
|
138 | * The editor is in one of the following states:
|
139 | *
|
140 | * * `initializing` – During the editor initialization (before
|
141 | * {@link module:core/editor/editor~Editor.create `Editor.create()`}) finished its job.
|
142 | * * `ready` – After the promise returned by the {@link module:core/editor/editor~Editor.create `Editor.create()`}
|
143 | * method is resolved.
|
144 | * * `destroyed` – Once the {@link #destroy `editor.destroy()`} method was called.
|
145 | *
|
146 | * @observable
|
147 | */
|
148 | state: 'initializing' | 'ready' | 'destroyed';
|
149 | /**
|
150 | * The default configuration which is built into the editor class.
|
151 | *
|
152 | * It is used in CKEditor 5 builds to provide the default configuration options which are later used during the editor initialization.
|
153 | *
|
154 | * ```ts
|
155 | * ClassicEditor.defaultConfig = {
|
156 | * foo: 1,
|
157 | * bar: 2
|
158 | * };
|
159 | *
|
160 | * ClassicEditor
|
161 | * .create( sourceElement )
|
162 | * .then( editor => {
|
163 | * editor.config.get( 'foo' ); // -> 1
|
164 | * editor.config.get( 'bar' ); // -> 2
|
165 | * } );
|
166 | *
|
167 | * // The default options can be overridden by the configuration passed to create().
|
168 | * ClassicEditor
|
169 | * .create( sourceElement, { bar: 3 } )
|
170 | * .then( editor => {
|
171 | * editor.config.get( 'foo' ); // -> 1
|
172 | * editor.config.get( 'bar' ); // -> 3
|
173 | * } );
|
174 | * ```
|
175 | *
|
176 | * See also {@link module:core/editor/editor~Editor.builtinPlugins}.
|
177 | */
|
178 | static defaultConfig?: EditorConfig;
|
179 | /**
|
180 | * An array of plugins built into this editor class.
|
181 | *
|
182 | * It is used in CKEditor 5 builds to provide a list of plugins which are later automatically initialized
|
183 | * during the editor initialization.
|
184 | *
|
185 | * They will be automatically initialized by the editor, unless listed in `config.removePlugins` and
|
186 | * unless `config.plugins` is passed.
|
187 | *
|
188 | * ```ts
|
189 | * // Build some plugins into the editor class first.
|
190 | * ClassicEditor.builtinPlugins = [ FooPlugin, BarPlugin ];
|
191 | *
|
192 | * // Normally, you need to define config.plugins, but since ClassicEditor.builtinPlugins was
|
193 | * // defined, now you can call create() without any configuration.
|
194 | * ClassicEditor
|
195 | * .create( sourceElement )
|
196 | * .then( editor => {
|
197 | * editor.plugins.get( FooPlugin ); // -> An instance of the Foo plugin.
|
198 | * editor.plugins.get( BarPlugin ); // -> An instance of the Bar plugin.
|
199 | * } );
|
200 | *
|
201 | * ClassicEditor
|
202 | * .create( sourceElement, {
|
203 | * // Do not initialize these plugins (note: it is defined by a string):
|
204 | * removePlugins: [ 'Foo' ]
|
205 | * } )
|
206 | * .then( editor => {
|
207 | * editor.plugins.get( FooPlugin ); // -> Undefined.
|
208 | * editor.config.get( BarPlugin ); // -> An instance of the Bar plugin.
|
209 | * } );
|
210 | *
|
211 | * ClassicEditor
|
212 | * .create( sourceElement, {
|
213 | * // Load only this plugin. It can also be defined by a string if
|
214 | * // this plugin was built into the editor class.
|
215 | * plugins: [ FooPlugin ]
|
216 | * } )
|
217 | * .then( editor => {
|
218 | * editor.plugins.get( FooPlugin ); // -> An instance of the Foo plugin.
|
219 | * editor.config.get( BarPlugin ); // -> Undefined.
|
220 | * } );
|
221 | * ```
|
222 | *
|
223 | * See also {@link module:core/editor/editor~Editor.defaultConfig}.
|
224 | */
|
225 | static builtinPlugins?: Array<PluginConstructor<Editor>>;
|
226 | /**
|
227 | * The editor UI instance.
|
228 | */
|
229 | abstract get ui(): EditorUI;
|
230 | /**
|
231 | * The editor context.
|
232 | * When it is not provided through the configuration, the editor creates it.
|
233 | */
|
234 | protected readonly _context: Context;
|
235 | /**
|
236 | * A set of lock IDs for the {@link #isReadOnly} getter.
|
237 | */
|
238 | protected readonly _readOnlyLocks: Set<symbol | string>;
|
239 | /**
|
240 | * Creates a new instance of the editor class.
|
241 | *
|
242 | * Usually, not to be used directly. See the static {@link module:core/editor/editor~Editor.create `create()`} method.
|
243 | *
|
244 | * @param config The editor configuration.
|
245 | */
|
246 | constructor(config?: EditorConfig);
|
247 | /**
|
248 | * Defines whether the editor is in the read-only mode.
|
249 | *
|
250 | * In read-only mode the editor { #commands commands} are disabled so it is not possible
|
251 | * to modify the document by using them. Also, the editable element(s) become non-editable.
|
252 | *
|
253 | * In order to make the editor read-only, you need to call the { #enableReadOnlyMode} method:
|
254 | *
|
255 | * ```ts
|
256 | * editor.enableReadOnlyMode( 'feature-id' );
|
257 | * ```
|
258 | *
|
259 | * Later, to turn off the read-only mode, call { #disableReadOnlyMode}:
|
260 | *
|
261 | * ```ts
|
262 | * editor.disableReadOnlyMode( 'feature-id' );
|
263 | * ```
|
264 | *
|
265 | *
|
266 | *
|
267 | */
|
268 | get isReadOnly(): boolean;
|
269 | set isReadOnly(value: boolean);
|
270 | /**
|
271 | * Turns on the read-only mode in the editor.
|
272 | *
|
273 | * Editor can be switched to or out of the read-only mode by many features, under various circumstances. The editor supports locking
|
274 | * 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
|
275 | * the same time, without conflicting with each other. It guarantees that you will not make the editor editable accidentally (which
|
276 | * could lead to errors).
|
277 | *
|
278 | * Each read-only mode request is identified by a unique id (also called "lock"). If multiple plugins requested to turn on the
|
279 | * read-only mode, then, the editor will become editable only after all these plugins turn the read-only mode off (using the same ids).
|
280 | *
|
281 | * Note, that you cannot force the editor to disable the read-only mode if other plugins set it.
|
282 | *
|
283 | * After the first `enableReadOnlyMode()` call, the {@link #isReadOnly `isReadOnly` property} will be set to `true`:
|
284 | *
|
285 | * ```ts
|
286 | * editor.isReadOnly; // `false`.
|
287 | * editor.enableReadOnlyMode( 'my-feature-id' );
|
288 | * editor.isReadOnly; // `true`.
|
289 | * ```
|
290 | *
|
291 | * You can turn off the read-only mode ("clear the lock") using the {@link #disableReadOnlyMode `disableReadOnlyMode()`} method:
|
292 | *
|
293 | * ```ts
|
294 | * editor.enableReadOnlyMode( 'my-feature-id' );
|
295 | * // ...
|
296 | * editor.disableReadOnlyMode( 'my-feature-id' );
|
297 | * editor.isReadOnly; // `false`.
|
298 | * ```
|
299 | *
|
300 | * All "locks" need to be removed to enable editing:
|
301 | *
|
302 | * ```ts
|
303 | * editor.enableReadOnlyMode( 'my-feature-id' );
|
304 | * editor.enableReadOnlyMode( 'my-other-feature-id' );
|
305 | * // ...
|
306 | * editor.disableReadOnlyMode( 'my-feature-id' );
|
307 | * editor.isReadOnly; // `true`.
|
308 | * editor.disableReadOnlyMode( 'my-other-feature-id' );
|
309 | * editor.isReadOnly; // `false`.
|
310 | * ```
|
311 | *
|
312 | * @param lockId A unique ID for setting the editor to the read-only state.
|
313 | */
|
314 | enableReadOnlyMode(lockId: string | symbol): void;
|
315 | /**
|
316 | * Removes the read-only lock from the editor with given lock ID.
|
317 | *
|
318 | * When no lock is present on the editor anymore, then the {@link #isReadOnly `isReadOnly` property} will be set to `false`.
|
319 | *
|
320 | * @param lockId The lock ID for setting the editor to the read-only state.
|
321 | */
|
322 | disableReadOnlyMode(lockId: string | symbol): void;
|
323 | /**
|
324 | * Sets the data in the editor.
|
325 | *
|
326 | * ```ts
|
327 | * editor.setData( '<p>This is editor!</p>' );
|
328 | * ```
|
329 | *
|
330 | * If your editor implementation uses multiple roots, you should pass an object with keys corresponding
|
331 | * to the editor root names and values equal to the data that should be set in each root:
|
332 | *
|
333 | * ```ts
|
334 | * editor.setData( {
|
335 | * header: '<p>Content for header part.</p>',
|
336 | * content: '<p>Content for main part.</p>',
|
337 | * footer: '<p>Content for footer part.</p>'
|
338 | * } );
|
339 | * ```
|
340 | *
|
341 | * By default the editor accepts HTML. This can be controlled by injecting a different data processor.
|
342 | * See the {@glink features/markdown Markdown output} guide for more details.
|
343 | *
|
344 | * @param data Input data.
|
345 | */
|
346 | setData(data: string | Record<string, string>): void;
|
347 | /**
|
348 | * Gets the data from the editor.
|
349 | *
|
350 | * ```ts
|
351 | * editor.getData(); // -> '<p>This is editor!</p>'
|
352 | * ```
|
353 | *
|
354 | * If your editor implementation uses multiple roots, you should pass root name as one of the options:
|
355 | *
|
356 | * ```ts
|
357 | * editor.getData( { rootName: 'header' } ); // -> '<p>Content for header part.</p>'
|
358 | * ```
|
359 | *
|
360 | * By default, the editor outputs HTML. This can be controlled by injecting a different data processor.
|
361 | * See the {@glink features/markdown Markdown output} guide for more details.
|
362 | *
|
363 | * A warning is logged when you try to retrieve data for a detached root, as most probably this is a mistake. A detached root should
|
364 | * be treated like it is removed, and you should not save its data. Note, that the detached root data is always an empty string.
|
365 | *
|
366 | * @param options Additional configuration for the retrieved data.
|
367 | * Editor features may introduce more configuration options that can be set through this parameter.
|
368 | * @param options.rootName Root name. Defaults to `'main'`.
|
369 | * @param options.trim Whether returned data should be trimmed. This option is set to `'empty'` by default,
|
370 | * which means that whenever editor content is considered empty, an empty string is returned. To turn off trimming
|
371 | * use `'none'`. In such cases exact content will be returned (for example `'<p> </p>'` for an empty editor).
|
372 | * @returns Output data.
|
373 | */
|
374 | getData(options?: {
|
375 | rootName?: string;
|
376 | trim?: 'empty' | 'none';
|
377 | [key: string]: unknown;
|
378 | }): string;
|
379 | /**
|
380 | * Loads and initializes plugins specified in the configuration.
|
381 | *
|
382 | * @returns A promise which resolves once the initialization is completed, providing an array of loaded plugins.
|
383 | */
|
384 | initPlugins(): Promise<LoadedPlugins>;
|
385 | /**
|
386 | * Destroys the editor instance, releasing all resources used by it.
|
387 | *
|
388 | * **Note** The editor cannot be destroyed during the initialization phase so if it is called
|
389 | * while the editor {@link #state is being initialized}, it will wait for the editor initialization before destroying it.
|
390 | *
|
391 | * @fires destroy
|
392 | * @returns A promise that resolves once the editor instance is fully destroyed.
|
393 | */
|
394 | destroy(): Promise<unknown>;
|
395 | /**
|
396 | * Executes the specified command with given parameters.
|
397 | *
|
398 | * Shorthand for:
|
399 | *
|
400 | * ```ts
|
401 | * editor.commands.get( commandName ).execute( ... );
|
402 | * ```
|
403 | *
|
404 | * @param commandName The name of the command to execute.
|
405 | * @param commandParams Command parameters.
|
406 | * @returns The value returned by the {@link module:core/commandcollection~CommandCollection#execute `commands.execute()`}.
|
407 | */
|
408 | execute<TName extends string>(commandName: TName, ...commandParams: Parameters<CommandsMap[TName]['execute']>): ReturnType<CommandsMap[TName]['execute']>;
|
409 | /**
|
410 | * Focuses the editor.
|
411 | *
|
412 | * **Note** To explicitly focus the editing area of the editor, use the
|
413 | * {@link module:engine/view/view~View#focus `editor.editing.view.focus()`} method of the editing view.
|
414 | *
|
415 | * Check out the {@glink framework/deep-dive/ui/focus-tracking#focus-in-the-editor-ui Focus in the editor UI} section
|
416 | * of the {@glink framework/deep-dive/ui/focus-tracking Deep dive into focus tracking} guide to learn more.
|
417 | */
|
418 | focus(): void;
|
419 | /**
|
420 | * Creates and initializes a new editor instance.
|
421 | *
|
422 | * This is an abstract method. Every editor type needs to implement its own initialization logic.
|
423 | *
|
424 | * See the `create()` methods of the existing editor types to learn how to use them:
|
425 | *
|
426 | * * {@link module:editor-classic/classiceditor~ClassicEditor.create `ClassicEditor.create()`}
|
427 | * * {@link module:editor-balloon/ballooneditor~BalloonEditor.create `BalloonEditor.create()`}
|
428 | * * {@link module:editor-decoupled/decouplededitor~DecoupledEditor.create `DecoupledEditor.create()`}
|
429 | * * {@link module:editor-inline/inlineeditor~InlineEditor.create `InlineEditor.create()`}
|
430 | */
|
431 | static create(...args: Array<unknown>): void;
|
432 | /**
|
433 | * The {@link module:core/context~Context} class.
|
434 | *
|
435 | * Exposed as static editor field for easier access in editor builds.
|
436 | */
|
437 | static Context: typeof Context;
|
438 | /**
|
439 | * The {@link module:watchdog/editorwatchdog~EditorWatchdog} class.
|
440 | *
|
441 | * Exposed as static editor field for easier access in editor builds.
|
442 | */
|
443 | static EditorWatchdog: typeof EditorWatchdog;
|
444 | /**
|
445 | * The {@link module:watchdog/contextwatchdog~ContextWatchdog} class.
|
446 | *
|
447 | * Exposed as static editor field for easier access in editor builds.
|
448 | */
|
449 | static ContextWatchdog: typeof ContextWatchdog;
|
450 | }
|
451 | /**
|
452 | * Fired when the {@link module:engine/controller/datacontroller~DataController#event:ready data} and all additional
|
453 | * editor components are ready.
|
454 | *
|
455 | * Note: This event is most useful for plugin developers. When integrating the editor with your website or
|
456 | * application, you do not have to listen to `editor#ready` because when the promise returned by the static
|
457 | * {@link module:core/editor/editor~Editor.create `Editor.create()`} event is resolved, the editor is already ready.
|
458 | * In fact, since the first moment when the editor instance is available to you is inside `then()`'s callback,
|
459 | * you cannot even add a listener to the `editor#ready` event.
|
460 | *
|
461 | * See also the {@link module:core/editor/editor~Editor#state `editor.state`} property.
|
462 | *
|
463 | * @eventName ~Editor#ready
|
464 | */
|
465 | export type EditorReadyEvent = {
|
466 | name: 'ready';
|
467 | args: [];
|
468 | };
|
469 | /**
|
470 | * Fired when this editor instance is destroyed. The editor at this point is not usable and this event should be used to
|
471 | * perform the clean-up in any plugin.
|
472 | *
|
473 | * See also the {@link module:core/editor/editor~Editor#state `editor.state`} property.
|
474 | *
|
475 | * @eventName ~Editor#destroy
|
476 | */
|
477 | export type EditorDestroyEvent = {
|
478 | name: 'destroy';
|
479 | args: [];
|
480 | };
|
481 | export {};
|
482 | /**
|
483 | * This error is thrown when trying to pass a `<textarea>` element to a `create()` function of an editor class.
|
484 | *
|
485 | * The only editor type which can be initialized on `<textarea>` elements is
|
486 | * the {@glink getting-started/setup/editor-types#classic-editor classic editor}.
|
487 | * This editor hides the passed element and inserts its own UI next to it. Other types of editors reuse the passed element as their root
|
488 | * editable element and therefore `<textarea>` is not appropriate for them. Use a `<div>` or another text container instead:
|
489 | *
|
490 | * ```html
|
491 | * <div id="editor">
|
492 | * <p>Initial content.</p>
|
493 | * </div>
|
494 | * ```
|
495 | *
|
496 | * @error editor-wrong-element
|
497 | */
|