Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add plugins to CKEditor for Angular "@ckeditor/ckeditor5-angular"?

I installed CKEditor for Angular following this guide: https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/frameworks/angular.html

I imported CKEditorModule to my Module and added it to my imports.

import { CKEditorModule } from "@ckeditor/ckeditor5-angular";

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    CKEditorModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})

In my component, I added the ClassicEditor build and assigned it to a public property.

import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';

export class AppComponent {
  title = 'AngularCkeditor';
  public Editor = ClassicEditor;
}

Finally I'm using the ckeditor tag in my html template:

<ckeditor [editor]="Editor" data="<p>Hello, world!</p>"></ckeditor>

It works pretty well!

Now I want to add some plugins to it but there is no explanation how to achieve that.

So I followed the default guide for installing plugins: https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/installing-plugins.html

For example I tried to install the Alignment plugin:

npm install --save @ckeditor/ckeditor5-alignment

Then I imported the plugin to my component and tried to load it.

import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment'; 

ClassicEditor.builtinPlugins = [
  Alignment
];

When I do this I keep stucked with an error:

TypeError: Cannot read property 'getAttribute' of null

enter image description here

It's so strange because I followed the same guide to edit the configuration of CKEditor, and it works perfectly.

ClassicEditor.defaultConfig = {
  toolbar: {
    items: [
      'heading',
      '|',
      'alignment',
      'bold',
      'italic',
      '|',
      'bulletedList',
      'numberedList',
      '|',
      'link',
      'blockQuote',
      '|',
      'imageUpload',
      '|',
      'undo',
      'redo'
    ]
  },
  image: {
    toolbar: [
      'imageStyle:full',
      'imageStyle:side',
      '|',
      'imageTextAlternative'
    ]
  },
  language: 'en'
};

enter image description here

like image 641
Kr1 Avatar asked Mar 09 '19 10:03

Kr1


People also ask

How to use CKEditor 5 with angular?

For easier setup you can try the CKEditor 5 online builder, just pick plugins you want to use, download ready to use build and attach it inside your Angular application. Sorry, something went wrong. Half a year later it's still extremely difficult to extend the classic build with plugins in an Angular environment.

How to install plugins in CKEditor 5 using NPM?

Install created project (the plugin) into CKEditor 5 build project (the one downloaded). If your plugin requires some modules from packages which are not available in this npm project, just feel free to run npm install --save-dev @ckeditor/ckeditor5-whateveryouneed. I am following the beginning of this tutorial.

Is it possible to install ckeditor5-build-classic inside node_modules?

No, you should download the ckeditor5-build-classic package outside of the node_modules directory (like a new project). Otherwise, you will lose all your changes done inside node_modules after calling npm install. Then after installing and generating the custom build, you should copy the file to the angular project and import it from the component.

Is CKEditor fully configurable?

Yes you are fully configurable and I've always loved CKEditor in the past, but this system you are suggesting is literally ridiculous. CKEditor needs to simplify how its customers are using its library. Adding a new button should be as simple as: use NEW_BUTTON or config= {toolbar: } where the config merges with the previous one.


2 Answers

Actually, the 'builtinPlugins' configuration must be done directly in the build instead our component, as explained in the guide: https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/installing-plugins.html#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.

We must create a 'custom build', and then import it in our component.

like image 79
Kr1 Avatar answered Nov 04 '22 12:11

Kr1


Take a look at the example of adding MathType plugin, you can do the same way for your case https://stackoverflow.com/a/59225002/6465520

like image 38
Maksim Vorontsov Avatar answered Nov 04 '22 12:11

Maksim Vorontsov