@@ -15,12 +15,15 @@ import {
1515 IAllSuggestionData ,
1616 IDict ,
1717 ISuggestionData ,
18+ ISuggestionMetadata ,
1819 ISuggestionsManager
1920} from '../types' ;
21+ import { User } from '@jupyterlab/services' ;
2022
2123export interface ISerializedSuggessionData {
2224 originalCellId : string ;
2325 newSource : string ;
26+ metadata : ISuggestionMetadata ;
2427}
2528
2629const METADATA_KEY = 'jupyter_suggestion' ;
@@ -40,10 +43,10 @@ export class LocalSuggestionsManager
4043
4144 async getAllSuggestions (
4245 notebook : NotebookPanel
43- ) : Promise < IAllSuggestionData | undefined > {
46+ ) : Promise < IAllSuggestionData > {
4447 const path = notebook . context . localPath ;
4548 if ( this . _suggestionsMap . has ( path ) ) {
46- return this . _suggestionsMap . get ( path ) ;
49+ return this . _suggestionsMap . get ( path ) ?? new Map ( ) ;
4750 } else {
4851 const savedSuggestions : IDict < IDict < ISerializedSuggessionData > > =
4952 notebook . context . model . getMetadata ( METADATA_KEY ) ;
@@ -70,6 +73,8 @@ export class LocalSuggestionsManager
7073 ) ;
7174 this . _suggestionsMap . set ( path , currentSuggestion ) ;
7275 return currentSuggestion ;
76+ } else {
77+ return new Map ( ) ;
7378 }
7479 }
7580 }
@@ -90,8 +95,9 @@ export class LocalSuggestionsManager
9095 async addSuggestion ( options : {
9196 notebook : NotebookPanel ;
9297 cell : Cell < ICellModel > ;
98+ author ?: User . IIdentity | null ;
9399 } ) : Promise < string > {
94- const { notebook, cell } = options ;
100+ const { notebook, cell, author } = options ;
95101 const path = notebook . context . localPath ;
96102 if ( ! this . _suggestionsMap . has ( path ) ) {
97103 this . _suggestionsMap . set ( path , new Map ( ) ) ;
@@ -105,7 +111,8 @@ export class LocalSuggestionsManager
105111 const suggestionId = UUID . uuid4 ( ) ;
106112 const suggestionContent : ISuggestionData = {
107113 originalCellId : cellId ,
108- cellModel : this . _cloneCellModel ( cell . model )
114+ cellModel : this . _cloneCellModel ( cell . model ) ,
115+ metadata : { author }
109116 } ;
110117 cellSuggesions [ suggestionId ] = suggestionContent ;
111118 await this . _saveSuggestionToMetadata ( {
@@ -216,7 +223,8 @@ export class LocalSuggestionsManager
216223 notebook . context . model . getMetadata ( METADATA_KEY ) ?? { } ;
217224 const serializedData : ISerializedSuggessionData = {
218225 originalCellId : suggestionContent . originalCellId ,
219- newSource : suggestionContent . cellModel . sharedModel . getSource ( )
226+ newSource : suggestionContent . cellModel . sharedModel . getSource ( ) ,
227+ metadata : suggestionContent . metadata
220228 } ;
221229 const newData = {
222230 ...currentSuggestions ,
@@ -226,7 +234,13 @@ export class LocalSuggestionsManager
226234 }
227235 } ;
228236 notebook . context . model . setMetadata ( METADATA_KEY , newData ) ;
229- await notebook . context . save ( ) ;
237+ await this . _saveNotebook ( notebook ) ;
238+ }
239+
240+ private async _saveNotebook ( notebook : NotebookPanel ) {
241+ if ( notebook . content . model && ! notebook . content . model . collaborative ) {
242+ await notebook . context . save ( ) ;
243+ }
230244 }
231245
232246 private async _removeSuggestionFromMetadata ( options : {
@@ -247,7 +261,7 @@ export class LocalSuggestionsManager
247261 delete currentSuggestions [ cellId ] ;
248262 }
249263 notebook . context . model . setMetadata ( METADATA_KEY , currentSuggestions ) ;
250- await notebook . context . save ( ) ;
264+ await this . _saveNotebook ( notebook ) ;
251265 }
252266
253267 private async _updateSuggestionInMetadata ( options : {
@@ -271,7 +285,7 @@ export class LocalSuggestionsManager
271285 currentSuggestions [ cellId ] [ suggestionId ] . newSource = newSource ;
272286
273287 notebook . context . model . setMetadata ( METADATA_KEY , currentSuggestions ) ;
274- await notebook . context . save ( ) ;
288+ await this . _saveNotebook ( notebook ) ;
275289 }
276290
277291 private _cloneCellModel (
@@ -311,12 +325,13 @@ export class LocalSuggestionsManager
311325 serializedData : ISerializedSuggessionData ,
312326 cellMap : IDict < ICellModel >
313327 ) : ISuggestionData {
314- const { originalCellId, newSource } = serializedData ;
328+ const { originalCellId, newSource, metadata } = serializedData ;
315329 const originalCellModel = cellMap [ serializedData . originalCellId ] ;
316330 const newCellModel = this . _cloneCellModel ( originalCellModel , newSource ) ;
317331 return {
318332 originalCellId,
319- cellModel : newCellModel
333+ cellModel : newCellModel ,
334+ metadata
320335 } ;
321336 }
322337}
0 commit comments