fix rtl and ltr text in new message

This commit is contained in:
hamid zarghami
2025-08-04 12:17:24 +03:30
parent 026f2472a4
commit 52508741ab
4 changed files with 45 additions and 13 deletions
+29
View File
@@ -9,3 +9,32 @@ import { twMerge } from "tailwind-merge";
export const clx = (...classes: (string | boolean | undefined)[]): string => {
return twMerge(classes.filter(Boolean).join(" "));
};
/**
* Detects the text direction based on the content
*
* @param text - The text to analyze
* @returns 'rtl' | 'ltr' | 'auto'
*/
export const detectTextDirection = (text: string): "rtl" | "ltr" | "auto" => {
// Remove HTML tags
const cleanText = text.replace(/<[^>]*>/g, "").trim();
if (!cleanText) return "auto";
// Count RTL and LTR characters
const rtlChars =
cleanText.match(
/[\u0590-\u083F\u08A0-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/g
) || [];
const ltrChars = cleanText.match(/[A-Za-z]/g) || [];
const rtlCount = rtlChars.length;
const ltrCount = ltrChars.length;
// If there's a significant difference, return that direction
if (rtlCount > ltrCount * 1.5) return "rtl";
if (ltrCount > rtlCount * 1.5) return "ltr";
// Otherwise, use auto
return "auto";
};