Add contacts by right-clicking anyone named in a message

Right-clicking a sender, or any address in the message details, opens a menu
offering to add that person to the address book - plus edit them when they are
already known, write to them, or copy the address.

"Add to contacts" opens the contact editor prefilled rather than saving
silently, so the address book gets a real card that the user can complete,
not a bare email address. contactFromAddress splits the display name into
JSContact name components: "Ada Lovelace" into given and surname, "Lovelace,
Ada" unpicked, a single word as the given name, and a name that is really
just an address left off entirely.

Addresses in the details block were joined into one string, so they are now
rendered per address to be individually targetable.

ContactEditor previously ignored a prefilled name on an unsaved card - it read
name components only when the card had an id - so it now reads them either
way.
This commit is contained in:
2026-08-23 14:46:38 -07:00
parent 40f0ad5fdb
commit d143e711d4
7 changed files with 179 additions and 10 deletions
+31
View File
@@ -147,3 +147,34 @@ function fold(line: string): string {
export function newKey(prefix = "k"): string {
return `${prefix}${Math.random().toString(36).slice(2, 8)}`;
}
/**
* A new contact card seeded from an email address.
*
* The display name in a From header is one string, so it has to be split into
* name components: "Ada Lovelace" gives given + surname, the "Lovelace, Ada"
* form is unpicked, and a single word becomes the given name. Anything that
* looks like an address rather than a name is left out — a card named
* "[email protected]" helps nobody.
*/
export function contactFromAddress(addr: EmailAddress): Partial<ContactCard> {
const card: Partial<ContactCard> = {
kind: "individual",
emails: { [newKey("e")]: { "@type": "EmailAddress", address: addr.email, pref: 1 } },
};
const raw = (addr.name ?? "").trim().replace(/^["']|["']$/g, "").trim();
if (!raw || raw.includes("@")) return card;
const [surnameFirst, givenRest] = raw.includes(",") ? raw.split(",", 2) : [];
const parts = surnameFirst && givenRest
? { given: givenRest.trim(), surname: surnameFirst.trim() }
: splitName(raw);
const name = buildName(parts);
if (name) card.name = name;
return card;
}
function splitName(full: string): { given: string; middle: string; surname: string } {
const words = full.split(/\s+/).filter(Boolean);
if (words.length === 1) return { given: words[0]!, middle: "", surname: "" };
return { given: words[0]!, middle: words.slice(1, -1).join(" "), surname: words[words.length - 1]! };
}