From dd214949ea987d73fae622ee872f49ca210a4a6e Mon Sep 17 00:00:00 2001 From: Roie Natan Date: Tue, 5 Dec 2023 12:40:13 -0500 Subject: [PATCH 1/2] in isRtlText: filter mentions in the text to ignore mentions text and check only message content charecters --- src/shared/utils/shared.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/shared/utils/shared.tsx b/src/shared/utils/shared.tsx index f57ffd1c1f..4904e23cda 100644 --- a/src/shared/utils/shared.tsx +++ b/src/shared/utils/shared.tsx @@ -8,6 +8,7 @@ import { BaseProposal } from "@/shared/models/governance/proposals"; import { transformFirebaseDataList } from "@/shared/utils/transformFirebaseDataToModel"; import { BASE_URL } from "../constants"; import { Common, DateFormat, Time, User } from "../models"; +import { ElementType } from "../ui-kit/TextEditor/constants"; export const getPrefix = (currency: Currency): string => { switch (currency) { @@ -201,8 +202,13 @@ export const isRTL = (text = ""): boolean => { }; export const isRtlText = (text = ""): boolean => { - for (let i = 0; i < text.length; i++) { - const charCode = text.charCodeAt(i); + const parsedText = JSON.parse(text); + const textWithNoMentions = JSON.stringify( + parsedText[0].children?.filter((item) => item.type !== ElementType.Mention), + ); + + for (let i = 0; i < textWithNoMentions.length; i++) { + const charCode = textWithNoMentions.charCodeAt(i); // Hebrew Block if (charCode >= 0x0590 && charCode <= 0x05ff) return true; From 7d1cfe25614b21730510d85a7be69032790b49a8 Mon Sep 17 00:00:00 2001 From: Roie Natan Date: Tue, 5 Dec 2023 14:12:57 -0500 Subject: [PATCH 2/2] added try catch to JSON.parse block --- src/shared/utils/shared.tsx | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/shared/utils/shared.tsx b/src/shared/utils/shared.tsx index 4904e23cda..4a8f2e9144 100644 --- a/src/shared/utils/shared.tsx +++ b/src/shared/utils/shared.tsx @@ -202,27 +202,34 @@ export const isRTL = (text = ""): boolean => { }; export const isRtlText = (text = ""): boolean => { - const parsedText = JSON.parse(text); - const textWithNoMentions = JSON.stringify( - parsedText[0].children?.filter((item) => item.type !== ElementType.Mention), - ); + try { + const parsedText = JSON.parse(text); + const textWithNoMentions = JSON.stringify( + parsedText[0].children?.filter( + (item) => item.type !== ElementType.Mention, + ), + ); - for (let i = 0; i < textWithNoMentions.length; i++) { - const charCode = textWithNoMentions.charCodeAt(i); + for (let i = 0; i < textWithNoMentions.length; i++) { + const charCode = textWithNoMentions.charCodeAt(i); - // Hebrew Block - if (charCode >= 0x0590 && charCode <= 0x05ff) return true; + // Hebrew Block + if (charCode >= 0x0590 && charCode <= 0x05ff) return true; - // Arabic Block - if (charCode >= 0x0600 && charCode <= 0x06ff) return true; + // Arabic Block + if (charCode >= 0x0600 && charCode <= 0x06ff) return true; - // Arabic Supplement Block - if (charCode >= 0x0750 && charCode <= 0x077f) return true; + // Arabic Supplement Block + if (charCode >= 0x0750 && charCode <= 0x077f) return true; - // Arabic Extended-A Block - if (charCode >= 0x08a0 && charCode <= 0x08ff) return true; + // Arabic Extended-A Block + if (charCode >= 0x08a0 && charCode <= 0x08ff) return true; + } + return false; + } catch (error) { + console.error(error); + return false; } - return false; }; /**