Context
Provides a common, higher-level environment for solutions that use multiple editors or plugins that work outside the editor. Use it instead of Editor.create() in advanced application integrations.
All configuration options passed to a context will be used as default options for the editor instances initialized in that context.
Context plugins passed to a context instance will be shared among all editor instances initialized in this context. These will be the same plugin instances for all the editors.
Note: The context can only be initialized with context plugins (e.g. comments). Regular plugins require an editor instance to work and cannot be added to a context.
Note: You can add a context plugin to an editor instance, though.
If you are using multiple editor instances on one page and use any context plugins, create a context to share the configuration and plugins among these editors. Some plugins will use the information about all existing editors to better integrate between them.
If you are using plugins that do not require an editor to work (e.g. comments), enable and configure them using the context.
If you are using only a single editor on each page, use Editor.create() instead. In such a case, a context instance will be created by the editor instance in a transparent way.
See Context.create() for usage examples.
Properties
config : Config<ContextConfig>readonlymodule:core/context~Context#configStores all the configurations specific to this context instance.
editors : Collection<Editor>readonlymodule:core/context~Context#editorsA list of editors that this context instance is injected to.
module:core/context~Context#localeplugins : PluginCollection<Editor | Context>readonlymodule:core/context~Context#pluginsThe plugins loaded and in use by this context instance.
t : LocaleTranslatereadonlymodule:core/context~Context#tShorthand for
t._contextOwner : null | Editorprivatemodule:core/context~Context#_contextOwnerReference to the editor which created the context. Null when the context was created outside of the editor.
It is used to destroy the context when removing the editor that has created the context.
Static properties
builtinPlugins : Array<PluginConstructor<Editor | Context>>staticmodule:core/context~Context.builtinPluginsAn array of plugins built into the
Contextclass.It was used in the now deprecated CKEditor 5 builds featuring
Contextto provide the default configuration options which are later used during the context initialization.They will be automatically initialized by
Contextunlessconfig.pluginsis passed.// Build some context plugins into the Context class first. Context.builtinPlugins = [ FooPlugin, BarPlugin ]; // Normally, you need to define config.plugins, but since Context.builtinPlugins was // defined, now you can call create() without any configuration. Context .create() .then( context => { context.plugins.get( FooPlugin ); // -> An instance of the Foo plugin. context.plugins.get( BarPlugin ); // -> An instance of the Bar plugin. } );Copy codeSee also
Context.defaultConfigandEditor.builtinPlugins.defaultConfig : ContextConfigstaticmodule:core/context~Context.defaultConfigThe default configuration which is built into the
Contextclass.It was used in the now deprecated CKEditor 5 builds featuring
Contextto provide the default configuration options which are later used during the context initialization.Context.defaultConfig = { foo: 1, bar: 2 }; Context .create() .then( context => { context.config.get( 'foo' ); // -> 1 context.config.get( 'bar' ); // -> 2 } ); // The default options can be overridden by the configuration passed to create(). Context .create( { bar: 3 } ) .then( context => { context.config.get( 'foo' ); // -> 1 context.config.get( 'bar' ); // -> 3 } );Copy codeSee also
Context.builtinPluginsandEditor.defaultConfig.
Methods
constructor( [ config ] )module:core/context~Context#constructorCreates a context instance with a given configuration.
Usually not to be used directly. See the static
create()method.Parameters
[ config ] : ContextConfigThe context configuration.
destroy() → Promise<unknown>module:core/context~Context#destroyDestroys the context instance and all editors used with the context, releasing all resources used by the context.
Returns
Promise<unknown>A promise that resolves once the context instance is fully destroyed.
initPlugins() → Promise<LoadedPlugins>module:core/context~Context#initPluginsLoads and initializes plugins specified in the configuration.
Returns
Promise<LoadedPlugins>A promise which resolves once the initialization is completed, providing an array of loaded plugins.
_addEditor( editor, isContextOwner ) → voidinternalmodule:core/context~Context#_addEditorAdds a reference to the editor which is used with this context.
When the given editor has created the context, the reference to this editor will be stored as a
_contextOwner.This method should only be used by the editor.
Parameters
editor : EditorisContextOwner : booleanStores the given editor as a context owner.
Returns
void
_getEditorConfig() → Partial<EditorConfig>internalmodule:core/context~Context#_getEditorConfigReturns the context configuration which will be copied to the editors created using this context.
The configuration returned by this method has the plugins configuration removed – plugins are shared with all editors through another mechanism.
This method should only be used by the editor.
Returns
Partial<EditorConfig>Configuration as a plain object.
_removeEditor( editor ) → Promise<unknown>internalmodule:core/context~Context#_removeEditorRemoves a reference to the editor which was used with this context. When the context was created by the given editor, the context will be destroyed.
This method should only be used by the editor.
Parameters
editor : Editor
Returns
Promise<unknown>A promise that resolves once the editor is removed from the context or when the context was destroyed.
Static methods
module:core/context~Context.createCreates and initializes a new context instance.
const commonConfig = { ... }; // Configuration for all the plugins and editors. const editorPlugins = [ ... ]; // Regular plugins here. Context .create( { // Only context plugins here. plugins: [ ... ], // Configure the language for all the editors (it cannot be overwritten). language: { ... }, // Configuration for context plugins. comments: { ... }, ... // Default configuration for editor plugins. toolbar: { ... }, image: { ... }, ... } ) .then( context => { const promises = []; promises.push( ClassicEditor.create( document.getElementById( 'editor1' ), { editorPlugins, context } ) ); promises.push( ClassicEditor.create( document.getElementById( 'editor2' ), { editorPlugins, context, toolbar: { ... } // You can overwrite the configuration of the context. } ) ); return Promise.all( promises ); } );Copy codeParameters
[ config ] : ContextConfigThe context configuration.
Returns
Promise<Context>A promise resolved once the context is ready. The promise resolves with the created context instance.