|
| 1 | +import { Extension } from '@tiptap/core'; |
| 2 | +import { Decoration, DecorationSet } from 'prosemirror-view'; |
| 3 | +import { Plugin } from 'prosemirror-state'; |
| 4 | +import { EditorState } from 'prosemirror-state'; |
| 5 | +import tinycolor from 'tinycolor2'; |
| 6 | + |
| 7 | +export const CollaborativeCursorExtension = Extension.create({ |
| 8 | + name: 'collaborativeCursor', |
| 9 | + addOptions() { |
| 10 | + return { provider: null }; |
| 11 | + }, |
| 12 | + addProseMirrorPlugins() { |
| 13 | + let deco = DecorationSet.empty; |
| 14 | + return [ |
| 15 | + new Plugin({ |
| 16 | + props: { |
| 17 | + decorations: (state: EditorState) => deco, |
| 18 | + }, |
| 19 | + view: view => { |
| 20 | + const provider = this.options.provider; |
| 21 | + const localClientId = provider?.awareness?.clientID; |
| 22 | + if (!provider) return { props: {} }; |
| 23 | + |
| 24 | + const updateDecorations = () => { |
| 25 | + const states = Array.from( |
| 26 | + provider.awareness.getStates().entries() |
| 27 | + ) as [any, any][]; |
| 28 | + const decorations = []; |
| 29 | + for (const [clientId, state] of states) { |
| 30 | + if (!state || !state.user || !state.cursor) continue; |
| 31 | + if (clientId === localClientId) { |
| 32 | + continue; |
| 33 | + } |
| 34 | + const { name, color } = state.user; |
| 35 | + if (!state.cursor) continue; |
| 36 | + const { anchor, head } = state.cursor; |
| 37 | + const from = Math.min(anchor, head); |
| 38 | + const to = Math.max(anchor, head); |
| 39 | + if (from !== to) { |
| 40 | + // Use a slightly darker border for contrast |
| 41 | + const borderColor = tinycolor(color).darken(15).toString(); |
| 42 | + decorations.push( |
| 43 | + Decoration.inline(from, to, { |
| 44 | + class: 'collab-selection', |
| 45 | + style: `background: ${color}; opacity: 0.6; border-radius: 2px; border: 2px solid ${borderColor};`, |
| 46 | + }) |
| 47 | + ); |
| 48 | + } |
| 49 | + decorations.push( |
| 50 | + Decoration.widget( |
| 51 | + head, |
| 52 | + () => { |
| 53 | + const caret = document.createElement('span'); |
| 54 | + caret.className = 'collab-cursor-caret'; |
| 55 | + caret.style.borderLeft = `2px solid ${color}`; |
| 56 | + caret.style.marginLeft = '-1px'; |
| 57 | + caret.style.height = '1em'; |
| 58 | + caret.style.position = 'relative'; |
| 59 | + caret.style.verticalAlign = 'text-bottom'; |
| 60 | + caret.style.zIndex = '10'; |
| 61 | + caret.style.pointerEvents = 'none'; |
| 62 | + const label = document.createElement('div'); |
| 63 | + label.className = 'collab-cursor-label'; |
| 64 | + label.textContent = name || 'Anonymous'; |
| 65 | + label.style.background = color; |
| 66 | + label.style.color = '#fff'; |
| 67 | + label.style.fontSize = '0.75em'; |
| 68 | + label.style.position = 'absolute'; |
| 69 | + label.style.top = '-1.5em'; |
| 70 | + label.style.left = '0'; |
| 71 | + label.style.padding = '2px 6px'; |
| 72 | + label.style.borderRadius = '4px'; |
| 73 | + label.style.whiteSpace = 'nowrap'; |
| 74 | + label.style.userSelect = 'none'; |
| 75 | + label.style.pointerEvents = 'none'; |
| 76 | + caret.appendChild(label); |
| 77 | + return caret; |
| 78 | + }, |
| 79 | + { side: 1 } |
| 80 | + ) |
| 81 | + ); |
| 82 | + } |
| 83 | + deco = DecorationSet.create(view.state.doc, decorations); |
| 84 | + view.dispatch(view.state.tr.setMeta('addToHistory', false)); |
| 85 | + }; |
| 86 | + |
| 87 | + provider.awareness.on('change', updateDecorations); |
| 88 | + setTimeout(updateDecorations, 0); |
| 89 | + |
| 90 | + return { |
| 91 | + destroy() { |
| 92 | + provider.awareness.off('change', updateDecorations); |
| 93 | + }, |
| 94 | + }; |
| 95 | + }, |
| 96 | + }), |
| 97 | + ]; |
| 98 | + }, |
| 99 | +}); |
0 commit comments