yorkie-team / codepair

Build your own AI-powered collaborative markdown editor in just 5 minutes
https://codepair.yorkie.dev
Apache License 2.0
49 stars 19 forks source link

Issues with cursor positioning and scroll behavior during Snapshot Events in CodeMirror #402

Open devleejb opened 2 weeks ago

devleejb commented 2 weeks ago

What happened: When a Snapshot Event occurs in CodeMirror, it overwrites the content within the editor with Snapshot data and moves the cursor based on Presence. However, several issues are observed:

https://github.com/user-attachments/assets/6145858b-e3ce-41b2-a9df-aa66e1d57327

  1. While dragging with the mouse, if a Snapshot Event occurs, the starting point of the drag moves to the topmost position.
  2. If a Snapshot Event occurs during text input, there are intermittent instances where the scroll jumps to the top and then returns to the current position.

It seems that this issue is related to the moment the code is executed, because when dragging is done via keyboard, the problem does not occur. The following code snippet appears to be the culprit:

view.dispatch({
    changes: { from: 0, to: view.state.doc.length, insert: text.toString() },
    annotations: [Transaction.remote.of(true)],
    selection: {
        anchor: ...,
        head: ...,
    }
});

What you expected to happen: The cursor should remain at the correct position during and after the Snapshot Event, without any scroll jumps or unexpected behavior.

How to reproduce it (as minimally and precisely as possible):

  1. Start dragging content in CodeMirror.
  2. Trigger a Snapshot Event during the drag action. (We can trigger snapshot event adjusting snapshot-threshold)
  3. Observe the cursor and scroll behavior.

Anything else we need to know?:

Environment:

devleejb commented 1 day ago

Related to https://github.com/codemirror/dev/issues/1473.