Add outgoing mail attachments
Support multiple attachments in desktop and Android compose, persist them in drafts and offline queues, and include them in desktop OpenPGP protection. Update docs and tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
+81
-2
@@ -398,6 +398,83 @@ function setupDraftComposer() {
|
||||
const modal = root.querySelector('.compose-modal');
|
||||
if (!modal) return;
|
||||
modal.dataset.draftId = draft?.id || '';
|
||||
let attachments = Array.isArray(draft?.attachments) ? draft.attachments : [];
|
||||
modal._composeAttachments = attachments;
|
||||
const fields = modal.querySelector('.compose-fields');
|
||||
const attachmentRow = document.createElement('div');
|
||||
attachmentRow.className = 'compose-line';
|
||||
const attachmentLabel = document.createElement('span');
|
||||
attachmentLabel.textContent = 'Attachments';
|
||||
const attachmentControls = document.createElement('div');
|
||||
attachmentControls.className = 'compose-attachments';
|
||||
const attachmentInput = document.createElement('input');
|
||||
attachmentInput.type = 'file';
|
||||
attachmentInput.multiple = true;
|
||||
attachmentInput.accept = '*/*';
|
||||
attachmentInput.hidden = true;
|
||||
attachmentInput.setAttribute('aria-label', 'Choose files to attach');
|
||||
const attachButton = document.createElement('button');
|
||||
attachButton.type = 'button';
|
||||
attachButton.className = 'discard';
|
||||
attachButton.textContent = 'Attach files…';
|
||||
const attachmentList = document.createElement('div');
|
||||
attachmentList.className = 'attachment-list';
|
||||
attachmentList.setAttribute('aria-live', 'polite');
|
||||
attachmentControls.append(attachButton, attachmentInput);
|
||||
attachmentRow.append(attachmentLabel, attachmentControls);
|
||||
fields.append(attachmentRow, attachmentList);
|
||||
attachButton.onclick = () => attachmentInput.click();
|
||||
const renderAttachments = () => {
|
||||
attachmentList.replaceChildren();
|
||||
attachments.forEach((file, index) => {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'attachment-row';
|
||||
const name = document.createElement('strong');
|
||||
name.textContent = file.filename;
|
||||
const size = document.createElement('small');
|
||||
size.textContent = `${((file.size || 0) / 1048576).toFixed(2)} MB`;
|
||||
const remove = document.createElement('button');
|
||||
remove.type = 'button';
|
||||
remove.className = 'discard';
|
||||
remove.textContent = 'Remove';
|
||||
remove.setAttribute('aria-label', `Remove ${file.filename}`);
|
||||
remove.onclick = () => {
|
||||
attachments.splice(index, 1);
|
||||
renderAttachments();
|
||||
void persist().catch(error => toast(error.message || 'Could not save the draft'));
|
||||
};
|
||||
row.append(name, size, remove);
|
||||
attachmentList.append(row);
|
||||
});
|
||||
};
|
||||
attachmentInput.onchange = async () => {
|
||||
attachButton.disabled = true;
|
||||
try {
|
||||
const selected = [];
|
||||
for (const file of attachmentInput.files) {
|
||||
const bytes = new Uint8Array(await file.arrayBuffer());
|
||||
let binary = '';
|
||||
for (let offset = 0; offset < bytes.length; offset += 0x8000) {
|
||||
binary += String.fromCharCode(...bytes.subarray(offset, offset + 0x8000));
|
||||
}
|
||||
selected.push({
|
||||
filename: file.name,
|
||||
contentType: file.type || 'application/octet-stream',
|
||||
size: file.size,
|
||||
contentBase64: btoa(binary)
|
||||
});
|
||||
}
|
||||
attachments.push(...selected);
|
||||
renderAttachments();
|
||||
await persist();
|
||||
} catch (error) {
|
||||
toast(error.message || 'Could not add the selected files');
|
||||
} finally {
|
||||
attachmentInput.value = '';
|
||||
attachButton.disabled = false;
|
||||
}
|
||||
};
|
||||
renderAttachments();
|
||||
if (draft) {
|
||||
$('#compose-to').value = draft.to || '';
|
||||
$('#compose-cc').value = draft.cc || '';
|
||||
@@ -420,9 +497,10 @@ function setupDraftComposer() {
|
||||
to: $('#compose-to').value,
|
||||
cc: $('#compose-cc').value,
|
||||
subject: $('#compose-subject').value,
|
||||
text: $('#compose-body').value
|
||||
text: $('#compose-body').value,
|
||||
attachments
|
||||
};
|
||||
if (!Object.values(values).some(value => typeof value === 'string' && value.trim())) return;
|
||||
if (!Object.values(values).some(value => typeof value === 'string' && value.trim()) && !attachments.length) return;
|
||||
modal.dataset.draftId = values.id;
|
||||
const result = await window.daybreakMail.saveDraft(values);
|
||||
modal.dataset.draftId = result.id;
|
||||
@@ -668,6 +746,7 @@ if (window.daybreakMail) {
|
||||
cc: $('#compose-cc').value,
|
||||
subject: $('#compose-subject').value.trim(),
|
||||
text: $('#compose-body').value,
|
||||
attachments: $('#modal-root .compose-modal')?._composeAttachments || [],
|
||||
displayName: connectedMailAccount.displayName || profileSettings.name,
|
||||
...(pgp ? { pgp } : {})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user