Installing plugins

CKEditor 5 plugins are distributed through npm packages and are implemented in a modular way, which means that a single plugin may consist of multiple JavaScript files.

In this guide you can learn how to add plugins to your editor in the two most common scenarios:

Requirements

In order to start developing CKEditor 5 you will require:

  • Node.js 6.9.0+
  • npm 4+ (note: some npm 5+ versions were known to cause problems, especially with deduplicating packages; upgrade npm when in doubt)

Adding a plugin to a build

Adding plugins to existing builds is done through their customization. Editor builds are maintained in their respective GitHub repositories. Therefore, assuming that you want to customize the classic editor build you need to:

  • Clone the build repository.
  • Install the plugin package.
  • Add it to the build configuration.
  • Bundle the build.
  1. git clone -b stable https://github.com/ckeditor/ckeditor5-build-classic.git
  2. cd ckeditor5-build-classic
  3. npm install

Now, install the plugin package:

  1. npm install --save-dev @ckeditor/ckeditor5-alignment

Edit the src/ckeditor.js file to add your plugin to the list of plugins which will be included in the build and to add your feature’s button to the toolbar:

  1. // The editor creator to use.
  2. import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  3. import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  4. import UploadAdapter from '@ckeditor/ckeditor5-adapter-ckfinder/src/uploadadapter';
  5. import Autoformat from '@ckeditor/ckeditor5-autoformat/src/autoformat';
  6. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  7. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  8. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  9. import EasyImage from '@ckeditor/ckeditor5-easy-image/src/easyimage';
  10. import Heading from '@ckeditor/ckeditor5-heading/src/heading';
  11. import Image from '@ckeditor/ckeditor5-image/src/image';
  12. import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
  13. import ImageStyle from '@ckeditor/ckeditor5-image/src/imagestyle';
  14. import ImageToolbar from '@ckeditor/ckeditor5-image/src/imagetoolbar';
  15. import ImageUpload from '@ckeditor/ckeditor5-image/src/imageupload';
  16. import Link from '@ckeditor/ckeditor5-link/src/link';
  17. import List from '@ckeditor/ckeditor5-list/src/list';
  18. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  19. import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment'; // <--- ADDED
  20. export default class ClassicEditor extends ClassicEditorBase {}
  21. // Plugins to include in the build.
  22. ClassicEditor.builtinPlugins = [
  23. Essentials,
  24. UploadAdapter,
  25. Autoformat,
  26. Bold,
  27. Italic,
  28. BlockQuote,
  29. EasyImage,
  30. Heading,
  31. Image,
  32. ImageCaption,
  33. ImageStyle,
  34. ImageToolbar,
  35. ImageUpload,
  36. Link,
  37. List,
  38. Paragraph,
  39. Alignment // <--- ADDED
  40. ];
  41. // Editor configuration.
  42. ClassicEditor.defaultConfig = {
  43. toolbar: {
  44. items: [
  45. 'heading',
  46. '|',
  47. 'alignment', // <--- ADDED
  48. 'bold',
  49. 'italic',
  50. 'link',
  51. 'bulletedList',
  52. 'numberedList',
  53. 'imageUpload',
  54. 'blockQuote',
  55. 'undo',
  56. 'redo'
  57. ]
  58. },
  59. image: {
  60. toolbar: [
  61. 'imageStyle:full',
  62. 'imageStyle:side',
  63. '|',
  64. 'imageTextAlternative'
  65. ]
  66. },
  67. // This value must be kept in sync with the language defined in webpack.config.js.
  68. language: 'en'
  69. };

Finally, bundle the build:

  1. yarn run build

If everything worked, the editor build (which is available in the build/ directory) should be updated.

You can open the sample/index.html file in your browser to see whether the plugin was installed correctly.

This was a quick version of how a build can be customized. Read more about customizing existing editor builds in a separate guide.

Adding a plugin to an editor

If you build the editor from source, then the process of installing a new plugin boils down to these three steps:

  • Installing the plugin package.
  • Adding it to your editor’s configuration.
  • Building your project. For example, if you wish to install the text alignment feature:
  1. npm install --save-dev @ckeditor/ckeditor5-alignment

