-
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 13 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,62 @@ | ||
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 | ||
*/ | ||
function canComposerBeConvertedToLTR(text: string): boolean { | ||
// this handle cases when user type only spaces | ||
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. Please start comments with a capital letter 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. "this handle cases" is not good grammar and should be more like "This regex handles the case when a user only types spaces into the composer. |
||
const containOnlySpaces = /^\s*$/; | ||
// this handle the case where someone has RTL enabled and they began typing an @mention for someone. | ||
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. Do comments end with a period or not? Let's be consistent |
||
const startsWithLTRAndAt = new RegExp(`^${CONST.UNICODE.LTR}@$`); | ||
// this handle cases could send empty messages when composer is multiline | ||
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. I'm not quite sure what this means and the grammar is a bit confusing. Are you trying to say this?
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. That's right, sorry 😣! |
||
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; | ||
* 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. 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. If the |
||
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.