Refractoring Stage 6.6 Migrated productivity.js and shortcuts.js mail-state access and render/preferences hooks to the mail feature API.
This commit is contained in:
+21
-21
@@ -1,8 +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 attachmentMailUi = window.epMailFeatures.mail;
|
||||
const selectAttachmentUi = attachmentMailUi.select;
|
||||
const selectAllAttachmentUi = attachmentMailUi.selectAll;
|
||||
const toastAttachmentUi = attachmentMailUi.toast;
|
||||
const attachmentFolders = ['Inbox', 'Sent', 'Archive', 'Trash'];
|
||||
let attachmentItems = [];
|
||||
|
||||
@@ -49,18 +49,18 @@ async function previewAttachment(target, details) {
|
||||
}
|
||||
|
||||
function downloadAttachment(details) {
|
||||
if (!details.uid || !window.daybreakMail?.saveAttachment) { toast('Attachment download is available for connected mailbox messages'); return; }
|
||||
if (!details.uid || !window.daybreakMail?.saveAttachment) { toastAttachmentUi('Attachment download is available for connected mailbox messages'); return; }
|
||||
window.daybreakMail.saveAttachment(details)
|
||||
.then(result => { if (result.saved) toast('Attachment saved'); })
|
||||
.catch(error => toast(error.message || 'Could not save attachment'));
|
||||
.then(result => { if (result.saved) toastAttachmentUi('Attachment saved'); })
|
||||
.catch(error => toastAttachmentUi(error.message || 'Could not save attachment'));
|
||||
}
|
||||
|
||||
// Add a Preview button to each attachment in the reader.
|
||||
mailUi.wrapOpenMessage(baseOpenMessage => function (id) {
|
||||
attachmentMailUi.wrapOpenMessage(baseOpenMessage => function (id) {
|
||||
baseOpenMessage(id);
|
||||
const message = mailUi.state.messages.find(item => item.id === id);
|
||||
const message = attachmentMailUi.state.messages.find(item => item.id === id);
|
||||
if (!message?.uid) return;
|
||||
$$('#reader-pane .attachment').forEach(row => {
|
||||
selectAllAttachmentUi('#reader-pane .attachment').forEach(row => {
|
||||
const download = row.querySelector('.download');
|
||||
if (!download || row.querySelector('.preview-attachment')) return;
|
||||
const button = document.createElement('button');
|
||||
@@ -75,7 +75,7 @@ mailUi.wrapOpenMessage(baseOpenMessage => function (id) {
|
||||
panel = document.createElement('div');
|
||||
panel.className = 'attachment-preview';
|
||||
row.after(panel);
|
||||
void previewAttachment(panel, { uid: message.uid, folder: message.sourceFolder || mailUi.state.activeFolder, filename: download.dataset.filename });
|
||||
void previewAttachment(panel, { uid: message.uid, folder: message.sourceFolder || attachmentMailUi.state.activeFolder, filename: download.dataset.filename });
|
||||
});
|
||||
download.before(button);
|
||||
});
|
||||
@@ -104,16 +104,16 @@ async function collectAttachments() {
|
||||
}
|
||||
|
||||
async function openAttachmentsView() {
|
||||
if (!mailUi.connectedAccount || !window.daybreakMail) { mailUi.openMailAccount(); return; }
|
||||
const root = $('#modal-root');
|
||||
if (!attachmentMailUi.connectedAccount || !window.daybreakMail) { attachmentMailUi.openMailAccount(); return; }
|
||||
const root = selectAttachmentUi('#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 = ''; };
|
||||
root.querySelector('.modal-close').onclick = close;
|
||||
root.querySelector('.modal-overlay').onclick = event => { if (event.target.classList.contains('modal-overlay')) close(); };
|
||||
const list = $('#attachment-list');
|
||||
const list = selectAttachmentUi('#attachment-list');
|
||||
const render = () => {
|
||||
const text = $('#attachment-search').value.trim().toLowerCase();
|
||||
const type = $('#attachment-type').value;
|
||||
const text = selectAttachmentUi('#attachment-search').value.trim().toLowerCase();
|
||||
const type = selectAttachmentUi('#attachment-type').value;
|
||||
const shown = attachmentItems.filter(item => (!type || item.kind === type) &&
|
||||
(!text || `${item.filename} ${item.subject} ${item.sender}`.toLowerCase().includes(text)));
|
||||
list.replaceChildren();
|
||||
@@ -147,16 +147,16 @@ async function openAttachmentsView() {
|
||||
list.append(row);
|
||||
});
|
||||
};
|
||||
$('#attachment-search').oninput = render;
|
||||
$('#attachment-type').onchange = render;
|
||||
selectAttachmentUi('#attachment-search').oninput = render;
|
||||
selectAttachmentUi('#attachment-type').onchange = render;
|
||||
try {
|
||||
attachmentItems = await collectAttachments();
|
||||
if ($('#attachment-list')) render();
|
||||
if (selectAttachmentUi('#attachment-list')) render();
|
||||
} catch (error) {
|
||||
if ($('#attachment-list')) $('#attachment-list').textContent = error.message || 'Could not load attachments.';
|
||||
if (selectAttachmentUi('#attachment-list')) selectAttachmentUi('#attachment-list').textContent = error.message || 'Could not load attachments.';
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
$('#attachments-nav')?.addEventListener('click', openAttachmentsView);
|
||||
selectAttachmentUi('#attachments-nav')?.addEventListener('click', openAttachmentsView);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user