Edit the code that initializes the editor:

  1. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  2. import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  3. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  4. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  5. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  6. import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment'; // <--- ADDED
  7. ClassicEditor
  8. .create( document.querySelector( '#editor' ), {
  9. plugins: [ Essentials, Paragraph, Bold, Italic, Alignment ], // <--- MODIFIED
  10. toolbar: [ 'bold', 'italic', 'alignment' ] // <--- MODIFIED
  11. } )
  12. .then( editor => {
  13. console.log( 'Editor was initialized', editor );
  14. } )
  15. .catch( error => {
  16. console.error( error.stack );
  17. } );

After rebuilding your project, the new feature will be available in the editor.

One of the possible mistakes is trying to add a plugin in this way to an existing (bundled) editor build. Installing an existing build and then trying to add a plugin to it may not work if that plugin needs to import any of the source editor modules.

The reason why this method will not work is that dependencies of the added plugin may duplicate the code already bundled in the used editor build. In the best scenario, this is going to raise the overall code size. In the worst scenario, an application built this way may be unstable.

Difference between both methods

What is the difference between adding a plugin to an editor build and adding a plugin by passing the config.plugins option to the static create() method?

The first method builds the plugin into the editor class. This means that you can then initialize the editor without passing config.plugins at all and the editor will automatically enable all built-in plugins:

  1. // Assuming you use e.g. webpack which can load UMD modules by using ES6 syntax.
  2. import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
  3. ClassicEditor
  4. .create( document.querySelector( '#editor' ), {
  5. // Look, ma! No plugins!
  6. } )
  7. .then( editor => {
  8. console.log( 'Editor was initialized', editor );
  9. } )
  10. .catch( error => {
  11. console.error( error.stack );
  12. } );

All this works because a typical src/ckeditor.js module that you can find in every editor build repository (see for example @ckeditor/ckeditor5-build-classic) looks like this:

  1. import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  2. import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  3. import UploadAdapter from '@ckeditor/ckeditor5-adapter-ckfinder/src/uploadadapter';
  4. import Autoformat from '@ckeditor/ckeditor5-autoformat/src/autoformat';
  5. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  6. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  7. import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  8. // ...
  9. export default class ClassicEditor extends ClassicEditorBase {}
  10. ClassicEditor.builtinPlugins = [
  11. Essentials,
  12. UploadAdapter,
  13. Autoformat,
  14. Bold,
  15. Italic,
  16. BlockQuote,
  17. // ...
  18. ];
  19. ClassicEditor.defaultConfig = {
  20. toolbar: {
  21. items: [
  22. 'heading',
  23. 'bold',
  24. // ...
  25. ]
  26. },
  27. // ...
  28. };

This code imports the source of the classic editor and extends it with a static builtinPlugins and defaultConfig properties where it defines a set of plugins and configuration to be used by this editor class.

In this approach, all editor instances created by using this editor build will by default load all these built-in plugins and configuration.

You can still use the config.removePlugins and config.plugins options to override the default configuration.

When building the editor from source and not using a build as a base, you can also use the static builtinPlugins and defaultConfig properties of editor classes. However, in this situation it is usually more convenient to simply pass all the plugins directly to the static create() method:

  1. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  2. import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
  3. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  4. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  5. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  6. ClassicEditor
  7. .create( document.querySelector( '#editor' ), {
  8. plugins: [ Essentials, Paragraph, Bold, Italic ],
  9. toolbar: [ 'bold', 'italic' ]
  10. } )
  11. .then( editor => {
  12. console.log( 'Editor was initialized', editor );
  13. } )
  14. .catch( error => {
  15. console.error( error.stack );
  16. } );

So, in short, both methods use very similar mechanisms. However, adding a plugin through the static builtinPlugins property (which happens in editor builds) lets you automatically enable it in all editor instances created using this editor class, while passing a plugin to create() will naturally affect only one instance.