context.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * @TODO
  14. */
  15. export default class Context {
  16. /**
  17. * Creates a context instance with a given configuration.
  18. *
  19. * Usually, not to be used directly. See the static {@link ~Context.create `create()`} method.
  20. *
  21. * @param {Object} [config={}] The context config.
  22. */
  23. constructor( config ) {
  24. /**
  25. * Holds all configurations specific to this context instance.
  26. *
  27. * @readonly
  28. * @type {module:utils/config~Config}
  29. */
  30. this.config = new Config( config );
  31. /**
  32. * The plugins loaded and in use by this context instance.
  33. *
  34. * @readonly
  35. * @type {module:core/plugincollection~PluginCollection}
  36. */
  37. this.plugins = new PluginCollection( this );
  38. const languageConfig = this.config.get( 'language' ) || {};
  39. /**
  40. * @readonly
  41. * @type {module:utils/locale~Locale}
  42. */
  43. this.locale = new Locale( {
  44. uiLanguage: typeof languageConfig === 'string' ? languageConfig : languageConfig.ui,
  45. contentLanguage: this.config.get( 'language.content' )
  46. } );
  47. /**
  48. * List of editors to which this context instance is injected.
  49. *
  50. * @private
  51. * @type {Set<module:core/editor/editor~Editor>}
  52. */
  53. this._editors = new Set();
  54. }
  55. /**
  56. * Adds a reference to the to which context is injected.
  57. *
  58. * @param {module:core/editor/editor~Editor} editor
  59. */
  60. addEditor( editor ) {
  61. this._editors.add( editor );
  62. }
  63. /**
  64. * Removes a reference to the editor to which context was injected.
  65. *
  66. * @param {module:core/editor/editor~Editor} editor
  67. */
  68. removeEditor( editor ) {
  69. return this._editors.delete( editor );
  70. }
  71. /**
  72. * Loads and initializes plugins specified in the config.
  73. *
  74. * @returns {Promise.<module:core/plugin~LoadedPlugins>} A promise which resolves
  75. * once the initialization is completed providing an array of loaded plugins.
  76. */
  77. initPlugins() {
  78. const plugins = this.config.get( 'plugins' ) || [];
  79. for ( const Plugin of plugins ) {
  80. if ( typeof Plugin != 'function' ) {
  81. throw new CKEditorError( 'context-initplugins: Only constructor is allowed as a Context plugin.', null, { Plugin } );
  82. }
  83. if ( Plugin.isContextPlugin !== true ) {
  84. throw new CKEditorError( 'context-initplugins: Only plugins marked as a ContextPlugin are allowed.', null, { Plugin } );
  85. }
  86. }
  87. return this.plugins.init( plugins );
  88. }
  89. /**
  90. * Destroys the context instance, releasing all resources used by it.
  91. *
  92. * @returns {Promise} A promise that resolves once the context instance is fully destroyed.
  93. */
  94. destroy() {
  95. const promises = [];
  96. for ( const editor of Array.from( this._editors ) ) {
  97. editor.context = null;
  98. promises.push( editor.destroy() );
  99. }
  100. return Promise.all( promises )
  101. .then( () => this.plugins.destroy() );
  102. }
  103. /**
  104. * @TODO
  105. *
  106. * @param {Object} [config] The context config.
  107. * @returns {Promise} A promise resolved once the context is ready. The promise resolves with the created context instance.
  108. */
  109. static create( config ) {
  110. return new Promise( resolve => {
  111. const context = new this( config );
  112. resolve( context.initPlugins().then( () => context ) );
  113. } );
  114. }
  115. }