-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fade out user cursor labels after inactivity
Implementation details: * Add a custom CollaborationCursor Tiptap extension * Add CSS to fade out the cursor label after some time. * Listen for Yjs updates. - If it's a doc change by ourself, update the timestamp of own user in awareness state. - If it's a remote awareness update, add back the CSS class to the corresponding cursor. - Wait 50ms before showing the cursor in the DOM to account for cases where the cursor gets re-rendered by y-prosemirror. Fixes: #4126 Signed-off-by: Jonas <[email protected]>
- Loading branch information
1 parent
8b287aa
commit b7baceb
Showing
3 changed files
with
97 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { CollaborationCursor as TiptapCollaborationCursor } from '@tiptap/extension-collaboration-cursor' | ||
|
||
/** | ||
* Show cursor for client ID | ||
* Wait 50ms for cases where the cursor gets re-rendered | ||
* | ||
* @param {number} clientId The Yjs client ID | ||
*/ | ||
function showCursorLabel(clientId) { | ||
setTimeout(() => { | ||
const el = document.getElementById(`collaboration-cursor__label__${clientId}`) | ||
if (!el) { | ||
return | ||
} | ||
|
||
el.classList.add('collaboration-cursor__label__active') | ||
setTimeout(() => { | ||
el?.classList.remove('collaboration-cursor__label__active') | ||
}, 50) | ||
}, 50) | ||
} | ||
|
||
const CollaborationCursor = TiptapCollaborationCursor.extend({ | ||
addOptions() { | ||
return { | ||
provider: null, | ||
user: { | ||
name: null, | ||
clientId: null, | ||
color: null, | ||
lastUpdate: null, | ||
}, | ||
render: user => { | ||
const cursor = document.createElement('span') | ||
|
||
cursor.classList.add('collaboration-cursor__caret') | ||
cursor.setAttribute('style', `border-color: ${user.color}`) | ||
|
||
const label = document.createElement('div') | ||
|
||
label.classList.add('collaboration-cursor__label') | ||
label.id = `collaboration-cursor__label__${user.clientId}` | ||
label.setAttribute('style', `background-color: ${user.color}`) | ||
label.insertBefore(document.createTextNode(user.name), null) | ||
cursor.insertBefore(label, null) | ||
|
||
return cursor | ||
}, | ||
} | ||
}, | ||
|
||
onCreate() { | ||
this.options.provider.awareness.on('change', ({ added, removed, updated }, origin) => { | ||
if (origin !== 'local') { | ||
for (const clientId of [...added, ...updated]) { | ||
if (clientId !== this.options.user.clientId) { | ||
showCursorLabel(clientId) | ||
} | ||
} | ||
} | ||
}) | ||
}, | ||
|
||
addCommands() { | ||
return { | ||
...this.parent(), | ||
updateSelf: () => ({ editor }) => { | ||
const attributes = { ...this.options.user, lastUpdate: Date.now() } | ||
return editor.commands.updateUser(attributes) | ||
}, | ||
} | ||
}, | ||
}) | ||
|
||
export default CollaborationCursor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters