Skip to content
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

Composer: Control selection using setSelection #17687

Closed
wants to merge 4 commits into from

Conversation

s77rt
Copy link
Contributor

@s77rt s77rt commented Apr 19, 2023

@hannojg

Details

Instead of controlling the selection the traditional way (passing selection prop) which is always render dependent and is so buggy on RN especially for Android. We are going to control the selection directly using an independent and render free method which is setSelection for Native and setSelectionRange for Web. We we will be using our setSelection library for that.

Fixed Issues

$ #12854
PROPOSAL: #12854 (comment)

Tests

  1. Open App
  2. Go to any chat
  3. Type message Hi there
  4. Move the cursor to be between Hi and there
  5. Type :wave:
  6. Verify the 👋 emoji got inserted correctly between Hi and there
  7. Verify the cursor position is after the emoji
  8. Verify the cursor did not jump around
  • Verify that no errors appear in the JS console

Offline tests

n/a

QA Steps

  1. Open App
  2. Go to any chat
  3. Type message Hi there
  4. Move the cursor to be between Hi and there
  5. Type :wave:
  6. Verify the 👋 emoji got inserted correctly between Hi and there
  7. Verify the cursor position is after the emoji
  8. Verify the cursor did not jump around
  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
web.mp4
Mobile Web - Chrome
mweb-chrome.mp4
Mobile Web - Safari
mweb-safari.mp4
Desktop
desktop.mp4
iOS
ios.mp4
Android
android.mp4

@s77rt s77rt requested a review from a team as a code owner April 19, 2023 21:11
@melvin-bot melvin-bot bot requested review from MariaHCD and mollfpr and removed request for a team April 19, 2023 21:11
@MelvinBot
Copy link

@MariaHCD @mollfpr One of you needs to copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@hannojg
Copy link
Contributor

hannojg commented Apr 21, 2023

I would highly recommend to run the tests from this PR, as it should cover most edge cases.

(Edit: I can also take some time to run these tests if needed, as I am by now quite experiences with the weird bugs that can happen when using SetSelection)

Here is a copy of it:

Regression tests:

Test plan

Helper list to confirm you ran every test

All platforms, make sure for web to run on every browser (Safari, Chrome, Firefox, Edge):

Selection is not reset after selecting an emoji

  1. Add multiple message on the composer.
  2. selected multiple lines.
  3. Open the emoji picker and choose an emoji.
  4. selection remained.
screen-2022-10-12_01.10.50.mp4

Broken emojis are rendered when picked from the picker in a series

  1. Add a bunch of text lines in the composer.
  2. Move the cursor to some middle position in the para.
  3. Now pick an emoji from the emoji picker.
  4. Try to pick the same emoji a few more times from the picker.
screen-2022-10-12_01.35.38.mp4

Cursor position is broken

  1. Steps
  1. Add a bunch of lines to the composer on web.
  2. Place your cursor somewhere in the middle.
  3. Go to any other page and copy some strings. (e.g. Copy step 2 in clipboard)
  4. Paste in the composer.
  5. Cursor is not at the end of pasted string.
screen-2022-10-12_01.46.21.mp4
  1. Steps

Steps.

  1. Open the app on the web.
  2. Copy long text from some where (only text no markup).
  3. Paste it on the composer.
  4. Cursor is at the start of the pasted text.

Expected: it should be at the end of the pasted text.

Adding emoji via EmojiPicker in replacement for multiple lines creates a delay before the composer height is calculated.

  1. Add multi-line message in composer.
  2. Selects the multi-line text.
  3. Add an emoji from the picker.
  4. Observe that there is a delay before the composer height is adjusted correctly.
screen-2022-10-19_01.19.07.mp4

Selected Emoji is placed in the wrong position.

  1. open the app.
  2. Add three lines of message in composer.
  3. Select some text from the middle line.
  4. Open Emoji Picker and select an emoji.
  5. Notice emoji is placed correctly and the selected text is gone.
  6. Now reopen the EmojiPicker.
  7. Select another emoji.
  8. Notice that new emojis are added to the end of the line (not the previous cursor position).
  9. Repeat from 4 to 6 to observe the same pattern.

[Web]

Pasting image does not send the attached text message.

  1. Type some message in the composer.
  2. Go to image and copy the image.
  3. Back to the app and focus after the typed text in the composer.
  4. Press CTRL+V to paste the image.
  5. Click send on the opened attachment modal.
  6. Observe the image is sent but the text remained in the composer and Send button is disabled.

