Skip to content

[HOLD for payment 2024-06-20] [$250] iOS - Chat - The cursor moves one space backward when inserting text after an emoji #42664

Closed

Description

@kbecciv

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Version Number: v1.4.76-1
Reproducible in staging?: y
Reproducible in production?: y
If this was caught during regression testing, add the test name, ID and link from TestRail: https://expensify.testrail.io/index.php?/tests/view/4577924
Issue reported by: Applause - Internal Team

Action Performed:

Pre-requisite: the user must be logged in.

  1. Go to any chat.
  2. Tap on the compose box.
  3. Enter any text and an emoji.
  4. After you have entered the emoji, enter any character again.
  5. Verify the cursor moves one space backward.

Expected Result:

The cursor should remain at the end of the compose box content.

Actual Result:

The cursor moves one space backward when inserting text after an emoji.

Workaround:

n/a

Platforms:

Which of our officially supported platforms is this issue occurring on?

  • Android: Native
  • Android: mWeb Chrome
  • iOS: Native
  • iOS: mWeb Safari
  • MacOS: Chrome / Safari
  • MacOS: Desktop

Screenshots/Videos

Add any screenshot/video evidence

Bug6493190_1716832834593.Ndpf2964_1_.mp4

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~011bc1604b6420e233
  • Upwork Job ID: 1795296506360057856
  • Last Price Increase: 2024-05-28
  • Automatic offers:
    • eh2077 | Reviewer | 102571526
    • bernhardoj | Contributor | 102571528
Issue OwnerCurrent Issue Owner: @kadiealexander

