|
3 | 3 | * Copyright (c) 2021 - 2026 Vaadin Ltd. |
4 | 4 | * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ |
5 | 5 | */ |
| 6 | +import { html } from 'lit'; |
| 7 | +import { ifDefined } from 'lit/directives/if-defined.js'; |
6 | 8 | import { FocusMixin } from '@vaadin/a11y-base/src/focus-mixin.js'; |
7 | 9 | import { SlotController } from '@vaadin/component-base/src/slot-controller.js'; |
8 | 10 |
|
@@ -78,6 +80,22 @@ export const MessageMixin = (superClass) => |
78 | 80 | type: Number, |
79 | 81 | }, |
80 | 82 |
|
| 83 | + /** |
| 84 | + * An array of attachment objects to display with the message. |
| 85 | + * Each attachment object can have the following properties: |
| 86 | + * - `name`: The name of the attachment file |
| 87 | + * - `url`: The URL of the attachment |
| 88 | + * - `type`: The MIME type of the attachment (e.g., 'image/png', 'application/pdf') |
| 89 | + * |
| 90 | + * Image attachments (type starting with "image/") show a thumbnail preview, |
| 91 | + * while other attachments show a document icon with the file name. |
| 92 | + * |
| 93 | + * @type {Array<{name?: string, url?: string, type?: string}>} |
| 94 | + */ |
| 95 | + attachments: { |
| 96 | + type: Array, |
| 97 | + }, |
| 98 | + |
81 | 99 | /** @private */ |
82 | 100 | _avatar: { |
83 | 101 | type: Object, |
@@ -113,4 +131,61 @@ export const MessageMixin = (superClass) => |
113 | 131 | }); |
114 | 132 | } |
115 | 133 | } |
| 134 | + |
| 135 | + /** |
| 136 | + * Renders attachments for the message. |
| 137 | + * @private |
| 138 | + */ |
| 139 | + __renderAttachments() { |
| 140 | + const attachments = this.attachments; |
| 141 | + if (!attachments || attachments.length === 0) { |
| 142 | + return ''; |
| 143 | + } |
| 144 | + |
| 145 | + return html` |
| 146 | + <div part="attachments">${attachments.map((attachment) => this.__renderAttachment(attachment))}</div> |
| 147 | + `; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Renders a single attachment. |
| 152 | + * @param {Object} attachment - The attachment object with name, url, and type properties |
| 153 | + * @private |
| 154 | + */ |
| 155 | + __renderAttachment(attachment) { |
| 156 | + const isImage = attachment.type && attachment.type.startsWith('image/'); |
| 157 | + |
| 158 | + if (isImage) { |
| 159 | + return html` |
| 160 | + <button |
| 161 | + type="button" |
| 162 | + part="attachment attachment-image" |
| 163 | + aria-label="${attachment.name || ''}" |
| 164 | + @click="${() => this.__onAttachmentClick(attachment)}" |
| 165 | + > |
| 166 | + <img part="attachment-preview" src="${ifDefined(attachment.url)}" alt="" /> |
| 167 | + </button> |
| 168 | + `; |
| 169 | + } |
| 170 | + |
| 171 | + return html` |
| 172 | + <button type="button" part="attachment attachment-file" @click="${() => this.__onAttachmentClick(attachment)}"> |
| 173 | + <span part="attachment-icon" aria-hidden="true"></span> |
| 174 | + <span part="attachment-name">${attachment.name || ''}</span> |
| 175 | + </button> |
| 176 | + `; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Dispatches an event when an attachment is clicked. |
| 181 | + * @param {Object} attachment - The attachment that was clicked |
| 182 | + * @private |
| 183 | + */ |
| 184 | + __onAttachmentClick(attachment) { |
| 185 | + this.dispatchEvent( |
| 186 | + new CustomEvent('attachment-click', { |
| 187 | + detail: { attachment }, |
| 188 | + }), |
| 189 | + ); |
| 190 | + } |
116 | 191 | }; |
0 commit comments