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

fix: scroll to right position on text insertion #9062

Merged
merged 6 commits into from
Jul 5, 2022

Conversation

b1tjoy
Copy link
Contributor

@b1tjoy b1tjoy commented May 18, 2022

Details

Text Insertion point goes out of the sight on typing in the middle of the paragraph. This PR fix the issue. And in order to fix #8663, we have to delete one line of code, which was add to fix #7351 previous, so this PR alse fix that one.

Why move textInput.blur() and textInput.focus() from ReportActionCompose.js to Composer/index.js?

Because these two lines was add to fix #7351, but the issue only affects Web, mWeb and Desktop platform, and only in pasting text copied from HTML page scenario, if we keep the code in ReportActionCompose.js, it will cause other issues, such as:

  1. Virtual keyboard keeps collapse and pop out on Android native when we type text in input field
  2. Input field keeps flash on Web when we type text

So I moved these codes to Composer/index.js:handlePasteHTML().

Fixed Issues

$ #8663

Tests

Test case for #8663:

  1. Open Expensify account
  2. Select any chat room in your list
  3. Write multiple lines of text, 20+ lines for web, 10+ lines for mWeb
  4. Type some words on the 4th-5th line
  5. Verify text insertion point does not move out of sight

Test case for #7351:

  1. Open Expensify account
  2. Select any chat room in your list
  3. Paste a large paragraph copy from HTML page, 20+ lines for web, 10+ lines for mWeb
  4. Verify the scroll bar scroll down to the end of input field and text insertion point remains in sight

Test case for paste on selection, a more complex scenario:

  1. Open Expensify account
  2. Select any chat room in your list
  3. Write multiple lines of text, 20+ lines for web, 10+ lines for mWeb
  4. Copy a large parapragh from HTML page
  5. Select some text from chat input field and paste
  6. Verify the text insertion point remains in sight
  • Verify that no errors appear in the JS console

PR Review Checklist

Contributor (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 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 included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • 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 was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team 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
  • 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 */
    • Any functional components have the displayName property
    • 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
  • 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 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.

PR Reviewer Checklist

  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible 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 checked that screenshots or videos are included for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • 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 verified proper code patterns were followed (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 was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team 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 verified that this PR follows the guidelines as stated in the Review Guidelines
  • 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
  • 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 */
    • Any functional components have the displayName property
    • 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 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 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.

QA Steps

Test case for #8663:

  1. Open Expensify account
  2. Select any chat room in your list
  3. Write multiple lines of text, 20+ lines for web, 10+ lines for mWeb
  4. Type some words on the 4th-5th line
  5. Verify text insertion point does not move out of sight

Test case for #7351:

  1. Open Expensify account
  2. Select any chat room in your list
  3. Paste a large paragraph copy from HTML page, 20+ lines for web, 10+ lines for mWeb
  4. Verify the scroll bar scroll down to the end of input field and text insertion point remains in sight

Test case for paste on selection, a more complex scenario:

  1. Open Expensify account
  2. Select any chat room in your list
  3. Write multiple lines of text, 20+ lines for web, 10+ lines for mWeb
  4. Copy a large parapragh from HTML page
  5. Select some text from chat input field and paste
  6. Verify the text insertion point remains in sight
  • Verify that no errors appear in the JS console

Screenshots

Web

Test case for #8663

Screen.Recording.2022-05-22.at.10.49.11-MacOS-Chrome-1.mp4

Test case for #7351

Screen.Recording.2022-05-22.at.10.50.23-MacOS-Chrome-2.mp4

Test case for paste on selection, a more complex scenario

Screen.Recording.2022-05-22.at.10.52.02-MacOS-Chrome-3.mp4

Mobile Web

Test case for #8663 Android/Chrome

Screenshot-8663-Android-chrome-1.mp4

Test case for #7351 Android/Chrome

Screenshot-8663-Android-chrome-2.mp4

Test case for paste on selection, a more complex scenario Android/Chrome

Screenshot-8663-Android-chrome-3.mp4

Test case for #8663 iOS/Safari

Simulator.Screen.Recording.-.iPhone.13.-.2022-05-22.at.11.37.26-iOS-safari-1.mp4

Test case for #7351 iOS/Safari

Simulator.Screen.Recording.-.iPhone.13.-.2022-05-22.at.11.39.18-iOS-safari-2.mp4

Test case for paste on selection, a more complex scenario iOS/Safari

Simulator.Screen.Recording.-.iPhone.13.-.2022-05-22.at.11.40.46-iOS-safari-3.mp4

Desktop

Test case for #8663

Screen.Recording.2022-05-22.at.10.55.34-MacOS-Desktop-1.mp4

Test case for #7351

Screen.Recording.2022-05-22.at.10.58.01-MacOS-Desktop-2.mp4

Test case for paste on selection, a more complex scenario

Screen.Recording.2022-05-22.at.10.59.00-MacOS-Desktop-3.mp4

iOS

Test case for #8663

Simulator.Screen.Recording.-.iPhone.13.-.2022-05-22.at.11.06.48-iOS-native-1.mp4

Test case for #7351

Simulator.Screen.Recording.-.iPhone.13.-.2022-05-22.at.11.10.35-iOS-native-2.mp4

Test case for paste on selection, a more complex scenario

Simulator.Screen.Recording.-.iPhone.13.-.2022-05-22.at.11.12.47-iOS-native-3.mp4

Android

Test case for #8663

Screenshot-20220522084200-Android-native-1.mp4

Test case for #7351

Screenshot-20220522084700-Android-native-2.mp4

Test case for paste on selection, a more complex scenario

Screenshot-20220522085300-Android-native-3.mp4

@github-actions
Copy link
Contributor

github-actions bot commented May 18, 2022

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@b1tjoy b1tjoy marked this pull request as ready for review May 22, 2022 14:45
@b1tjoy b1tjoy requested a review from a team as a code owner May 22, 2022 14:45
@melvin-bot melvin-bot bot requested review from parasharrajat and pecanoro and removed request for a team May 22, 2022 14:45
@b1tjoy
Copy link
Contributor Author

b1tjoy commented May 22, 2022

I have read the CLA Document and I hereby sign the CLA

Copy link
Member

@parasharrajat parasharrajat 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 detailed PR.

@@ -244,6 +244,8 @@ class Composer extends React.Component {
try {
document.execCommand('insertText', false, markdownText);
this.updateNumberOfLines();
this.textInput.blur();
Copy link
Member

Choose a reason for hiding this comment

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

A comment will be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment added.

@parasharrajat
Copy link
Member

The review of this PR will be delayed. I need to run a few tests. The current solution is also a hack to fix the cursor. I will try to find a better solution to the problem.

@b1tjoy
Copy link
Contributor Author

b1tjoy commented May 25, 2022

The review of this PR will be delayed. I need to run a few tests. The current solution is also a hack to fix the cursor. I will try to find a better solution to the problem.

@parasharrajat I agree with you, if we use a hack to fix one issue at the current moment, it may lead to another issue in the future. Always find the root cause.

First I thought it was related to react-native-web, but after some digging, it may be problem with Chrome. I write snippets with basic JS to show how document.execCommand('insertText') works:

Text Insertion Example

If you run with Chrome, when you paste a large paragraph, the text insertion point goes out of sight. But if you run the example with Firefox, everything works fine.

@parasharrajat
Copy link
Member

Thanks for waiting. I will try to steal some time from my schedule to do some research on this issue.

@parasharrajat
Copy link
Member

parasharrajat commented Jun 5, 2022

No update yet. But I will share some update very soon. On my main list.

@parasharrajat
Copy link
Member

@b1tjoy Thank you for your patience. I was looking into your other proposals. And it seems that this #8663 (comment) could be the next potential solution.

You mentioned that this is working fine. Was there any issues with it?

@b1tjoy
Copy link
Contributor Author

b1tjoy commented Jun 11, 2022

@parasharrajat When we select text from bottom to top, then paste the content copy from html page, the text insertion point moves out of sight.

Screencast.from.06-11-2022.11.49.27.PM.mp4

@parasharrajat
Copy link
Member

Ok, I am trying to determine the expected behavior when we paste text in textArea. There are a few case possible for pasting. I will try to list them down and test your solution over it.

@b1tjoy
Copy link
Contributor Author

b1tjoy commented Jun 21, 2022

Ok, I am trying to determine the expected behavior when we paste text in textArea. There are a few case possible for pasting. I will try to list them down and test your solution over it.

@parasharrajat Possible cases for pasting I thought of:

  1. textArea is empty, paste text more than 20 lines
  2. textArea not empty, no selection in textArea, paste in the beginning, middle or end of textArea
  3. textArea not empty, select partial text from top to bottom in the beginning, middle or end of textArea, then paste
  4. textArea not empty, select partial text from bottom to top in the beginning, middle or end of textArea, then paste

I don't want to be bothersome and I know you are pretty busy, could you please take some time to review this PR for me? As a new contributor, I am not allowed to work on other issues for now, although I have submitted good proposal. Thanks a lot! :)

@parasharrajat
Copy link
Member

Sorry for the delay. I just kind of lost this one. Adding it to my list now. I will get to the bottom of it in a day or two.

Anyways, you should be good for new tasks. You can ping again on the same issue.

@parasharrajat
Copy link
Member

Ok, I tested your #8663 (comment) proposal and it seems to be working. Could you do the following and run a few tests? Otherwise, I can go with the existing solution.

  1. Add a state update callback as second param and add these statements to the callback.
 const heightToBottom = this.textInput.scrollHeight - paddingTopAndBottom - this.textInput.scrollTop;
                this.textInput.scrollTop = this.textInput.scrollHeight - paddingTopAndBottom - heightToBottom;

Now run your tests.

@parasharrajat
Copy link
Member

Also, please merge main.

@b1tjoy
Copy link
Contributor Author

b1tjoy commented Jun 26, 2022

I don't think it's going to work. The main idea of #8663 (comment) is to store the height from the insertion point to the last line of textArea before document.execCommand('insertText'), after text insertion we can calculate scrollTop by the stored value and manually scroll the textArea to the appropriate position. This works fine for the following cases:

  1. textArea is empty, paste text more than 20 lines
  2. textArea not empty, no selection in textArea, paste in the beginning, middle or end of textArea
  3. textArea not empty, select partial text from top to bottom in the beginning, middle or end of textArea, then paste

If we have 40 lines text for example, line height is 20, maxLines = 16, select from line 11 to 30, and paste 30 lines text:

Before insert:
scrollHeight = 800
selection lines (select from top to bottom) = [11, 30]
insertion point line = 30
visible area lines = [15, 30]
upper lines not visible = [1, 14]
lower lines not visible = [31, 40]
scrollTop = 14 * 20 = 280
heightToBottom = 800 - 280 = 520

After insert:
scrollHeight = 1000
scrollTop = 1000 - 520 = 480 = 24 * 20
upper lines not visible = [1, 24]
visible area lines = [25, 40]
lower lines not visible = [41, 50]
insertion point line = 40 (because we replace line 11-30 with 30 lines text)

Insertion point is in the visible area and everything works fine.

But in some edge case it doesn't work:

  • textArea not empty, select partial text from bottom to top (more than 16 lines and make scroll bar goes backward) in textArea, then paste

If we have 40 lines text for example, line height is 20, maxLines = 16, select from line 30 to 11, and paste 30 lines text:

Before insert:
scrollHeight = 800
selection lines (select from bottom to top) = [30, 11]
insertion point line = 30
visible area lines = [11, 26]
upper lines not visible = [1, 10]
lower lines not visible = [27, 40]
scrollTop = 10 * 20 = 200
heightToBottom = 800 - 200 = 600

After insert:
scrollHeight = 1000
scrollTop = 1000 - 600 = 400 = 20 * 20
upper lines not visible = [1, 20]
visible area lines = [21, 36]
lower lines not visible = [37, 50]
insertion point line = 40 (because we replace line 11-30 with 30 lines text)

Insertion point is not in the visible area.

I think we can combine these two proposals, use the calculation one to handle regular case, and use blur() & focus() one to handle select backward case. We can add a variable to track if user scroll backward, if user scroll backward and selection is not empty, call blur() & focus() to keep insertion point insight, otherwise calculate scrollTop and manually scroll to appropriate position. How about that?

@parasharrajat
Copy link
Member

A slight difference from your proposal is that I am not tracking the scroll before paste. Instead, I am relying on the reset of numberOfLines to 1.

Anyways, I see your analysis. Testing the current solution in this PR. I would say that if are not able to set a manual scroll position then this solution is fine. I will open the discussion on slack to get more eyes before moving ahead with the PR.

It would be done by tomorrow.

@parasharrajat
Copy link
Member

Meanwhile please merge main into this PR.

@b1tjoy
Copy link
Contributor Author

b1tjoy commented Jul 1, 2022

A slight difference from your proposal is that I am not tracking the scroll before paste. Instead, I am relying on the reset of numberOfLines to 1.

@parasharrajat After run a few tests, I think that reset numberOfLines to 1 does not make textarea scroll to appropriate position. Do you get the same result?

@parasharrajat
Copy link
Member

Yeah, I saw that the current solution of the PR has better results. I will open the discussion today.

Copy link
Member

@parasharrajat parasharrajat left a comment

Choose a reason for hiding this comment

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

@b1tjoy Please merge main into this.

@@ -239,6 +239,10 @@ class Composer extends React.Component {
try {
document.execCommand('insertText', false, markdownText);
this.updateNumberOfLines();
// Manually make text input scroll to appropriate position, otherwise the text insertion
// point will goes out of sight when we paste a large paragraph on Chrome or Desktop
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// point will goes out of sight when we paste a large paragraph on Chrome or Desktop
// Pointer will go out of sight when a large paragraph is pasted on the web. Refocusing the input keeps the cursor in view.

@@ -239,6 +239,10 @@ class Composer extends React.Component {
try {
document.execCommand('insertText', false, markdownText);
this.updateNumberOfLines();
// Manually make text input scroll to appropriate position, otherwise the text insertion
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Manually make text input scroll to appropriate position, otherwise the text insertion

@b1tjoy b1tjoy requested a review from parasharrajat July 5, 2022 14:45
Copy link
Member

@parasharrajat parasharrajat left a comment

Choose a reason for hiding this comment

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

LGTM.

cc: @pecanoro

PR Reviewer Checklist

  • I verified the PR has a small number of commits behind main
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the testing environment is mentioned in the test steps
  • I verified testing steps cover success & fail scenarios (if applicable)
  • I checked that screenshots or videos are included for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • I verified there are no console errors related to changes in this PR
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified comments were added when the code was not self explanatory
    • I verified any copy / text shown in the product was added in all src/languages/* files (if applicable)
    • I verified proper naming convention for platform-specific files was followed (if applicable)
    • I verified style guidelines were followed
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components are not impacted by changes in this PR (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 the UI performance was not affected (the performance is the same than main branch)
  • If a new component is created I verified that a similar component doesn't exist in the codebase

@pecanoro pecanoro merged commit 4774898 into Expensify:main Jul 5, 2022
@OSBotify
Copy link
Contributor

OSBotify commented Jul 5, 2022

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

OSBotify commented Jul 8, 2022

🚀 Deployed to staging by @pecanoro in version: 1.1.81-0 🚀

platform result
🤖 android 🤖 failure ❌
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 failure ❌

@OSBotify
Copy link
Contributor

🚀 Deployed to production by @roryabraham in version: 1.1.82-5 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants