Enter key in Angular Rich text editor component

27 Apr 202415 minutes to read

Rich Text Editor allows to customize the tag that is inserted when pressing the enter key and shift + enter key in the Rich Text Editor.

Enter key customization

By default, the <p> tag will be created while pressing the enter key. The enter key can be customized by using the enterKey property, where the possible tags that can be used to customize are <p>, <div>, and <br>.

When the enter key is customized with any of the possible values, pressing the enter key in the editor will create a new tag that is configured. Also, when the enter key is configured the default value of the Rich Text Editor will also change respectively with the configured values.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { RichTextEditorAllModule } from '@syncfusion/ej2-angular-richtexteditor'
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns'
import { DialogModule } from '@syncfusion/ej2-angular-popups'



    import { Component, ViewChild, ViewEncapsulation } from '@angular/core';
    import { createElement } from '@syncfusion/ej2-base';
    import { RichTextEditorComponent, ToolbarService, LinkService, ImageService, HtmlEditorService } from '@syncfusion/ej2-angular-richtexteditor';
    import { DropDownListComponent, FieldSettingsModel } from '@syncfusion/ej2-angular-dropdowns';
    @Component({
imports: [
        
        RichTextEditorAllModule,
        DialogModule,
        DropDownListModule
    ],


standalone: true,
    selector: 'app-root',
    template:`<div class="control-section">
        <table class='api'>
            <tbody>
                <tr>
                    <td>
                        <div>
                            <ejs-dropdownlist id='enterOption' #enterOption
                            [dataSource]='enterOptionData' (change)='enterChange()'
                            [value]='enterValue' [fields]='fields' [popupHeight]='height'
                            [placeholder]='enterPlaceHolder' [floatLabelType]='floatLabel'></ejs-dropdownlist>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
        <br>
        <ejs-richtexteditor id='defaultRTE' #defaultRTE [height]='rteHeight'>
            <ng-template #valueTemplate>
                <p>In Rich text Editor, the enter key and shift + enter key actions can be customized using the enterKey and shiftEnterKey APIs. And the possible values are as follows:</p><ul><li>P - When 'P' is configured, pressing enter or shift + enter will create a 'p' tag</li><li>DIV - When 'DIV' is configured, pressing enter or shift + enter will create a 'div' tag</li><li>BR - When 'BR' is configured, pressing enter or shift + enter will create a 'br' tag</li></ul>
            </ng-template>
        </ejs-richtexteditor>
    </div>`,
    encapsulation: ViewEncapsulation.None,
    providers: [ToolbarService, LinkService, ImageService, HtmlEditorService]
    })
    export class AppComponent  {
         @ViewChild('defaultRTE')
        public rteObj?: RichTextEditorComponent;

        @ViewChild('enterOption')
        public enterObj?: DropDownListComponent;

        public enterOptionData: { [key: string]: Object }[] = [
            { Text: 'Create a new <p>', Value: 'P' },
            { Text: 'Create a new <div>', Value: 'DIV' },
            { Text: 'Create a new <br>', Value: 'BR' }
        ];

        public enterPlaceHolder: string = 'When pressing the enter key';
        public floatLabel: string = 'Always';
        public fields: FieldSettingsModel = { text: 'Text', value: 'Value' };
        public rteHeight = 220;
        public height: string = '200px';
        public enterValue: string = 'P';

        public enterChange(): void {
            if (this.enterObj!.value === 'P') {
                this.rteObj!.enterKey = 'P';
                this.rteObj!.value = `<p>In Rich text Editor, the enter key and shift + enter key actions can be customized using the enterKey and shiftEnterKey APIs. And the possible values are as follows:</p><ul><li>P - When 'P' is configured, pressing enter or shift + enter will create a 'p' tag</li><li>DIV - When 'DIV' is configured, pressing enter or shift + enter will create a 'div' tag</li><li>BR - When 'BR' is configured, pressing enter or shift + enter will create a 'br' tag</li></ul>`;
            } else if (this.enterObj!.value === 'DIV') {
                this.rteObj!.enterKey = 'DIV';
                this.rteObj!.value = `<div>In Rich text Editor, the enter key and shift + enter key actions can be customized using the enterKey and shiftEnterKey APIs. And the possible values are as follows:</div><ul><li>P - When 'P' is configured, pressing enter or shift + enter will create a 'p' tag</li><li>DIV - When 'DIV' is configured, pressing enter or shift + enter will create a 'div' tag</li><li>BR - When 'BR' is configured, pressing enter or shift + enter will create a 'br' tag</li></ul>`;
            } else if (this.enterObj!.value === 'BR') {
                this.rteObj!.enterKey = 'BR';
                this.rteObj!.value = `In Rich text Editor, the enter key and shift + enter key actions can be customized using the enterKey and shiftEnterKey APIs. And the possible values are as follows:<ul><li>P - When 'P' is configured, pressing enter or shift + enter will create a 'p' tag</li><li>DIV - When 'DIV' is configured, pressing enter or shift + enter will create a 'div' tag</li><li>BR - When 'BR' is configured, pressing enter or shift + enter will create a 'br' tag</li></ul>`;
            }
        }
    }
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Shift-Enter key customization

