4
4
*/
5
5
6
6
import { chatIcon } from '@jupyter/chat' ;
7
- import { IGlobalAwareness } from '@jupyter/collaboration' ;
8
7
import { ICollaborativeDrive } from '@jupyter/docprovider' ;
9
8
import {
10
9
ILayoutRestorer ,
@@ -29,7 +28,6 @@ import { Contents } from '@jupyterlab/services';
29
28
import { ISettingRegistry } from '@jupyterlab/settingregistry' ;
30
29
import { ITranslator , nullTranslator } from '@jupyterlab/translation' ;
31
30
import { launchIcon } from '@jupyterlab/ui-components' ;
32
- import { Awareness } from 'y-protocols/awareness' ;
33
31
34
32
import {
35
33
WidgetConfig ,
@@ -56,7 +54,7 @@ export const docFactories: JupyterFrontEndPlugin<IWidgetConfig> = {
56
54
id : pluginIds . docFactories ,
57
55
description : 'A document factories for collaborative chat' ,
58
56
autoStart : true ,
59
- requires : [ IGlobalAwareness , IRenderMimeRegistry ] ,
57
+ requires : [ IRenderMimeRegistry ] ,
60
58
optional : [
61
59
ICollaborativeDrive ,
62
60
ILayoutRestorer ,
@@ -68,7 +66,6 @@ export const docFactories: JupyterFrontEndPlugin<IWidgetConfig> = {
68
66
provides : IWidgetConfig ,
69
67
activate : (
70
68
app : JupyterFrontEnd ,
71
- awareness : Awareness ,
72
69
rmRegistry : IRenderMimeRegistry ,
73
70
drive : ICollaborativeDrive | null ,
74
71
restorer : ILayoutRestorer | null ,
@@ -149,12 +146,22 @@ export const docFactories: JupyterFrontEndPlugin<IWidgetConfig> = {
149
146
drive . sharedModelFactory . registerDocumentFactory ( 'chat' , chatFactory ) ;
150
147
}
151
148
152
- // Creating and registering the model factory for our custom DocumentModel
153
- const modelFactory = new CollaborativeChatModelFactory ( {
154
- awareness,
155
- widgetConfig
156
- } ) ;
157
- app . docRegistry . addModelFactory ( modelFactory ) ;
149
+ app . serviceManager . ready
150
+ . then ( ( ) => {
151
+ const user = app . serviceManager . user . identity ;
152
+ // Creating and registering the model factory for our custom DocumentModel
153
+ const modelFactory = new CollaborativeChatModelFactory ( {
154
+ user,
155
+ widgetConfig
156
+ } ) ;
157
+ app . docRegistry . addModelFactory ( modelFactory ) ;
158
+ } )
159
+ . catch ( e =>
160
+ console . error (
161
+ 'The collaborative chat model factory is not initialized' ,
162
+ e
163
+ )
164
+ ) ;
158
165
159
166
// Creating the widget factory to register it so the document manager knows about
160
167
// our new DocumentWidget
@@ -203,12 +210,11 @@ const chatCommands: JupyterFrontEndPlugin<void> = {
203
210
id : pluginIds . chatCommands ,
204
211
description : 'The commands to create or open a chat' ,
205
212
autoStart : true ,
206
- requires : [ ICollaborativeDrive , IGlobalAwareness , IWidgetConfig ] ,
213
+ requires : [ ICollaborativeDrive , IWidgetConfig ] ,
207
214
optional : [ IChatPanel , ICommandPalette , ILauncher ] ,
208
215
activate : (
209
216
app : JupyterFrontEnd ,
210
217
drive : ICollaborativeDrive ,
211
- awareness : Awareness ,
212
218
widgetConfig : IWidgetConfig ,
213
219
chatPanel : ChatPanel | null ,
214
220
commandPalette : ICommandPalette | null ,
@@ -220,7 +226,12 @@ const chatCommands: JupyterFrontEndPlugin<void> = {
220
226
* Command to create a new chat.
221
227
*
222
228
* args:
223
- * name: the name of the chat to create.
229
+ * name - optional, the name of the chat to create.
230
+ * Open a dialog if not provided.
231
+ * inSidePanel - optional (default to false).
232
+ * Whether to open the chat in side panel or in main area.
233
+ * isPalette - optional (default to false).
234
+ * Whether the command is in commands palette or not.
224
235
*/
225
236
commands . addCommand ( CommandIDs . createChat , {
226
237
label : args => ( args . isPalette ? 'Create a new chat' : 'Chat' ) ,
@@ -283,81 +294,12 @@ const chatCommands: JupyterFrontEndPlugin<void> = {
283
294
filepath = model . path ;
284
295
}
285
296
286
- return commands . execute ( CommandIDs . openChat , { filepath, inSidePanel } ) ;
287
- }
288
- } ) ;
289
-
290
- /*
291
- * Command to open a chat.
292
- *
293
- * args:
294
- * filepath - the chat file to open.
295
- */
296
- commands . addCommand ( CommandIDs . openChat , {
297
- label : 'Open a chat' ,
298
- execute : async args => {
299
- const inSidePanel : boolean = ( args . inSidePanel as boolean ) ?? false ;
300
- let filepath : string | null = ( args . filepath as string ) ?? null ;
301
- if ( filepath === null ) {
302
- filepath = (
303
- await InputDialog . getText ( {
304
- label : 'File path' ,
305
- placeholder : '/path/to/the/chat/file' ,
306
- title : 'Path of the chat'
307
- } )
308
- ) . value ;
309
- }
310
-
311
- if ( ! filepath ) {
312
- return ;
313
- }
314
-
315
- let fileExist = true ;
316
- await drive . get ( filepath , { content : false } ) . catch ( ( ) => {
317
- fileExist = false ;
318
- } ) ;
319
-
320
- if ( ! fileExist ) {
321
- showErrorMessage (
322
- 'Error opening chat' ,
323
- `'${ filepath } ' is not a valid path`
324
- ) ;
325
- return ;
326
- }
327
-
328
- if ( inSidePanel && chatPanel ) {
329
- // The chat is opened in the chat panel.
330
- app . shell . activateById ( chatPanel . id ) ;
331
- const model = await drive . get ( filepath ) ;
332
-
333
- /**
334
- * Create a share model from the chat file
335
- */
336
- const sharedModel = drive . sharedModelFactory . createNew ( {
337
- path : model . path ,
338
- format : model . format ,
339
- contentType : chatFileType . contentType ,
340
- collaborative : true
341
- } ) as YChat ;
342
-
343
- /**
344
- * Initialize the chat model with the share model
345
- */
346
- const chat = new CollaborativeChatModel ( {
347
- awareness,
348
- sharedModel,
349
- widgetConfig
297
+ if ( commands . hasCommand ( CommandIDs . openChat ) ) {
298
+ return commands . execute ( CommandIDs . openChat , {
299
+ filepath,
300
+ inSidePanel
350
301
} ) ;
351
-
352
- /**
353
- * Add a chat widget to the side panel.
354
- */
355
- chatPanel . addChat (
356
- chat ,
357
- PathExt . basename ( model . name , chatFileType . extensions [ 0 ] )
358
- ) ;
359
302
} else {
360
- // The chat is opened in the main area
361
303
commands . execute ( 'docmanager:open' , {
362
304
path : `RTC:${ filepath } ` ,
363
305
factory : FACTORY
@@ -366,17 +308,13 @@ const chatCommands: JupyterFrontEndPlugin<void> = {
366
308
}
367
309
} ) ;
368
310
369
- // Add the commands to the palette
311
+ // Add the command to the palette
370
312
if ( commandPalette ) {
371
313
commandPalette . addItem ( {
372
314
category : 'Chat' ,
373
315
command : CommandIDs . createChat ,
374
316
args : { isPalette : true }
375
317
} ) ;
376
- commandPalette . addItem ( {
377
- category : 'Chat' ,
378
- command : CommandIDs . openChat
379
- } ) ;
380
318
}
381
319
382
320
// Add the create command to the launcher
@@ -387,6 +325,100 @@ const chatCommands: JupyterFrontEndPlugin<void> = {
387
325
rank : 1
388
326
} ) ;
389
327
}
328
+
329
+ app . serviceManager . ready
330
+ . then ( ( ) => {
331
+ const user = app . serviceManager . user . identity ;
332
+ /*
333
+ * Command to open a chat.
334
+ *
335
+ * args:
336
+ * filepath - the chat file to open.
337
+ */
338
+ commands . addCommand ( CommandIDs . openChat , {
339
+ label : 'Open a chat' ,
340
+ execute : async args => {
341
+ const inSidePanel : boolean = ( args . inSidePanel as boolean ) ?? false ;
342
+ let filepath : string | null = ( args . filepath as string ) ?? null ;
343
+ if ( filepath === null ) {
344
+ filepath = (
345
+ await InputDialog . getText ( {
346
+ label : 'File path' ,
347
+ placeholder : '/path/to/the/chat/file' ,
348
+ title : 'Path of the chat'
349
+ } )
350
+ ) . value ;
351
+ }
352
+
353
+ if ( ! filepath ) {
354
+ return ;
355
+ }
356
+
357
+ let fileExist = true ;
358
+ await drive . get ( filepath , { content : false } ) . catch ( ( ) => {
359
+ fileExist = false ;
360
+ } ) ;
361
+
362
+ if ( ! fileExist ) {
363
+ showErrorMessage (
364
+ 'Error opening chat' ,
365
+ `'${ filepath } ' is not a valid path`
366
+ ) ;
367
+ return ;
368
+ }
369
+
370
+ if ( inSidePanel && chatPanel ) {
371
+ // The chat is opened in the chat panel.
372
+ app . shell . activateById ( chatPanel . id ) ;
373
+ const model = await drive . get ( filepath ) ;
374
+
375
+ /**
376
+ * Create a share model from the chat file
377
+ */
378
+ const sharedModel = drive . sharedModelFactory . createNew ( {
379
+ path : model . path ,
380
+ format : model . format ,
381
+ contentType : chatFileType . contentType ,
382
+ collaborative : true
383
+ } ) as YChat ;
384
+
385
+ /**
386
+ * Initialize the chat model with the share model
387
+ */
388
+ const chat = new CollaborativeChatModel ( {
389
+ user,
390
+ sharedModel,
391
+ widgetConfig
392
+ } ) ;
393
+
394
+ /**
395
+ * Add a chat widget to the side panel.
396
+ */
397
+ chatPanel . addChat (
398
+ chat ,
399
+ PathExt . basename ( model . name , chatFileType . extensions [ 0 ] )
400
+ ) ;
401
+ } else {
402
+ // The chat is opened in the main area
403
+ commands . execute ( 'docmanager:open' , {
404
+ path : `RTC:${ filepath } ` ,
405
+ factory : FACTORY
406
+ } ) ;
407
+ }
408
+ }
409
+ } ) ;
410
+
411
+ // Add the command to the palette
412
+ if ( commandPalette ) {
413
+ commandPalette . addItem ( {
414
+ category : 'Chat' ,
415
+ command : CommandIDs . openChat
416
+ } ) ;
417
+ }
418
+ } )
419
+ . catch ( e =>
420
+ console . error ( 'The command to open a chat is not initialized\n' , e )
421
+ ) ;
390
422
}
391
423
} ;
392
424
@@ -451,6 +483,7 @@ const chatPanel: JupyterFrontEndPlugin<ChatPanel> = {
451
483
label : 'Move the chat to the side panel' ,
452
484
caption : 'Move the chat to the side panel' ,
453
485
icon : launchIcon ,
486
+ isEnabled : ( ) => commands . hasCommand ( CommandIDs . openChat ) ,
454
487
execute : async ( ) => {
455
488
const widget = app . shell . currentWidget ;
456
489
// Ensure widget is a CollaborativeChatWidget and is in main area
0 commit comments