plugin.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 implements {@link module:core/editor/editorwithui~EditorWithUI} interface in addition
  25. * to the base {@link module:core/editor/editor~Editor} interface. However, editors with external UI
  26. * (i.e. Bootstrap based) or headless editor may not implement {@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. * - "Editing" part which use only {@link module:core/editor/editor~Editor} interface,
  31. * - "UI" part which use both {@link module:core/editor/editor~Editor} interface and
  32. * {@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 it can be a simple function (it will be used as a constructor) which accepts
  51. * {@link module:core/editor/editor~Editor the editor} as a parm.
  52. * It can also implement a few methods which, when present, will be used to properly initialize and destroy the plugin.
  53. *
  54. * // A simple plugin which enables a data processor.
  55. * function MyPlugin( editor ) {
  56. * editor.data.processor = new MyDataProcessor();
  57. * }
  58. *
  59. * In most cases, however, you'll want to inherit from the {@link module:core/plugin~Plugin} class which implements the
  60. * {@link module:utils/observablemixin~ObservableMixin} and is, therefore, more convenient:
  61. *
  62. * class MyPlugin extends Plugin {
  63. * init() {
  64. * // `listenTo()` and `editor` are available thanks to `Plugin`.
  65. * // By using `listenTo()` you'll ensure that the listener will be removed when
  66. * // the plugin is destroyed.
  67. * this.listenTo( this.editor, 'dataReady', () => {
  68. * // Do something when data is ready.
  69. * } );
  70. * }
  71. * }
  72. *
  73. * @interface PluginInterface
  74. */
  75. /**
  76. * Creates a new plugin instance. This is the first step of a plugin initialization.
  77. * See also {@link #init} and {@link #afterInit}.
  78. *
  79. * A plugin is always instantiated after its {@link module:core/plugin~PluginInterface.requires dependencies} and the
  80. * {@link #init} and {@link #afterInit} methods are called in the same order.
  81. *
  82. * Usually, you'll want to put your plugin's initialization code in the {@link #init} method.
  83. * The constructor can be understood as "before init" and used in special cases, just like
  84. * {@link #afterInit} servers for the special "after init" scenarios (e.g. code which depends on other
  85. * plugins, but which doesn't {@link module:core/plugin~PluginInterface.requires explicitly require} them).
  86. *
  87. * @method #constructor
  88. * @param {module:core/editor/editor~Editor} editor
  89. */
  90. /**
  91. * An array of plugins required by this plugin.
  92. *
  93. * To keep a plugin class definition tight it's recommended to define this property as a static getter:
  94. *
  95. * import Image from './image.js';
  96. *
  97. * export default class ImageCaption {
  98. * static get requires() {
  99. * return [ Image ];
  100. * }
  101. * }
  102. *
  103. * @static
  104. * @readonly
  105. * @member {Array.<Function>|undefined} module:core/plugin~PluginInterface.requires
  106. */
  107. /**
  108. * Optional name of the plugin. If set, the plugin will be available in
  109. * {@link module:core/plugincollection~PluginCollection#get} by its
  110. * name and its constructor. If not, then only by its constructor.
  111. *
  112. * The name should reflect the constructor name.
  113. *
  114. * To keep a plugin class definition tight it's recommended to define this property as a static getter:
  115. *
  116. * export default class ImageCaption {
  117. * static get pluginName() {
  118. * return 'ImageCaption';
  119. * }
  120. * }
  121. *
  122. * Note: The native `Function.name` property could not be used to keep the plugin name because
  123. * it will be mangled during code minification.
  124. *
  125. * Naming a plugin is necessary to enable removing it through the
  126. * {@link module:core/editor/editorconfig~EditorConfig#removePlugins `config.removePlugins`} option.
  127. *
  128. * @static
  129. * @readonly
  130. * @member {String|undefined} module:core/plugin~PluginInterface.pluginName
  131. */
  132. /**
  133. * The second stage (after plugin {@link #constructor}) of plugin initialization.
  134. * Unlike the plugin constructor this method can be asynchronous.
  135. *
  136. * A plugin's `init()` method is called after its {@link module:core/plugin~PluginInterface.requires dependencies} are initialized,
  137. * so in the same order as constructors of these plugins.
  138. *
  139. * **Note:** This method is optional. A plugin instance does not need to have to have it defined.
  140. *
  141. * @method #init
  142. * @returns {null|Promise}
  143. */
  144. /**
  145. * The third (and last) stage of plugin initialization. See also {@link #constructor} and {@link #init}.
  146. *
  147. * **Note:** This method is optional. A plugin instance does not need to have to have it defined.
  148. *
  149. * @method #afterInit
  150. * @returns {null|Promise}
  151. */
  152. /**
  153. * Destroys the plugin.
  154. *
  155. * **Note:** This method is optional. A plugin instance does not need to have to have it defined.
  156. *
  157. * @method #destroy
  158. * @returns {null|Promise}
  159. */