@@ -58,15 +58,36 @@ export function caretColorSlot(color: unknown): number {
5858 */
5959const CARET_ACTIVE_CLASS = 'collaboration-carets__caret--active'
6060
61- /** The class that flips the label to the caret's left near the editor 's right edge. */
61+ /** The class that flips the label to the caret's left near the viewport 's right edge. */
6262const CARET_FLIP_CLASS = 'collaboration-carets__caret--flip'
6363
6464/** Per-caret fade timers, keyed by the (reused) caret DOM node. */
6565const caretFadeTimers = new WeakMap < HTMLElement , ReturnType < typeof setTimeout > > ( )
6666
67+ function positionCaretLabel ( caret : HTMLElement , editorBounds ?: DOMRect ) {
68+ const label = caret . querySelector < HTMLElement > ( '.collaboration-carets__label' )
69+ const bounds =
70+ caret . closest ( '.tableWrapper' ) ?. getBoundingClientRect ( ) ??
71+ editorBounds ??
72+ caret . closest ( '.ProseMirror' ) ?. getBoundingClientRect ( )
73+ if ( ! label || ! bounds ) return
74+ const anchor = caret . getBoundingClientRect ( ) . left
75+ const outside = anchor < bounds . left || anchor > bounds . right
76+ caret . toggleAttribute ( 'data-caret-outside' , outside )
77+ if ( outside ) return
78+ const available = Math . max ( 0 , bounds . width - 2 )
79+ caret . style . setProperty ( '--caret-label-max-width' , `${ available } px` )
80+ const width = label . getBoundingClientRect ( ) . width
81+ const flip = anchor - 1 + width > bounds . right - 1
82+ caret . classList . toggle ( CARET_FLIP_CLASS , flip )
83+ const left = flip ? anchor + 1 - width : anchor - 1
84+ const bounded = Math . min ( Math . max ( left , bounds . left + 1 ) , bounds . right - 1 - width )
85+ caret . style . setProperty ( '--caret-label-offset' , `${ bounded - left } px` )
86+ }
87+
6788/**
6889 * Show a peer's name label, (re)start its fade timer, and flip it to the caret's
69- * left when it would run off the editor's right edge .
90+ * left when it would run off its visible viewport .
7091 *
7192 * yCursorPlugin renders each caret as a keyed widget decoration, so ProseMirror
7293 * REUSES the same DOM node as the caret moves (verified: the render function is
@@ -75,7 +96,7 @@ const caretFadeTimers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>(
7596 * (unlike `update`) fires only on a real state change, not the 15s heartbeat, so
7697 * an idle peer's label correctly stays hidden.
7798 */
78- export function activateCaretLabel ( caret : HTMLElement , editorRight ?: number ) {
99+ export function activateCaretLabel ( caret : HTMLElement , editorBounds ?: DOMRect ) {
79100 caret . classList . add ( CARET_ACTIVE_CLASS )
80101 const existing = caretFadeTimers . get ( caret )
81102 if ( existing ) clearTimeout ( existing )
@@ -87,14 +108,7 @@ export function activateCaretLabel(caret: HTMLElement, editorRight?: number) {
87108 } , CARET_LABEL_HOLD_MS )
88109 )
89110
90- if ( editorRight === undefined ) return
91- const label = caret . querySelector < HTMLElement > ( '.collaboration-carets__label' )
92- if ( ! label ) return
93- // Measure the default (rightward) position, then flip left only if it overflows.
94- caret . classList . remove ( CARET_FLIP_CLASS )
95- if ( label . getBoundingClientRect ( ) . right > editorRight ) {
96- caret . classList . add ( CARET_FLIP_CLASS )
97- }
111+ positionCaretLabel ( caret , editorBounds )
98112}
99113
100114/**
@@ -144,33 +158,50 @@ export function createCaretActivityExtension(awareness: Awareness): Extension {
144158 new Plugin ( {
145159 key : new PluginKey ( 'collaborationCaretActivity' ) ,
146160 view : ( editorView ) => {
147- // Coalesce bursts of awareness changes into a single rAF per frame, however many
148- // peers moved: accumulate the changed client ids, then re-activate each matching
149- // (reused) caret node once. The shared editorRight is read once up front; each moving
150- // caret then does one getBoundingClientRect for its edge-flip measure (bounded by the
151- // small number of concurrently-moving peers, not the awareness event rate).
161+ // Layout changes reposition idle names without restarting their activity timers.
152162 let raf = 0
163+ let repositionAll = false
153164 const pending = new Set < number > ( )
154165 const flush = ( ) => {
155166 raf = 0
156- const editorRight = editorView . dom . getBoundingClientRect ( ) . right
157- for ( const id of pending ) {
158- const caret = editorView . dom . querySelector < HTMLElement > (
159- `.collaboration-carets__caret[data-caret-client-id="${ id } "]`
160- )
161- if ( caret ) activateCaretLabel ( caret , editorRight )
167+ const bounds = editorView . dom . getBoundingClientRect ( )
168+ const carets = repositionAll
169+ ? editorView . dom . querySelectorAll < HTMLElement > ( '.collaboration-carets__caret' )
170+ : [ ...pending ] . flatMap ( ( id ) => {
171+ const caret = editorView . dom . querySelector < HTMLElement > (
172+ `.collaboration-carets__caret[data-caret-client-id="${ id } "]`
173+ )
174+ return caret ? [ caret ] : [ ]
175+ } )
176+ for ( const caret of carets ) {
177+ const id = caret . dataset . caretClientId
178+ if ( id !== undefined && pending . has ( Number ( id ) ) ) activateCaretLabel ( caret , bounds )
179+ else positionCaretLabel ( caret , bounds )
162180 }
181+ repositionAll = false
163182 pending . clear ( )
164183 }
184+ const onViewportChange = ( ) => {
185+ repositionAll = true
186+ if ( ! raf ) raf = requestAnimationFrame ( flush )
187+ }
165188 const onChange = ( { added, updated } : { added : number [ ] ; updated : number [ ] } ) => {
166189 for ( const id of added ) pending . add ( id )
167190 for ( const id of updated ) pending . add ( id )
168191 if ( pending . size > 0 && ! raf ) raf = requestAnimationFrame ( flush )
169192 }
170193 awareness . on ( 'change' , onChange )
194+ editorView . dom . addEventListener ( 'scroll' , onViewportChange , true )
195+ window . addEventListener ( 'resize' , onViewportChange )
196+ const observer =
197+ typeof ResizeObserver === 'undefined' ? null : new ResizeObserver ( onViewportChange )
198+ observer ?. observe ( editorView . dom )
171199 return {
172200 destroy : ( ) => {
173201 awareness . off ( 'change' , onChange )
202+ editorView . dom . removeEventListener ( 'scroll' , onViewportChange , true )
203+ window . removeEventListener ( 'resize' , onViewportChange )
204+ observer ?. disconnect ( )
174205 if ( raf ) cancelAnimationFrame ( raf )
175206 // Clear any pending fade timers for this editor's carets so they don't fire on
176207 // detached nodes after unmount (harmless no-op, but a genuine leaked timer).
0 commit comments