CometChatTextFormatter
. 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.
CometChatTextFormatter
:
To integrate the CometChatTextFormatter
class into your application:
CometChatTextFormatter
from the CometChat UI Kit angular library.CometChatTextFormatter
class to create your custom text formatter class. In this case, let’s create a HashTagTextFormatter
.trackCharacter
, regexPatterns
and regexToReplaceFormatting
. For a hashtag formatter, we’re setting the track character to #
.getFormattedText
, getOriginalText
and custom logic to format text 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. |
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.reRender
, the composer will invoke the getFormattedText
function for all text formatters, following the same order in which they were received as props.