plugincollection.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module core/plugincollection
  7. */
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import log from '@ckeditor/ckeditor5-utils/src/log';
  10. /**
  11. * Manages a list of CKEditor plugins, including loading, resolving dependencies and initialization.
  12. */
  13. export default class PluginCollection {
  14. /**
  15. * Creates an instance of the PluginCollection class.
  16. * Allows loading and initializing plugins and their dependencies.
  17. *
  18. * @param {module:core/editor/editor~Editor} editor
  19. * @param {Array.<Function>} [availablePlugins] Plugins (constructors) which the collection will be able to use
  20. * when {@link module:core/plugincollection~PluginCollection#load} is used with plugin names (strings, instead of constructors).
  21. * Usually, the editor will pass its built-in plugins to the collection so they can later be
  22. * used in `config.plugins` or `config.removePlugins` by names.
  23. */
  24. constructor( editor, availablePlugins = [] ) {
  25. /**
  26. * @protected
  27. * @member {module:core/editor/editor~Editor} module:core/plugin~PluginCollection#_editor
  28. */
  29. this._editor = editor;
  30. /**
  31. * Map of plugin constructors which can be retrieved by their names.
  32. *
  33. * @protected
  34. * @member {Map.<String|Function,Function>} module:core/plugin~PluginCollection#_availablePlugins
  35. */
  36. this._availablePlugins = new Map();
  37. /**
  38. * @protected
  39. * @member {Map} module:core/plugin~PluginCollection#_plugins
  40. */
  41. this._plugins = new Map();
  42. for ( const PluginConstructor of availablePlugins ) {
  43. this._availablePlugins.set( PluginConstructor, PluginConstructor );
  44. if ( PluginConstructor.pluginName ) {
  45. this._availablePlugins.set( PluginConstructor.pluginName, PluginConstructor );
  46. }
  47. }
  48. }
  49. /**
  50. * Collection iterator. Returns `[ PluginConstructor, pluginInstance ]` pairs.
  51. */
  52. * [ Symbol.iterator ]() {
  53. for ( const entry of this._plugins ) {
  54. if ( typeof entry[ 0 ] == 'function' ) {
  55. yield entry;
  56. }
  57. }
  58. }
  59. /**
  60. * Gets the plugin instance by its constructor or name.
  61. *
  62. * @param {Function|String} key The plugin constructor or {@link module:core/plugin~PluginInterface.pluginName name}.
  63. * @returns {module:core/plugin~PluginInterface}
  64. */
  65. get( key ) {
  66. return this._plugins.get( key );
  67. }
  68. /**
  69. * Loads a set of plugins and adds them to the collection.
  70. *
  71. * @param {Array.<Function|String>} plugins An array of {@link module:core/plugin~PluginInterface plugin constructors}
  72. * or {@link module:core/plugin~PluginInterface.pluginName plugin names}. The second option (names) work only if
  73. * `availablePlugins` were passed to the {@link #constructor}.
  74. * @param {Array.<String|Function>} [removePlugins] Names of plugins or plugin constructors
  75. * which should not be loaded (despite being specified in the `plugins` array).
  76. * @returns {Promise} A promise which gets resolved once all plugins are loaded and available into the
  77. * collection.
  78. * @returns {Promise.<Array.<module:core/plugin~PluginInterface>>} returns.loadedPlugins The array of loaded plugins.
  79. */
  80. load( plugins, removePlugins = [] ) {
  81. const that = this;
  82. const editor = this._editor;
  83. const loading = new Set();
  84. const loaded = [];
  85. const pluginConstructors = mapToAvailableConstructors( plugins );
  86. const removePluginConstructors = mapToAvailableConstructors( removePlugins );
  87. const missingPlugins = getMissingPluginNames( plugins );
  88. if ( missingPlugins ) {
  89. // TODO update this error docs with links to docs because it will be a frequent problem.
  90. /**
  91. * Some plugins are not available and could not be loaded.
  92. *
  93. * Plugin classes (constructors) need to be provided to the editor before they can be loaded by name.
  94. * This is usually done by the builder by setting the {@link module:core/editor/editor~Editor.build}
  95. * property.
  96. *
  97. * @error plugincollection-plugin-not-found
  98. * @param {Array.<String>} plugins The name of the plugins which could not be loaded.
  99. */
  100. const errorMsg = 'plugincollection-plugin-not-found: Some plugins are not available and could not be loaded.';
  101. // Log the error so it's more visible on the console. Hopefuly, for better DX.
  102. log.error( errorMsg, { plugins: missingPlugins } );
  103. return Promise.reject( new CKEditorError( errorMsg, { plugins: missingPlugins } ) );
  104. }
  105. return Promise.all( pluginConstructors.map( loadPlugin ) )
  106. .then( () => loaded );
  107. function loadPlugin( PluginConstructor ) {
  108. if ( removePluginConstructors.includes( PluginConstructor ) ) {
  109. return;
  110. }
  111. // The plugin is already loaded or being loaded - do nothing.
  112. if ( that.get( PluginConstructor ) || loading.has( PluginConstructor ) ) {
  113. return;
  114. }
  115. return instantiatePlugin( PluginConstructor )
  116. .catch( err => {
  117. /**
  118. * It was not possible to load the plugin.
  119. *
  120. * @error plugincollection-load
  121. * @param {String} plugin The name of the plugin that could not be loaded.
  122. */
  123. log.error( 'plugincollection-load: It was not possible to load the plugin.', { plugin: PluginConstructor } );
  124. throw err;
  125. } );
  126. }
  127. function instantiatePlugin( PluginConstructor ) {
  128. return new Promise( resolve => {
  129. loading.add( PluginConstructor );
  130. if ( PluginConstructor.requires ) {
  131. PluginConstructor.requires.forEach( RequiredPluginConstructorOrName => {
  132. const RequiredPluginConstructor = getPluginConstructor( RequiredPluginConstructorOrName );
  133. if ( removePlugins.includes( RequiredPluginConstructor ) ) {
  134. /**
  135. * Cannot load a plugin because one of its dependencies is listed in the `removePlugins` option.
  136. *
  137. * @error plugincollection-required
  138. * @param {Function} plugin The required plugin.
  139. * @param {Function} requiredBy The parent plugin.
  140. */
  141. throw new CKEditorError(
  142. 'plugincollection-required: Cannot load a plugin because one of its dependencies is listed in' +
  143. 'the `removePlugins` option.',
  144. { plugin: RequiredPluginConstructor, requiredBy: PluginConstructor }
  145. );
  146. }
  147. loadPlugin( RequiredPluginConstructor );
  148. } );
  149. }
  150. const plugin = new PluginConstructor( editor );
  151. that._add( PluginConstructor, plugin );
  152. loaded.push( plugin );
  153. resolve();
  154. } );
  155. }
  156. function getPluginConstructor( PluginConstructorOrName ) {
  157. if ( typeof PluginConstructorOrName == 'function' ) {
  158. return PluginConstructorOrName;
  159. }
  160. return that._availablePlugins.get( PluginConstructorOrName );
  161. }
  162. function getMissingPluginNames( plugins ) {
  163. const missingPlugins = [];
  164. for ( const pluginNameOrConstructor of plugins ) {
  165. if ( !getPluginConstructor( pluginNameOrConstructor ) ) {
  166. missingPlugins.push( pluginNameOrConstructor );
  167. }
  168. }
  169. return missingPlugins.length ? missingPlugins : null;
  170. }
  171. function mapToAvailableConstructors( plugins ) {
  172. return plugins
  173. .map( pluginNameOrConstructor => getPluginConstructor( pluginNameOrConstructor ) )
  174. .filter( PluginConstructor => !!PluginConstructor );
  175. }
  176. }
  177. /**
  178. * Destroys all loaded plugins.
  179. *
  180. * @returns {Promise}
  181. */
  182. destroy() {
  183. const promises = Array.from( this )
  184. .map( ( [ , pluginInstance ] ) => pluginInstance )
  185. .filter( pluginInstance => typeof pluginInstance.destroy == 'function' )
  186. .map( pluginInstance => pluginInstance.destroy() );
  187. return Promise.all( promises );
  188. }
  189. /**
  190. * Adds the plugin to the collection. Exposed mainly for testing purposes.
  191. *
  192. * @protected
  193. * @param {Function} PluginConstructor The plugin constructor.
  194. * @param {module:core/plugin~PluginInterface} plugin The instance of the plugin.
  195. */
  196. _add( PluginConstructor, plugin ) {
  197. this._plugins.set( PluginConstructor, plugin );
  198. if ( PluginConstructor.pluginName ) {
  199. this._plugins.set( PluginConstructor.pluginName, plugin );
  200. }
  201. }
  202. }