Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | 3x 483x 65x 447x 447x 796x 796x 796x 164x 1x 19x 19x 32x 30x 32x 30x 32x 32x 32x 9x 21x 28x 26x 23x 12x 10x 7x 29x 29x 29x 29x 29x 31x 29x 183x 30x 30x 30x 30x 30x 16x 16x 16x 16x 16x 16x 5x 5x 5x 5x 5x 7x 7x 5x 7x 92x 77x 69x 8x 7x 7x 8x 8x 6x 6x 4x 4x 4x 1x 2x 7x 18x 18x 18x 18x 18x 28x 28x 23x 18x 11x 11x 9x 18x | import type { Message } from "./api";
import { formatAddressList, parseAddressField } from "./utils";
export type ComposeFormat = "plain" | "html";
export interface Draft {
to: string;
cc: string;
bcc: string;
subject: string;
body: string;
htmlBody?: string;
format?: ComposeFormat;
}
export type ComposeMode =
| { type: "new" }
| { type: "reply"; original: Message }
| { type: "reply-all"; original: Message }
| { type: "forward"; original: Message };
const DRAFT_PREFIX = "stork-compose-draft";
/** Build a localStorage key that is unique per compose mode + original message */
export function draftKey(mode: ComposeMode): string {
if (mode.type === "new") return DRAFT_PREFIX;
return `${DRAFT_PREFIX}:${mode.type}:${mode.original.id}`;
}
export function saveDraft(key: string, draft: Draft) {
try {
localStorage.setItem(key, JSON.stringify(draft));
} catch {
// localStorage unavailable — ignore
}
}
export function loadDraft(key: string): Draft | null {
try {
const raw = localStorage.getItem(key);
if (!raw) return null;
return JSON.parse(raw) as Draft;
} catch {
return null;
}
}
export function clearDraft(key: string) {
try {
localStorage.removeItem(key);
} catch {
// ignore
}
}
/** Validate a comma-separated list of email addresses.
* Accepts "user@domain.tld" or "Name <user@domain.tld>" formats. */
export function validateEmails(raw: string): string | null {
if (!raw.trim()) return "At least one recipient is required";
const parts = raw
.split(",")
.map((s) => s.trim())
.filter(Boolean);
for (const part of parts) {
// Extract email from "Name <email>" or bare "email"
const match = part.match(/<([^>]+)>/) || [null, part];
const email = (match[1] ?? "").trim();
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
return `Invalid email address: ${email || part}`;
}
}
return null;
}
export function buildReplySubject(subject: string | null): string {
if (!subject) return "Re: (no subject)";
if (/^re:/i.test(subject)) return subject;
return `Re: ${subject}`;
}
export function buildForwardSubject(subject: string | null): string {
if (!subject) return "Fwd: (no subject)";
if (/^fwd:/i.test(subject)) return subject;
return `Fwd: ${subject}`;
}
export function buildReplyBody(msg: Message): string {
const d = msg.date ? new Date(msg.date) : null;
const date = d && !Number.isNaN(d.getTime()) ? d.toLocaleString() : "unknown date";
const header = `\n\nOn ${date}, ${msg.from_name || msg.from_address} wrote:\n`;
const body = msg.text_body || "";
const quoted = body
.split("\n")
.map((l) => `> ${l}`)
.join("\n");
return header + quoted;
}
export function escapeHtml(text: string): string {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
}
export function buildReplyHtmlBody(msg: Message): string {
const d = msg.date ? new Date(msg.date) : null;
const date = d && !Number.isNaN(d.getTime()) ? d.toLocaleString() : "unknown date";
const sender = msg.from_name || msg.from_address;
const quotedContent = msg.html_body || escapeHtml(msg.text_body || "").replace(/\n/g, "<br>");
return `<br><br><div>On ${escapeHtml(date)}, ${escapeHtml(sender)} wrote:</div><blockquote style="margin:0 0 0 0.8ex;border-left:1px solid #ccc;padding-left:1ex">${quotedContent}</blockquote>`;
}
export function buildForwardHtmlBody(msg: Message): string {
const from = msg.from_name
? `${escapeHtml(msg.from_name)} <${escapeHtml(msg.from_address || "unknown")}>`
: escapeHtml(msg.from_address || "unknown");
const d = msg.date ? new Date(msg.date) : null;
const date = d && !Number.isNaN(d.getTime()) ? d.toLocaleString() : "unknown date";
const forwardedContent = msg.html_body || escapeHtml(msg.text_body || "").replace(/\n/g, "<br>");
const toDisplay = formatAddressList(msg.to_addresses);
return `<br><br><div>---------- Forwarded message ----------</div><div>From: ${from}</div><div>Date: ${escapeHtml(date)}</div><div>Subject: ${escapeHtml(msg.subject || "(no subject)")}</div><div>To: ${escapeHtml(toDisplay)}</div><br>${forwardedContent}`;
}
/** Convert HTML to plain text (strips tags, decodes entities) */
export function htmlToPlainText(html: string): string {
const div = document.createElement("div");
div.innerHTML = html;
// Convert <br> and block elements to newlines
for (const br of div.querySelectorAll("br")) {
br.replaceWith("\n");
}
for (const el of div.querySelectorAll("p, div, blockquote")) {
el.prepend(document.createTextNode("\n"));
el.append(document.createTextNode("\n"));
}
return (div.textContent || "").replace(/\n{3,}/g, "\n\n").trim();
}
/** Convert plain text to simple HTML */
export function plainTextToHtml(text: string): string {
return escapeHtml(text).replace(/\n/g, "<br>");
}
/** Determine initial format: HTML if replying to/forwarding an HTML message */
export function getInitialFormat(mode: ComposeMode, saved: Draft | null): ComposeFormat {
if (saved?.format) return saved.format;
if (mode.type !== "new" && mode.original.html_body) return "html";
return "plain";
}
/**
* Parse a `references` field (stored as JSON array from IMAP sync or space-separated)
* and build the In-Reply-To + References headers for replies/forwards.
*/
export function buildThreadingHeaders(original: Message): {
inReplyTo: string | undefined;
references: string[] | undefined;
} {
if (!original.message_id) return { inReplyTo: undefined, references: undefined };
const inReplyTo = original.message_id;
const rawRefs = original.references ?? "";
let existingRefs: string[] = [];
if (rawRefs) {
const t = rawRefs.trim();
if (t.startsWith("[")) {
try {
const parsed = JSON.parse(t) as unknown;
existingRefs = Array.isArray(parsed) ? (parsed as string[]).filter(Boolean) : [];
} catch {
existingRefs = t.split(/\s+/).filter(Boolean);
}
} else {
existingRefs = t.split(/\s+/).filter(Boolean);
}
}
return {
inReplyTo,
references: [...existingRefs, original.message_id],
};
}
export function buildReplyAllCc(msg: Message, userEmail?: string): string {
// Include all To and CC addresses except the sender (who goes in To)
// and the current user (who is sending the reply)
const exclude = new Set<string>();
if (msg.from_address) exclude.add(msg.from_address.toLowerCase());
if (userEmail) exclude.add(userEmail.toLowerCase());
const addresses: string[] = [];
for (const addr of parseAddressField(msg.to_addresses)) {
// Extract bare email for comparison (handles "Name <email>" format)
const email = (addr.match(/<([^>]+)>/)?.[1] ?? addr).toLowerCase();
if (!exclude.has(email)) {
addresses.push(addr);
}
}
for (const addr of parseAddressField(msg.cc_addresses)) {
const email = (addr.match(/<([^>]+)>/)?.[1] ?? addr).toLowerCase();
if (!exclude.has(email)) {
addresses.push(addr);
}
}
return addresses.join(", ");
}
|