# Context

class

Provides a common, higher-level environment for solutions that use multiple [editors](module_core_editor_editor-Editor.md) or plugins that work outside the editor. Use it instead of [`Editor.create()`](module_core_editor_editor-Editor.md#static-function-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](module_core_contextplugin-ContextPlugin.md) 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](module_core_contextplugin-ContextPlugin.md) (e.g. [comments](https://ckeditor.com/collaboration/comments/)). Regular [plugins](module_core_plugin-Plugin.md) 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](https://ckeditor.com/collaboration/comments/)), enable and configure them using the context.

If you are using only a single editor on each page, use [`Editor.create()`](module_core_editor_editor-Editor.md#static-function-create) instead. In such a case, a context instance will be created by the editor instance in a transparent way.

See [`Context.create()`](#static-function-create) for usage examples.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L50)

<a id="properties">

## Properties

<a id="member-config">

### `config: Config<ContextConfig>` _(readonly)_

Stores all the configurations specific to this context instance.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L54)

<a id="member-editors">

### `editors: Collection<Editor>` _(readonly)_

A list of editors that this context instance is injected to.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L71)

<a id="member-locale">

### `locale: Locale` _(readonly)_

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L61)

<a id="member-plugins">

### `plugins: PluginCollection<Editor | Context>` _(readonly)_

The plugins loaded and in use by this context instance.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L59)

<a id="member-t">

### `t: LocaleTranslate` _(readonly)_

Shorthand for [`t`](module_utils_locale-Locale.md#member-t).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L66)

<a id="member-_contextOwner">

### `_contextOwner: Editor | null` _(private)_

Reference 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.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L139)

#### Static properties

<a id="static-member-builtinPlugins">

### `builtinPlugins: Array<PluginConstructor<Editor | Context>>` _(static)_

An array of plugins built into the `Context` class.

It was used in the now deprecated CKEditor 5 builds featuring `Context` to provide the default configuration options which are later used during the context initialization.

They will be automatically initialized by `Context` unless `config.plugins` is passed.

```typescript
// 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.
	} );
```

See also [`Context.defaultConfig`](#static-member-defaultConfig) and [`Editor.builtinPlugins`](module_core_editor_editor-Editor.md#static-member-builtinPlugins).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L131)

<a id="static-member-defaultConfig">

### `defaultConfig: ContextConfig` _(static)_

The default configuration which is built into the `Context` class.

It was used in the now deprecated CKEditor 5 builds featuring `Context` to provide the default configuration options which are later used during the context initialization.

```typescript
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
	} );
```

See also [`Context.builtinPlugins`](#static-member-builtinPlugins) and [`Editor.defaultConfig`](module_core_editor_editor-Editor.md#static-member-defaultConfig).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L104)

<a id="static-member-_throwErrorWhenUsedAsAPlugin">

### `_throwErrorWhenUsedAsAPlugin: true` _(internal)_

`Context` class is commonly put in `config.plugins` array.

This property helps with better error detection.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L365)

<a id="methods">

## Methods

<a id="function-constructor">

### `constructor( config? )`

Creates a context instance with a given configuration.

Usually not to be used directly. See the static [`create()`](#static-function-create) method.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L148)

#### Parameters

* `config?: ContextConfig`

  The context configuration.

<a id="function-destroy">

### `destroy() → Promise<unknown>`

Destroys the context instance and all editors used with the context, releasing all resources used by the context.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L222)

#### Returns

* `Promise<unknown>`

  A promise that resolves once the context instance is fully destroyed.

<a id="function-initPlugins">

### `initPlugins() → Promise<LoadedPlugins>`

Loads and initializes plugins specified in the configuration.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L179)

#### Returns

* `Promise<LoadedPlugins>`

  A promise which resolves once the initialization is completed, providing an array of loaded plugins.

<a id="function-_addEditor">

### `_addEditor( editor, isContextOwner ) → void` _(internal)_

Adds 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`](#member-_contextOwner).

This method should only be used by the editor.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L238)

#### Parameters

* `editor: Editor`

* `isContextOwner: boolean`

  Stores the given editor as a context owner.

#### Returns

* `void`

<a id="function-_getEditorConfig">

### `_getEditorConfig() → Partial<EditorConfig>` _(internal)_

Returns 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.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L287)

#### Returns

* `Partial<EditorConfig>`

  Configuration as a plain object.

<a id="function-_removeEditor">

### `_removeEditor( editor ) → Promise<unknown>` _(internal)_

Removes 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.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L264)

#### 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

<a id="static-function-create">

### `create( config? ) → Promise<Context>` _(static)_

Creates and initializes a new context instance.

```typescript
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 );
	} );
```

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-core/src/context.ts#L350)

#### Parameters

* `config?: ContextConfig`

  The context configuration.

#### Returns

* `Promise<Context>`

  A promise resolved once the context is ready. The promise resolves with the created context instance.

---

Full index of the CKEditor 5 API reference: [llms.txt](llms.txt)
