Cleanup ComposerPopup code

This commit is contained in:
the-djmaze 2022-09-27 09:45:35 +02:00
parent 03dabc1537
commit ba8188978d
3 changed files with 59 additions and 102 deletions

View file

@ -95,13 +95,12 @@ const
* @param {Object} unic * @param {Object} unic
* @param {Map} localEmails * @param {Map} localEmails
*/ */
replyHelper = (emails, unic, localEmails) => { replyHelper = (emails, unic, localEmails) =>
emails.forEach(email => { emails.forEach(email => {
if (!unic[email.email] && !localEmails.has(email.email)) { if (!unic[email.email] && !localEmails.has(email.email)) {
localEmails.set(email.email, email); localEmails.set(email.email, email);
} }
}); });
};
doc.body.append(hcont); doc.body.append(hcont);
@ -441,9 +440,8 @@ export class MessageModel extends AbstractModel {
unic = excludeEmails || {}; unic = excludeEmails || {};
replyHelper(this.replyTo, unic, toResult); replyHelper(this.replyTo, unic, toResult);
if (!toResult.size) { toResult.size || replyHelper(this.from, unic, toResult);
replyHelper(this.from, unic, toResult);
}
replyHelper(this.to, unic, toResult); replyHelper(this.to, unic, toResult);
replyHelper(this.cc, unic, ccResult); replyHelper(this.cc, unic, ccResult);

View file

@ -86,30 +86,9 @@ const
} }
}, },
findIdentityByMessage = (composeType, message) => { findIdentity = addresses => {
let resultIdentity = null; addresses = addresses.map(item => item.email);
const find = addresses => { return IdentityUserStore.find(item => addresses.includes(item.email()));
addresses = addresses.map(item => item.email);
return IdentityUserStore.find(item => addresses.includes(item.email()));
};
if (message) {
switch (composeType) {
case ComposeType.Reply:
case ComposeType.ReplyAll:
case ComposeType.Forward:
case ComposeType.ForwardAsAttachment:
resultIdentity = find(message.to.concat(message.cc, message.bcc))/* || find(message.deliveredTo)*/;
break;
case ComposeType.Draft:
resultIdentity = find(message.from.concat(message.replyTo));
break;
// no default
// case ComposeType.Empty:
}
}
return resultIdentity || IdentityUserStore()[0] || null;
}, },
/** /**
@ -664,7 +643,7 @@ export class ComposePopupView extends AbstractViewPopup {
identity = identity?.item; identity = identity?.item;
if (identity) { if (identity) {
this.currentIdentity(identity); this.currentIdentity(identity);
this.setSignatureFromIdentity(identity); this.setSignature(identity);
} }
} }
@ -703,8 +682,8 @@ export class ComposePopupView extends AbstractViewPopup {
} }
} }
setSignatureFromIdentity(identity) { setSignature(identity, msgComposeType) {
if (identity) { if (identity && ComposeType.Draft !== msgComposeType && ComposeType.EditAsNew !== msgComposeType) {
this.editor(editor => { this.editor(editor => {
let signature = identity.signature() || '', let signature = identity.signature() || '',
isHtml = ':HTML:' === signature.slice(0, 6), isHtml = ':HTML:' === signature.slice(0, 6),
@ -798,7 +777,7 @@ export class ComposePopupView extends AbstractViewPopup {
// excludeEmail = new Set(), // excludeEmail = new Set(),
excludeEmail = {}, excludeEmail = {},
mEmail = AccountUserStore.email(), mEmail = AccountUserStore.email(),
lineComposeType = sType || ComposeType.Empty; msgComposeType = sType || ComposeType.Empty;
oLastMessage = message; oLastMessage = message;
@ -809,7 +788,23 @@ export class ComposePopupView extends AbstractViewPopup {
this.reset(); this.reset();
identity = findIdentityByMessage(lineComposeType, message); if (message) {
switch (msgComposeType) {
case ComposeType.Reply:
case ComposeType.ReplyAll:
case ComposeType.Forward:
case ComposeType.ForwardAsAttachment:
identity = findIdentity(message.to.concat(message.cc, message.bcc))
/* || findIdentity(message.deliveredTo)*/;
break;
case ComposeType.Draft:
identity = findIdentity(message.from.concat(message.replyTo));
break;
// no default
// case ComposeType.Empty:
}
}
identity = identity || IdentityUserStore()[0];
if (identity) { if (identity) {
// excludeEmail.add(identity.email()); // excludeEmail.add(identity.email());
excludeEmail[identity.email()] = true; excludeEmail[identity.email()] = true;
@ -827,47 +822,32 @@ export class ComposePopupView extends AbstractViewPopup {
this.bcc(emailArrayToStringLineHelper(aBccEmails)); this.bcc(emailArrayToStringLineHelper(aBccEmails));
} }
if (lineComposeType && message) { if (msgComposeType && message) {
sDate = timestampToString(message.dateTimeStampInUTC(), 'FULL'); sDate = timestampToString(message.dateTimeStampInUTC(), 'FULL');
sSubject = message.subject(); sSubject = message.subject();
aDraftInfo = message.draftInfo; aDraftInfo = message.draftInfo;
switch (lineComposeType) { switch (msgComposeType) {
case ComposeType.Empty:
break;
case ComposeType.Reply: case ComposeType.Reply:
this.to(emailArrayToStringLineHelper(message.replyEmails(excludeEmail))); case ComposeType.ReplyAll:
if (ComposeType.Reply === msgComposeType) {
this.to(emailArrayToStringLineHelper(message.replyEmails(excludeEmail)));
} else {
let parts = message.replyAllEmails(excludeEmail);
this.to(emailArrayToStringLineHelper(parts[0]));
this.cc(emailArrayToStringLineHelper(parts[1]));
}
this.subject(replySubjectAdd('Re', sSubject)); this.subject(replySubjectAdd('Re', sSubject));
this.prepareMessageAttachments(message, lineComposeType); this.prepareMessageAttachments(message, msgComposeType);
this.aDraftInfo = ['reply', message.uid, message.folder]; this.aDraftInfo = ['reply', message.uid, message.folder];
this.sInReplyTo = message.messageId; this.sInReplyTo = message.messageId;
this.sReferences = (message.messageId + ' ' + message.references).trim(); this.sReferences = (message.messageId + ' ' + message.references).trim();
break; break;
case ComposeType.ReplyAll: {
let parts = message.replyAllEmails(excludeEmail);
this.to(emailArrayToStringLineHelper(parts[0]));
this.cc(emailArrayToStringLineHelper(parts[1]));
this.subject(replySubjectAdd('Re', sSubject));
this.prepareMessageAttachments(message, lineComposeType);
this.aDraftInfo = ['reply', message.uid, message.folder];
this.sInReplyTo = message.messageId;
this.sReferences = (message.messageId + ' ' + message.references).trim();
break;
}
case ComposeType.Forward: case ComposeType.Forward:
this.subject(replySubjectAdd('Fwd', sSubject));
this.prepareMessageAttachments(message, lineComposeType);
this.aDraftInfo = ['forward', message.uid, message.folder];
this.sInReplyTo = message.messageId;
this.sReferences = (message.messageId + ' ' + message.references).trim();
break;
case ComposeType.ForwardAsAttachment: case ComposeType.ForwardAsAttachment:
this.subject(replySubjectAdd('Fwd', sSubject)); this.subject(replySubjectAdd('Fwd', sSubject));
this.prepareMessageAttachments(message, lineComposeType); this.prepareMessageAttachments(message, msgComposeType);
this.aDraftInfo = ['forward', message.uid, message.folder]; this.aDraftInfo = ['forward', message.uid, message.folder];
this.sInReplyTo = message.messageId; this.sInReplyTo = message.messageId;
this.sReferences = (message.messageId + ' ' + message.references).trim(); this.sReferences = (message.messageId + ' ' + message.references).trim();
@ -885,7 +865,7 @@ export class ComposePopupView extends AbstractViewPopup {
this.draftUid(message.uid); this.draftUid(message.uid);
this.subject(sSubject); this.subject(sSubject);
this.prepareMessageAttachments(message, lineComposeType); this.prepareMessageAttachments(message, msgComposeType);
this.aDraftInfo = 3 === arrayLength(aDraftInfo) ? aDraftInfo : null; this.aDraftInfo = 3 === arrayLength(aDraftInfo) ? aDraftInfo : null;
this.sInReplyTo = message.inReplyTo; this.sInReplyTo = message.inReplyTo;
@ -899,12 +879,15 @@ export class ComposePopupView extends AbstractViewPopup {
this.replyTo(emailArrayToStringLineHelper(message.replyTo)); this.replyTo(emailArrayToStringLineHelper(message.replyTo));
this.subject(sSubject); this.subject(sSubject);
this.prepareMessageAttachments(message, lineComposeType); this.prepareMessageAttachments(message, msgComposeType);
this.aDraftInfo = 3 === arrayLength(aDraftInfo) ? aDraftInfo : null; this.aDraftInfo = 3 === arrayLength(aDraftInfo) ? aDraftInfo : null;
this.sInReplyTo = message.inReplyTo; this.sInReplyTo = message.inReplyTo;
this.sReferences = message.references; this.sReferences = message.references;
break; break;
// case ComposeType.Empty:
// break;
// no default // no default
} }
@ -917,7 +900,7 @@ export class ComposePopupView extends AbstractViewPopup {
); );
sText = tpl.innerHTML.trim(); sText = tpl.innerHTML.trim();
switch (lineComposeType) { switch (msgComposeType) {
case ComposeType.Reply: case ComposeType.Reply:
case ComposeType.ReplyAll: case ComposeType.ReplyAll:
sFrom = message.fromToLine(false, true); sFrom = message.fromToLine(false, true);
@ -962,51 +945,26 @@ export class ComposePopupView extends AbstractViewPopup {
this.editor(editor => { this.editor(editor => {
encrypted || editor.setHtml(sText); encrypted || editor.setHtml(sText);
if (encrypted || this.isPlainEditor() || !message.isHtml()) { if (encrypted || this.isPlainEditor() || !message.isHtml()) {
editor.modePlain(); editor.modePlain();
} }
encrypted && editor.setPlain(sText);
!encrypted || editor.setPlain(sText); this.setSignature(identity, msgComposeType);
if (identity && ComposeType.Draft !== lineComposeType && ComposeType.EditAsNew !== lineComposeType) {
this.setSignatureFromIdentity(identity);
}
this.setFocusInPopup(); this.setFocusInPopup();
}); });
} else if (ComposeType.Empty === lineComposeType) { } else if (ComposeType.Empty === msgComposeType) {
this.subject(null != sCustomSubject ? '' + sCustomSubject : ''); this.subject(null != sCustomSubject ? '' + sCustomSubject : '');
sText = null != sCustomPlainText ? '' + sCustomPlainText : '';
this.editor(editor => { this.editor(editor => {
editor.setHtml(sText); editor.setHtml(sCustomPlainText ? '' + sCustomPlainText : '');
isPlainEditor() && editor.modePlain();
if (isPlainEditor()) { this.setSignature(identity);
editor.modePlain();
}
if (identity) {
this.setSignatureFromIdentity(identity);
}
this.setFocusInPopup(); this.setFocusInPopup();
}); });
} else if (arrayLength(oMessageOrArray)) { } else if (arrayLength(oMessageOrArray)) {
oMessageOrArray.forEach(item => this.addMessageAsAttachment(item)); oMessageOrArray.forEach(item => this.addMessageAsAttachment(item));
this.editor(editor => { this.editor(editor => {
if (isPlainEditor()) { isPlainEditor() ? editor.setPlain('') : editor.setHtml('');
editor.setPlain('') this.setSignature(identity, msgComposeType);
} else {
editor.setHtml('');
}
if (identity && ComposeType.Draft !== lineComposeType && ComposeType.EditAsNew !== lineComposeType) {
this.setSignatureFromIdentity(identity);
}
this.setFocusInPopup(); this.setFocusInPopup();
}); });
} else { } else {
@ -1048,16 +1006,17 @@ export class ComposePopupView extends AbstractViewPopup {
); );
} }
if (identity) { this.currentIdentity(identity);
this.currentIdentity(identity);
}
} }
setFocusInPopup() { setFocusInPopup() {
setTimeout(() => { setTimeout(() => {
if (!this.to()) { if (!this.to()) {
this.to.focused(true); this.to.focused(true);
} else if (!this.to.focused()) { // TODO https://github.com/the-djmaze/snappymail/issues/501#issuecomment-1255906243
// } else if (!this.subject()) {
// this.subject.focus();
} else {
this.oEditor?.focus(); this.oEditor?.focus();
} }
}, 100); }, 100);

View file

@ -106,7 +106,7 @@
<tr class="reply-to-row" data-bind="visible: showReplyTo"> <tr class="reply-to-row" data-bind="visible: showReplyTo">
<td data-i18n="GLOBAL/REPLY_TO"></div> <td data-i18n="GLOBAL/REPLY_TO"></div>
<td> <td>
<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" data-bind="emailsTags: replyTo, autoCompleteSource: emailsSource"> <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" data-bind="emailsTags: replyTo">
</td> </td>
</tr> </tr>
<tr> <tr>