Skip to content

Commit

Permalink
Handle cell deletion and cell move (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc authored Dec 18, 2024
1 parent 24cf4f0 commit bc16dbd
Show file tree
Hide file tree
Showing 16 changed files with 607 additions and 162 deletions.
1 change: 1 addition & 0 deletions packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@jupyterlab/notebook": "^4.0.0",
"@jupyterlab/observables": "^4.0.0",
"@jupyterlab/rendermime": "^4.0.0",
"@jupyterlab/services": "^7.0.0",
"@jupyterlab/ui-components": "^4.0.0",
"@lumino/coreutils": "^2.0.0",
"@lumino/disposable": "^2.0.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/base/src/baseSuggestionsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ISuggestionData,
ISuggestionsManager
} from './types';
import { User } from '@jupyterlab/services';

export abstract class BaseSuggestionsManager implements ISuggestionsManager {
constructor(options: BaseSuggestionsManager.IOptions) {
Expand Down Expand Up @@ -36,7 +37,7 @@ export abstract class BaseSuggestionsManager implements ISuggestionsManager {

abstract getAllSuggestions(
notebook: NotebookPanel
): Promise<IAllSuggestionData | undefined>;
): Promise<IAllSuggestionData>;

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

abstract acceptSuggestion(options: {
Expand Down
1 change: 1 addition & 0 deletions packages/base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './tokens';
export * from './localSuggestionsManager';
export * from './baseSuggestionsManager';
export * from './registry';
export * from './tools';
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import {
IAllSuggestionData,
IDict,
ISuggestionData,
ISuggestionMetadata,
ISuggestionsManager
} from '../types';
import { User } from '@jupyterlab/services';

export interface ISerializedSuggessionData {
originalCellId: string;
newSource: string;
metadata: ISuggestionMetadata;
}

const METADATA_KEY = 'jupyter_suggestion';
Expand All @@ -40,10 +43,10 @@ export class LocalSuggestionsManager

async getAllSuggestions(
notebook: NotebookPanel
): Promise<IAllSuggestionData | undefined> {
): Promise<IAllSuggestionData> {
const path = notebook.context.localPath;
if (this._suggestionsMap.has(path)) {
return this._suggestionsMap.get(path);
return this._suggestionsMap.get(path) ?? new Map();
} else {
const savedSuggestions: IDict<IDict<ISerializedSuggessionData>> =
notebook.context.model.getMetadata(METADATA_KEY);
Expand All @@ -70,6 +73,8 @@ export class LocalSuggestionsManager
);
this._suggestionsMap.set(path, currentSuggestion);
return currentSuggestion;
} else {
return new Map();
}
}
}
Expand All @@ -90,8 +95,9 @@ export class LocalSuggestionsManager
async addSuggestion(options: {
notebook: NotebookPanel;
cell: Cell<ICellModel>;
author?: User.IIdentity | null;
}): Promise<string> {
const { notebook, cell } = options;
const { notebook, cell, author } = options;
const path = notebook.context.localPath;
if (!this._suggestionsMap.has(path)) {
this._suggestionsMap.set(path, new Map());
Expand All @@ -105,7 +111,8 @@ export class LocalSuggestionsManager
const suggestionId = UUID.uuid4();
const suggestionContent: ISuggestionData = {
originalCellId: cellId,
cellModel: this._cloneCellModel(cell.model)
cellModel: this._cloneCellModel(cell.model),
metadata: { author }
};
cellSuggesions[suggestionId] = suggestionContent;
await this._saveSuggestionToMetadata({
Expand Down Expand Up @@ -216,7 +223,8 @@ export class LocalSuggestionsManager
notebook.context.model.getMetadata(METADATA_KEY) ?? {};
const serializedData: ISerializedSuggessionData = {
originalCellId: suggestionContent.originalCellId,
newSource: suggestionContent.cellModel.sharedModel.getSource()
newSource: suggestionContent.cellModel.sharedModel.getSource(),
metadata: suggestionContent.metadata
};
const newData = {
...currentSuggestions,
Expand All @@ -226,7 +234,13 @@ export class LocalSuggestionsManager
}
};
notebook.context.model.setMetadata(METADATA_KEY, newData);
await notebook.context.save();
await this._saveNotebook(notebook);
}

private async _saveNotebook(notebook: NotebookPanel) {
if (notebook.content.model && !notebook.content.model.collaborative) {
await notebook.context.save();
}
}

private async _removeSuggestionFromMetadata(options: {
Expand All @@ -247,7 +261,7 @@ export class LocalSuggestionsManager
delete currentSuggestions[cellId];
}
notebook.context.model.setMetadata(METADATA_KEY, currentSuggestions);
await notebook.context.save();
await this._saveNotebook(notebook);
}

private async _updateSuggestionInMetadata(options: {
Expand All @@ -271,7 +285,7 @@ export class LocalSuggestionsManager
currentSuggestions[cellId][suggestionId].newSource = newSource;

notebook.context.model.setMetadata(METADATA_KEY, currentSuggestions);
await notebook.context.save();
await this._saveNotebook(notebook);
}

private _cloneCellModel(
Expand Down Expand Up @@ -311,12 +325,13 @@ export class LocalSuggestionsManager
serializedData: ISerializedSuggessionData,
cellMap: IDict<ICellModel>
): ISuggestionData {
const { originalCellId, newSource } = serializedData;
const { originalCellId, newSource, metadata } = serializedData;
const originalCellModel = cellMap[serializedData.originalCellId];
const newCellModel = this._cloneCellModel(originalCellModel, newSource);
return {
originalCellId,
cellModel: newCellModel
cellModel: newCellModel,
metadata
};
}
}
Loading

0 comments on commit bc16dbd

Please sign in to comment.