cordis/client/webapp/elements/msg-txt.ts

54 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-10-30 17:26:41 +00:00
import * as moment from 'moment';
import ElementsUtil from './require/elements-util';
import { Message, Member, IDummyTextMessage } from '../data-types';
2021-10-30 17:26:41 +00:00
import ClientController from '../client-controller';
import Q from '../q-module';
2021-10-30 17:26:41 +00:00
2021-11-21 18:29:42 +00:00
export default function createTextMessage(q: Q, guild: CombinedGuild, message: Message | IDummyTextMessage): HTMLElement {
2021-10-30 17:26:41 +00:00
let memberInfo: {
roleColor: string | null,
displayName: string,
avatarResourceId: string | null
};
if (message instanceof Message) {
if (message.member instanceof Member) {
memberInfo = {
roleColor: message.member.roleColor,
displayName: message.member.displayName,
avatarResourceId: message.member.avatarResourceId
};
} else {
memberInfo = {
roleColor: null,
displayName: 'Unknown Member',
avatarResourceId: null
};
}
} else {
memberInfo = {
roleColor: null,
displayName: message.member.displayName,
avatarResourceId: message.member.avatarResourceId
};
}
let nameStyle = memberInfo.roleColor ? 'color: ' + memberInfo.roleColor : '';
let element = q.create({ class: 'message', 'meta-id': message.id, 'meta-member-id': message.member.id, 'meta-server-id': server.id, content: [
2021-10-30 17:26:41 +00:00
{ class: 'member-avatar', content: { tag: 'img', src: './img/loading.svg', alt: memberInfo.displayName } },
{ class: 'right', content: [
{ class: 'header', content: [
{ class: 'member-name', style: nameStyle, content: memberInfo.displayName },
{ class: 'timestamp', content: moment(message.sent).calendar(ElementsUtil.calendarFormats) }
] },
{ class: 'content text', content: ElementsUtil.parseMessageText(message.text || '') }
] }
] }) as HTMLElement;
(async () => {
(q.$$$(element, '.member-avatar img') as HTMLImageElement).src =
await ElementsUtil.getImageBufferFromResourceFailSoftly(server, memberInfo.avatarResourceId);
2021-10-30 17:26:41 +00:00
})();
return element;
}