import {
Component,
ElementRef,
HostListener,
OnInit,
TemplateRef,
ViewChild,
} from "@angular/core";
import { CometChat } from "@cometchat/chat-sdk-javascript";
import {
CometChatMessageComposerAction,
CometChatMessageEvents,
CometChatMessageOption,
CometChatMessageTemplate,
CometChatTheme,
CometChatUIKitConstants,
fontHelper,
} from "@cometchat/uikit-resources";
import {
MessagesConfiguration,
ComposerId,
CometChatUIKitHelper,
CometChatUIKitUtility,
MessageStatus,
} from "@cometchat/uikit-shared";
import {
CometChatUIKit,
CometChatThemeService,
} from "@cometchat/chat-uikit-angular";
import "@cometchat/uikit-elements"; //accesing web component image bubble
@Component({
selector: "app-message-option",
templateUrl: "./message-option.component.html",
styleUrls: ["./message-option.component.scss"],
})
export class MessageOptionComponent implements OnInit {
//template ref for text bubble
@ViewChild("textBubbleRef") textBubbleRef!: TemplateRef<any>;
messagesConfiguration: MessagesConfiguration = new MessagesConfiguration({});
loggedInUser: CometChat.User | null = null;
//accessing global theme object.
constructor(private themeService: CometChatThemeService) {}
//get the existing message template and override text bubble options on init.
ngOnInit() {
CometChat.getLoggedinUser().then((user) => {
this.loggedInUser = user;
});
//default templates
let templates = CometChatUIKit.getDataSource().getAllMessageTemplates(
this.themeService.theme
);
//updating the contentview for text bubble
for (let i = 0; i < templates.length; i++) {
if (
templates[i].category ==
CometChatUIKitConstants.MessageCategory.message &&
templates[i].type == CometChatUIKitConstants.MessageTypes.image
) {
//templates[i].contentView = () => { return this.textBubbleRef };
templates[i].options = (
loggedInUser: CometChat.User,
message: CometChat.BaseMessage,
theme: CometChatTheme,
group?: CometChat.Group | undefined
) => {
let options = CometChatUIKit.getDataSource().getTextMessageOptions(
loggedInUser,
message,
theme,
group
);
//if (!message.getSender() || (this.loggedInUser?.getUid() == message.getSender().getUid())) {
options.unshift(this.createOption(message, theme));
//}
return options;
};
break;
}
}
//creating new configuration
this.messagesConfiguration.messageListConfiguration.templates = templates;
this.messagesConfiguration = { ...this.messagesConfiguration };
}
createOption(message: CometChat.BaseMessage, theme: CometChatTheme) {
let mediaMessage = message as CometChat.MediaMessage;
return new CometChatMessageOption({
id: "download",
title: "Download",
iconURL: "assets/star.svg",
onClick: () => this.downloadImage(mediaMessage),
iconTint: theme.palette.getAccent600(),
backgroundColor: "transparent",
titleFont: fontHelper(theme.typography.subtitle1),
titleColor: theme.palette.getAccent600(),
});
}
downloadImage = async (message: CometChat.MediaMessage) => {
const attachments = message.getAttachments();
if (Array.isArray(attachments) && attachments.length) {
const attachment = attachments[0];
const imageName = attachment.getName();
const image = await fetch(attachment.getUrl());
const imageBlob = await image.blob();
const imageURL = URL.createObjectURL(imageBlob);
const link = document.createElement("a");
link.href = imageURL;
link.download = imageName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
};
}