plugin.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/plugin
  7. */
  8. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  9. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  10. /**
  11. * The base class for CKEditor plugin classes.
  12. *
  13. * @implements module:core/plugin~PluginInterface
  14. * @mixes module:utils/observablemixin~ObservableMixin
  15. */
  16. export default class Plugin {
  17. /**
  18. * @inheritDoc
  19. */
  20. constructor( editor ) {
  21. /**
  22. * The editor instance.
  23. *
  24. * Note that most editors implement the {@link module:core/editor/editorwithui~EditorWithUI} interface in addition
  25. * to the base {@link module:core/editor/editor~Editor} interface. However, editors with an external UI
  26. * (i.e. Bootstrap-based) or a headless editor may not implement the {@link module:core/editor/editorwithui~EditorWithUI}
  27. * interface.
  28. *
  29. * Because of above, to make plugins more universal, it is recommended to split features into:
  30. * - The "editing" part that only uses the {@link module:core/editor/editor~Editor} interface.
  31. * - The "UI" part that uses both the {@link module:core/editor/editor~Editor} interface and
  32. * the {@link module:core/editor/editorwithui~EditorWithUI} interface.
  33. *
  34. * @readonly
  35. * @member {module:core/editor/editor~Editor} #editor
  36. */
  37. this.editor = editor;
  38. }
  39. /**
  40. * @inheritDoc
  41. */
  42. destroy() {
  43. this.stopListening();
  44. }
  45. }
  46. mix( Plugin, ObservableMixin );
  47. /**
  48. * The base interface for CKEditor plugins.
  49. *
  50. * In its minimal form a plugin can be a simple function that accepts {@link module:core/editor/editor~Editor the editor}
  51. * as a parameter:
  52. *
  53. * // A simple plugin that enables a data processor.
  54. * function MyPlugin( editor ) {
  55. * editor.data.processor = new MyDataProcessor();
  56. * }
  57. *
  58. * In most cases however, you will want to inherit from the {@link module:core/plugin~Plugin} class which implements the
  59. * {@link module:utils/observablemixin~ObservableMixin} and is, therefore, more convenient:
  60. *
  61. * class MyPlugin extends Plugin {
  62. * init() {
  63. * // `listenTo()` and `editor` are available thanks to `Plugin`.
  64. * // By using `listenTo()` you will ensure that the listener is removed when
  65. * // the plugin is destroyed.
  66. * this.listenTo( this.editor.data, 'ready', () => {
  67. * // Do something when the data is ready.
  68. * } );
  69. * }
  70. * }
  71. *
  72. * The plugin can also implement methods (e.g. {@link module:core/plugin~PluginInterface#init `init()`} or
  73. * {@link module:core/plugin~PluginInterface#destroy `destroy()`}) which, when present, will be used to properly
  74. * initialize and destroy the plugin.
  75. *
  76. * **Note:** When defined as a plain function, the plugin acts as a constructor and will be
  77. * called in parallel with other plugins' {@link module:core/plugin~PluginInterface#constructor constructors}.
  78. * This means the code of that plugin will be executed **before** {@link module:core/plugin~PluginInterface#init `init()`} and
  79. * {@link module:core/plugin~PluginInterface#afterInit `afterInit()`} methods of other plugins and, for instance,
  80. * you cannot use it to extend other plugins' {@glink framework/guides/architecture/editing-engine#schema schema}
  81. * rules as they are defined later on during the `init()` stage.
  82. *
  83. * @interface PluginInterface
  84. */
  85. /**
  86. * Creates a new plugin instance. This is the first step of the plugin initialization.
  87. * See also {@link #init} and {@link #afterInit}.
  88. *
  89. * A plugin is always instantiated after its {@link module:core/plugin~PluginInterface.requires dependencies} and the
  90. * {@link #init} and {@link #afterInit} methods are called in the same order.
  91. *
  92. * Usually, you will want to put your plugin's initialization code in the {@link #init} method.
  93. * The constructor can be understood as "before init" and used in special cases, just like
  94. * {@link #afterInit} serves the special "after init" scenarios (e.g.the code which depends on other
  95. * plugins, but which does not {@link module:core/plugin~PluginInterface.requires explicitly require} them).
  96. *
  97. * @method #constructor
  98. * @param {module:core/editor/editor~Editor} editor
  99. */
  100. /**
  101. * An array of plugins required by this plugin.
  102. *
  103. * To keep the plugin class definition tight it is recommended to define this property as a static getter:
  104. *
  105. * import Image from './image.js';
  106. *
  107. * export default class ImageCaption {
  108. * static get requires() {
  109. * return [ Image ];
  110. * }
  111. * }
  112. *
  113. * @static
  114. * @readonly
  115. * @member {Array.<Function>|undefined} module:core/plugin~PluginInterface.requires
  116. */
  117. /**
  118. * An optional name of the plugin. If set, the plugin will be available in
  119. * {@link module:core/plugincollection~PluginCollection#get} by its
  120. * name and its constructor. If not, then only by its constructor.
  121. *
  122. * The name should reflect the constructor name.
  123. *
  124. * To keep the plugin class definition tight it is recommended to define this property as a static getter:
  125. *
  126. * export default class ImageCaption {
  127. * static get pluginName() {
  128. * return 'ImageCaption';
  129. * }
  130. * }
  131. *
  132. * Note: The native `Function.name` property could not be used to keep the plugin name because
  133. * it will be mangled during code minification.
  134. *
  135. * Naming a plugin is necessary to enable removing it through the
  136. * {@link module:core/editor/editorconfig~EditorConfig#removePlugins `config.removePlugins`} option.
  137. *
  138. * @static
  139. * @readonly
  140. * @member {String|undefined} module:core/plugin~PluginInterface.pluginName
  141. */
  142. /**
  143. * The second stage (after plugin {@link #constructor}) of plugin initialization.
  144. * Unlike the plugin constructor this method can be asynchronous.
  145. *
  146. * A plugin's `init()` method is called after its {@link module:core/plugin~PluginInterface.requires dependencies} are initialized,
  147. * so in the same order as constructors of these plugins.
  148. *
  149. * **Note:** This method is optional. A plugin instance does not need to have it defined.
  150. *
  151. * @method #init
  152. * @returns {null|Promise}
  153. */
  154. /**
  155. * The third (and last) stage of plugin initialization. See also {@link #constructor} and {@link #init}.
  156. *
  157. * **Note:** This method is optional. A plugin instance does not need to have it defined.
  158. *
  159. * @method #afterInit
  160. * @returns {null|Promise}
  161. */
  162. /**
  163. * Destroys the plugin.
  164. *
  165. * **Note:** This method is optional. A plugin instance does not need to have it defined.
  166. *
  167. * @method #destroy
  168. * @returns {null|Promise}
  169. */
  170. /**
  171. * Array of loaded plugins.
  172. *
  173. * @typedef {Array.<module:core/plugin~PluginInterface>} module:core/plugin~LoadedPlugins
  174. */