From 6d2d20c7586ba796660ee1762bdbb203254cb37e Mon Sep 17 00:00:00 2001 From: Andric Tham Date: Thu, 11 Jul 2024 19:16:05 +0800 Subject: [PATCH 1/6] `createDecorations` to read from `cursorStateField` --- src/plugins/cursor-plugin.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/plugins/cursor-plugin.js b/src/plugins/cursor-plugin.js index 5c7b2b1..39eb82f 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 @@ -85,10 +87,11 @@ export const createDecorations = ( awareness.getStates().forEach((aw, clientId) => { if (!awarenessFilter(y.clientID, clientId, aw)) { return + 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 +104,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 +170,8 @@ export const yCursorPlugin = ( awareness, awarenessStateFilter, cursorBuilder, - selectionBuilder + selectionBuilder, + cursorStateField ) }, apply (tr, prevState, _oldState, newState) { From b7b66b1aabb7e10a890740ffbe50c759492c3cb2 Mon Sep 17 00:00:00 2001 From: Andric Tham Date: Thu, 11 Jul 2024 19:19:12 +0800 Subject: [PATCH 2/6] Update cursor-plugin.js --- src/plugins/cursor-plugin.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/plugins/cursor-plugin.js b/src/plugins/cursor-plugin.js index 39eb82f..3bec3cd 100644 --- a/src/plugins/cursor-plugin.js +++ b/src/plugins/cursor-plugin.js @@ -186,7 +186,8 @@ export const yCursorPlugin = ( awareness, awarenessStateFilter, cursorBuilder, - selectionBuilder + selectionBuilder, + cursorStateField ) } return prevState.map(tr.mapping, tr.doc) From dcdd6e2c6b686577dfda3f625d6dd68de52135ba Mon Sep 17 00:00:00 2001 From: Andric Tham Date: Thu, 11 Jul 2024 19:34:39 +0800 Subject: [PATCH 3/6] Update updateCursorInfo to use cursorStateField --- src/plugins/cursor-plugin.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/plugins/cursor-plugin.js b/src/plugins/cursor-plugin.js index 3bec3cd..d98e821 100644 --- a/src/plugins/cursor-plugin.js +++ b/src/plugins/cursor-plugin.js @@ -231,13 +231,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 ) ) { @@ -247,11 +247,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 ) { From 292335da8441909b3fdf621d32d6c64d06775f45 Mon Sep 17 00:00:00 2001 From: Andric Tham Date: Thu, 11 Jul 2024 21:21:12 +0800 Subject: [PATCH 4/6] Remove stray return --- src/plugins/cursor-plugin.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/plugins/cursor-plugin.js b/src/plugins/cursor-plugin.js index d98e821..aa9ae37 100644 --- a/src/plugins/cursor-plugin.js +++ b/src/plugins/cursor-plugin.js @@ -87,7 +87,6 @@ export const createDecorations = ( awareness.getStates().forEach((aw, clientId) => { if (!awarenessFilter(y.clientID, clientId, aw)) { return - return; } if (aw[cursorStateField] != null) { From 01f2c05d3b8d01a3a85370f9763a0dabac03cd54 Mon Sep 17 00:00:00 2001 From: Andric Tham Date: Thu, 11 Jul 2024 21:56:02 +0800 Subject: [PATCH 5/6] WIP Bugfix: `focusout` event not resetting cursors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `focusout` isn’t resetting cursors to `null` if the `cursorStateField` is set to anything other than default. This is an attempted fix. --- src/plugins/cursor-plugin.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/plugins/cursor-plugin.js b/src/plugins/cursor-plugin.js index aa9ae37..11e3d19 100644 --- a/src/plugins/cursor-plugin.js +++ b/src/plugins/cursor-plugin.js @@ -90,7 +90,7 @@ export const createDecorations = ( } if (aw[cursorStateField] != null) { - const user = aw.user || {}; + const user = aw.user || {} if (user.color == null) { user.color = '#ffa500' } else if (!rxValidColor.test(user.color)) { @@ -105,7 +105,7 @@ export const createDecorations = ( ystate.type, Y.createRelativePositionFromJSON(aw[cursorStateField].anchor), ystate.binding.mapping - ); + ) let head = relativePositionToAbsolutePosition( y, ystate.type, @@ -246,13 +246,7 @@ export const yCursorPlugin = ( }) } } else if ( - current[cursorStateField] != null && - relativePositionToAbsolutePosition( - ystate.doc, - ystate.type, - Y.createRelativePositionFromJSON(current[cursorStateField].anchor), - ystate.binding.mapping - ) !== null + current[cursorStateField] != null ) { // delete cursor information if current cursor information is owned by this editor binding awareness.setLocalStateField(cursorStateField, null) From 837050f2556035b912eaf680cbc8ffb34edffaab Mon Sep 17 00:00:00 2001 From: Andric Tham Date: Thu, 11 Jul 2024 23:27:07 +0800 Subject: [PATCH 6/6] Revert conditional check --- src/plugins/cursor-plugin.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/plugins/cursor-plugin.js b/src/plugins/cursor-plugin.js index 11e3d19..aa9ae37 100644 --- a/src/plugins/cursor-plugin.js +++ b/src/plugins/cursor-plugin.js @@ -90,7 +90,7 @@ export const createDecorations = ( } if (aw[cursorStateField] != null) { - const user = aw.user || {} + const user = aw.user || {}; if (user.color == null) { user.color = '#ffa500' } else if (!rxValidColor.test(user.color)) { @@ -105,7 +105,7 @@ export const createDecorations = ( ystate.type, Y.createRelativePositionFromJSON(aw[cursorStateField].anchor), ystate.binding.mapping - ) + ); let head = relativePositionToAbsolutePosition( y, ystate.type, @@ -246,7 +246,13 @@ export const yCursorPlugin = ( }) } } else if ( - current[cursorStateField] != null + current[cursorStateField] != null && + relativePositionToAbsolutePosition( + ystate.doc, + ystate.type, + Y.createRelativePositionFromJSON(current[cursorStateField].anchor), + ystate.binding.mapping + ) !== null ) { // delete cursor information if current cursor information is owned by this editor binding awareness.setLocalStateField(cursorStateField, null)