By default, the <br> tag will be created while pressing the shift + enter key. The shift + enter key can be customized by using the shiftEnterKey property where the possible tags that can be used to customize are <br>, <p>, <div>.

When the shift + enter key is customized with any of the possible values, pressing the shift + enter key in the editor will create a new tag that is configured. Also, when the shift + enter key is configured the default value of the Rich Text Editor will change respectively with the configured values.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { RichTextEditorAllModule } from '@syncfusion/ej2-angular-richtexteditor'
import { DropDownListModule } from '@syncfusion/ej2-angular-dropdowns'
import { DialogModule } from '@syncfusion/ej2-angular-popups'



    import { Component, ViewChild, ViewEncapsulation } from '@angular/core';
    import { createElement } from '@syncfusion/ej2-base';
    import { RichTextEditorComponent, ToolbarService, LinkService, ImageService, HtmlEditorService } from '@syncfusion/ej2-angular-richtexteditor';
    import { DropDownListComponent, FieldSettingsModel } from '@syncfusion/ej2-angular-dropdowns';
    @Component({
imports: [
        
        RichTextEditorAllModule,
        DialogModule,
        DropDownListModule
    ],


standalone: true,
    selector: 'app-root',
    template:`<div class="control-section">
        <table class='api'>
            <tbody>
                <tr>
                    <td>
                        <div>
                            <ejs-dropdownlist id='shiftEnterOption' #shiftEnterOption
                            [dataSource]='shiftEnterData' (change)='shiftEnterChange()'
                            [value]='shiftEnterValue' [fields]='fields' [popupHeight]='height'
                            [placeholder]='shiftEnterPlaceHolder' [floatLabelType]='floatLabel'></ejs-dropdownlist>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
        <br>
        <ejs-richtexteditor id='defaultRTE' #defaultRTE [height]='rteHeight'>
            <ng-template #valueTemplate>
                <p>In Rich text Editor, the enter key and shift + enter key actions can be customized using the enterKey and shiftEnterKey APIs. And the possible values are as follows:</p><ul><li>P - When 'P' is configured, pressing enter or shift + enter will create a 'p' tag</li><li>DIV - When 'DIV' is configured, pressing enter or shift + enter will create a 'div' tag</li><li>BR - When 'BR' is configured, pressing enter or shift + enter will create a 'br' tag</li></ul>
            </ng-template>
        </ejs-richtexteditor>
    </div>`,
    encapsulation: ViewEncapsulation.None,
    providers: [ToolbarService, LinkService, ImageService, HtmlEditorService]
    })
    export class AppComponent  {
         @ViewChild('defaultRTE')
        public rteObj?: RichTextEditorComponent;

        @ViewChild('shiftEnterOption')
        public shiftEnterObj?: DropDownListComponent;

        public shiftEnterData: { [key: string]: Object }[] = [
            { Text: 'Create a new <br>', Value: 'BR' },
            { Text: 'Create a new <div>', Value: 'DIV' },
            { Text: 'Create a new <p>', Value: 'P' }
        ];

        public shiftEnterPlaceHolder: string = 'When pressing the shift + enter key';
        public floatLabel: string = 'Always';
        public fields: FieldSettingsModel = { text: 'Text', value: 'Value' };
        public rteHeight = 220;
        public height: string = '200px';
        public shiftEnterValue: string = 'BR';
        public shiftEnterChange(): void {
            if (this.shiftEnterObj!.value === 'BR') {
                this.rteObj!.shiftEnterKey = 'BR';
            } else if (this.shiftEnterObj!.value === 'DIV') {
                this.rteObj!.shiftEnterKey = 'DIV';
            } else if (this.shiftEnterObj!.value === 'P') {
                this.rteObj!.shiftEnterKey = 'P';
            }
        }
    }
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));