@@ -15,6 +15,12 @@ import {
1515 notebookCellExecutor
1616} from './docprovider' ;
1717
18+ import { IStateDB , StateDB } from '@jupyterlab/statedb' ;
19+ import { IGlobalAwareness } from '@jupyter/collaborative-drive' ;
20+ import * as Y from 'yjs' ;
21+ import { Awareness } from 'y-protocols/awareness' ;
22+ import { IAwareness } from '@jupyter/ydoc' ;
23+
1824/**
1925 * Initialization data for the @jupyter/rtc-core extension.
2026 */
@@ -55,12 +61,56 @@ const plugin: JupyterFrontEndPlugin<void> = {
5561 }
5662} ;
5763
64+ /**
65+ * Jupyter plugin creating a global awareness for RTC.
66+ */
67+ export const rtcGlobalAwarenessPlugin : JupyterFrontEndPlugin < IAwareness > = {
68+ id : '@jupyter/rtc-core/collaboration-extension:rtcGlobalAwareness' ,
69+ description : 'Add global awareness to share working document of users.' ,
70+ requires : [ IStateDB ] ,
71+ provides : IGlobalAwareness ,
72+ activate : ( app : JupyterFrontEnd , state : StateDB ) : IAwareness => {
73+ // @ts -ignore
74+ const { user } = app . serviceManager ;
75+
76+ const ydoc = new Y . Doc ( ) ;
77+ const awareness = new Awareness ( ydoc ) ;
78+
79+ // TODO: Uncomment once global awareness is working
80+ /*const server = ServerConnection.makeSettings();
81+ const url = URLExt.join(server.wsUrl, 'api/collaboration/room');
82+
83+ new WebSocketAwarenessProvider({
84+ url: url,
85+ roomID: 'JupyterLab:globalAwareness',
86+ awareness: awareness,
87+ user: user
88+ });*/
89+
90+ state . changed . connect ( async ( ) => {
91+ const data : any = await state . toJSON ( ) ;
92+ const current : string = data [ 'layout-restorer:data' ] ?. main ?. current || '' ;
93+
94+ // For example matches `notebook:Untitled.ipynb` or `editor:untitled.txt`,
95+ // but not when in launcher or terminal.
96+ if ( current . match ( / ^ \w + : .+ / ) ) {
97+ awareness . setLocalStateField ( 'current' , current ) ;
98+ } else {
99+ awareness . setLocalStateField ( 'current' , null ) ;
100+ }
101+ } ) ;
102+
103+ return awareness ;
104+ }
105+ } ;
106+
58107const plugins : JupyterFrontEndPlugin < unknown > [ ] = [
59108 rtcContentProvider ,
60109 yfile ,
61110 ynotebook ,
62111 logger ,
63112 notebookCellExecutor ,
113+ rtcGlobalAwarenessPlugin ,
64114 plugin
65115] ;
66116
0 commit comments