Skip to main content

Introduction

CometChatTextFormatter is an abstract utility class that provides the foundation for enabling rich text formatting in message composers and message bubbles. It can be extended to create custom formatter classes, allowing for specific text formatting such as mentions, hashtags, markdown, or any other custom styles within chat interfaces.

Prerequisites

Before implementing custom text formatters, ensure you have the following:
  • An active CometChat account and application.
  • Basic understanding of JavaScript and web development.
  • Knowledge of regular expressions and their implementation in JavaScript.

Text Formatter Fields

Text formatters have several fields to manage the formatting state and behavior:
Directly modifying the textContent or innerHtml of the input element in formatters is not recommended. Use the reRender method to update text content in the UI.
The getFormattedText function of all formatters is invoked upon calling reRender, maintaining the order in which they were received as props.

Custom Formatter Implementation

To create a custom formatter, extend the CometChatTextFormatter class and override the necessary methods. Ensure to handle text matching, formatting, and event listening as per your requirements.

Example Implementation

Here’s an example of how to implement a hashtag text formatter: