Skip to content

Commit 94c6552

Browse files
committed
Improve notebook save
1 parent 0655315 commit 94c6552

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

packages/notebook/src/browser/view-model/notebook-model.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export class NotebookModel implements Saveable, Disposable {
9696
protected nextHandle: number = 0;
9797

9898
protected _dirty = false;
99+
protected _lastData?: NotebookData;
99100

100101
set dirty(dirty: boolean) {
101102
const oldState = this._dirty;
@@ -132,6 +133,8 @@ export class NotebookModel implements Saveable, Disposable {
132133
initialize(): void {
133134
this.dirty = false;
134135

136+
this._lastData = this.props.data;
137+
135138
this.cells = this.props.data.cells.map((cell, index) => this.cellModelFactory({
136139
uri: CellUri.generate(this.props.resource.uri, index),
137140
handle: index,
@@ -165,10 +168,9 @@ export class NotebookModel implements Saveable, Disposable {
165168
this.dirtyCells = [];
166169
this.dirty = false;
167170

168-
const serializedNotebook = await this.props.serializer.fromNotebook({
169-
cells: this.cells.map(cell => cell.getData()),
170-
metadata: this.metadata
171-
});
171+
const data = this.getData();
172+
this._lastData = data;
173+
const serializedNotebook = await this.props.serializer.fromNotebook(data);
172174
this.fileService.writeFile(this.uri, serializedNotebook);
173175

174176
this.onDidSaveNotebookEmitter.fire();
@@ -178,10 +180,7 @@ export class NotebookModel implements Saveable, Disposable {
178180
const model = this;
179181
return {
180182
read(): string {
181-
return JSON.stringify({
182-
cells: model.cells.map(cell => cell.getData()),
183-
metadata: model.metadata
184-
});
183+
return JSON.stringify(model.getData());
185184
}
186185
};
187186
}
@@ -192,10 +191,14 @@ export class NotebookModel implements Saveable, Disposable {
192191
throw new Error('could not read notebook snapshot');
193192
}
194193
const data = JSON.parse(rawData) as NotebookData;
194+
this._lastData = data;
195195
this.setData(data);
196196
}
197197

198198
async revert(options?: Saveable.RevertOptions): Promise<void> {
199+
if (this._lastData && !options?.soft) {
200+
this.setData(this._lastData);
201+
}
199202
this.dirty = false;
200203
}
201204

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

227+
getData(): NotebookData {
228+
return {
229+
cells: this.cells.map(cell => cell.getData()),
230+
metadata: this.metadata
231+
};
232+
}
233+
224234
undo(): void {
225235
// TODO we probably need to check if a monaco editor is focused and if so, not undo
226236
this.undoRedoService.undo(this.uri);
@@ -263,7 +273,7 @@ export class NotebookModel implements Saveable, Disposable {
263273
end: edit.editType === CellEditType.Replace ? edit.index + edit.count : cellIndex,
264274
originalIndex: index
265275
};
266-
}).filter(edit => !!edit);
276+
});
267277

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

0 commit comments

Comments
 (0)