Skip to main content

Rich Text Formatting Guide

This guide explains how to use rich text formatting in the CometChat Angular V5 UIKit, including text formatting, mentions, links, and more.

Overview

The UIKit provides a custom rich text editor built on native browser APIs, offering:
  • Lightweight implementation (no external dependencies)
  • Full formatting support (bold, italic, underline, etc.)
  • Mentions integration
  • Undo/redo history
  • Keyboard shortcuts
  • Full accessibility

Basic Usage

In MessageComposer

The CometChatMessageComposer component includes rich text formatting by default:

Custom Implementation

To use the rich text editor in your own components:

Text Formatting

Inline Formatting

Apply formatting to selected text or at cursor position:

Keyboard Shortcuts

Users can apply formatting using keyboard shortcuts:

Block Formatting

Apply formatting to entire blocks:

Lists

Creating Lists

List Behavior

  • Enter: Creates a new list item
  • Enter (twice): Exits the list
  • Tab: Indents the list item
  • Shift+Tab: Outdents the list item

Example

URL Validation

The editor automatically validates URLs:
  • Invalid URLs display an error
  • URLs without protocol are prefixed with https://
  • Pasted URLs are automatically converted to clickable links

Example

Mentions

Basic Mention Integration

The editor integrates with CometChatMentionsFormatter for @mention functionality:

Inserting Mentions

Mention Styling

Mentions are styled differently based on whether they’re for the current user:

Mention Limits

  • Maximum of 10 unique mentions per message
  • Attempting to add more displays an error

Getting Mention Data

Loading Messages with Mentions

When loading a message that contains mentions:

Undo/Redo

Using History

History Grouping

Rapid typing is automatically grouped into single undo steps:
  • Typing delay: 500ms
  • Each formatting operation creates a new undo step
  • History stack size: 100 entries

Example

Content Management

Getting Content

Setting Content

Checking Content State

Format State

Tracking Format State

Use the reactive signal to track which formats are active:

Manual Format State Check

Copy, Paste, and Drag

Paste Handling

The editor automatically handles pasted content:

Drag and Drop

Users can drag and drop text within the editor:
  • Formatting is preserved
  • Cursor position is updated

Copy

When users copy formatted text:
  • Formatting is preserved in clipboard
  • Both HTML and plain text formats are available

Accessibility

Keyboard Navigation

All features are accessible via keyboard:
  • Tab: Focus editor
  • Arrow keys: Navigate content
  • Escape: Close mention popup
  • Enter: New line or list item
  • Formatting shortcuts: See table above

Screen Reader Support

The editor announces:
  • Formatting changes (“Bold applied”)
  • Mention insertions (“Mentioned John Doe”)
  • Undo/redo operations (“Undone”, “Redone”)

ARIA Attributes

The editor includes proper ARIA attributes:
  • role="textbox"
  • aria-label="Message editor"
  • aria-multiline="true"
  • aria-placeholder="Type your message..."

Styling

Using CSS Variables

Customize the editor appearance using CSS variables:

Custom Styles

Apply custom styles to the editor:

Performance

Optimization Tips

  1. Debounce updates: Use debouncing for expensive operations
  2. Lazy load mentions: Load mention suggestions on demand
  3. Limit history: History is automatically limited to 100 entries
  4. Clean up: Always destroy editors when done

Performance Metrics

The editor is optimized for:
  • Typing latency: < 50ms per keystroke (10,000 characters)
  • Formatting operations: < 100ms
  • Initialization: < 50ms
  • Paste operations: < 200ms

Security

XSS Protection

All HTML is sanitized to prevent XSS attacks:
  • Script tags removed
  • Event handlers removed
  • Dangerous attributes removed
  • Only safe HTML tags allowed

Content Validation

  • URLs are validated before insertion
  • Mention data is sanitized
  • Pasted content is sanitized

Best Practices

1. Always Clean Up

2. Use Reactive Signals

3. Handle Empty State

4. Validate Before Sending

Troubleshooting

Editor Not Focusing

Formatting Not Working

Mentions Not Showing

See Also