Rich text editor component for Angular

npm version

CKEditor 5 consists of ready-to-use editor builds and CKEditor 5 Framework upon which the builds are based.

Currently, the CKEditor 5 component for Angular supports integrating CKEditor 5 only via builds. Integrating CKEditor 5 built from source is not possible yet due to the lack of ability to adjust webpack configuration in angular-cli.

While there is no support to integrate CKEditor 5 from source yet, you can still create a custom build of CKEditor 5 and include it in your Angular application.

Quick start

In your existing Angular project, install the CKEditor 5 WYSIWYG editor component for Angular:

  1. npm install --save @ckeditor/ckeditor5-angular

Install one of the official editor builds or create a custom one (e.g. if you want to install more plugins or customize something that cannot be controlled with the editor configuration).

Assuming that you picked @ckeditor/ckeditor5-build-classic:

  1. npm install --save @ckeditor/ckeditor5-build-classic

Now, add CKEditorModule to your application module imports:

  1. import { CKEditorModule } from '@ckeditor/ckeditor5-angular';
  2. @NgModule( {
  3. imports: [
  4. CKEditorModule,
  5. // ...
  6. ],
  7. // ...
  8. } )

Import the editor build in your Angular component and assign it to a public property so it becomes accessible in the template:

  1. import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
  2. @Component( {
  3. // ...
  4. } )
  5. export class MyComponent {
  6. public Editor = ClassicEditor;
  7. // ...
  8. }

Finally, use the <ckeditor> tag in the template to run the rich text editor:

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

Rebuild your application and CKEditor 5 should greet you with “Hello, world!”.

Note: Using the Document editor build

