context.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module core/context
  7. */
  8. import Config from '@ckeditor/ckeditor5-utils/src/config';
  9. import PluginCollection from './plugincollection';
  10. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. /**
  13. * Provides a common, higher level environment for solutions which use multiple {@link module:core/editor/editor~Editor editors}
  14. * or/and plugins that work outside of an editor. Use it instead of {@link module:core/editor/editor~Editor.create `Editor.create()`}
  15. * in advanced application integrations.
  16. *
  17. * All configuration options passed to a `Context` will be used as default options for editor instances initialized in that context.
  18. *
  19. * {@link module:core/contextplugin~ContextPlugin `ContextPlugin`s} passed to a `Context` instance will be shared among all
  20. * editor instances initialized in that context. These will be the same plugin instances for all the editors.
  21. *
  22. * **Note:** `Context` can only be initialized with {@link module:core/contextplugin~ContextPlugin `ContextPlugin`s}
  23. * (e.g. [comments](https://ckeditor.com/collaboration/comments/)). Regular {@link module:core/plugin~Plugin `Plugin`s} require an
  24. * editor instance to work and cannot be added to a `Context`.
  25. *
  26. * **Note:** You can add `ContextPlugin` to an editor instance, though.
  27. *
  28. * If you are using multiple editor instances on one page and use any `ContextPlugin`s, create `Context` to share configuration and plugins
  29. * among those editors. Some plugins will use the information about all existing editors to better integrate between them.
  30. *
  31. * If you are using plugins that do not require an editor to work (e.g. [comments](https://ckeditor.com/collaboration/comments/))
  32. * enable and configure them using `Context`.
  33. *
  34. * If you are using only a single editor on each page use {@link module:core/editor/editor~Editor.create `Editor.create()`} instead.
  35. * In such case, `Context` instance will be created by the editor instance in a transparent way.
  36. *
  37. * See {@link module:core/context~Context.create `Context.create()`} for usage examples.
  38. */
  39. export default class Context {
  40. /**
  41. * Creates a context instance with a given configuration.
  42. *
  43. * Usually, not to be used directly. See the static {@link module:core/context~Context.create `create()`} method.
  44. *
  45. * @param {Object} [config={}] The context config.
  46. */
  47. constructor( config ) {
  48. /**
  49. * Holds all configurations specific to this context instance.
  50. *
  51. * @readonly
  52. * @type {module:utils/config~Config}
  53. */
  54. this.config = new Config( config );
  55. /**
  56. * The plugins loaded and in use by this context instance.
  57. *
  58. * @readonly
  59. * @type {module:core/plugincollection~PluginCollection}
  60. */
  61. this.plugins = new PluginCollection( this );
  62. const languageConfig = this.config.get( 'language' ) || {};
  63. /**
  64. * @readonly
  65. * @type {module:utils/locale~Locale}
  66. */
  67. this.locale = new Locale( {
  68. uiLanguage: typeof languageConfig === 'string' ? languageConfig : languageConfig.ui,
  69. contentLanguage: this.config.get( 'language.content' )
  70. } );
  71. /**
  72. * Shorthand for {@link module:utils/locale~Locale#t}.
  73. *
  74. * @see module:utils/locale~Locale#t
  75. * @method #t
  76. */
  77. this.t = this.locale.t;
  78. /**
  79. * List of editors to which this context instance is injected.
  80. *
  81. * @private
  82. * @type {Set.<module:core/editor/editor~Editor>}
  83. */
  84. this._editors = new Set();
  85. /**
  86. * Reference to the editor which created the context.
  87. * Null when the context was created outside of the editor.
  88. *
  89. * It is used to destroy the context when removing the editor that has created the context.
  90. *
  91. * @private
  92. * @type {module:core/editor/editor~Editor|null}
  93. */
  94. this._contextOwner = null;
  95. }
  96. /**
  97. * Loads and initializes plugins specified in the config.
  98. *
  99. * @returns {Promise.<module:core/plugin~LoadedPlugins>} A promise which resolves
  100. * once the initialization is completed providing an array of loaded plugins.
  101. */
  102. initPlugins() {
  103. const plugins = this.config.get( 'plugins' ) || [];
  104. for ( const Plugin of plugins ) {
  105. if ( typeof Plugin != 'function' ) {
  106. /**
  107. * Only constructor is allowed as a {@link module:core/contextplugin~ContextPlugin}.
  108. *
  109. * @error context-initplugins-constructor-only
  110. */
  111. throw new CKEditorError(
  112. 'context-initplugins-constructor-only: Only constructor is allowed as a Context plugin.',
  113. null,
  114. { Plugin }
  115. );
  116. }
  117. if ( Plugin.isContextPlugin !== true ) {
  118. /**
  119. * Only plugin marked as a {@link module:core/contextplugin~ContextPlugin} is allowed to be used with a context.
  120. *
  121. * @error context-initplugins-invalid-plugin
  122. */
  123. throw new CKEditorError(
  124. 'context-initplugins-invalid-plugin: Only plugin marked as a ContextPlugin is allowed.',
  125. null,
  126. { Plugin }
  127. );
  128. }
  129. }
  130. return this.plugins.init( plugins );
  131. }
  132. /**
  133. * Destroys the context instance, and all editors used with the context.
  134. * Releasing all resources used by the context.
  135. *
  136. * @returns {Promise} A promise that resolves once the context instance is fully destroyed.
  137. */
  138. destroy() {
  139. return Promise.all( Array.from( this._editors, editor => editor.destroy() ) )
  140. .then( () => this.plugins.destroy() );
  141. }
  142. /**
  143. * Adds a reference to the editor which is used with this context.
  144. *
  145. * When the given editor has created the context then the reference to this editor will be stored
  146. * as a {@link ~Context#_contextOwner}.
  147. *
  148. * This method should be used only by the editor.
  149. *
  150. * @protected
  151. * @param {module:core/editor/editor~Editor} editor
  152. * @param {Boolean} isContextOwner Stores the given editor as a context owner.
  153. */
  154. _addEditor( editor, isContextOwner ) {
  155. if ( this._contextOwner ) {
  156. /**
  157. * Cannot add multiple editors to the context which is created by the editor.
  158. *
  159. * @error context-addEditor-private-context
  160. */
  161. throw new CKEditorError(
  162. 'context-addEditor-private-context: Cannot add multiple editors to the context which is created by the editor.'
  163. );
  164. }
  165. this._editors.add( editor );
  166. if ( isContextOwner ) {
  167. this._contextOwner = editor;
  168. }
  169. }
  170. /**
  171. * Removes a reference to the editor which was used with this context.
  172. * When the context was created by the given editor then the context will be destroyed.
  173. *
  174. * This method should be used only by the editor.
  175. *
  176. * @protected
  177. * @param {module:core/editor/editor~Editor} editor
  178. * @return {Promise} A promise that resolves once the editor is removed from the context or when the context has been destroyed.
  179. */
  180. _removeEditor( editor ) {
  181. this._editors.delete( editor );
  182. if ( this._contextOwner === editor ) {
  183. return this.destroy();
  184. }
  185. return Promise.resolve();
  186. }
  187. /**
  188. * Returns context configuration which will be copied to editors created using this context.
  189. *
  190. * The configuration returned by this method has removed plugins configuration - plugins are shared with all editors
  191. * through another mechanism.
  192. *
  193. * This method should be used only by the editor.
  194. *
  195. * @protected
  196. * @returns {Object} Configuration as a plain object.
  197. */
  198. _getEditorConfig() {
  199. const result = {};
  200. for ( const name of this.config.names() ) {
  201. if ( ![ 'plugins', 'removePlugins', 'extraPlugins' ].includes( name ) ) {
  202. result[ name ] = this.config.get( name );
  203. }
  204. }
  205. return result;
  206. }
  207. /**
  208. * Creates and initializes a new context instance.
  209. *
  210. * const commonConfig = { ... }; // Configuration for all the plugins and editors.
  211. * const editorPlugins = [ ... ]; // Regular `Plugin`s here.
  212. *
  213. * Context
  214. * .create( {
  215. * // Only `ContextPlugin`s here.
  216. * plugins: [ ... ],
  217. *
  218. * // Configure language for all the editors (it cannot be overwritten).
  219. * language: { ... },
  220. *
  221. * // Configuration for context plugins.
  222. * comments: { ... },
  223. * ...
  224. *
  225. * // Default configuration for editor plugins.
  226. * toolbar: { ... },
  227. * image: { ... },
  228. * ...
  229. * } )
  230. * .then( context => {
  231. * const promises = [];
  232. *
  233. * promises.push( ClassicEditor.create(
  234. * document.getElementById( 'editor1' ),
  235. * {
  236. * editorPlugins,
  237. * context
  238. * }
  239. * ) );
  240. *
  241. * promises.push( ClassicEditor.create(
  242. * document.getElementById( 'editor2' ),
  243. * {
  244. * editorPlugins,
  245. * context,
  246. * toolbar: { ... } // You can overwrite context's configuration.
  247. * }
  248. * ) );
  249. *
  250. * return Promise.all( promises );
  251. * } );
  252. *
  253. * @param {Object} [config] The context config.
  254. * @returns {Promise} A promise resolved once the context is ready. The promise resolves with the created context instance.
  255. */
  256. static create( config ) {
  257. return new Promise( resolve => {
  258. const context = new this( config );
  259. resolve( context.initPlugins().then( () => context ) );
  260. } );
  261. }
  262. }