Skip to content

Commit bc16dbd

Browse files
authored
Handle cell deletion and cell move (#13)
1 parent 24cf4f0 commit bc16dbd

File tree

16 files changed

+607
-162
lines changed

16 files changed

+607
-162
lines changed

packages/base/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@jupyterlab/notebook": "^4.0.0",
4444
"@jupyterlab/observables": "^4.0.0",
4545
"@jupyterlab/rendermime": "^4.0.0",
46+
"@jupyterlab/services": "^7.0.0",
4647
"@jupyterlab/ui-components": "^4.0.0",
4748
"@lumino/coreutils": "^2.0.0",
4849
"@lumino/disposable": "^2.0.0",

packages/base/src/baseSuggestionsManager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ISuggestionData,
99
ISuggestionsManager
1010
} from './types';
11+
import { User } from '@jupyterlab/services';
1112

1213
export abstract class BaseSuggestionsManager implements ISuggestionsManager {
1314
constructor(options: BaseSuggestionsManager.IOptions) {
@@ -36,7 +37,7 @@ export abstract class BaseSuggestionsManager implements ISuggestionsManager {
3637

3738
abstract getAllSuggestions(
3839
notebook: NotebookPanel
39-
): Promise<IAllSuggestionData | undefined>;
40+
): Promise<IAllSuggestionData>;
4041

4142
async getSuggestion(options: {
4243
notebookPath: string;
@@ -54,6 +55,7 @@ export abstract class BaseSuggestionsManager implements ISuggestionsManager {
5455
abstract addSuggestion(options: {
5556
notebook: NotebookPanel;
5657
cell: Cell<ICellModel>;
58+
author?: User.IIdentity | null;
5759
}): Promise<string>;
5860

5961
abstract acceptSuggestion(options: {

packages/base/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './tokens';
55
export * from './localSuggestionsManager';
66
export * from './baseSuggestionsManager';
77
export * from './registry';
8+
export * from './tools';

packages/base/src/localSuggestionsManager/localSuggestionsManager.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2123
export interface ISerializedSuggessionData {
2224
originalCellId: string;
2325
newSource: string;
26+
metadata: ISuggestionMetadata;
2427
}
2528

2629
const 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

Comments
 (0)