Skip to content

Commit

Permalink
Improve notebook save
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed May 7, 2024
1 parent c294198 commit 9f05af6
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/notebook/src/browser/view-model/notebook-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class NotebookModel implements Saveable, Disposable {
protected nextHandle: number = 0;

protected _dirty = false;
protected _lastData?: NotebookData;

set dirty(dirty: boolean) {
const oldState = this._dirty;
Expand Down Expand Up @@ -132,6 +133,8 @@ export class NotebookModel implements Saveable, Disposable {
initialize(): void {
this.dirty = false;

this._lastData = this.props.data;

this.cells = this.props.data.cells.map((cell, index) => this.cellModelFactory({
uri: CellUri.generate(this.props.resource.uri, index),
handle: index,
Expand Down Expand Up @@ -165,10 +168,9 @@ export class NotebookModel implements Saveable, Disposable {
this.dirtyCells = [];
this.dirty = false;

const serializedNotebook = await this.props.serializer.fromNotebook({
cells: this.cells.map(cell => cell.getData()),
metadata: this.metadata
});
const data = this.getData();
this._lastData = data;
const serializedNotebook = await this.props.serializer.fromNotebook(data);
this.fileService.writeFile(this.uri, serializedNotebook);

this.onDidSaveNotebookEmitter.fire();
Expand All @@ -178,10 +180,7 @@ export class NotebookModel implements Saveable, Disposable {
const model = this;
return {
read(): string {
return JSON.stringify({
cells: model.cells.map(cell => cell.getData()),
metadata: model.metadata
});
return JSON.stringify(model.getData());
}
};
}
Expand All @@ -192,10 +191,14 @@ export class NotebookModel implements Saveable, Disposable {
throw new Error('could not read notebook snapshot');
}
const data = JSON.parse(rawData) as NotebookData;
this._lastData = data;
this.setData(data);
}

async revert(options?: Saveable.RevertOptions): Promise<void> {
if (this._lastData && !options?.soft) {
this.setData(this._lastData);
}
this.dirty = false;
}

Expand All @@ -217,10 +220,17 @@ export class NotebookModel implements Saveable, Disposable {
// Replace all cells in the model
this.replaceCells(0, this.cells.length, data.cells, false);
this.metadata = data.metadata;
this.dirty = false;
this.dirty = true;
this.onDidChangeContentEmitter.fire();
}

getData(): NotebookData {
return {
cells: this.cells.map(cell => cell.getData()),
metadata: this.metadata
};
}

undo(): void {
// TODO we probably need to check if a monaco editor is focused and if so, not undo
this.undoRedoService.undo(this.uri);
Expand Down Expand Up @@ -263,7 +273,7 @@ export class NotebookModel implements Saveable, Disposable {
end: edit.editType === CellEditType.Replace ? edit.index + edit.count : cellIndex,
originalIndex: index
};
}).filter(edit => !!edit);
});

for (const { edit, cellIndex } of editsWithDetails) {
const cell = this.cells[cellIndex];
Expand Down

0 comments on commit 9f05af6

Please sign in to comment.