-
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
Merged
tgolen
merged 16 commits into
Expensify:main
from
samilabud:android_chat_displayed_right_to_left_30584
Dec 26, 2023
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3a10688
Fixed Android - Chat - Message gets displayed from right to left
samilabud 7c11e93
Removed ignore lint rule and doing the early return
samilabud fbd37e8
Fixed early return when useEffect bug
samilabud 5ffd754
Fixed bugs: when mention, sending empty messages, no placeholder
samilabud 59f4466
Added comments and refactoring code to better understanding
samilabud 431d7e2
Fixed prettier errors
samilabud 33b5733
Fixed observations, added suggestions and added extra comments
samilabud 89d738e
Moving functions from convertToLTR to convertToLTRForComposer lib, re…
samilabud 2a9f636
Added space to the list of characters to consider as empty for the co…
samilabud 3740949
Added method to determinate if we should convert the composer to LTR …
samilabud 04c95e0
Fixed suggestions: variable names, comments, misspelling
samilabud 1735f5a
Fixed comments and variables to better understanding
samilabud e737dcb
Comments and variables changed to better understanding
samilabud be6ee76
Fixed grammar in comments
samilabud 5be5dc0
Fixed comments grammar and using @param doc
samilabud 8a75507
Using jsdoc format for comments
samilabud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,69 @@ | ||
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; | ||
* | ||
* @param {String} newComment - the comment written by the user | ||
* @param {Boolean} force - always remove the LTR unicode, going to be used when composer is consider as empty | ||
* @return {String} | ||
*/ | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.