Skip to content

Y-ProseMirror always set the caret at the end of the document after initialize the editor #126

@ocavue

Description

@ocavue

Checklist

Describe the bug

I want to initialize the editor with a particular selection position (e.g. at the start of the document), but y-prosemirror always changes the initial selection to the end of the document.

ProseMirror allows me to set the initial selection by providing an initial editor state with a selection. The code is shown below:

  const prosemirrorView = new EditorView(editor, {
    state: EditorState.create({
      doc,
      selection, // <-- here is my initial selection
      schema,
      plugins: [...]
    })
  })

Even if the initial selection is not implicitly passed, the default selection is at the start of the document.

When I use ySyncPlugin, _forceRerender will replace the content of the whole document, which make the selection at the end of the document.

To Reproduce

I prepare a simple example based on the demo/ example in this repo. You can find the code here.

$ git clone https://github.com/ocavue/y-prosemirror -b ocavue/init_selection
$ cd y-prosemirror
$ npm install 
$ npm run start 

Go to http://127.0.0.1:8081/demo/prosemirror.html. The editor should be focused and the caret is at the end of the document.

If ENABLE_YJS in demo/prosemirror.js is set as false, then the y-prosemirror in the heading will be selected after a reload.

Notice that in this example, to set the initial selection, I have to pass the initial document with two different ways in the same time:

  • pass the doc to EditorState.
  • pass the yDoc to ySyncPlugin.

Expected behavior

I can set the initial selection when using y-prosemirror.

Screenshots

yjs-selection.mp4

Environment Information

  • Browser: Chrome version 105
  • Yjs version and the versions of the y-* modules you are using
    • y-prosemirror@1.1.3
    • yjs@13.5.38

Additional context

I created a PR #127 to resolve this issue.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions