Refractoring Stage 6.5 Migrated the snooze, attachment, and OpenPGP mail UI to the explicit mail feature API.

This commit is contained in:
2026-10-16 20:31:59 +03:00
parent f19573a337
commit 84a63441ef
14 changed files with 178 additions and 108 deletions
+9 -8
View File
@@ -1,4 +1,8 @@
// Attachment previews and a dedicated view for finding and managing attachments across mailboxes.
const mailUi = window.epMailFeatures.mail;
const $ = mailUi.select;
const $$ = mailUi.selectAll;
const toast = mailUi.toast;
const attachmentFolders = ['Inbox', 'Sent', 'Archive', 'Trash'];
let attachmentItems = [];
@@ -52,11 +56,9 @@ function downloadAttachment(details) {
}
// Add a Preview button to each attachment in the reader.
if (typeof openMessage === 'function') {
const baseOpenMessage = openMessage;
openMessage = function (id) {
mailUi.wrapOpenMessage(baseOpenMessage => function (id) {
baseOpenMessage(id);
const message = messages.find(item => item.id === id);
const message = mailUi.state.messages.find(item => item.id === id);
if (!message?.uid) return;
$$('#reader-pane .attachment').forEach(row => {
const download = row.querySelector('.download');
@@ -73,12 +75,11 @@ if (typeof openMessage === 'function') {
panel = document.createElement('div');
panel.className = 'attachment-preview';
row.after(panel);
void previewAttachment(panel, { uid: message.uid, folder: message.sourceFolder || activeFolder, filename: download.dataset.filename });
void previewAttachment(panel, { uid: message.uid, folder: message.sourceFolder || mailUi.state.activeFolder, filename: download.dataset.filename });
});
download.before(button);
});
};
}
});
async function collectAttachments() {
const items = [];
@@ -103,7 +104,7 @@ async function collectAttachments() {
}
async function openAttachmentsView() {
if (!connectedMailAccount || !window.daybreakMail) { openMailAccount(); return; }
if (!mailUi.connectedAccount || !window.daybreakMail) { mailUi.openMailAccount(); return; }
const root = $('#modal-root');
root.innerHTML = `<div class="modal-overlay"><div class="compose-modal attachments-modal"><div class="modal-head"><span>Attachments</span><button class="modal-close" aria-label="Close">×</button></div><div class="compose-fields"><div class="attachment-toolbar"><input id="attachment-search" placeholder="Search file, subject, or sender"><select id="attachment-type" aria-label="File type"><option value="">All types</option><option>Images</option><option>Documents</option><option>Archives</option><option>Other</option></select></div><div id="attachment-list" class="attachment-list">Loading attachments…</div></div></div></div>`;
const close = () => { root.innerHTML = ''; };
+13 -3
View File
@@ -735,7 +735,9 @@ if (window.daybreakMail) {
sendButton.disabled = true;
sendButton.textContent = 'Sending…';
try {
const pgp = window.pgpPrepareSend ? await window.pgpPrepareSend(recipient, $('#compose-cc').value) : null;
const pgp = window.epMailFeatures.pgp?.prepareSend
? await window.epMailFeatures.pgp.prepareSend(recipient, $('#compose-cc').value)
: null;
if (pgp === false) {
sendButton.disabled = false;
sendButton.textContent = 'Send ↗';
@@ -794,8 +796,16 @@ const mailUiApi = window.epMailUiModules.createMailUiApi({
updateAccountCard,
getRenderList: () => renderList,
getOpenMessage: () => openMessage,
getShowCompose: () => showCompose,
baseRenderList: renderList,
emptyReader,
openMailAccount
openMailAccount,
select: $,
selectAll: $$,
toast,
setRenderList: renderer => { renderList = renderer; },
setOpenMessage: renderer => { openMessage = renderer; },
setShowCompose: renderer => { showCompose = renderer; }
});
window.epMailFeatures = Object.freeze({ ...(window.epMailFeatures || {}), mail: mailUiApi });
window.epMailFeatures = window.epMailFeatures || {};
window.epMailFeatures.mail = mailUiApi;
+14 -14
View File
@@ -1,4 +1,8 @@
// Reading and sending OpenPGP mail (desktop). Inert where there is no key store.
const mailUi = window.epMailFeatures.mail;
const $ = mailUi.select;
const $$ = mailUi.selectAll;
const toast = mailUi.toast;
const pgpApi = () => window.daybreakMail?.pgp;
function pgpAskPassphrase(title, detail) {
@@ -95,7 +99,7 @@ async function pgpUnlockMessage(message, interactive) {
body: result.body, html: result.html, links: result.links, attachments: result.attachments, attachment: result.attachment, size: result.size,
snippet: 'Encrypted message', pgpDecrypted: true, pgpResult: { signatures: result.signatures, attachedKeys: result.attachedKeys }
});
if (activeMessage === message.id) openMessage(message.id);
if (mailUi.state.activeMessage === message.id) mailUi.openMessage(message.id);
return;
}
if (result.code === 'PASSPHRASE_REQUIRED' || result.code === 'BAD_PASSPHRASE') {
@@ -111,24 +115,19 @@ async function pgpUnlockMessage(message, interactive) {
}
}
if (typeof openMessage === 'function') {
const baseOpenMessage = openMessage;
openMessage = function (id) {
mailUi.wrapOpenMessage(baseOpenMessage => function (id) {
baseOpenMessage(id);
const message = messages.find(item => item.id === id);
const message = mailUi.state.messages.find(item => item.id === id);
if (!message || !pgpApi()) return;
pgpRenderReaderState(message);
if (message.encrypted && !message.pgpDecrypted && !message.pgpAutoTried) {
message.pgpAutoTried = true;
void pgpUnlockMessage(message, false);
}
};
}
});
// Compose: encrypt/sign options.
if (typeof showCompose === 'function') {
const baseShowCompose = showCompose;
showCompose = function (reply, forward = false) {
mailUi.wrapShowCompose(baseShowCompose => function (reply, forward = false) {
baseShowCompose(reply, forward);
const footer = $('#modal-root .compose-footer');
if (!footer || !pgpApi()) return;
@@ -163,11 +162,10 @@ if (typeof showCompose === 'function') {
['#compose-to', '#compose-cc'].forEach(selector => $(selector)?.addEventListener('input', schedule));
sign.addEventListener('change', schedule);
schedule();
};
}
});
// Called by the send handler. Returns null (plain mail), false (cancelled) or send options.
window.pgpPrepareSend = async function pgpPrepareSend(to, cc) {
async function prepareSend(to, cc) {
if (!pgpApi()) return null;
let encrypt = Boolean($('#pgp-encrypt')?.checked);
const sign = Boolean($('#pgp-sign')?.checked);
@@ -195,4 +193,6 @@ window.pgpPrepareSend = async function pgpPrepareSend(to, cc) {
}
}
return { encrypt, sign, passphrase };
};
}
window.epMailFeatures.pgp = Object.freeze({ prepareSend });
+16 -2
View File
@@ -8,9 +8,16 @@
updateAccountCard,
getRenderList,
getOpenMessage,
getShowCompose,
baseRenderList,
emptyReader,
openMailAccount
openMailAccount,
select,
selectAll,
toast,
setRenderList,
setOpenMessage,
setShowCompose
}) {
return Object.freeze({
state,
@@ -21,9 +28,16 @@
updateAccountCard,
get renderList() { return getRenderList(); },
get openMessage() { return getOpenMessage(); },
get showCompose() { return getShowCompose(); },
baseRenderList,
emptyReader,
openMailAccount
openMailAccount,
select,
selectAll,
toast,
wrapRenderList(decorator) { setRenderList(decorator(getRenderList())); },
wrapOpenMessage(decorator) { setOpenMessage(decorator(getOpenMessage())); },
wrapShowCompose(decorator) { setShowCompose(decorator(getShowCompose())); }
});
}
+21 -23
View File
@@ -16,10 +16,10 @@ function nextMorning(daysAhead) {
function openSnoozeDialog(items) {
const eligible = items.filter(message => message?.uid && mailUi.connectedAccount);
if (!eligible.length) {
toast('Snoozing is available for messages in a connected mailbox.');
mailUi.toast('Snoozing is available for messages in a connected mailbox.');
return;
}
const root = $('#modal-root');
const root = mailUi.select('#modal-root');
const tomorrow = nextMorning(1);
const laterToday = new Date(Date.now() + 3 * 60 * 60 * 1000);
root.innerHTML = `<div class="modal-overlay"><div class="compose-modal"><div class="modal-head"><span>${eligible.length === 1 ? 'Snooze message' : `Snooze ${eligible.length} messages`}</span><button class="modal-close" aria-label="Close">×</button></div><div class="compose-fields"><p class="account-note">The message will be hidden in ep-mail until then; the server copy remains in place.</p><div class="snooze-options" style="display:grid;gap:8px;padding:10px 0"><button class="outline-button" data-snooze-at="${laterToday.getTime()}">In 3 hours · ${laterToday.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' })}</button><button class="outline-button" data-snooze-at="${tomorrow.getTime()}">Tomorrow · 9:00 AM</button><button class="outline-button" data-snooze-at="${nextMorning(7).getTime()}">Next week · 9:00 AM</button></div><label class="compose-line"><span>Custom reminder</span><input id="snooze-custom-time" type="datetime-local" value="${localDateTimeValue(tomorrow)}"></label><p id="snooze-status" class="account-status" aria-live="polite"></p></div><div class="compose-footer"><button class="discard modal-close">Cancel</button><button class="send-button" id="snooze-custom">Set reminder</button></div></div></div>`;
@@ -31,7 +31,7 @@ function openSnoozeDialog(items) {
const schedule = async dueAt => {
if (!Number.isFinite(dueAt) || dueAt <= Date.now()) {
$('#snooze-status').textContent = 'Choose a reminder time in the future.';
mailUi.select('#snooze-status').textContent = 'Choose a reminder time in the future.';
return;
}
const buttons = [...root.querySelectorAll('button')];
@@ -49,11 +49,11 @@ function openSnoozeDialog(items) {
});
}
close();
toast(eligible.length === 1 ? 'Message snoozed' : `${eligible.length} messages snoozed`);
mailUi.toast(eligible.length === 1 ? 'Message snoozed' : `${eligible.length} messages snoozed`);
mailUi.state.selectedIds.clear();
await mailUi.syncMailbox(false, { offset: 0 });
} catch (error) {
$('#snooze-status').textContent = error.message || 'Could not set the reminder.';
mailUi.select('#snooze-status').textContent = error.message || 'Could not set the reminder.';
buttons.forEach(button => { button.disabled = false; });
}
};
@@ -61,8 +61,8 @@ function openSnoozeDialog(items) {
root.querySelectorAll('[data-snooze-at]').forEach(button => {
button.onclick = () => schedule(Number(button.dataset.snoozeAt));
});
$('#snooze-custom').onclick = () => schedule(new Date($('#snooze-custom-time').value).getTime());
$('#snooze-custom-time').focus();
mailUi.select('#snooze-custom').onclick = () => schedule(new Date(mailUi.select('#snooze-custom-time').value).getTime());
mailUi.select('#snooze-custom-time').focus();
}
function unsnoozeMessage(message) {
@@ -72,13 +72,13 @@ function unsnoozeMessage(message) {
sourceFolder,
dueAt: null
}).then(async () => {
toast('Message returned to its mailbox');
mailUi.toast('Message returned to its mailbox');
await mailUi.syncMailbox(false, { offset: 0 });
}).catch(error => toast(error.message || 'Could not return the message'));
}).catch(error => mailUi.toast(error.message || 'Could not return the message'));
}
function showMessageActions(message) {
const root = $('#modal-root');
const root = mailUi.select('#modal-root');
root.innerHTML = `<div class="modal-overlay"><div class="compose-modal"><div class="modal-head"><span>Message actions</span><button class="modal-close" aria-label="Close">×</button></div><div class="compose-fields snooze-options" style="display:grid;gap:8px;padding-bottom:16px"><button class="outline-button" data-message-action="unread">Mark unread</button><button class="outline-button" data-message-action="snooze">Snooze</button><button class="outline-button" data-message-action="label">Add label</button><button class="outline-button" data-message-action="inbox">Move to Inbox</button></div></div></div>`;
root.querySelectorAll('.modal-close').forEach(button => button.onclick = () => { root.innerHTML = ''; });
root.querySelector('.modal-overlay').onclick = event => {
@@ -86,8 +86,7 @@ function showMessageActions(message) {
};
}
const renderListBeforeSnooze = mailUi.renderList;
renderList = function () {
mailUi.wrapRenderList(renderListBeforeSnooze => function () {
if (mailUi.state.activeFolder === 'Snoozed') {
const originalMessages = mailUi.state.messages.slice();
const snoozedMessages = mailUi.state.messages.filter(message => message.snoozedUntil);
@@ -103,7 +102,7 @@ renderList = function () {
message.deleted = deleted;
});
mailUi.state.messages.splice(0, mailUi.state.messages.length, ...originalMessages);
for (const row of $$('.message')) {
for (const row of mailUi.selectAll('.message')) {
const message = mailUi.state.messages.find(item => item.id === Number(row.dataset.id));
if (!message?.snoozedUntil) continue;
const due = new Date(message.snoozedUntil);
@@ -113,21 +112,20 @@ renderList = function () {
}
return;
}
renderListBeforeSnooze();
};
return renderListBeforeSnooze();
});
const openMessageBeforeSnooze = mailUi.openMessage;
openMessage = function (id) {
mailUi.wrapOpenMessage(openMessageBeforeSnooze => function (id) {
openMessageBeforeSnooze(id);
const message = mailUi.state.messages.find(item => item.id === id);
const tools = $('.reader-tools');
const tools = mailUi.select('.reader-tools');
if (!message || !tools) return;
const snoozed = mailUi.state.activeFolder === 'Snoozed';
tools.insertAdjacentHTML('afterbegin', `<button class="icon-button" id="${snoozed ? 'reader-unsnooze' : 'reader-snooze'}" title="${snoozed ? 'Return to inbox' : 'Snooze'}">${snoozed ? '↩' : '◷'}</button>`);
};
});
document.addEventListener('DOMContentLoaded', () => {
$('#mark-unread').insertAdjacentHTML('afterend', '<button class="icon-button" id="snooze-selected" title="Snooze selected messages">◷</button>');
mailUi.select('#mark-unread').insertAdjacentHTML('afterend', '<button class="icon-button" id="snooze-selected" title="Snooze selected messages">◷</button>');
if (window.daybreakMail?.checkSnoozeReminders) {
const checkReminders = () => window.daybreakMail.checkSnoozeReminders()
.catch(error => console.error('Could not check mail reminders:', error));
@@ -146,7 +144,7 @@ document.addEventListener('click', event => {
event.stopImmediatePropagation();
if (action) {
const message = mailUi.state.messages.find(item => item.id === mailUi.state.activeMessage);
$('#modal-root').innerHTML = '';
mailUi.select('#modal-root').innerHTML = '';
if (!message) return;
if (action.dataset.messageAction === 'snooze') openSnoozeDialog([message]);
else if (action.dataset.messageAction === 'label') addMessageLabel(message);
@@ -156,8 +154,8 @@ document.addEventListener('click', event => {
if (type === 'unread') message.unread = true;
if (type === 'inbox') message.archived = false;
mailUi.renderList();
toast(type === 'unread' ? 'Marked unread' : 'Moved to Inbox');
}).catch(error => toast(error.message || 'Could not update the message'));
mailUi.toast(type === 'unread' ? 'Marked unread' : 'Moved to Inbox');
}).catch(error => mailUi.toast(error.message || 'Could not update the message'));
}
return;
}