@@ -31,11 +31,12 @@ import {
3131} from 'yjs' ;
3232
3333/**
34- * CodeMirror extension for displaying persistent collaborator labels.
34+ * CodeMirror extension for displaying persistent collaborator cursor labels.
35+ * This file provides functionality to show user names above cursors in collaborative editing.
3536 */
3637
3738/**
38- * Yjs document objects
39+ * Yjs document objects containing awareness and text data
3940 */
4041export type EditorAwareness = {
4142 /**
@@ -48,6 +49,9 @@ export type EditorAwareness = {
4849 ytext : Text ;
4950} ;
5051
52+ /**
53+ * Represents the state of a cursor in the collaborative editor
54+ */
5155interface ICursorState {
5256 /**
5357 * Cursor anchor
@@ -72,7 +76,7 @@ interface ICursorState {
7276}
7377
7478/**
75- * Awareness state definition
79+ * Awareness state definition for collaborative editing
7680 */
7781interface IAwarenessState extends Record < string , any > {
7882 /**
@@ -93,7 +97,7 @@ interface IAwarenessState extends Record<string, any> {
9397}
9498
9599/**
96- * Facet storing the Yjs document objects
100+ * Facet for storing the Yjs document objects in CodeMirror state
97101 */
98102const editorAwarenessFacet = Facet . define < EditorAwareness , EditorAwareness > ( {
99103 combine ( configs : readonly EditorAwareness [ ] ) {
@@ -102,7 +106,7 @@ const editorAwarenessFacet = Facet.define<EditorAwareness, EditorAwareness>({
102106} ) ;
103107
104108/**
105- * Theme for persistent cursor labels
109+ * CSS theme for persistent cursor labels styling
106110 */
107111const persistentLabelTheme = EditorView . baseTheme ( {
108112 '.jp-PersistentCursorLabel' : {
@@ -132,20 +136,31 @@ const persistentLabelTheme = EditorView.baseTheme({
132136
133137
134138/**
135- * Extension for persistent user labels at cursor positions
139+ * CodeMirror ViewPlugin for managing persistent user labels at cursor positions.
140+ * Handles the creation, positioning, and cleanup of label elements.
136141 */
137142const persistentUserLabels = ViewPlugin . fromClass (
138143 class {
144+ /** Editor awareness instance */
139145 editorAwareness ! : EditorAwareness ;
146+ /** Awareness change listener function */
140147 _listener ! : ( t : {
141148 added : Array < any > ;
142149 updated : Array < any > ;
143150 removed : Array < any > ;
144151 } ) => void ;
152+ /** CodeMirror decorations set */
145153 decorations : any ;
154+ /** Map of user keys to DOM label elements */
146155 labelElements : Map < string , HTMLElement > = new Map ( ) ;
156+ /** Map of user keys to hide timer IDs */
147157 hideTimers : Map < string , number > = new Map ( ) ;
148158
159+ /**
160+ * Initialize the plugin with editor view
161+ *
162+ * @param view - The CodeMirror editor view
163+ */
149164 constructor ( view : EditorView ) {
150165 try {
151166 this . editorAwareness = view . state . facet ( editorAwarenessFacet ) ;
@@ -171,6 +186,9 @@ const persistentUserLabels = ViewPlugin.fromClass(
171186 }
172187 }
173188
189+ /**
190+ * Clean up resources when the plugin is destroyed
191+ */
174192 destroy ( ) : void {
175193 this . editorAwareness . awareness . off ( 'change' , this . _listener ) ;
176194
@@ -190,6 +208,11 @@ const persistentUserLabels = ViewPlugin.fromClass(
190208
191209 }
192210
211+ /**
212+ * Handle editor updates and refresh labels as needed
213+ *
214+ * @param update - The view update information
215+ */
193216 update ( update : ViewUpdate ) : void {
194217 if (
195218 update . docChanged ||
@@ -203,6 +226,12 @@ const persistentUserLabels = ViewPlugin.fromClass(
203226 }
204227 }
205228
229+ /**
230+ * Update the overlay labels based on current awareness state.
231+ * This is the main function that creates and positions labels.
232+ *
233+ * @param view - The CodeMirror editor view
234+ */
206235 updateOverlayLabels ( view : EditorView ) : void {
207236
208237 const { awareness, ytext } = this . editorAwareness ;
@@ -311,6 +340,14 @@ const persistentUserLabels = ViewPlugin.fromClass(
311340 } ) ;
312341 }
313342
343+ /**
344+ * Create a DOM element for displaying a user's name label.
345+ *
346+ * @param userName - The user's display name
347+ * @param userColor - The user's associated color
348+ * @param avatarUrl - Optional avatar URL (currently unused)
349+ * @returns The created label element
350+ */
314351 createLabelElement ( userName : string , userColor : string , avatarUrl ?: string ) : HTMLElement {
315352 const wrap = document . createElement ( 'div' ) ;
316353 wrap . className = 'jp-PersistentCursorLabel' ;
@@ -339,21 +376,26 @@ const persistentUserLabels = ViewPlugin.fromClass(
339376) ;
340377
341378/**
342- * CodeMirror extension to display persistent user labels at cursor positions
343- *
344- * @param config Editor source and awareness
345- * @returns CodeMirror extension
379+ * Create a CodeMirror extension for displaying persistent user cursor labels.
380+ *
381+ * @param config - Editor source and awareness configuration
382+ * @returns CodeMirror extension array
346383 */
347384export function persistentUserCursorLabels ( config : EditorAwareness ) : Extension {
348385 return [ editorAwarenessFacet . of ( config ) , persistentUserLabels ] ;
349386}
350387
351388/**
352- * Widget extension for adding persistent cursor labels to editors
389+ * Widget extension for adding persistent cursor labels to different document types.
390+ * Handles the integration with JupyterLab's document system.
353391 */
354392class PersistentCursorLabelsExtension implements DocumentRegistry . IWidgetExtension < any , any > {
355393 /**
356- * Create a new extension for the document widget.
394+ * Create a new extension instance for a document widget.
395+ *
396+ * @param panel - The document panel
397+ * @param context - The document context
398+ * @returns A disposable object for cleanup
357399 */
358400 createNew (
359401 panel : any ,
@@ -369,6 +411,12 @@ class PersistentCursorLabelsExtension implements DocumentRegistry.IWidgetExtensi
369411 } ;
370412 }
371413
414+ /**
415+ * Add cursor labels to the editor after the context is ready.
416+ *
417+ * @param panel - The document panel
418+ * @param context - The document context
419+ */
372420 private async _addLabelsToEditor ( panel : any , context : DocumentRegistry . IContext < any > ) : Promise < void > {
373421 try {
374422 await context . ready ;
@@ -391,6 +439,13 @@ class PersistentCursorLabelsExtension implements DocumentRegistry.IWidgetExtensi
391439 }
392440 }
393441
442+ /**
443+ * Find editors in the panel and enhance them with awareness-based cursor labels.
444+ *
445+ * @param panel - The document panel
446+ * @param awareness - The Yjs awareness instance
447+ * @param sharedModel - The shared document model
448+ */
394449 private _findAndEnhanceEditorWithAwareness ( panel : any , awareness : Awareness , sharedModel : any ) : void {
395450 // For file editors
396451 if ( panel . content && panel . content . editor && panel . content . editor . editor ) {
@@ -471,14 +526,16 @@ class PersistentCursorLabelsExtension implements DocumentRegistry.IWidgetExtensi
471526}
472527
473528/**
474- * Persistent cursor labels plugin
529+ * JupyterLab plugin for persistent cursor labels.
530+ * Registers the cursor label extension with different document types.
475531 */
476532const persistentCursorLabelsPlugin : JupyterFrontEndPlugin < void > = {
477533 id : 'jupyterlab-document-collaborators:persistent-cursor-labels' ,
478534 description : 'A JupyterLab extension for showing persistent collaborator names above their cursors' ,
479535 autoStart : true ,
480536 requires : [ IDocumentManager ] ,
481537 activate : ( app : JupyterFrontEnd , docManager : IDocumentManager ) => {
538+ console . log ( 'Persistent cursor labels extension is activated!' ) ;
482539 // Create the extension
483540 const extension = new PersistentCursorLabelsExtension ( ) ;
484541
0 commit comments