If you want to use the document editor build, you need to add the toolbar to the DOM manually.

  1. import * as DecoupledEditor from '@ckeditor/ckeditor5-build-decoupled-document';
  2. @Component( {
  3. // ...
  4. export class MyComponent {
  5. public Editor = DecoupledEditor;
  6. public onReady( editor ) {
  7. editor.ui.getEditableElement().parentElement.insertBefore(
  8. editor.ui.view.toolbar.element,
  9. editor.ui.getEditableElement()
  10. );
  11. }
  12. }

And then, in the template:

  1. <ckeditor [editor]="Editor" data="<p>Hello, world!</p>" (ready)="onReady($event)"></ckeditor>

Integration with ngModel

The component implements the ControlValueAccessor interface and works with the ngModel. Here is how to use it:

  • Create some model in your component to share with the editor:
  1. @Component( {
  2. // ...
  3. } )
  4. export class MyComponent {
  5. public model = {
  6. editorData: '<p>Hello, world!</p>'
  7. };
  8. // ...
  9. }
  • Use the model in the template to enable a two–way data binding:
  1. <ckeditor [(ngModel)]="model.editorData" [editor]="Editor"></ckeditor>

Supported @Input properties

The following @Input properties are supported by the CKEditor 5 rich text editor component for Angular:

editor (required)

The Editor which provides the static create() method to create an instance of the editor:

  1. <ckeditor [editor]="Editor"></ckeditor>

config

The configuration of the editor:

  1. <ckeditor [config]="{ toolbar: [ 'heading', '|', 'bold', 'italic' ] }" ...></ckeditor>

data

The initial data of the editor. It can be a static value:

  1. <ckeditor data="<p>Hello, world!</p>" ...></ckeditor>

or a shared parent component’s property

  1. @Component( {
  2. // ...
  3. } )
  4. export class MyComponent {
  5. public editorData = '<p>Hello, world!</p>';
  6. // ...
  7. }
  1. <ckeditor [data]="editorData" ...></ckeditor>

tagName

The tag name of the HTML element on which the rich text editor will be created.

The default tag is <div>.

  1. <ckeditor tagName="textarea" ...></ckeditor>

disabled

Controls the editor’s read–only state:

  1. @Component( {
  2. // ...
  3. } )
  4. export class MyComponent {
  5. public isDisabled = false;
  6. // ...
  7. toggleDisabled() {
  8. this.isDisabled = !this.isDisabled
  9. }
  10. }
  1. <ckeditor [disabled]="isDisabled" ...></ckeditor>
  2. <button (click)="toggleDisabled()">
  3. {{ isDisabled ? 'Enable editor' : 'Disable editor' }}
  4. </button>

Supported @Output properties

The following @Output properties are supported by the CKEditor 5 rich text editor component for Angular:

ready

Fired when the editor is ready. It corresponds with the editor#ready event.It is fired with the editor instance.

change

Fired when the content of the editor has changed. It corresponds with the editor.model.document#change:data event.It is fired with an object containing the editor and the CKEditor 5 change:data event object.

  1. <ckeditor [editor]="Editor" (change)="onChange($event)"></ckeditor>
  1. import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
  2. import { ChangeEvent } from '@ckeditor/ckeditor5-angular/ckeditor.component';
  3. @Component( {
  4. // ...
  5. } )
  6. export class MyComponent {
  7. public Editor = ClassicEditor;
  8. public onChange( { editor }: ChangeEvent ) {
  9. const data = editor.getData();
  10. console.log( data );
  11. }
  12. ...
  13. }

blur

Fired when the editing view of the editor is blurred. It corresponds with the editor.editing.view.document#blur event.It is fired with an object containing the editor and the CKEditor 5 blur event data.

focus

Fired when the editing view of the editor is focused. It corresponds with the editor.editing.view.document#focus event.It is fired with an object containing the editor and the CKEditor 5 focus event data.

Styling

The CKEditor 5 rich text editor component for Angular can be styled using the component stylesheet or using a global stylesheet. See how to set the CKEditor 5 component’s height using these two approaches.

Setting the height via the component stylesheet

First, create a (S)CSS file in the parent component’s directory and style the given editor’s part preceded by the :host and ::ng-deep pseudo selectors:

  1. /* src/app/app.component.css */
  2. :host ::ng-deep .ck-editor__editable_inline {
  3. min-height: 500px;
  4. }

Then in the parent component add the relative path to the above stylesheet:

  1. /* src/app/app.component.ts */
  2. @Component( {
  3. // ...
  4. styleUrls: [ './app.component.css' ]
  5. } )

Setting the height via a global stylesheet

To style the component using a global stylesheet, first, create it:

  1. /* src/styles.css */
  2. .ck-editor__editable_inline {
  3. min-height: 500px;
  4. }

Then, add it in the angular.json configuration file:

  1. "architect": {
  2. "build": {
  3. "options": {
  4. "styles": [
  5. { "input": "src/styles.css" }
  6. ]
  7. }
  8. }
  9. }

Setting the placeholder

To display the placeholder in the main editable element, set the placeholder field in the CKEditor 5 rich text editor component configuration:

  1. @Component( {
  2. // ...
  3. } )
  4. export class MyComponent {
  5. public config = {
  6. placeholder: 'Type the content here!'
  7. }
  8. }

Localization

The CKEditor 5 rich text editor component can be localized in two steps.

1. Loading translation files

First, you need to add translation files to the bundle. This step can be achieved in two ways:

  • By importing translations for given languages directly in your component file:
  1. import '@ckeditor/ckeditor5-build-classic/build/translations/de';
  2. import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
  3. ...
  • By adding paths to translation files to the "scripts" array in angular.json:
  1. "architect": {
  2. "build": {
  3. "options": {
  4. "scripts": [ "node_modules/@ckeditor/ckeditor5-build-classic/build/translations/de.js" ]
  5. }
  6. }
  7. }

2. Configuring the language

Then, you need to configure the editor to use the given language:

  1. @Component( {
  2. // ...
  3. } )
  4. export class MyComponent {
  5. public Editor = ClassicEditor;
  6. public config = {
  7. language: 'de'
  8. };
  9. }

For advanced usage see the Setting UI language guide.

Contributing and reporting issues

The source code of the CKEditor 5 rich text editor component for Angular is available on GitHub in https://github.com/ckeditor/ckeditor5-angular.