EditorSelection.cursor() ignores assoc parameter on initial view creation #1656
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Describe the issue
Summary
When creating an
EditorViewwith an initial selection that has a non-zeroassocvalue, the cursor association is not visually respected at soft-wrap boundaries. The cursor appears on the wrong visual line until the selection is programmatically updated after view creation.Reproduction
https://github.com/rubywwwilde/codemirror-assoc-bug
Expected Behavior
With assoc=-1 at a soft-wrap boundary, the cursor should appear at the end of the first visual line.
Actual Behavior
The cursor appears at the start of the second visual line, as if assoc=1.
Root Cause
In the enforceCursorAssoc condition:
This only triggers when update.selectionSet is true. On initial view creation, the selection comes from the initial state rather than from a transaction that "sets" it, so selectionSet is false and the assoc enforcement is skipped.
Proposed Fix
Include update.focusChanged in the condition so that cursor association is enforced when the view first receives focus:
This ensures that when a user focuses a freshly-created view (or refocuses after blur), the cursor association is correctly applied.
Environment
Browser and platform
Mac, Chrome
Reproduction link
https://github.com/rubywwwilde/codemirror-assoc-bug
I hope I did everything right in pull request! There are some failing tests, but as it appears, they are not caused by my change.
Figuring out this bug took me almost the whole day, though I liked exploring CodeMirror in this way.
This issue is important for me, because my app restores selection on page reload and it's important for me to restore the selection correctly.
That looks like precisely the correct way to fix this. Thanks for figuring that out.
How are you running the tests, that you see failures? They are all passing on my machine on both Chrome and Firefox, but yeah, browsers and platforms may influence that, so I'd be interested to know what's going on.
I ran tests with
npm run testinsideviewrepo and got 6 failing ones.Which platform are you on?
I'm running on mac m4 pro with Sequoia 15.5.
@marijnh thank you for such a fantastic editor! It's very extensible and allows me to build a block-based rich text editor easily, focusing fully on the higher-level features I care about. I initialize CodeMirror when the user clicks or focuses a block in a different way, and this approach has been very performant for me.
Thanks! I was able to reproduce the MacOS Chrome issues with the tests (all related to obscure font handling differences), and work around them.