diff --git a/src/plugins/cursor-plugin.js b/src/plugins/cursor-plugin.js index fd79f14c..64c34481 100644 --- a/src/plugins/cursor-plugin.js +++ b/src/plugins/cursor-plugin.js @@ -63,6 +63,7 @@ const rxValidColor = /^#[0-9a-fA-F]{6}$/ * @param {function(number, number, any):boolean} awarenessFilter * @param {function({ name: string, color: string }):Element} createCursor * @param {function({ name: string, color: string }):import('prosemirror-view').DecorationAttrs} createSelection + * @param {string} cursorStateField * @return {any} DecorationSet */ export const createDecorations = ( @@ -70,7 +71,8 @@ export const createDecorations = ( awareness, awarenessFilter, createCursor, - createSelection + createSelection, + cursorStateField ) => { const ystate = ySyncPluginKey.getState(state) const y = ystate.doc @@ -87,8 +89,8 @@ export const createDecorations = ( return } - if (aw.cursor != null) { - const user = aw.user || {} + if (aw[cursorStateField] != null) { + const user = aw.user || {}; if (user.color == null) { user.color = '#ffa500' } else if (!rxValidColor.test(user.color)) { @@ -101,13 +103,13 @@ export const createDecorations = ( let anchor = relativePositionToAbsolutePosition( y, ystate.type, - Y.createRelativePositionFromJSON(aw.cursor.anchor), + Y.createRelativePositionFromJSON(aw[cursorStateField].anchor), ystate.binding.mapping - ) + ); let head = relativePositionToAbsolutePosition( y, ystate.type, - Y.createRelativePositionFromJSON(aw.cursor.head), + Y.createRelativePositionFromJSON(aw[cursorStateField].head), ystate.binding.mapping ) if (anchor !== null && head !== null) { @@ -167,7 +169,8 @@ export const yCursorPlugin = ( awareness, awarenessStateFilter, cursorBuilder, - selectionBuilder + selectionBuilder, + cursorStateField ) }, apply (tr, prevState, _oldState, newState) { @@ -182,7 +185,8 @@ export const yCursorPlugin = ( awareness, awarenessStateFilter, cursorBuilder, - selectionBuilder + selectionBuilder, + cursorStateField ) } return prevState.map(tr.mapping, tr.doc) @@ -223,13 +227,13 @@ export const yCursorPlugin = ( ystate.binding.mapping ) if ( - current.cursor == null || + current[cursorStateField] == null || !Y.compareRelativePositions( - Y.createRelativePositionFromJSON(current.cursor.anchor), + Y.createRelativePositionFromJSON(current[cursorStateField].anchor), anchor ) || !Y.compareRelativePositions( - Y.createRelativePositionFromJSON(current.cursor.head), + Y.createRelativePositionFromJSON(current[cursorStateField].head), head ) ) { @@ -239,11 +243,11 @@ export const yCursorPlugin = ( }) } } else if ( - current.cursor != null && + current[cursorStateField] != null && relativePositionToAbsolutePosition( ystate.doc, ystate.type, - Y.createRelativePositionFromJSON(current.cursor.anchor), + Y.createRelativePositionFromJSON(current[cursorStateField].anchor), ystate.binding.mapping ) !== null ) {