Skip to content

Fix dirty state not matching save button state #9993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/client/datascience/interactive-ipynb/nativeEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ import {
WebViewViewChangeEventArgs
} from '../types';

// tslint:disable-next-line: no-require-imports
import cloneDeep = require('lodash/cloneDeep');

const nativeEditorDir = path.join(EXTENSION_ROOT_DIR, 'out', 'datascience-ui', 'notebook');
@injectable()
export class NativeEditor extends InteractiveBase implements INotebookEditor {
Expand Down Expand Up @@ -345,7 +348,7 @@ export class NativeEditor extends InteractiveBase implements INotebookEditor {
source: 'user',
kind: 'modify',
newCells: modified,
oldCells: unmodified,
oldCells: cloneDeep(unmodified),
oldDirty: this._model.isDirty,
newDirty: true
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ export class NativeEditorProvider implements INotebookEditorProvider, WebviewCus

private closedEditor(editor: INotebookEditor): void {
this.openedEditors.delete(editor);
// If last editor, dispose of the storage
const key = editor.file.toString();
if (![...this.openedEditors].find(e => e.file.toString() === key)) {
this.modelChangedHandlers.delete(key);
this.models.delete(key);
}
this._onDidCloseNotebookEditor.fire(editor);
}

Expand Down
13 changes: 7 additions & 6 deletions src/client/datascience/interactive-ipynb/nativeEditorStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ const NotebookTransferKey = 'notebook-transfered';
interface INativeEditorStorageState {
file: Uri;
cells: ICell[];
changeCountSinceSave: number;
changeCount: number;
saveChangeCount: number;
notebookJson: Partial<nbformat.INotebookContent>;
}

@injectable()
export class NativeEditorStorage implements INotebookModel, INotebookStorage {
public get isDirty(): boolean {
return this._state.changeCountSinceSave > 0;
return this._state.changeCount !== this._state.saveChangeCount && this._state.changeCount !== 0;
}
public get changed(): Event<NotebookModelChange> {
return this._changedEmitter.event;
Expand All @@ -49,7 +50,7 @@ export class NativeEditorStorage implements INotebookModel, INotebookStorage {
return this._state.cells;
}
private _changedEmitter = new EventEmitter<NotebookModelChange>();
private _state: INativeEditorStorageState = { file: Uri.file(''), changeCountSinceSave: 0, cells: [], notebookJson: {} };
private _state: INativeEditorStorageState = { file: Uri.file(''), changeCount: 0, saveChangeCount: 0, cells: [], notebookJson: {} };
private indentAmount: string = ' ';

constructor(
Expand Down Expand Up @@ -145,7 +146,7 @@ export class NativeEditorStorage implements INotebookModel, INotebookStorage {
case 'file':
changed = !this.fileSystem.arePathsSame(this._state.file.fsPath, change.newFile.fsPath);
this._state.file = change.newFile;
this._state.changeCountSinceSave = 0;
this._state.saveChangeCount = this._state.changeCount;
break;
default:
break;
Expand All @@ -154,7 +155,7 @@ export class NativeEditorStorage implements INotebookModel, INotebookStorage {
// Dirty state comes from undo. At least VS code will track it that way. However
// skip version and file changes as we don't forward those to VS code
if (change.kind !== 'file' && change.kind !== 'version') {
this._state.changeCountSinceSave += 1;
this._state.changeCount += 1;
}

return changed;
Expand Down Expand Up @@ -194,7 +195,7 @@ export class NativeEditorStorage implements INotebookModel, INotebookStorage {
// Dirty state comes from undo. At least VS code will track it that way.
// Note unlike redo, 'file' and 'version' are not possible on undo as
// we don't send them to VS code.
this._state.changeCountSinceSave -= 1;
this._state.changeCount -= 1;

return changed;
}
Expand Down
17 changes: 16 additions & 1 deletion src/datascience-ui/native-editor/redux/reducers/creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,16 @@ export namespace Creation {
...disabledQueueArg,
payload: { ...arg.payload, data: { firstCellId: arg.payload.data.secondCellId, secondCellId: arg.payload.data.firstCellId } }
});
case 'modify':
// Undo for modify should reapply the outputs. Go through each and apply the update
let result = arg.prevState;
arg.payload.data.oldCells.forEach(c => {
result = updateCell({ ...disabledQueueArg, prevState: result, payload: { ...arg.payload, data: c } });
});
return result;

default:
// Modify, file, version can all be ignored.
// File, version can be ignored.
break;
}

Expand Down Expand Up @@ -307,6 +315,13 @@ export namespace Creation {
...disabledQueueArg,
payload: { ...arg.payload, data: { firstCellId: arg.payload.data.secondCellId, secondCellId: arg.payload.data.firstCellId } }
});
case 'modify':
// Redo for modify should reapply the outputs. Go through each and apply the update
let result = arg.prevState;
arg.payload.data.newCells.forEach(c => {
result = updateCell({ ...disabledQueueArg, prevState: result, payload: { ...arg.payload, data: c } });
});
return result;
default:
// Modify, file, version can all be ignored.
break;
Expand Down