Expected: Pressing send on the attachment modal should first send the text and then the image. (See staging for correct behaviour).

There is a delay before Composer expands the height on the web.

  1. Open the app on the web.
  2. type text quickly.
  3. Use Shift+Enter to enter a couple of lines.
screen-2022-11-16_19.39.59.mp4

Cursor position is not focused after picking a emoji in Composer.

  1. Paste the following content in composer.
ASDSADSA
DA😆 🤫 😙 
SD
ASDSA
D
ASDAS
DSA
DSA
DAS
D
AS
D


😋 🤫 😋 😋 😋 
SADSA
D
ASD
SA
D
SADS
AD
ASDAS
DAS

  1. Now place the cursor after the first three emojies.
  2. Scroll the composer content below so that cursor is no more visible but composer remains focused.
  3. open emoji Picker.
  4. select an emoji.
  5. Notice that cursor position is not revealed.
screen-2023-01-05_00.03.18.mp4

Content flashing while pasting

  1. Paste the following content in composer.
ASDSADSA
DA😆 🤫 😙 
SD
ASDSA
D
ASDAS
DSA
DSA
DAS
D
AS
D


😋 🤫 😋 😋 😋 
SADSA
D
ASD
SA
D
SADS
AD
ASDAS
DAS

  1. Now use Cmd + A to select the whole content in the composer.
  2. Paste the same copied content to replace.
  3. Notice that there is a flashing of content. (Not present on staging).
screen-2023-01-05_00.11.41.mp4

Edit Message: Copy-pasting is slow.

  1. Edit an old send message.
  2. Copy following to clipboard.
ASDSADSA
DA😆 🤫 😙 
SD
ASDSA
D
ASDAS
DSA
DSA
DAS
D
AS
D


😋 🤫 😋 😋 😋 
SADSA
D
ASD
SA
D
SADS
AD
ASDAS
DASASDSADSA
DA😆 🤫 😙 
SD
ASDSA
D
ASDAS
DSA
DSA
DAS
D
AS
D


😋 🤫 😋 😋 😋 
SADSA
D
ASD
SA
D
SADS
AD
ASDAS
DAS

  1. Replace the content on the edit message input with the copied content.
  2. Select everything on the edit composer.
  3. Paste via shortcuts.
  4. Notice that it takes a few secs to replace.
screen-2023-01-09_16.19.55.mp4

Cursor jumping or moving positions after picking emoji.

  1. Edit a message or draft a long message on the composer. (Use the content from the previous report).
  2. Place the cursor either before the center or after the center line.
  3. Now pick an emoji.
  4. Notice that the cursor does not remain in the same position even though the previous cursor position was in the view.
screen-2023-01-09_16.26.09.mp4

Compose text is not cleared after sending the message

bug1-web.mov

@s77rt
Copy link
Contributor Author

s77rt commented Apr 21, 2023

@hannojg That's a LIST. By the time we get to finish testing RN would have fixed the bug and Safari would be awesome 😂.

@s77rt
Copy link
Contributor Author

s77rt commented Apr 21, 2023

@hannojg

I am by now quite experiences with the weird bugs that can happen when using SetSelection

Can you please highlight some of those? or you are referring to those in the list?

Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes @s77rt and @mollfpr would you be able to go through all those tests?

The job price is already at $2000, if you think we should increase the reward for this work, let me know

@mountiny
Copy link
Contributor

Also there are some merge conflicts @s77rt

@mollfpr
Copy link
Contributor

mollfpr commented Apr 21, 2023

Thanks @mountiny! I’ll start testing after the conflict resolved.

@parasharrajat
Copy link
Member

We should test all the bugs from the list over this PR. I tried to capture as much as I could.

@s77rt
Copy link
Contributor Author

s77rt commented Apr 22, 2023

Resolved conflicts. Should be ready for test/review.

@hannojg
Copy link
Contributor

hannojg commented Apr 25, 2023

I am just loosely going through a few items of the list, @parasharrajat will probably do a full audit.
What's following is just a few bugs I have noticed

iOS: Enter multiline text, set the cursor to the top position, scroll down, insert emoji. Bug: text input doesn't scroll up

Simulator.Screen.Recording.-.iPhone.14.Pro.-.2023-04-25.at.15.49.37.mp4

@hannojg
Copy link
Contributor

hannojg commented Apr 25, 2023

