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

Preserve cursor position in editors #3485

Closed
wants to merge 12 commits into from

Conversation

Gazook89
Copy link
Collaborator

This PR modifies the editor so that it preserves the cursor position in each the Brew and Style editors. It saves the locations in localStorage, and loads the related position when switching between editors and across page loads.

One side effect is that it does focus the Brew editor on page load. I don't think that is necessarily the worst thing, but I'm not sure that this change should be doing that. But until there is a more focused effort on how focus moves about the page, I think it's fine to leave as-is.

Another thing I thought about while working on this: it might be nice if when you open an editor (changing view or loading the page), the cursor did one or two stronger flashes so you can more easily spot where your cursor is when you first jump into a view. Not likely going in this PR, or any, but mentioning it in case anyone has further thoughts.

I feel like the commit messages (and expandable descriptions) are clear enough. i did try JSDoc annotations as well.

Setter takes a 'view', grabs the current cursor position, and dumps it into localStorage with a related key.

Getter takes a 'view', loads the related localStorage key, and *if* an editor instance is focused, sets the cursor position in it.  The CM instance must have focus on it already-- related code is in next commit.
When the component is mounted, it focuses the editor and the getter method to set the cursor.  It checks for a CM instance first, to avoid issues with properties editor.

When component is unmounted, it saves the cursor position.
When the view/editor changes (text to style etc), the current view cursor is saved, and the new view cursor is loaded.
@Gazook89 Gazook89 added UI/UX User Interface, user experience P3 - low priority Obscure tweak or fix for a single user 🔍 R0 - Needs first review 👀 PR ready but has not been reviewed labels May 18, 2024
@5e-Cleric
Copy link
Member

Need a deployment to test

@dbolack-ab
Copy link
Collaborator

One side effect is that it does focus the Brew editor on page load. I don't think that is necessarily the worst thing, but I'm not sure that this change should be doing that. But until there is a more focused effort on how focus moves about the page, I think it's fine to leave as-is.

This comes across as a feature, not a bug, to me.

Another thing I thought about while working on this: it might be nice if when you open an editor (changing view or loading the page), the cursor did one or two stronger flashes so you can more easily spot where your cursor is when you first jump into a view. Not likely going in this PR, or any, but mentioning it in case anyone has further thoughts.

This seems like a good idea for a future PR.

@5e-Cleric
Copy link
Member

Another thing I thought about while working on this: it might be nice if when you open an editor (changing view or loading the page), the cursor did one or two stronger flashes so you can more easily spot where your cursor is when you first jump into a view. Not likely going in this PR, or any, but mentioning it in case anyone has further thoughts.

In CodeMirror, as far as i know, the cursor is an html div element:

image

Therefore it would be fairly simple to add styling to make this possible, added and then removed by js.

@Gazook89
Copy link
Collaborator Author

Yeah, not hard, I was already messing with it but still going to leave it for another PR where I’ll present some options and solicit other ideas.

@5e-Cleric
Copy link
Member

image

Not seeing any localstorage item popping up, neither across loads.

@dbolack-ab
Copy link
Collaborator

Duplidcumenting here from chat:

Seems to work after I had my session loaded. Though I'm not sure if it didn't trigger right initially? I didn't see any local storage added. I switched to styles and it crashed. Reloaded to see if I could duplicate it and it then had the values. Worked fine. Reloaded. Deleted the local storage values, switched panes, crash. Looks like it doesn't gracefully handle the values not being in local storage.

@5e-Cleric 5e-Cleric temporarily deployed to homebrewery-pr-3485 May 20, 2024 19:51 Inactive
Copy link
Collaborator

@dbolack-ab dbolack-ab left a comment

Choose a reason for hiding this comment

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

This looks good to me.

@5e-Cleric
Copy link
Member

5e-Cleric commented Sep 1, 2024

Does this work for either of you? perhaps i am testing wrong

