-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed Android - Chat - Message gets displayed from right to left #32297
Changes from 15 commits
3a10688
7c11e93
fbd37e8
5ffd754
59f4466
431d7e2
33b5733
89d738e
2a9f636
3740949
04c95e0
1735f5a
e737dcb
be6ee76
5be5dc0
8a75507
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import CONST from '@src/CONST'; | ||
import ConvertToLTR from './types'; | ||
|
||
/** | ||
* Android only - convert RTL text to a LTR text using Unicode controls. | ||
* https://www.w3.org/International/questions/qa-bidi-unicode-controls | ||
* | ||
* In React Native, when working with bidirectional text (RTL - Right-to-Left or LTR - Left-to-Right), you may encounter issues related to text rendering, especially on Android devices. These issues arise because Android's default behavior for text direction might not always align with the desired directionality of your app. | ||
*/ | ||
import CONST from '@src/CONST'; | ||
import ConvertToLTR from './types'; | ||
|
||
const convertToLTR: ConvertToLTR = (text) => `${CONST.UNICODE.LTR}${text}`; | ||
|
||
export default convertToLTR; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,66 @@ | ||
import CONST from '@src/CONST'; | ||
import ConvertToLTRForComposer from './types'; | ||
|
||
/** | ||
* Android only - The composer can be converted to LTR if its content is the LTR character followed by an @ or space | ||
* because to mention sugggestion works the @ character must not have any character at the beginning e.g.: \u2066@ doesn't work | ||
* also to avoid sending empty messages the unicode character with space could enable the send button. | ||
*/ | ||
function canComposerBeConvertedToLTR(text: string): boolean { | ||
// This regex handles the case when a user only types spaces into the composer. | ||
const containOnlySpaces = /^\s*$/; | ||
// This regex handles the case where someone has RTL enabled and they began typing an @mention for someone. | ||
const startsWithLTRAndAt = new RegExp(`^${CONST.UNICODE.LTR}@$`); | ||
// This regex handles the case where the composer can contain multiple lines of whitespace | ||
const startsWithLTRAndSpace = new RegExp(`${CONST.UNICODE.LTR}\\s*$`); | ||
const emptyExpressions = [containOnlySpaces, startsWithLTRAndAt, startsWithLTRAndSpace]; | ||
return emptyExpressions.some((exp) => exp.test(text)); | ||
} | ||
|
||
/** | ||
* Android only - We should remove the LTR unicode when the input is empty to prevent: | ||
* Sending an empty message; | ||
* Mention suggestions not works if @ or \s (at or space) is the first character; | ||
* Placeholder is not displayed if the unicode character is the only character remaining; | ||
* | ||
* @newComment: the comment written by the user | ||
* @force: always remove the LTR unicode, going to be used when composer is consider as empty */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These need to follow the JSDocs format: https://jsdoc.app/tags-param. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, thanks for the link, I appreciate it (TIL). |
||
const resetLTRWhenEmpty = (newComment: string, force?: boolean) => { | ||
const result = newComment.length <= 1 || force ? newComment.replaceAll(CONST.UNICODE.LTR, '') : newComment; | ||
return result; | ||
}; | ||
|
||
/** | ||
* Android only - Do not convert RTL text to a LTR text for input box using Unicode controls. | ||
* Android does not properly support bidirectional text for mixed content for input box | ||
*/ | ||
const convertToLTRForComposer: ConvertToLTRForComposer = (text) => text; | ||
const convertToLTRForComposer: ConvertToLTRForComposer = (text, isComposerEmpty) => { | ||
const shouldComposerMaintainAsLTR = canComposerBeConvertedToLTR(text); | ||
const newText = resetLTRWhenEmpty(text, shouldComposerMaintainAsLTR); | ||
if (shouldComposerMaintainAsLTR) { | ||
return newText; | ||
} | ||
return isComposerEmpty ? `${CONST.UNICODE.LTR}${newText}` : newText; | ||
}; | ||
|
||
/** | ||
* This is necessary to convert the input to LTR, there is a delay that causes the cursor not to go to the end of the input line when pasting text or typing fast. The delay is caused for the time that takes the input to convert from RTL to LTR and viceversa. | ||
*/ | ||
const moveCursorToEndOfLine = ( | ||
commentLength: number, | ||
setSelection: ( | ||
value: React.SetStateAction<{ | ||
start: number; | ||
end: number; | ||
}>, | ||
) => void, | ||
) => { | ||
setSelection({ | ||
start: commentLength + 1, | ||
end: commentLength + 1, | ||
}); | ||
}; | ||
|
||
export {moveCursorToEndOfLine}; | ||
|
||
export default convertToLTRForComposer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
type ConvertToLTRForComposer = (text: string) => string; | ||
type ConvertToLTRForComposer = (text: string, isComposerEmpty?: boolean) => string; | ||
|
||
export default ConvertToLTRForComposer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment should also explain why it's OK to convert it in these cases and why it shouldn't be converted. Consider adding "because..." and then fill out the rest.