Ver V0.1.12
This commit is contained in:
+37
-19
@@ -16,45 +16,62 @@ function updateAccountCard(account) {
|
||||
card.querySelector('.online-dot').style.background = '#70ad7b';
|
||||
}
|
||||
|
||||
function openMailAccount() {
|
||||
function openMailAccount(existingAccount = null) {
|
||||
const root = $('#modal-root');
|
||||
const current = {};
|
||||
root.innerHTML = `<div class="modal-overlay"><div class="compose-modal"><div class="modal-head"><span>Add a mailbox</span><button class="modal-close" aria-label="Close">×</button></div><div class="compose-fields"><p class="account-note">Your password is encrypted with this computer’s credential storage. Connections require TLS.</p><label class="compose-line"><span>Email</span><input id="mail-account-email" type="email" autocomplete="username" value="" placeholder="name@example.com"></label><label class="compose-line"><span>Password</span><input id="mail-account-password" type="password" autocomplete="current-password" placeholder="Enter mailbox password"></label><label class="compose-line"><span>IMAP host</span><input id="mail-account-imap" value="" autocomplete="off" placeholder="imap.example.com"></label><label class="compose-line"><span>SMTP host</span><input id="mail-account-smtp" value="" autocomplete="off" placeholder="smtp.example.com"></label><p class="account-note">IMAP uses STARTTLS on port 143 · SMTP uses STARTTLS on port 587</p><p id="mail-account-status" class="account-status" aria-live="polite"></p></div><div class="compose-footer"><button class="discard" id="disconnect-mail">Cancel</button><button class="send-button" id="connect-mail">Test & connect</button></div></div></div>`;
|
||||
const editing = Boolean(existingAccount);
|
||||
const displayName = profileSettings.name || '';
|
||||
root.innerHTML = `<div class="modal-overlay"><div class="compose-modal"><div class="modal-head"><span>${editing ? 'Edit mailbox' : 'Add a mailbox'}</span><button class="modal-close" aria-label="Close">×</button></div><div class="compose-fields"><p class="account-note">Your password is encrypted with this computer's credential storage. Connections require TLS.</p><label class="compose-line"><span>Email</span><input id="mail-account-email" type="email" autocomplete="username" value="${esc(existingAccount?.email || '')}" placeholder="name@example.com"></label><label class="compose-line"><span>Display name</span><input id="mail-account-display-name" maxlength="80" autocomplete="name" value="${esc(displayName)}" placeholder="Your name"></label><label class="compose-line"><span>Password</span><input id="mail-account-password" type="password" autocomplete="${editing ? 'new-password' : 'current-password'}" placeholder="${editing ? 'Leave blank to keep current password' : 'Enter mailbox password'}"></label><label class="compose-line"><span>IMAP host</span><input id="mail-account-imap" value="${esc(existingAccount?.imapHost || '')}" autocomplete="off" placeholder="imap.example.com"></label><label class="compose-line"><span>SMTP host</span><input id="mail-account-smtp" value="${esc(existingAccount?.smtpHost || '')}" autocomplete="off" placeholder="smtp.example.com"></label><p class="account-note">IMAP uses STARTTLS on port 143 · SMTP uses STARTTLS on port 587.${editing ? ' Leave the password empty to keep the saved password.' : ' Your display name is also used as the sender name.'}</p><p id="mail-account-status" class="account-status" aria-live="polite"></p></div><div class="compose-footer"><button class="discard" id="disconnect-mail">Cancel</button><button class="send-button" id="connect-mail">${editing ? 'Save & reconnect' : 'Test & connect'}</button></div></div></div>`;
|
||||
const close = () => { root.innerHTML = ''; };
|
||||
$$('.modal-close').forEach(button => button.onclick = close);
|
||||
$('.modal-overlay').addEventListener('click', event => { if (event.target.classList.contains('modal-overlay')) close(); });
|
||||
$('#disconnect-mail').onclick = async () => {
|
||||
close();
|
||||
};
|
||||
$('#disconnect-mail').onclick = close;
|
||||
$('#connect-mail').onclick = async () => {
|
||||
const button = $('#connect-mail');
|
||||
const status = $('#mail-account-status');
|
||||
const password = $('#mail-account-password').value;
|
||||
if (!password) { status.textContent = 'Enter the mailbox password to connect.'; $('#mail-account-password').focus(); return; }
|
||||
const name = $('#mail-account-display-name').value.trim();
|
||||
if (!editing && !password) { status.textContent = 'Enter the mailbox password to connect.'; $('#mail-account-password').focus(); return; }
|
||||
button.disabled = true;
|
||||
button.textContent = 'Checking secure connections…';
|
||||
status.textContent = 'Checking IMAP and SMTP sign-in. This may take a moment.';
|
||||
button.textContent = editing ? 'Checking updated settings…' : 'Checking secure connections…';
|
||||
status.textContent = editing ? 'Testing the updated mailbox settings before saving.' : 'Checking IMAP and SMTP sign-in. This may take a moment.';
|
||||
try {
|
||||
const account = await window.daybreakMail.connect({
|
||||
const details = {
|
||||
email: $('#mail-account-email').value,
|
||||
password,
|
||||
imapHost: $('#mail-account-imap').value,
|
||||
smtpHost: $('#mail-account-smtp').value
|
||||
});
|
||||
connectedMailAccount = account;
|
||||
updateAccountCard(account);
|
||||
close();
|
||||
toast('Mailbox connected securely');
|
||||
await syncMailbox(true);
|
||||
};
|
||||
const account = editing
|
||||
? await window.daybreakMail.updateAccount(existingAccount.id, details)
|
||||
: await window.daybreakMail.connect(details);
|
||||
if (name) setProfileName(name);
|
||||
if (editing) {
|
||||
const accountList = await window.daybreakMail.listAccounts();
|
||||
if (accountList.activeId === account.id) {
|
||||
connectedMailAccount = account;
|
||||
updateAccountCard(account);
|
||||
mailPageOffset = 0;
|
||||
close();
|
||||
await syncMailbox(false, { offset: 0 });
|
||||
} else {
|
||||
close();
|
||||
}
|
||||
toast('Mailbox settings updated');
|
||||
} else {
|
||||
connectedMailAccount = account;
|
||||
updateAccountCard(account);
|
||||
close();
|
||||
toast('Mailbox connected securely');
|
||||
await syncMailbox(true);
|
||||
}
|
||||
} catch (error) {
|
||||
status.textContent = error.message || 'Could not connect. Check the account details and try again.';
|
||||
button.disabled = false;
|
||||
button.textContent = 'Test & connect';
|
||||
button.textContent = editing ? 'Save & reconnect' : 'Test & connect';
|
||||
}
|
||||
};
|
||||
$('#mail-account-email').focus();
|
||||
}
|
||||
|
||||
async function syncMailbox(showToast, options = {}) {
|
||||
if (!connectedMailAccount || !window.daybreakMail) {
|
||||
openMailAccount();
|
||||
@@ -138,7 +155,8 @@ if (window.daybreakMail) {
|
||||
to: recipient,
|
||||
cc: $('#compose-cc').value,
|
||||
subject: $('#compose-subject').value.trim(),
|
||||
text: $('#compose-body').value
|
||||
text: $('#compose-body').value,
|
||||
displayName: profileSettings.name
|
||||
});
|
||||
$('#modal-root').innerHTML = '';
|
||||
toast(result.savedToSent ? 'Message sent and saved in Sent' : 'Message sent, but its Sent copy could not be saved');
|
||||
|
||||
Reference in New Issue
Block a user