From 1e81fe88cc2d099a50739e5e8a451f28f7f4f8b4 Mon Sep 17 00:00:00 2001 From: Brian Genisio Date: Tue, 13 Oct 2026 16:40:44 -0400 Subject: [PATCH 1/2] Show thumbnails for image attachments. Co-authored-by: Cursor --- public/app.css | 24 ++++++++++++ public/app.js | 83 +++++++++++++++++++++++++++++++++++++++-- tests/a11y-mail.test.js | 47 +++++++++++++++++++++++ 3 files changed, 151 insertions(+), 3 deletions(-) diff --git a/public/app.css b/public/app.css index 34f6431..60c8ecd 100644 --- a/public/app.css +++ b/public/app.css @@ -1508,6 +1508,22 @@ body { gap: var(--UI-Spacing-spacing-xxs); } +.tag.attachment--image { + height: auto; + min-height: var(--UI-Input-sm); + padding-block: var(--UI-Spacing-spacing-xxs); + padding-left: var(--UI-Spacing-spacing-xxs); +} + +.attachment-thumb { + flex-shrink: 0; + width: 40px; + height: 40px; + object-fit: cover; + border-radius: var(--UI-Radius-tag-medium); + background: var(--Colors-Border-Subtle); +} + .composer__attachment-remove { display: inline-flex; align-items: center; @@ -2112,6 +2128,14 @@ body { padding-bottom: 0; } +.attachment-preview-modal__image { + display: block; + max-width: 100%; + max-height: min(60vh, 28rem); + margin: 0 auto; + object-fit: contain; +} + .attachment-preview-modal__body { margin: 0; white-space: pre-wrap; diff --git a/public/app.js b/public/app.js index fa0facb..c2fab6e 100644 --- a/public/app.js +++ b/public/app.js @@ -1479,20 +1479,92 @@ function renderAttachmentList(attachments) { for (const attachment of attachments) { const name = attachment.name || t('Attachment'); const hasText = typeof attachment.text === 'string' && attachment.text.length > 0; - const chip = document.createElement(hasText ? 'button' : 'span'); + const imageUrl = isImageAttachment(attachment) ? attachment.ref?.url || '' : ''; + const previewable = hasText || Boolean(imageUrl); + const chip = document.createElement(previewable ? 'button' : 'span'); chip.className = 'tag outline email__attachment'; chip.textContent = name; - if (hasText) { + if (previewable) { chip.type = 'button'; chip.classList.add('email__attachment--preview'); chip.setAttribute('aria-label', `${t('Preview attachment')}: ${name}`); - chip.addEventListener('click', () => openAttachmentPreview(attachment)); + chip.addEventListener('click', () => { + if (chip.classList.contains('attachment--image')) openImagePreview(name, imageUrl); + else openAttachmentPreview(attachment); + }); + } + // Upload URLs are presigned and expire; fall back to the plain name chip. + if (imageUrl) { + prependAttachmentThumb(chip, imageUrl, () => { + if (!hasText) chip.replaceWith(Object.assign(document.createElement('span'), { + className: 'tag outline email__attachment', + textContent: name, + })); + }); } list.appendChild(chip); } return list; } +function isImageAttachment({ type, name } = {}) { + if (typeof type === 'string' && type.startsWith('image/')) return true; + return /\.(png|jpe?g|gif|webp)$/i.test(name || ''); +} + +function prependAttachmentThumb(chip, src, onError) { + if (!src) return; + const img = document.createElement('img'); + img.className = 'attachment-thumb'; + img.alt = ''; + img.decoding = 'async'; + img.addEventListener('error', () => { + img.remove(); + chip.classList.remove('attachment--image'); + onError?.(); + }, { once: true }); + img.src = src; + chip.classList.add('attachment--image'); + chip.prepend(img); +} + +const filePreviewUrls = new WeakMap(); + +function filePreviewUrl(file) { + if (!file || !isImageAttachment(file) || typeof URL.createObjectURL !== 'function') return ''; + let url = filePreviewUrls.get(file); + if (!url) { + url = URL.createObjectURL(file); + filePreviewUrls.set(file, url); + } + return url; +} + +function releaseFilePreview(file) { + const url = filePreviewUrls.get(file); + if (!url) return; + URL.revokeObjectURL(url); + filePreviewUrls.delete(file); +} + +function openImagePreview(name, url) { + const modal = new Modal({ + size: 'medium', + title: name, + content: `${escapeHtml(name)}`, + closeOnOverlayClick: true, + closeButtonLabel: t('Close'), + footerButtons: [ + { + label: t('Close'), + type: 'secondary', + onClick: () => modal.close(), + }, + ], + }); + modal.open(); +} + // The original message carried by a forward, quoted under the cover note. // The composer preview names its own scrolling container instead. function renderForwardedBlock(forwarded, { labelled = true } = {}) { @@ -2296,6 +2368,7 @@ function renderAttachmentPreview() { const label = item.status === 'uploading' ? `${item.file.name} ${t('(uploading…)')}` : item.status === 'error' ? `${item.file.name} ${t('(failed)')}` : item.file.name; chip.innerHTML = `${escapeHtml(label)}`; + prependAttachmentThumb(chip, filePreviewUrl(item.file)); const remove = document.createElement('button'); remove.type = 'button'; remove.className = 'composer__attachment-remove'; @@ -2303,6 +2376,7 @@ function renderAttachmentPreview() { remove.innerHTML = ''; remove.addEventListener('click', () => { state.attachments.splice(idx, 1); + releaseFilePreview(item.file); renderAttachmentPreview(); }); chip.appendChild(remove); @@ -4102,6 +4176,7 @@ function renderAssistantAttachments() { const label = item.status === 'uploading' ? `${item.file.name} ${t('(uploading…)')}` : item.status === 'error' ? `${item.file.name} ${t('(failed)')}` : item.file.name; chip.innerHTML = `${escapeHtml(label)}`; + prependAttachmentThumb(chip, filePreviewUrl(item.file)); const remove = document.createElement('button'); remove.type = 'button'; remove.className = 'composer__attachment-remove'; @@ -4109,6 +4184,7 @@ function renderAssistantAttachments() { remove.innerHTML = ''; remove.addEventListener('click', () => { state.assistant.attachments.splice(idx, 1); + releaseFilePreview(item.file); renderAssistantAttachments(); syncAssistantBusyControls(); els.assistantInput?.focus(); @@ -4275,6 +4351,7 @@ export function mailAppTestHooks() { sendEmail, handleComposerFiles, handleAssistantFiles, + renderAttachmentList, sendAssistant, scrollToEmail, startReply, diff --git a/tests/a11y-mail.test.js b/tests/a11y-mail.test.js index 403e9f3..ccdd071 100644 --- a/tests/a11y-mail.test.js +++ b/tests/a11y-mail.test.js @@ -1375,6 +1375,53 @@ describe('M13 failed upload', () => { }); }); +describe('Attachment thumbnails', () => { + const { createObjectURL, revokeObjectURL } = URL; + afterEach(() => { + URL.createObjectURL = createObjectURL; + URL.revokeObjectURL = revokeObjectURL; + }); + + it('shows a local preview for an image in the composer and revokes it on remove', async () => { + clickThread('thread-quote'); + await nextFrame(); + URL.createObjectURL = vi.fn(() => 'blob:photo'); + URL.revokeObjectURL = vi.fn(); + mail.state.config.attachments = { enabled: true, allowedTypes: ['.png', '.pdf'] }; + mail.state.assistant.chat = { uploadFiles: vi.fn(async (files) => files.map((file) => ({ name: file.name }))) }; + + await mail.handleComposerFiles([ + new File(['png'], 'photo.png', { type: 'image/png' }), + new File(['%PDF'], 'brief.pdf', { type: 'application/pdf' }), + ]); + + const [imageChip, pdfChip] = document.querySelectorAll('.composer__attachment'); + const thumb = imageChip.querySelector('img.attachment-thumb'); + expect(thumb.getAttribute('src')).toBe('blob:photo'); + expect(thumb.alt).toBe(''); + expect(pdfChip.querySelector('img')).toBeNull(); + expect(URL.createObjectURL).toHaveBeenCalledTimes(1); + + imageChip.querySelector('.composer__attachment-remove').click(); + expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:photo'); + }); + + it('shows a sent image thumbnail and falls back to the name when the URL fails', () => { + const list = mail.renderAttachmentList([ + { name: 'photo.png', type: 'image/png', ref: { url: 'https://files.test/photo.png' } }, + ]); + const chip = list.querySelector('.email__attachment--preview'); + expect(computeAccessibleName(chip)).toBe(`${strings['Preview attachment']}: photo.png`); + const thumb = chip.querySelector('img.attachment-thumb'); + expect(thumb.getAttribute('src')).toBe('https://files.test/photo.png'); + + thumb.dispatchEvent(new Event('error')); + expect(list.querySelector('img')).toBeNull(); + expect(list.querySelector('button')).toBeNull(); + expect(list.querySelector('.email__attachment').textContent).toBe('photo.png'); + }); +}); + describe('Assistant chat attachments', () => { const pdf = () => new File(['%PDF'], 'brief.pdf', { type: 'application/pdf' }); const docx = () => new File(['doc'], 'notes.docx', { From 8d49c73513d2419d46d45f35f350f3f17ce81add Mon Sep 17 00:00:00 2001 From: Brian Genisio Date: Tue, 13 Oct 2026 17:03:49 -0400 Subject: [PATCH 2/2] Revoke attachment preview URLs on clear and pick image preview from local state. Co-authored-by: Cursor --- public/app.js | 8 ++++++-- tests/a11y-mail.test.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/public/app.js b/public/app.js index c2fab6e..07a11f7 100644 --- a/public/app.js +++ b/public/app.js @@ -1480,7 +1480,8 @@ function renderAttachmentList(attachments) { const name = attachment.name || t('Attachment'); const hasText = typeof attachment.text === 'string' && attachment.text.length > 0; const imageUrl = isImageAttachment(attachment) ? attachment.ref?.url || '' : ''; - const previewable = hasText || Boolean(imageUrl); + let canPreviewImage = Boolean(imageUrl); + const previewable = hasText || canPreviewImage; const chip = document.createElement(previewable ? 'button' : 'span'); chip.className = 'tag outline email__attachment'; chip.textContent = name; @@ -1489,13 +1490,14 @@ function renderAttachmentList(attachments) { chip.classList.add('email__attachment--preview'); chip.setAttribute('aria-label', `${t('Preview attachment')}: ${name}`); chip.addEventListener('click', () => { - if (chip.classList.contains('attachment--image')) openImagePreview(name, imageUrl); + if (canPreviewImage) openImagePreview(name, imageUrl); else openAttachmentPreview(attachment); }); } // Upload URLs are presigned and expire; fall back to the plain name chip. if (imageUrl) { prependAttachmentThumb(chip, imageUrl, () => { + canPreviewImage = false; if (!hasText) chip.replaceWith(Object.assign(document.createElement('span'), { className: 'tag outline email__attachment', textContent: name, @@ -2435,6 +2437,7 @@ function readyAttachments() { } function clearAttachments() { + for (const item of state.attachments) releaseFilePreview(item.file); state.attachments = []; renderAttachmentPreview(); } @@ -4235,6 +4238,7 @@ async function handleAssistantFiles(files) { } function clearAssistantAttachments() { + for (const item of state.assistant.attachments) releaseFilePreview(item.file); state.assistant.attachments = []; renderAssistantAttachments(); } diff --git a/tests/a11y-mail.test.js b/tests/a11y-mail.test.js index ccdd071..f1b34be 100644 --- a/tests/a11y-mail.test.js +++ b/tests/a11y-mail.test.js @@ -1406,6 +1406,20 @@ describe('Attachment thumbnails', () => { expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:photo'); }); + it('releases preview URLs when the composer is cleared', async () => { + clickThread('thread-quote'); + await nextFrame(); + URL.createObjectURL = vi.fn(() => 'blob:photo'); + URL.revokeObjectURL = vi.fn(); + mail.state.config.attachments = { enabled: true, allowedTypes: ['.png'] }; + mail.state.assistant.chat = { uploadFiles: vi.fn(async (files) => files.map((file) => ({ name: file.name }))) }; + + await mail.handleComposerFiles([new File(['png'], 'photo.png', { type: 'image/png' })]); + mail.startCompose({ blank: true }); + + expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:photo'); + }); + it('shows a sent image thumbnail and falls back to the name when the URL fails', () => { const list = mail.renderAttachmentList([ { name: 'photo.png', type: 'image/png', ref: { url: 'https://files.test/photo.png' } },