- Enabling Code Block Formatting
- Applying Code Block Formatting
- Exiting Code Block Format
Contact Support
Code Block Formatting in the Angular Rich Text Editor Component
24 Jan 20253 minutes to read
The Angular Rich Text Editor component offers a powerful feature to format text as preformatted code blocks, making it ideal for displaying programming snippets or structured contents.
Enabling Code Block Formatting
To enable code block formatting, ensure that the Formats item is included in the toolbar items of your Rich Text Editor configuration.
Below are examples and code snippets demonstrating how to add and effectively use the code block formatting option in the Rich Text Editor.
import { Component } from '@angular/core';
import { RichTextEditorModule, ToolbarSettingsModel, ToolbarService, HtmlEditorService, QuickToolbarService, LinkService, ImageService, TableService, PasteCleanupService } from '@syncfusion/ej2-angular-richtexteditor';
@Component({
imports: [RichTextEditorModule],
standalone: true,
selector: 'app-root',
template: `<ejs-richtexteditor id='editor' [toolbarSettings]='tools' [value]="value">
</ejs-richtexteditor>`,
providers: [ToolbarService, HtmlEditorService, QuickToolbarService, LinkService, ImageService, TableService, PasteCleanupService]
})
export class AppComponent {
public value: string = "<p>The Rich Text Editor component is WYSIWYG (\"what you see is what you get\") editor that provides the best user experience to create and update the content. Users can format their content using standard toolbar commands.</p><p><b>Key features:</b></p><ul><li><p>Provides <IFRAME> and <DIV> modes</p></li><li><p>Capable of handling markdown editing.</p></li><li><p>Contains a modular library to load the necessary functionality on demand.</p></li><li><p>Provides a fully customizable toolbar.</p></li><li><p>Provides HTML view to edit the source directly for developers.</p></li><li><p>Supports third-party library integration.</p></li><li><p>Allows preview of modified content before saving it.</p></li><li><p>Handles images, hyperlinks, video, hyperlinks, uploads, etc.</p></li><li><p>Contains undo/redo manager.</p></li><li><p>Creates bulleted and numbered lists.</p></li></ul>";
public tools: ToolbarSettingsModel = {
items: ['Formats']
};
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Applying Code Block Formatting
Follow these steps to format text as a code block:
- Select the desired text in the editor.
- Click on the
Formats
dropdown in the toolbar. - Choose
Preformatted
from the dropdown menu.
Exiting Code Block Format
To exit the code block format:
- Place the cursor at the end of your code block content.
- Press the Enter key twice.
This action will move the cursor out of the code block and return to normal text formatting.
Enhancing Code Block Appearance
To make your code blocks more visually appealing, you can add custom styling. Here’s an example of how to style your <pre>
tag:
<style>
.e-richtexteditor .e-rte-content .e-content pre{
background-color: #f4f4f4; color: #333; font-family: 'Courier New', Courier, monospace; font-size: 14px; padding: 15px; border-radius: 5px; border: 1px solid #ccc; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;
}
</style>
This styling adds a light gray background, sets a monospace font, and includes padding and borders for better visual separation.