mime change

This commit is contained in:
alma 2025-04-24 17:05:48 +02:00
parent 3c81473239
commit ed2752ad0b

View File

@ -83,45 +83,26 @@ export default function ComposeEmail({
useEffect(() => { useEffect(() => {
if (composeBodyRef.current && !isInitialized) { if (composeBodyRef.current && !isInitialized) {
let content = ''; // Initialize the content structure with both new reply area and original content in a single contentEditable div
const content = replyTo || forwardFrom ? `
if (replyTo) { <div class="compose-area" contenteditable="true"></div>
// For replies <div class="quoted-content" contenteditable="false">
content = ` ${forwardFrom ? `
<div class="compose-area" contenteditable="true" style="min-height: 100px; padding: 10px; border: 1px solid #e5e7eb; border-radius: 4px; margin-bottom: 20px;"></div> ---------- Forwarded message ---------<br/>
<div class="quoted-content" contenteditable="false" style="color: #6b7280; font-size: 0.875rem;"> From: ${forwardFrom.from}<br/>
<div style="margin-bottom: 10px;"> Date: ${new Date(forwardFrom.date).toLocaleString()}<br/>
On ${new Date(replyTo.date).toLocaleString()}, ${replyTo.from} wrote: Subject: ${forwardFrom.subject}<br/>
</div> To: ${forwardFrom.to}<br/>
<blockquote style="margin: 0; padding-left: 1em; border-left: 2px solid #e5e7eb;"> ${forwardFrom.cc ? `Cc: ${forwardFrom.cc}<br/>` : ''}
${composeBody} <br/>
</blockquote> ` : `
</div> On ${new Date(replyTo?.date || '').toLocaleString()}, ${replyTo?.from} wrote:<br/>
`; `}
} else if (forwardFrom) { <blockquote style="margin: 0 0 0 0.8ex; border-left: 1px solid rgb(204,204,204); padding-left: 1ex;">
// For forwards ${composeBody}
content = ` </blockquote>
<div class="compose-area" contenteditable="true" style="min-height: 100px; padding: 10px; border: 1px solid #e5e7eb; border-radius: 4px; margin-bottom: 20px;"></div> </div>
<div class="quoted-content" contenteditable="false" style="color: #6b7280; font-size: 0.875rem;"> ` : '';
<div style="margin-bottom: 10px;">
---------- Forwarded message ---------<br/>
From: ${forwardFrom.from}<br/>
Date: ${new Date(forwardFrom.date).toLocaleString()}<br/>
Subject: ${forwardFrom.subject}<br/>
To: ${forwardFrom.to}<br/>
${forwardFrom.cc ? `Cc: ${forwardFrom.cc}<br/>` : ''}
</div>
<blockquote style="margin: 0; padding-left: 1em; border-left: 2px solid #e5e7eb;">
${composeBody}
</blockquote>
</div>
`;
} else {
// For new messages
content = `
<div class="compose-area" contenteditable="true" style="min-height: 200px; padding: 10px; border: 1px solid #e5e7eb; border-radius: 4px;"></div>
`;
}
composeBodyRef.current.innerHTML = content; composeBodyRef.current.innerHTML = content;
setIsInitialized(true); setIsInitialized(true);
@ -135,20 +116,16 @@ export default function ComposeEmail({
range.collapse(true); range.collapse(true);
sel?.removeAllRanges(); sel?.removeAllRanges();
sel?.addRange(range); sel?.addRange(range);
(composeArea as HTMLElement).focus();
} }
} }
}, [composeBody, replyTo, forwardFrom, isInitialized]); }, [composeBody, replyTo, forwardFrom, isInitialized]);
// Modified input handler to work with the single contentEditable area
const handleInput = (e: React.FormEvent<HTMLDivElement>) => { const handleInput = (e: React.FormEvent<HTMLDivElement>) => {
if (!composeBodyRef.current) return; if (!composeBodyRef.current) return;
const composeArea = composeBodyRef.current.querySelector('.compose-area'); const composeArea = composeBodyRef.current.querySelector('.compose-area');
if (composeArea) { if (composeArea) {
const newContent = composeArea.innerHTML; setComposeBody(composeArea.innerHTML);
setComposeBody(newContent);
if (onBodyChange) {
onBodyChange(newContent);
}
} }
}; };
@ -166,11 +143,12 @@ export default function ComposeEmail({
} }
try { try {
// Read file as base64
const base64Content = await new Promise<string>((resolve) => { const base64Content = await new Promise<string>((resolve) => {
const reader = new FileReader(); const reader = new FileReader();
reader.onloadend = () => { reader.onloadend = () => {
const base64 = reader.result as string; const base64 = reader.result as string;
resolve(base64.split(',')[1]); resolve(base64.split(',')[1]); // Remove data URL prefix
}; };
reader.readAsDataURL(file); reader.readAsDataURL(file);
}); });
@ -220,147 +198,150 @@ export default function ComposeEmail({
<div className="h-full flex flex-col p-6 space-y-4 overflow-y-auto"> <div className="h-full flex flex-col p-6 space-y-4 overflow-y-auto">
{/* To Field */} {/* To Field */}
<div className="flex-none"> <div className="flex-none">
<Label htmlFor="to">To</Label> <Label htmlFor="to" className="block text-sm font-medium text-gray-700">To</Label>
<Input <Input
id="to" id="to"
value={composeTo} value={composeTo}
onChange={(e) => setComposeTo(e.target.value)} onChange={(e) => setComposeTo(e.target.value)}
placeholder="Recipients" placeholder="recipient@example.com"
className="mt-1" className="w-full mt-1 bg-white border-gray-300 text-gray-900"
/> />
</div> </div>
{/* Cc Field */} {/* CC/BCC Toggle Buttons */}
<div className="flex-none flex items-center gap-4">
<button
type="button"
className="text-blue-600 hover:text-blue-700 text-sm font-medium"
onClick={() => setShowCc(!showCc)}
>
{showCc ? 'Hide Cc' : 'Add Cc'}
</button>
<button
type="button"
className="text-blue-600 hover:text-blue-700 text-sm font-medium"
onClick={() => setShowBcc(!showBcc)}
>
{showBcc ? 'Hide Bcc' : 'Add Bcc'}
</button>
</div>
{/* CC Field */}
{showCc && ( {showCc && (
<div className="flex-none"> <div className="flex-none">
<Label htmlFor="cc">Cc</Label> <Label htmlFor="cc" className="block text-sm font-medium text-gray-700">Cc</Label>
<Input <Input
id="cc" id="cc"
value={composeCc} value={composeCc}
onChange={(e) => setComposeCc(e.target.value)} onChange={(e) => setComposeCc(e.target.value)}
placeholder="Carbon copy" placeholder="cc@example.com"
className="mt-1" className="w-full mt-1 bg-white border-gray-300 text-gray-900"
/> />
</div> </div>
)} )}
{/* Bcc Field */} {/* BCC Field */}
{showBcc && ( {showBcc && (
<div className="flex-none"> <div className="flex-none">
<Label htmlFor="bcc">Bcc</Label> <Label htmlFor="bcc" className="block text-sm font-medium text-gray-700">Bcc</Label>
<Input <Input
id="bcc" id="bcc"
value={composeBcc} value={composeBcc}
onChange={(e) => setComposeBcc(e.target.value)} onChange={(e) => setComposeBcc(e.target.value)}
placeholder="Blind carbon copy" placeholder="bcc@example.com"
className="mt-1" className="w-full mt-1 bg-white border-gray-300 text-gray-900"
/> />
</div> </div>
)} )}
{/* Subject Field */} {/* Subject Field */}
<div className="flex-none"> <div className="flex-none">
<Label htmlFor="subject">Subject</Label> <Label htmlFor="subject" className="block text-sm font-medium text-gray-700">Subject</Label>
<Input <Input
id="subject" id="subject"
value={composeSubject} value={composeSubject}
onChange={(e) => setComposeSubject(e.target.value)} onChange={(e) => setComposeSubject(e.target.value)}
placeholder="Subject" placeholder="Enter subject"
className="mt-1" className="w-full mt-1 bg-white border-gray-300 text-gray-900"
/> />
</div> </div>
{/* Message Body */} {/* Message Body - Single contentEditable area with separated regions */}
<div className="flex-1 min-h-0"> <div className="flex-1 min-h-[200px] flex flex-col">
<Label htmlFor="message" className="flex-none block text-sm font-medium text-gray-700 mb-2">Message</Label>
<div <div
ref={composeBodyRef} ref={composeBodyRef}
onInput={handleInput} className="flex-1 w-full bg-white border border-gray-300 rounded-md p-4 text-gray-900 overflow-y-auto focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
className="h-full" style={{ direction: 'ltr' }}
style={{ overflowY: 'auto' }} dir="ltr"
/> spellCheck="true"
</div> role="textbox"
aria-multiline="true"
{/* Attachments */} tabIndex={0}
{attachments.length > 0 && ( >
<div className="flex-none"> <style>{`
<div className="flex flex-wrap gap-2"> .compose-area {
{attachments.map((attachment, index) => ( min-height: 100px;
<div margin-bottom: 20px;
key={index} outline: none;
className="flex items-center gap-2 bg-gray-100 px-3 py-1 rounded-full text-sm" }
> .quoted-content {
<Paperclip className="h-4 w-4 text-gray-500" /> color: #666;
<span>{attachment.name}</span> user-select: text;
<button -webkit-user-select: text;
onClick={() => { }
const newAttachments = [...attachments]; .quoted-content blockquote {
newAttachments.splice(index, 1); margin: 0.8ex;
setAttachments(newAttachments); padding-left: 1ex;
}} border-left: 1px solid #ccc;
className="text-gray-500 hover:text-gray-700" }
> `}</style>
<X className="h-4 w-4" />
</button>
</div>
))}
</div>
</div>
)}
{/* Action Buttons */}
<div className="flex-none flex items-center justify-between pt-4 border-t border-gray-200">
<div className="flex items-center gap-2">
<Button
variant="ghost"
size="sm"
onClick={() => {
const input = document.createElement('input');
input.type = 'file';
input.multiple = true;
input.onchange = (e) => handleFileAttachment(e as any);
input.click();
}}
>
<Paperclip className="h-4 w-4 mr-2" />
Attach
</Button>
{!showCc && (
<Button
variant="ghost"
size="sm"
onClick={() => setShowCc(true)}
>
Cc
</Button>
)}
{!showBcc && (
<Button
variant="ghost"
size="sm"
onClick={() => setShowBcc(true)}
>
Bcc
</Button>
)}
</div>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="sm"
onClick={onCancel}
>
Cancel
</Button>
<Button
size="sm"
onClick={handleSend}
>
Send
</Button>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
{/* Modal Footer */}
<div className="flex-none flex items-center justify-between px-6 py-3 border-t border-gray-200 bg-white">
<div className="flex items-center gap-2">
{/* File Input for Attachments */}
<input
type="file"
id="file-attachment"
className="hidden"
multiple
onChange={handleFileAttachment}
/>
<label htmlFor="file-attachment">
<Button
variant="outline"
size="icon"
className="rounded-full bg-white hover:bg-gray-100 border-gray-300"
onClick={(e) => {
e.preventDefault();
document.getElementById('file-attachment')?.click();
}}
>
<Paperclip className="h-4 w-4 text-gray-600" />
</Button>
</label>
</div>
<div className="flex items-center gap-3">
<Button
variant="ghost"
className="text-gray-600 hover:text-gray-700 hover:bg-gray-100"
onClick={onCancel}
>
Cancel
</Button>
<Button
className="bg-blue-600 text-white hover:bg-blue-700"
onClick={handleSend}
>
Send
</Button>
</div>
</div>
</div> </div>
</div> </div>
); );