yjs / y-prosemirror

ProseMirror editor binding for Yjs
https://demos.yjs.dev/prosemirror/prosemirror.html
MIT License
328 stars 115 forks source link

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

Closed ocavue closed 2 months ago

ocavue commented 1 year ago

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:

Expected behavior

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

Screenshots

https://user-images.githubusercontent.com/24715727/191005843-46a93d9c-9e5e-4ffc-85cc-4f46a60a0294.mp4

Environment Information

Additional context

I created a PR https://github.com/yjs/y-prosemirror/pull/127 to resolve this issue.

HondryTravis commented 1 year ago

same question

totorofly commented 9 months ago

I also encountered a similar situation, but in my case, I wanted the cursor to automatically focus on the beginning of the document. However, every time, I noticed the cursor appearing at the end, and then I had to wait for some time after creating the editorView, using setTimeout, before executing the operation to move the cursor to the starting position. Was this issue resolved later?

dmonad commented 2 months ago

A fix is now available in y-prosemirror@1.2.6