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

Save note scroll position and restore when returning to note #2977

Merged
merged 6 commits into from
Sep 3, 2021

Conversation

sandymcfadden
Copy link
Contributor

@sandymcfadden sandymcfadden commented Aug 16, 2021

Fix

This will help fix #2951 and help fix #2861

Currently, if you have a longer note that needs to be scrolled when you leave the note and come back the scroll position is set back to the top. This includes if you are returning from a Markdown preview, or even if you change a setting that causes the editor to re-render.

This saves the line and scroll position of a note when you leave and then restores it when you come back. This is only saved for the current session.

Test

  1. Open a note which needs to be scrolled
  2. Scroll to a point in the note
  3. Leave the note
  4. Open the note again and ensure you are taken to the same spot in the note

Release

  • Updated to save the scroll position of a note so you can be restored when the note is viewed again

@sandymcfadden sandymcfadden added this to the 2.18.0 milestone Aug 16, 2021
@sandymcfadden sandymcfadden self-assigned this Aug 16, 2021
@sandymcfadden sandymcfadden marked this pull request as draft August 16, 2021 00:56
@sandymcfadden sandymcfadden changed the title WIP: Save note scroll position and restore when returning to note Save note scroll position and restore when returning to note Aug 16, 2021
@sandymcfadden sandymcfadden marked this pull request as ready for review August 16, 2021 14:46
Copy link
Member

@codebykat codebykat left a comment

Choose a reason for hiding this comment

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

Code looks good and mostly tests well. I noticed a few oddities that may or may not be fixable...

  • When you switch back to a note, it loads at the top and then jumps to the scroll position. I'm wondering if there's any way to get that to happen when we pass new content to the editor? Does it need the timeout? Can we move it higher up in the componentDidMount function (maybe even inside the bootTimer where we set the editor content)?

  • Switching into Markdown preview obviously doesn't know to scroll to the same part of the note, which is unfortunate. I am not sure there's any easy way to do that but we might want to consider it in future.

  • Left an in-line comment about revealLineInCenter which I think has the potential to cause additional jumpiness.

if (position) {
this.editor?.revealLineInCenter(position.line);
window.setTimeout(() => {
this.editor?.setScrollPosition({
Copy link
Member

Choose a reason for hiding this comment

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

  • I'm curious why we're calling both revealLineInCenter and setScrollPosition? revealLineInCenter will scroll the note to the position requested. Similarly, do we really need to save both the position and the scrollTop value?
  • Did you test this without the timeout? It seems odd that we need it for setScrollPosition but not for revealLineInCenter.
  • See also line 1106 where we use a promise that immediately resolves to update the cursor position; perhaps that would be faster than the 200ms timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, there really isn't a need to keep both and call both. In this case I think keeping and setting the scroll position is the best way. The reason for this is the selected line may not match where you are scrolled to. Also if the line is long and wraps it will really only bring you to the top of that line and not where you were scrolled to.

The downside with scroll position and something I haven't accounted for yet is if the window size changes from the time when the position is saved and when you open the note again. There is a good chance it doesn't take you to the same spot in the note in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated to use only scroll position now and clear the saved positions of things are resized as I think it is likely better to go to the top of a note rather than a random place if the size changes.

@sandymcfadden
Copy link
Contributor Author

Thanks @codebykat!

When you switch back to a note, it loads at the top and then jumps to the scroll position. I'm wondering if there's any way to get that to happen when we pass new content to the editor? Does it need the timeout? Can we move it higher up in the componentDidMount function (maybe even inside the bootTimer where we set the editor content)?

I've moved it inside the bootTimer as you suggested and it seems there we don't need the timeout at all. You still get a bit of a jump, but I think the content has to load into the editor before you scroll so I'm not sure there is a way to eliminate it completely.

Switching into Markdown preview obviously doesn't know to scroll to the same part of the note, which is unfortunate. I am not sure there's any easy way to do that but we might want to consider it in future.

Yes, this would require more work and I think a different PR. The same scroll position wouldn't work in preview especially if you have things like markdown images or something that are going to take up a lot of space.

Saving the line I don't think it will really work because of what I mentioned in another comment about long lines that wrap. So I think to do that well would require a fair bit more work.

@sandymcfadden sandymcfadden modified the milestones: 2.18.0, 2.19.0 Aug 22, 2021
@codebykat codebykat self-requested a review September 1, 2021 05:48
Copy link
Member

@codebykat codebykat left a comment

Choose a reason for hiding this comment

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

Looks good, I think this is working better without the timeout (or it's a placebo effect).

Nice thinking on the resize. I suppose if we wanted to try to make it more bulletproof, we could store both the scroll position and the line, and use revealLineInCenter instead of the scroll position after a resize... but it's probably an edge case and I'm not sure it would work that well anyhow.

@sandymcfadden sandymcfadden merged commit 5f81b71 into develop Sep 3, 2021
@sandymcfadden sandymcfadden deleted the add/save-note-scroll-position branch September 3, 2021 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants