plugin.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @license Copyright (c) 2003-2017, 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. * @mixes module:utils/observablemixin~ObservaleMixin
  14. */
  15. export default class Plugin {
  16. /**
  17. * Creates a new Plugin instance. This is the first step of a plugin initialization.
  18. * See also {@link #init} and {@link #afterInit}.
  19. *
  20. * A plugin is always instantiated after its {@link module:core/plugin~Plugin.requires dependencies} and the
  21. * {@link #init} and {@link #afterInit} methods are called in the same order.
  22. *
  23. * Usually, you'll want to put your plugin's initialization code in the {@link #init} method.
  24. * The constructor can be understood as "before init" and used in special cases, just like
  25. * {@link #afterInit} servers for the special "after init" scenarios (e.g. code which depends on other
  26. * plugins, but which doesn't {@link module:core/plugin~Plugin.requires explicitly require} them).
  27. *
  28. * @param {module:core/editor/editor~Editor} editor
  29. */
  30. constructor( editor ) {
  31. /**
  32. * The editor instance.
  33. *
  34. * @readonly
  35. * @member {module:core/editor/editor~Editor} module:core/plugin~Plugin#editor
  36. */
  37. this.editor = editor;
  38. }
  39. /**
  40. * An array of plugins required by this plugin.
  41. *
  42. * To keep a plugin class definition tight it's recommended to define this property as a static getter:
  43. *
  44. * import Image from './image.js';
  45. *
  46. * export default class ImageCaption extends Plugin {
  47. * static get requires() {
  48. * return [ Image ];
  49. * }
  50. * }
  51. *
  52. * @static
  53. * @member {Array.<Function>|undefined} module:core/plugin~Plugin.requires
  54. */
  55. /**
  56. * Optional name of the plugin. If set, the plugin will be available in
  57. * {@link module:core/plugincollection~PluginCollection#get} by its
  58. * name and its constructor. If not, then only by its constructor.
  59. *
  60. * The name should reflect the package name + path to that module. E.g. `ckeditor5-image/src/image.js` plugin
  61. * should be named `image/image` (the `ckeditor5-` prefix is stripped during compilation).
  62. *
  63. * To keep a plugin class definition tight it's recommended to define this property as a static getter:
  64. *
  65. * export default class ImageCaption {
  66. * static get pluginName() {
  67. * return 'image/imagecaption';
  68. * }
  69. * }
  70. *
  71. * @static
  72. * @member {String|undefined} module:core/plugin~Plugin.pluginName
  73. */
  74. /**
  75. * The second stage (after plugin {@link #constructor}) of plugin initialization.
  76. * Unlike the plugin constructor this method can perform asynchronous.
  77. *
  78. * A plugin's `init()` method is called after its {@link module:core/plugin~Plugin.requires dependencies} are initialized,
  79. * so in the same order as constructors of these plugins.
  80. *
  81. * @returns {null|Promise}
  82. */
  83. init() {}
  84. /**
  85. * The third (and last) stage of plugin initialization. See also {@link #constructor} and {@link #init}.
  86. *
  87. * @returns {null|Promise}
  88. */
  89. afterInit() {}
  90. /**
  91. * Destroys the plugin.
  92. *
  93. * @returns {null|Promise}
  94. */
  95. destroy() {}
  96. }
  97. mix( Plugin, ObservableMixin );