Introduction
CometChatUrlTextFormatter is a specialized subclass of CometChatTextFormatter designed to automatically detect URLs in text messages and convert them into clickable links, allowing users to navigate to the web addresses effortlessly within your CometChat application.
Prerequisites
To implement the URL Text Formatter in your chat application, make sure you have:- A valid CometChat account and application set up.
- Basic knowledge of JavaScript and familiarity with DOM manipulation.
- An understanding of regular expressions for pattern matching.
Implementation
CometChatUrlTextFormatter utilizes regular expressions to identify URLs and applies styles to make them visually distinct as clickable links. Here’s an example of how to extend the CometChatTextFormatter to create a URL text formatter:
Customizing Style and Behavior
When implementing theCometChatUrlTextFormatter, you can customize the appearance of links and the behavior when clicked:
- Modify the onRegexMatchmethod to wrap detected URLs in aspanelement with custom classes for styling.
- In registerEventListeners, define theonUrlClickmethod to handle opening URLs, tracking analytics, or other custom behavior upon click.
Styling Links
Apply CSS to style your clickable links, for example:Handling Click Events
Implement theonUrlClick method within your formatter class to define custom logic for when a link is clicked:
UrlFormatterStyle
Styling properties forCometChatUrlTextFormatter component.
| Attributes | Type | Description | 
|---|---|---|
| rightAlignedLinkColor | string | Sets color for links for right aligned messages | 
| rightAlignedLinkFont | string | Sets font for links for right aligned messages | 
| leftAlignedLinkColor | string | Sets color for links for left aligned messages | 
| leftAlignedLinkFont | string | Sets font for links for left aligned messages |