-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmessage-attachments.ts
More file actions
219 lines (200 loc) · 7.16 KB
/
message-attachments.ts
File metadata and controls
219 lines (200 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { consume } from '@lit/context';
import { html, LitElement, nothing } from 'lit';
import { property } from 'lit/decorators.js';
import { cache } from 'lit/directives/cache.js';
import { repeat } from 'lit/directives/repeat.js';
import { until } from 'lit/directives/until.js';
import { addThemingController } from '../../theming/theming-controller.js';
import IgcIconButtonComponent from '../button/icon-button.js';
import { chatContext } from '../common/context.js';
import { registerComponent } from '../common/definitions/register.js';
import { partMap } from '../common/part-map.js';
import { trimmedHtml } from '../common/util.js';
import IgcIconComponent from '../icon/icon.js';
import type { ChatState } from './chat-state.js';
import { all } from './themes/attachments.js';
import { styles } from './themes/message-attachments.base.css.js';
import { styles as shared } from './themes/shared/message-attachments/message-attachments.common.css.js';
import type {
ChatAttachmentRenderContext,
ChatTemplateRenderer,
IgcChatMessage,
IgcChatMessageAttachment,
} from './types.js';
import {
ChatFileTypeIcons,
createAttachmentURL,
getFileExtension,
isImageAttachment,
} from './utils.js';
type DefaultAttachmentRenderers = {
attachment: ChatTemplateRenderer<ChatAttachmentRenderContext>;
attachmentHeader: ChatTemplateRenderer<ChatAttachmentRenderContext>;
attachmentContent: ChatTemplateRenderer<ChatAttachmentRenderContext>;
};
/**
* A component that renders message attachments within a chat.
*
* Displays attachments such as images or files, supporting custom templates
* and default rendering using expansion panels.
*
* @element igc-message-attachments
*
* @csspart attachments-container - Container wrapping all attachments.
* @csspart attachment - Wrapper for a single attachment.
* @csspart attachment-header - Wrapper for a single attachment header.
* @csspart attachments-content - Part representing the attachment preview.
* @csspart attachment-icon - Icon part representing the attachment type.
* @csspart file-name - Part representing the attachment's file name.
* @csspart actions - Container for header action buttons.
* @csspart image-attachment - Part for the image element inside an image attachment.
*
* @fires igcAttachmentClick - Fired when an attachment header is toggled (clicked).
* @hidden @internal
*/
export default class IgcMessageAttachmentsComponent extends LitElement {
public static readonly tagName = 'igc-message-attachments';
public static override styles = [styles, shared];
/* blazorSuppress */
public static register(): void {
registerComponent(
IgcMessageAttachmentsComponent,
IgcIconComponent,
IgcIconButtonComponent
);
}
private readonly _defaults: Readonly<DefaultAttachmentRenderers> =
Object.freeze<DefaultAttachmentRenderers>({
attachment: (ctx) => this._renderAttachment(ctx.attachment),
attachmentHeader: (ctx) => this.renderHeader(ctx.attachment),
attachmentContent: (ctx) => this._renderContent(ctx.attachment),
});
@consume({ context: chatContext, subscribe: true })
private readonly _state!: ChatState;
/**
* The array of attachments to render.
*/
@property({ attribute: false })
public message?: IgcChatMessage;
constructor() {
super();
addThemingController(this, all);
}
private _getRenderer(name: keyof DefaultAttachmentRenderers) {
return this._state.options?.renderers
? (this._state.options.renderers[name] ?? this._defaults[name])
: this._defaults[name];
}
private _handleHeaderClick = (attachment: IgcChatMessageAttachment) => {
this._state.emitEvent('igcAttachmentClick', { detail: attachment });
};
/**
* Default attachment header template used when no custom template is provided.
* Renders the attachment icon and name.
* @param attachment The message attachment to render
* @returns TemplateResult containing the rendered attachment header
*/
private renderHeader(attachment: IgcChatMessageAttachment) {
const isCurrentUser = this._state.isCurrentUserMessage(this.message);
const iconName = isImageAttachment(attachment)
? 'attach_image'
: 'document_thumbnail';
return html`
${!isCurrentUser
? html`<igc-icon name=${iconName} part="attachment-icon"></igc-icon>`
: nothing}
<span part="file-name">${attachment.name}</span>
`;
}
/**
* Default attachment content template used when no custom template is provided.
* Renders the attachment content based on its type.
* @param attachment The message attachment to render
* @returns TemplateResult containing the rendered attachment content
*/
private _renderContent(attachment: IgcChatMessageAttachment) {
const iconName =
ChatFileTypeIcons.get(getFileExtension(attachment.name)) ??
ChatFileTypeIcons.get('default')!;
return isImageAttachment(attachment)
? html`
<img
part="image-attachment-icon"
src=${createAttachmentURL(attachment)}
alt=${attachment.name}
/>
`
: html`<igc-icon
part="file-attachment-icon"
name=${iconName}
></igc-icon>`;
}
private _renderAttachment(attachment: IgcChatMessageAttachment) {
const isCurrentUser = this._state.isCurrentUserMessage(this.message);
const contentParts = {
'attachment-content': true,
sent: isCurrentUser,
};
const headerParts = {
'attachment-header': true,
sent: isCurrentUser,
};
const ctx: ChatAttachmentRenderContext = {
attachment,
message: this.message!,
instance: this._state.host,
};
const content = html`<div part=${partMap(contentParts)}>
${until(this._getRenderer('attachmentContent')(ctx))}
</div>`;
const header = html` <div
part=${partMap(headerParts)}
role="button"
@click=${() => this._handleHeaderClick(attachment)}
>
<div part="details">
${until(this._getRenderer('attachmentHeader')(ctx))}
</div>
</div>`;
return html`
${isCurrentUser ? content : nothing} ${header}
${!isCurrentUser ? content : nothing}
`;
}
protected override render() {
const attachments = this.message?.attachments ?? [];
const isCurrentUser = this._state.isCurrentUserMessage(this.message);
const attachmentParts = {
attachment: true,
sent: isCurrentUser,
};
return html`${cache(
this.message
? html`
<div part="attachments-container">
${repeat(
attachments,
(attachment) => attachment.id,
(attachment) => trimmedHtml`
<div part="${partMap(attachmentParts)}">
${until(
this._getRenderer('attachment')({
attachment,
message: this.message!,
instance: this._state.host,
})
)}
</div>
`
)}
</div>
`
: nothing
)}`;
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-message-attachments': IgcMessageAttachmentsComponent;
}
}