Activity

  1. added
    Daily
    Bug
    on May 27, 2024
  2. melvin-bot commented on May 27, 2024

    @melvin-bot

    Triggered auto assignment to @kadiealexander (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.

  3. kbecciv commented on May 27, 2024

    @kbecciv
    Author

    We think that this bug might be related to #vip-vsb

  4. kbecciv commented on May 27, 2024

    @kbecciv
    Author

    @kadiealexander FYI I haven't added the External label as I wasn't 100% sure about this issue. Please take a look and add the label if you agree it's a bug and can be handled by external contributors.

  5. added
    External
    on May 28, 2024
  6. changed the title [-]iOS - Chat - The cursor moves one space backward when inserting text after an emoji[/-] [+][$250] iOS - Chat - The cursor moves one space backward when inserting text after an emoji[/+] on May 28, 2024
  7. melvin-bot commented on May 28, 2024

    @melvin-bot
  8. added
    Help Wanted
    on May 28, 2024
  9. melvin-bot commented on May 28, 2024

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @eh2077 (External)

  10. bernhardoj commented on May 28, 2024

    @bernhardoj
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    The cursor selection moves to the left by one character when adding a character after inserting an emoji.

    What is the root cause of that problem?

    When we add a text to the composer, it will save the new selection position to a ref if the commentValue is not equal to newComment

    if (commentValue !== newComment) {
    const position = Math.max(selection.end + (newComment.length - commentRef.current.length), cursorPosition ?? 0);
    if (isIOSNative) {
    syncSelectionWithOnChangeTextRef.current = {position, value: newComment};
    }

    commentValue is the text that we type while the newComment is the commentValue that has emoji code replaced to emoji character (for example :smile: to 😄).

    If they are different, we want to manually correct the selection, however, on iOS, the selection prop is buggy so we use the ref sync solution that imperatively updates the selection that is introduced in #30835.

    It will update/sync the selection in onChangeText.

    if (isIOSNative && syncSelectionWithOnChangeTextRef.current) {
    const positionSnapshot = syncSelectionWithOnChangeTextRef.current.position;
    syncSelectionWithOnChangeTextRef.current = null;
    // ensure that selection is set imperatively after all state changes are effective
    InteractionManager.runAfterInteractions(() => {
    // note: this implementation is only available on non-web RN, thus the wrapping
    // 'if' block contains a redundant (since the ref is only used on iOS) platform check
    textInputRef.current?.setSelection(positionSnapshot, positionSnapshot);
    });
    }

    However, there are a few things to notice here.

    First, when we add an emoji from the emoji picker, commentValue is not equal to newComment even though there is no emoji code converted to an emoji character. The reason behind this is that we add a whitespace to the text

    } = EmojiUtils.replaceAndExtractEmojis(isEmojiInserted ? ComposerUtils.insertWhiteSpaceAtIndex(commentValue, endIndex) : commentValue, preferredSkinTone, preferredLocale);

    So, commentValue doesn't have the whitespace while the newComment has the whitespace. Previously, commentValue already contained the whitespace every time we added an emoji from the emoji picker, but this PR changes it so the whitespace is also added when we add the emoji from the device keyboard emoji picker.

    This will then set the selection to the ref (syncSelectionWithOnChangeTextRef).

    Next, if we add an emoji from the emoji picker, onChangeText won't be triggered. onChangeText will only be triggered if we input the text manually from keyboard.

    So, when we add a character, it will run the selection sync logic which contains the old selection.

    if (isIOSNative && syncSelectionWithOnChangeTextRef.current) {
    const positionSnapshot = syncSelectionWithOnChangeTextRef.current.position;
    syncSelectionWithOnChangeTextRef.current = null;
    // ensure that selection is set imperatively after all state changes are effective
    InteractionManager.runAfterInteractions(() => {
    // note: this implementation is only available on non-web RN, thus the wrapping
    // 'if' block contains a redundant (since the ref is only used on iOS) platform check
    textInputRef.current?.setSelection(positionSnapshot, positionSnapshot);
    });
    }

    What changes do you think we should make in order to solve the problem?

    We need to compare newComment with the commentValue with whitespace appended because the manual selection correction is only used when we add emoji through emoji code.

    const commentWithSpaceInserted = isEmojiInserted ? ComposerUtils.insertWhiteSpaceAtIndex(commentValue, endIndex) : commentValue;
    ...
    if (commentWithSpaceInserted !== newComment) {
    ...
    

    What alternative solutions did you explore? (Optional)

    Clear the ref every time we call updateComment. This is to invalidate any old selection sync ref.

    const updateComment = useCallback(
    (commentValue: string, shouldDebounceSaveComment?: boolean) => {
    raiseIsScrollLikelyLayoutTriggered();
    const {startIndex, endIndex, diff} = findNewlyAddedChars(lastTextRef.current, commentValue);

    const updateComment = useCallback(
        (commentValue: string, shouldDebounceSaveComment?: boolean) => {
            if (isIOSNative) {
                syncSelectionWithOnChangeTextRef.current = null;
            }
    
  11. 22 remaining items

  12. removed
    Reviewing
    on Jun 11, 2024
  13. melvin-bot commented on Jun 11, 2024

    @melvin-bot

    Reviewing label has been removed, please complete the "BugZero Checklist".

  14. added
    Weekly
    and removed
    Weekly
    on Jun 13, 2024
  15. changed the title [-][HOLD for payment 2024-06-18] [$250] iOS - Chat - The cursor moves one space backward when inserting text after an emoji[/-] [+][HOLD for payment 2024-06-20] [HOLD for payment 2024-06-18] [$250] iOS - Chat - The cursor moves one space backward when inserting text after an emoji[/+] on Jun 13, 2024
  16. melvin-bot commented on Jun 13, 2024

    @melvin-bot

    The solution for this issue has been 🚀 deployed to production 🚀 in version 1.4.82-4 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

    If no regressions arise, payment will be issued on 2024-06-20. 🎊

    For reference, here are some details about the assignees on this issue:

  17. melvin-bot commented on Jun 13, 2024

    @melvin-bot

    BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

  18. changed the title [-][HOLD for payment 2024-06-20] [HOLD for payment 2024-06-18] [$250] iOS - Chat - The cursor moves one space backward when inserting text after an emoji[/-] [+][HOLD for payment 2024-06-20] [$250] iOS - Chat - The cursor moves one space backward when inserting text after an emoji[/+] on Jun 14, 2024
  19. deleted a comment from melvin-bot on Jun 14, 2024
  20. deleted a comment from melvin-bot on Jun 14, 2024
  21. eh2077 commented on Jun 15, 2024

    @eh2077
    Contributor

    Checklist

    Regression test

    1. Open any chat
    2. Type anything
    3. Add an emoji from emoji picker
    4. Type a character
    5. Verify the cursor/selection stays at its place

    Do we agree 👍 or 👎

  22. added
    Daily
    and removed
    Weekly
    on Jun 18, 2024
  23. kadiealexander commented on Jun 19, 2024

    @kadiealexander
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting PaymentBugDailyExternal

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions