@@ -0,0 +1,161 @@
// Attachment previews and a dedicated view for finding and managing attachments across mailboxes.
const attachmentFolders = [ 'Inbox' , 'Sent' , 'Archive' , 'Trash' ] ;
let attachmentItems = [ ] ;
function attachmentKind ( filename ) {
const ext = String ( filename ) . toLowerCase ( ) . split ( '.' ) . pop ( ) ;
if ( [ 'png' , 'jpg' , 'jpeg' , 'gif' , 'webp' , 'bmp' , 'svg' ] . includes ( ext ) ) return 'Images' ;
if ( [ 'pdf' , 'doc' , 'docx' , 'odt' , 'txt' , 'rtf' , 'md' , 'xls' , 'xlsx' , 'csv' , 'ppt' , 'pptx' ] . includes ( ext ) ) return 'Documents' ;
if ( [ 'zip' , 'rar' , '7z' , 'tar' , 'gz' ] . includes ( ext ) ) return 'Archives' ;
return 'Other' ;
}
function attachmentSize ( size ) {
if ( ! size ) return '' ;
if ( typeof size === 'string' ) return size ;
return size < 1048576 ? ` ${ Math . max ( 1 , Math . round ( size / 1024 ) ) } KB ` : ` ${ ( size / 1048576 ) . toFixed ( 1 ) } MB ` ;
}
async function previewAttachment ( target , details ) {
if ( ! details . uid || ! window . daybreakMail ? . previewAttachment ) {
target . textContent = 'Previews are available for connected mailbox messages.' ;
return ;
}
target . textContent = 'Loading preview…' ;
try {
const result = await window . daybreakMail . previewAttachment ( details ) ;
target . replaceChildren ( ) ;
if ( result . kind === 'image' ) {
const image = document . createElement ( 'img' ) ;
image . className = 'attachment-preview-image' ;
image . alt = details . filename ;
image . src = result . dataUrl ;
target . append ( image ) ;
} else if ( result . kind === 'text' ) {
const pre = document . createElement ( 'pre' ) ;
pre . className = 'attachment-preview-text' ;
pre . textContent = result . text + ( result . truncated ? '\n… (preview truncated)' : '' ) ;
target . append ( pre ) ;
} else {
target . textContent = result . reason || 'No preview is available for this file type.' ;
}
} catch ( error ) {
target . textContent = error . message || 'Could not load the preview.' ;
}
}
function downloadAttachment ( details ) {
if ( ! details . uid || ! window . daybreakMail ? . saveAttachment ) { toast ( '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' ) ) ;
}
// Add a Preview button to each attachment in the reader.
if ( typeof openMessage === 'function' ) {
const baseOpenMessage = openMessage ;
openMessage = function ( id ) {
baseOpenMessage ( id ) ;
const message = messages . find ( item => item . id === id ) ;
if ( ! message ? . uid ) return ;
$$ ( '#reader-pane .attachment' ) . forEach ( row => {
const download = row . querySelector ( '.download' ) ;
if ( ! download || row . querySelector ( '.preview-attachment' ) ) return ;
const button = document . createElement ( 'button' ) ;
button . className = 'preview-attachment icon-button' ;
button . title = 'Preview' ;
button . setAttribute ( 'aria-label' , ` Preview ${ download . dataset . filename } ` ) ;
button . textContent = '👁' ;
button . addEventListener ( 'click' , event => {
event . stopPropagation ( ) ;
let panel = row . nextElementSibling ? . classList . contains ( 'attachment-preview' ) ? row . nextElementSibling : null ;
if ( panel ) { panel . remove ( ) ; return ; }
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 } ) ;
} ) ;
download . before ( button ) ;
} ) ;
} ;
}
async function collectAttachments ( ) {
const items = [ ] ;
for ( const folder of attachmentFolders ) {
let offset = 0 ;
for ( let page = 0 ; page < 5 ; page += 1 ) {
const result = await window . daybreakMail . syncFolder ( folder , offset , { hasAttachments : true } ) ;
for ( const message of result . messages || [ ] ) {
for ( const file of message . attachments || [ ] ) {
items . push ( {
uid : message . uid , folder , filename : file . filename , size : file . size ,
subject : message . subject , sender : message . sender , date : message . date , timestamp : Number ( message . timestamp || 0 ) ,
kind : attachmentKind ( file . filename )
} ) ;
}
}
offset += ( result . messages || [ ] ) . length ;
if ( ! result . messages ? . length || offset >= ( result . total || 0 ) ) break ;
}
}
return items . sort ( ( a , b ) => b . timestamp - a . timestamp ) ;
}
async function openAttachmentsView ( ) {
if ( ! connectedMailAccount || ! window . daybreakMail ) { 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 = '' ; } ;
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 render = ( ) => {
const text = $ ( '#attachment-search' ) . value . trim ( ) . toLowerCase ( ) ;
const type = $ ( '#attachment-type' ) . value ;
const shown = attachmentItems . filter ( item => ( ! type || item . kind === type ) &&
( ! text || ` ${ item . filename } ${ item . subject } ${ item . sender } ` . toLowerCase ( ) . includes ( text ) ) ) ;
list . replaceChildren ( ) ;
if ( ! shown . length ) { list . textContent = attachmentItems . length ? 'No attachments match.' : 'No attachments found.' ; return ; }
shown . forEach ( item => {
const row = document . createElement ( 'div' ) ;
row . className = 'attachment-row' ;
const info = document . createElement ( 'div' ) ;
info . className = 'attachment-row-info' ;
const name = document . createElement ( 'strong' ) ;
name . textContent = item . filename ;
const meta = document . createElement ( 'small' ) ;
meta . textContent = [ attachmentSize ( item . size ) , item . sender , item . subject , item . folder , item . date ] . filter ( Boolean ) . join ( ' · ' ) ;
info . append ( name , meta ) ;
const preview = document . createElement ( 'button' ) ;
preview . className = 'discard' ;
preview . textContent = 'Preview' ;
const save = document . createElement ( 'button' ) ;
save . className = 'discard' ;
save . textContent = 'Download' ;
const panel = document . createElement ( 'div' ) ;
panel . className = 'attachment-preview' ;
panel . hidden = true ;
const details = { uid : item . uid , folder : item . folder , filename : item . filename } ;
preview . onclick = ( ) => {
panel . hidden = ! panel . hidden ;
if ( ! panel . hidden && ! panel . childElementCount && ! panel . textContent ) void previewAttachment ( panel , details ) ;
} ;
save . onclick = ( ) => downloadAttachment ( details ) ;
row . append ( info , preview , save , panel ) ;
list . append ( row ) ;
} ) ;
} ;
$ ( '#attachment-search' ) . oninput = render ;
$ ( '#attachment-type' ) . onchange = render ;
try {
attachmentItems = await collectAttachments ( ) ;
if ( $ ( '#attachment-list' ) ) render ( ) ;
} catch ( error ) {
if ( $ ( '#attachment-list' ) ) $ ( '#attachment-list' ) . textContent = error . message || 'Could not load attachments.' ;
}
}
document . addEventListener ( 'DOMContentLoaded' , ( ) => {
$ ( '#attachments-nav' ) ? . addEventListener ( 'click' , openAttachmentsView ) ;
} ) ;