diff --git a/components/ComposeEmail.tsx b/components/ComposeEmail.tsx index 999d9a37..69d757d3 100644 --- a/components/ComposeEmail.tsx +++ b/components/ComposeEmail.tsx @@ -64,15 +64,15 @@ export default function ComposeEmail({ if (composeBodyRef.current) { const decodedContent = decodeComposeContent(composeBody); - // Create a structure with clear separation + // Create the clear structure with proper content composeBodyRef.current.innerHTML = ` -
-
+
+
${decodedContent}
`; - // Place cursor at the beginning of new reply area + // Place cursor in the new reply area const newReplyArea = composeBodyRef.current.querySelector('#new-reply-area'); if (newReplyArea) { const range = document.createRange(); @@ -85,10 +85,31 @@ export default function ComposeEmail({ } }, [composeBody]); + // Enhanced input handler to prevent text reversal const handleInput = (e: React.FormEvent) => { if (composeBodyRef.current) { - const encodedContent = encodeComposeContent(composeBodyRef.current.innerHTML); - setComposeBody(encodedContent); + // Get the current HTML content + const fullContent = e.currentTarget.innerHTML; + + // Split at the divider to separate new reply from original content + const parts = fullContent.split(' diff --git a/lib/compose-mime-decoder.ts b/lib/compose-mime-decoder.ts index 47fc6349..4ba9cf83 100644 --- a/lib/compose-mime-decoder.ts +++ b/lib/compose-mime-decoder.ts @@ -6,7 +6,10 @@ export function decodeComposeContent(content: string): string { if (!content) return ''; - // Basic HTML cleaning + // Debug logging + console.log("Before decode:", content); + + // Basic HTML cleaning without any string manipulation that could reverse text let cleaned = content // Remove script and style tags .replace(/]*>[\s\S]*?<\/script>/gi, '') @@ -57,20 +60,31 @@ export function decodeComposeContent(content: string): string { .replace(/\s+/g, ' ') .trim(); + // Debug logging + console.log("After decode:", cleaned); + // Ensure all content has proper direction - cleaned = `
${cleaned}
`; + cleaned = `
${cleaned}
`; return cleaned; } export function encodeComposeContent(content: string): string { if (!content) return ''; - // Basic HTML encoding - return content + // Debug logging + console.log("Before encode:", content); + + // Basic HTML encoding without reversing + const encoded = content .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, ''') .replace(/\n/g, '
'); + + // Debug logging + console.log("After encode:", encoded); + + return encoded; } \ No newline at end of file