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 selection with autofocus when editor content is empty on startup #1595

Conversation

mmachatschek
Copy link
Contributor

@mmachatschek mmachatschek commented Jul 15, 2021

Fixes #1588

It seams that the selection of the anchor and head is not zero based.

When initializing an empty editor and setting the focus by clicking into the editor. The editor.state.selection will have:

selection: Object
- type: "text"
- anchor: 1
- head: 1

image

@mmachatschek
Copy link
Contributor Author

mmachatschek commented Jul 15, 2021

@hanspagel I couldn't find an appropriate test for this where I can put this, as the only error that happens without the fix is the one I describe in my issue.

But maybe a vue/react component could be added here: https://www.tiptap.dev/api/commands/focus

@hanspagel
Copy link
Contributor

Thanks! I think it was 0 for a reason, but I’m not sure. I need to check this before merging.

@BrianHung
Copy link
Contributor

I was digging through prosemirror-state, and the way selection is instantiated by default is by using Selection.atStart.

Using that function should give you a valid position or selection independent of the topNode (default doc might be doc -> paragraph, which gives offset of 1 but another doc could be doc -> blockquote -> paragraph which gives offset of 2).

@philippkuehn
Copy link
Contributor

Thanks for that hint @BrianHung! I was able to fix it with that patch so I’m closing this.

@mmachatschek mmachatschek deleted the issue/fix_autofocus_empty_content branch August 11, 2021 13:09
@mmachatschek
Copy link
Contributor Author

Thanks @BrianHung and @philippkuehn for fixing and looking into this 🙌

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.

Pasting into empty editor with autofocus adds newlines
4 participants