iOS: Inserting an emoji by using the : moves the text input's content:

Simulator.Screen.Recording.-.iPhone.14.Pro.-.2023-04-25.at.15.59.28.mp4

@hannojg
Copy link
Contributor

hannojg commented Apr 25, 2023

Android: Selection is not reset after inserting an emoji

Screen.Recording.2023-04-25.at.16.02.46.mov

@s77rt
Copy link
Contributor Author

s77rt commented Apr 25, 2023

@hannojg Those bugs does not occur with traditional selection prop right? If so then we should probably fix the controlled selection bug instead

@mountiny
Copy link
Contributor

@mollfpr will you have time to go through the testing?

Whats the plan @s77rt regarding what Hanno pointed out?

@s77rt
Copy link
Contributor Author

s77rt commented Apr 25, 2023

@mountiny I was hoping that setSelection will magically work regression-free 😅. I have no idea how to fix the reported bugs on E/App, it's probably something that must escalates to RN. And if it's the case we should just fix the buggy selection prop. That bug is about 2-3 years so it may not be that straightforward.

@hannojg Do you have some comments on those bugs? Or what would you suggest in this case?

@mountiny
Copy link
Contributor

I see, I guess if we can clearly define some problem/ solution for that buggy selection issue, we could get some bigger job running 8k+ and someone will have to fix this in RN eventually :D

@parasharrajat
Copy link
Member

For now, we can close this PR and hold the issue on the main issue. We have been working on the main issue for quite sometime.

@mollfpr
Copy link
Contributor

mollfpr commented Apr 26, 2023

Selection is not reset after selecting an emoji
❌ Android
✅ iOS
✅ mWeb/Chrome
✅ mWeb/Safari
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

Broken emojis are rendered when picked from the picker in a series
✅ Android
✅ iOS
✅ mWeb/Chrome
✅ mWeb/Safari
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

Cursor position is broken
✅ Android
✅ iOS
✅ mWeb/Chrome
✅ mWeb/Safari
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

Adding emoji via EmojiPicker in replacement for multiple lines creates a delay before the composer height is calculated.
✅ Android
✅ iOS
✅ mWeb/Chrome
✅ mWeb/Safari
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

Selected Emoji is placed in the wrong position.
✅ Android
❌ iOS
✅ mWeb/Safari
✅ mWeb/Chrome
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

Pasting image does not send the attached text message.
✅ mWeb/Safari
✅ mWeb/Chrome
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

There is a delay before Composer expands the height on the web.
✅ mWeb/Safari
✅ mWeb/Chrome
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

Cursor position is not focused after picking a emoji in Composer.
✅ mWeb/Safar
✅ mWeb/Chrome
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

Content flashing while pasting.
✅ mWeb/Safari
✅ mWeb/Chrome
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

Edit Message: Copy-pasting is slow.
✅ mWeb/Safari
✅ mWeb/Chrome
❌ Safari - is not slower than the video step but slower than Chrome and Firefox
✅ Chrome
✅ Firefox
❌ Desktop - the same speed as Safari

Cursor jumping or moving positions after picking emoji.
✅ mWeb/Safari
✅ mWeb/Chrome
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

Compose text is not cleared after sending the message
✅ mWeb/Safari
✅ mWeb/Chrome
✅ Safari
✅ Chrome
✅ Firefox
✅ Desktop

@mollfpr
Copy link
Contributor

mollfpr commented Apr 26, 2023

Issue when executing Adding emoji via EmojiPicker in replacement for multiple lines creates a delay before the composer height is calculated.

Firefox - Adding emoji from the picker adding a bottom space inside the input.

  1. Focus on the input and open the emoji picker
  2. Select any emoji
  3. Observed the input bottom
Video
Screen.Recording.2023-04-26.at.11.35.06.mov

@mollfpr
Copy link
Contributor

mollfpr commented Apr 26, 2023

Issue when executing Selected Emoji is placed in the wrong position.

Android - The selection selects the previous letter and will select the previous text

  1. Type multiline text e.g.:
abcdefg abcdefg
abcdefg abcdefg
  1. Select the letter cde from the second line
  2. Open emoji picker and select an emoji
  3. Notice that the selection highlights the letter b, and the emoji added
  4. Do step 3 again
  5. Notice that the letter b is removed, and now the selection highlights the previous a
  6. Do step 3 again
  7. Notice that the letter a is removed, and the selection highlights space in the first line
  8. Do step 3 again
  9. Notice that the selection randomly highlights the letter of the second word from the first line with the emoji added
Video
Screen_Recording_20230426_150138_New.Expensify.mp4

Note: It's only reproduce when select 3 or more letter

@s77rt
Copy link
Contributor Author

s77rt commented Apr 26, 2023

@parasharrajat

For now, we can close this PR and hold the issue on the main issue. We have been working on the main issue for quite sometime.

I think the other issue is also following the setSelection approach.


@mollfpr Thanks for testing! The results are not that bad 😅 but Android selection issue is so concerning.

Can you please add a video for falling iOS - Selected Emoji is placed in the wrong position. That issue should be fixed, it would be strange if it's still reproducible.

@mollfpr
Copy link
Contributor

mollfpr commented Apr 26, 2023

@s77rt

Simulator.Screen.Recording.-.iPhone.14.-.2023-04-26.at.20.42.18.mp4

@parasharrajat
Copy link
Member

I think the other issue is also following the setSelection approach.

Yes. Based on your last comment, I thought we don't have a clear solution for moving forward. I suggested this as we have done extensive research and testing on the other issue. We are even exploring an upstream solution. Unless we have a different approach, we should not replicate the effort.

@s77rt
Copy link
Contributor Author

s77rt commented Apr 26, 2023

@mollfpr Ah this requires a selection range. Unfortunately both Android and iOS are buggy using selection range.

@s77rt
Copy link
Contributor Author

s77rt commented Apr 26, 2023

@parasharrajat Yes sadly we don't have a clear solution. I believe the other issue is only for Android. I think we should do what @mountiny proposed, raise a new clear P/S statement in an attempt to fix the buggy selection prop on RN for both Android and iOS.

@parasharrajat
Copy link
Member

The issue title is misleading. But all the issues are tested and perfected on each platform before we merge PRs. So I tested that PR on all platforms and make sure that it works. This pushed us to get a common solution for all platforms.

raise a new clear P/S statement in an attempt to fix the buggy selection prop on RN for both Android and iOS.

Sounds good.

@mountiny
Copy link
Contributor

There is not that many fails, but lets go with fixing this upstream. Anyone is willing to take this on? I will close this PR after

@s77rt
Copy link
Contributor Author

s77rt commented Apr 27, 2023

@mountiny I will but can I get a clarification on the plan, is it:

  1. Create 2 E/App issues (one for Android and other for iOS)
  2. Wait for proposals
  3. Fix the bugs

Or we will be taking this to RN first?

@hannojg
Copy link
Contributor

hannojg commented Apr 27, 2023

Hey, Hanno here from Margelo 👋 I am here to help find a solid fix for this issue, also in hindsight of my other PR #11684.

I am ready to make a proposal in some appropriate place, that will handle all the issues discussed.

I just want to note that the proposal likely won't be a upstream fix, as I figured that some of the problems are simply not fixable upstream (there exists no solution that is working generically enough, to suite all use cases with inputs in react native). My proposal would very likely include creating an own native component for TextInput on android (which will extend/inherit the existing react native's text input).
I just want to point out that if you set as a hard requirement that it should be fixed upstream, you are very likely to deadlock these issues, as -again - its not really possible to fix it upstream to serve all our use cases.

@mountiny
Copy link
Contributor

Alright, I think we could go deep haha but what I suggest then @hannojg is to create a P/S in the #expensify-open-source channel to explain what problems we are solving (this should be quite straightforward we have real bugs right here haha) and then in the threads also please dont forget to mention why its not fixable upstream ideally with backing of some exisitng tries from other people. I am sure some people will come and ask "are you sure" so adding osmething like this will help with it.

Would @s77rt and @hannojg be interested in working on this a bit together? Not sure how you like to work but there could be help with debugging nad testing for sure :) That do you think?

@s77rt
Copy link
Contributor Author

s77rt commented Apr 27, 2023

@hannojg I wouldn't be too surprised if the upstream fix is a lot of work but this was actually working on RN, so maybe we can just drop the "features" that broke it?

Also please note that we are looking to fix the buggy selection on iOS too (which would also require upstream changes)

I'm interested on working on this as @mountiny suggested, let me know what do you think

@mountiny
Copy link
Contributor

Can we move this discussion to Slack? I think that would be easier (the problem solution statement can be nailed later, I will close this Pr for now)

@mountiny mountiny closed this Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants