yjs / y-prosemirror

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

Fix `cursorStateField` bug #159

Open andrictham opened 1 month ago

andrictham commented 1 month ago

Summary

This PR fixes an issue with the cursorStateField param that’s passed into yCursorPlugin.

Setting cursorStateField enables awareness state to be namespaced, so that cursors from different subdocuments syncing over the same provider will be able to read and write their own cursor state.

However, it is currently broken. Setting cursorStateField doesn’t work as intended: the cursor plugin writes cursor state to the correct field, but doesn’t read back from it when rendering cursors.

This fixes #86

Current Behavior

If a custom value of cursorStateField is set, the cursor plugin correctly uses it to set awareness state: https://github.com/yjs/y-prosemirror/blob/fd08cf3c7c307c1446178765baa94603a3a8416a/src/plugins/cursor-plugin.js#L239-L242

However, in the createDecorations function, the value aw.cursor is still used. This is reading a hardcoded field with the name "cursor" from the awareness state. https://github.com/yjs/y-prosemirror/blob/fd08cf3c7c307c1446178765baa94603a3a8416a/src/plugins/cursor-plugin.js#L101-L112

This results in cursors not rendering properly if you set cursorStateField to a custom value.

Proposed Changes

Tests

andrictham commented 1 week ago

@dmonad I wonder if it’s possible to get this merged, or if not, is there something I’m misunderstanding about how cursorStateField is supposed to work, or something I'm missing in my PR?

I’m reliant on this fix so I can get subdocuments working in my app.

Let me know how I can help 😀