Overview
You can create your custom text formatter for CometChat using theCometChatTextFormatter
. CometChatTextFormatter
is an abstract utility class that serves as a foundational structure for enabling text formatting in the message composer and text message bubbles. It can be extended to create custom formatter classes, tailored to suit specific application needs, making it a valuable tool for text customization and enhancement in chat interfaces.
Features
- Text Formatting:: Enables automatic formatting of text within messages based on specified styles and settings, enhancing the visual presentation of chat content.
- Customizable Styles:: Tailor text styles, including colors, fonts, and background colors, to match the desired appearance for formatted text.
- Dynamic Text Replacement:: Utilizes regular expression patterns to identify and replace specific text patterns with formatted content, offering flexibility in text manipulation.
- Input Field Integration:: Seamlessly integrates with the text input field, allowing for real-time monitoring and processing of user input for formatting purposes.
- Callback Functions:: Implement callback functions for key up and key down events, providing hooks for custom actions or formatting logic based on user interactions.
Always wrap formatted data in a span with a unique CSS class (for example, “custom-hashtag”). This lets the UI Kit render it as-is instead of sanitizing it along with other tags.
Usage
here are the steps to create your custom text formatter for CometChat using theCometChatTextFormatter
:
To integrate the CometChatTextFormatter
class into your application:
- Firstly, you need to import
CometChatTextFormatter
from the CometChat UI Kit react library.
- Now, extend the
CometChatTextFormatter
class to create your custom text formatter class. In this case, let’s create aHashTagTextFormatter
.
- set up the
trackCharacter
,regexPatterns
andregexToReplaceFormatting
. For a hashtag formatter, we’re setting the track character to#
.
- Set callback functions for key up and key down events.
- Implement the
getFormattedText
,getOriginalText
and custom logic to format text methods.
Example
Below is an example demonstrating how to use a custom formatter class in components such as CometChatConversations, CometChatMessageList, CometChatMessageComposer.
Methods
Methods | Setter | Description |
---|---|---|
protected trackCharacter | setTrackingCharacter(trackCharacter: string) | The character to track in the text input field. This can be used to start capturing texts to format. Example: This would be # in case of HashTags. And a HashTag formatter would start capturing texts as soon as it encounters # . |
protected currentCaretPosition | setCaretPositionAndRange(currentCaretPosition: Selection, currentRange: Range) | Message Composer sets the current selection in this field. |
protected currentRange | setCaretPositionAndRange(currentCaretPosition: Selection, currentRange: Range) | Message Composer sets the text range that the user has selected or the cursor position in this field. |
protected inputElementReference | setInputElementReference(inputElementReference: HTMLElement) | Message Composer sets the Reference to the text input field(DOM element) in this field. This can be used to read the textContent . |
protected regexPatterns | setRegexPatterns(regexPatterns: Array<RegExp>) | Array of regex patterns to find specific text pattern in the user input text in order to replace those with formatted texts. |
protected regexToReplaceFormatting | setRegexToReplaceFormatting(regexToReplaceFormatting: Array<RegExp>) | Array of regex patterns to replace the formatter text with original text. |
protected keyUpCallBack | setKeyUpCallBack(keyUpCallBack: Function) | The callback function to be called for key up events. |
protected keyDownCallBack | setKeyDownCallBack(keyDownCallBack: Function) | The callback function to be called for key down events. |
protected reRender | setReRender(reRender: Function) | Callback function to be called in order to trigger a re-render of the message composer component. This is useful for force rerendering the composer in order to update text content on the UI. |
protected loggedInUser | setLoggedInUser(loggedInUser: CometChat.User) | Composer and text bubbles will set the logged in user object in this field. |
protected id | setId(id: string) | Use this to set a unique id for the formatter instance. |
The
textContent
or innerHtml
of the reference element should not be directly modified in a formatter. The formatters should instead call the reRender
callback to force the composer to call the getFormattedText
function of the formatter instead.Upon calling
reRender
, the composer will invoke the getFormattedText
function for all text formatters, following the same order in which they were received as props.