Use this file to discover all available pages before exploring further.
The FormatterConfigService is a centralized Angular service for managing default text formatters across the CometChat UIKit. It provides a single source of truth for formatter configuration, allowing you to set formatters once and have them automatically applied across all text-displaying components.
Replaces the built-in default formatters with custom formatters.Parameters:
formatters: Array of custom formatters to use as defaults
Use Case: When you want complete control over which formatters are used globally.Example:
// In app initialization (main.ts or app.component.ts)export class AppComponent implements OnInit { private formatterConfig = inject(FormatterConfigService); ngOnInit() { // Replace defaults with custom formatters const customFormatters = [ new MyCustomMentionsFormatter(), new MyCustomUrlFormatter(), new HashtagFormatter(), new EmailFormatter() ]; this.formatterConfig.setDefaultFormatters(customFormatters); }}
Note: Once custom formatters are set, addFormatters() has no effect. To add to custom formatters, include them in the array passed to setDefaultFormatters().
Resets to the built-in default formatters, clearing any custom or additional formatters.Use Case: When you want to restore the original formatter configuration.Example:
export class SettingsComponent { private formatterConfig = inject(FormatterConfigService); resetFormatters() { // Clear all customizations this.formatterConfig.resetToDefaults(); // Now getDefaultFormatters() returns: // [mentions, URLs] }}
Replace the built-in formatters with your own custom set.
// In app initialization (main.ts or app.component.ts)export class AppComponent implements OnInit { private formatterConfig = inject(FormatterConfigService); ngOnInit() { // Define custom formatters const customFormatters = [ new MyCustomMentionsFormatter(), new MyCustomUrlFormatter(), new HashtagFormatter() ]; // Set as defaults this.formatterConfig.setDefaultFormatters(customFormatters); }}
When to use: When you need complete control over formatter behavior globally.
To create a custom formatter, extend CometChatTextFormatter:
import { CometChatTextFormatter } from '@cometchat/chat-uikit-angular';export class HashtagFormatter extends CometChatTextFormatter { constructor() { super(); this.priority = 50; // Execute after mentions (20) but before URLs (100) } getRegex(): RegExp { // Match hashtags: #word return /#(\w+)/g; } format(text: string, message?: CometChat.BaseMessage): string { return text.replace(this.getRegex(), (match, tag) => { return `<span class="cometchat-hashtag" data-tag="${tag}">#${tag}</span>`; }); } shouldFormat(text: string, message?: CometChat.BaseMessage): boolean { // Only format if text contains hashtags return this.getRegex().test(text); }}
Using the custom formatter:
// Add to defaultsthis.formatterConfig.addFormatters([new HashtagFormatter()]);// Or set as custom defaultsthis.formatterConfig.setDefaultFormatters([ new CometChatMentionsFormatter(), new HashtagFormatter(), new CometChatUrlFormatter()]);
Formatters are created once and reused across all components:
// ✅ GOOD: Service reuses instancesconst formatters1 = this.formatterConfig.getDefaultFormatters();const formatters2 = this.formatterConfig.getDefaultFormatters();// formatters1 and formatters2 contain the same instances
// ✅ GOOD: Configure once in AppComponentexport class AppComponent implements OnInit { private formatterConfig = inject(FormatterConfigService); ngOnInit() { this.formatterConfig.addFormatters([ new HashtagFormatter(), new EmailFormatter() ]); }}// All components automatically use these formatters
Problem: Custom formatters added but not executing.Solution: Check if custom formatters were set (which overrides additions):
// If you called setDefaultFormatters(), addFormatters() has no effectthis.formatterConfig.setDefaultFormatters([...]); // Overrides everythingthis.formatterConfig.addFormatters([...]); // This won't work// Solution: Include all formatters in setDefaultFormatters()this.formatterConfig.setDefaultFormatters([ new CometChatMentionsFormatter(), new CometChatUrlFormatter(), new MyCustomFormatter() // Include custom here]);
FormatterConfigService is provided at the root level (providedIn: 'root'), so all components share the same formatter configuration by default. If you need different formatters for different message lists (e.g., a main chat with full formatting vs. a thread panel with minimal formatting), scope the service to a wrapper component:
@Component({ selector: 'app-thread-panel', standalone: true, imports: [CometChatMessageListComponent], providers: [FormatterConfigService], // Scoped instance template: `<cometchat-message-list [user]="user" [parentMessageId]="parentMessageId"></cometchat-message-list>`})export class ThreadPanelComponent implements OnInit { // This is the LOCAL instance, not the root singleton private formatterConfig = inject(FormatterConfigService); ngOnInit(): void { // Only affects text rendering inside this wrapper // e.g., thread panel uses only URL formatting, no mentions this.formatterConfig.setDefaultFormatters([ new CometChatUrlFormatter() ]); }}