clicking the editor doesn't seem to generate any localstorage element, neither does reloading.

It just loads the cursor in the first position.

Add a 'beforeunload' event listener to watch for the user leaving the page so that the cursor position can be saved between page reloads.  For reliability, the setStoredCursorPosition method now returns null, which when run through the beforeunload listener triggers it, without opening a window prompt.
Just updating the old ref syntax to thew newer createRef syntax.
@Gazook89
Copy link
Collaborator Author

Gazook89 commented Sep 2, 2024

I believe this should work now, and is updated to the master branch as well. I should give it another once-over but I'm headed to bed and then busy day tomorrow. If someone wants to review it and find the bumps before I do, please do.

@Gazook89 Gazook89 requested review from dbolack-ab and 5e-Cleric and removed request for ericscheid and calculuschild September 2, 2024 04:27
@5e-Cleric
Copy link
Member

Okay, seems to work on homepage fine. Testing in \new doesn't seem to work, not sure why, and the metadata editor makes the PR crash

@Gazook89
Copy link
Collaborator Author

Gazook89 commented Sep 2, 2024

Ah okay. New makes sense because there isn’t a style editor. Obviously I didn’t test it well enough. Will fix

Edit: am dumb, there is a style editor in /new/. Not sure why it wasn't working there.

@calculuschild calculuschild temporarily deployed to homebrewery-pr-3485 September 2, 2024 14:47 Inactive
@Gazook89
Copy link
Collaborator Author

Gazook89 commented Sep 2, 2024

Pushed a fix that checks if there is an editor instance before storing cursor position. So should be fixed now.

Please keep breaking it.

@dbolack-ab
Copy link
Collaborator

Two 'issues' I'm seeing now that I didn't notice before.

  1. When you fold-all and unfold-all it puts the cursor at the end of the doc (at least in my short test doc )
  2. If you have multiple documents open, it uses the same cursor positions. I think this needs to track per editId

@Gazook89
Copy link
Collaborator Author

Gazook89 commented Sep 2, 2024

Number 1 seems tricky.

Number 2: how many ids do we want to be concurrently tracking cursor position across? 10 most recent documents?

@calculuschild
Copy link
Member

calculuschild commented Sep 2, 2024

We do have some logic already that saves scroll position when switching between the text and style tab. I haven't looked at this code yet, but are we making sure not to re-invent what we have already, or can the two be made to share some logic since thet seem to be related?

@Gazook89
Copy link
Collaborator Author

Gazook89 commented Sep 2, 2024

We do have some logic already that saves scroll position when switching between the text and style tab. I haven't looked at this code yet, but are we making sure not to re-invent what we have already, or can the two be made to share some logic since thet seem to be related?

Worse!

image

It actually fights the scroll saved scroll position. When you switch back, it sets focus in the editor which means it pulls the cursor into view. So if I set the cursor near the end of a document, and then scroll up to the top, and then switch to Style editor and back, the scroll position will now be further down the document such that the cursor is just in view.

So, might have to rethink that. Good call out.

@Gazook89
Copy link
Collaborator Author

Gazook89 commented Sep 3, 2024

I'm going to close this PR at this point, though it will continue to live locally on my machine (not in master, just as a forgotten branch).

I don't think it's tremendously useful, and there are some considerations to be considered first:

  • When switching to an editor, should prior scroll position take precedence over prior cursor position, if you can't have both?
  • Can both be preserved, where the cursor position is preserved even if it's outside the current scroll view?

And as a reminder to myself, calculuschild pointed out this older merged PR which made me think that possibly the cursor position is already preserved (along with scroll position), but that switching documents/editors just isn't setting the focus in the editor after the switch. I'll play with that a little later, locally.

@Gazook89 Gazook89 closed this Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 - low priority Obscure tweak or fix for a single user 🔍 R0 - Needs first review 👀 PR ready but has not been reviewed UI/UX User Interface, user experience
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants