Skip to content

Commit

Permalink
Undo/Redo should restore focus state
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Apr 23, 2020
1 parent b9eafe5 commit ca16acf
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 17 deletions.
Expand Up @@ -193,6 +193,14 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> implements ID
}
}));

this._viewModelStore.add(model.onDidChangeSelection(() => {
// convert model selections to view selections
const viewSelections = model.selectionHandles.map(handle => {
return model.getCellByHandle(handle);
}).filter(cell => !!cell).map(cell => this._getViewIndexUpperBound(cell!));
this.setFocus(viewSelections);
}));

const hiddenRanges = model.getHiddenRanges();
this.setHiddenAreas(hiddenRanges, false);
const newRanges = reduceCellRanges(hiddenRanges);
Expand Down Expand Up @@ -356,7 +364,7 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> implements ID

setFocus(indexes: number[], browserEvent?: UIEvent): void {
if (this._viewModel) {
this._viewModel.selections = indexes.map(index => this.element(index)).map(cell => cell.handle);
this._viewModel.selectionHandles = indexes.map(index => this.element(index)).map(cell => cell.handle);
}

super.setFocus(indexes, browserEvent);
Expand Down
25 changes: 21 additions & 4 deletions src/vs/workbench/contrib/notebook/browser/viewModel/cellEdit.ts
Expand Up @@ -17,6 +17,7 @@ export interface ICellEditingDelegate {
deleteCell?(index: number): void;
moveCell?(fromIndex: number, toIndex: number): void;
createCellViewModel?(cell: ICell): BaseCellViewModel;
setSelections(selections: number[]): void;
}

export class InsertCellEdit implements IResourceUndoRedoElement {
Expand All @@ -26,7 +27,9 @@ export class InsertCellEdit implements IResourceUndoRedoElement {
public resource: URI,
private insertIndex: number,
private cell: BaseCellViewModel,
private editingDelegate: ICellEditingDelegate
private editingDelegate: ICellEditingDelegate,
private beforedSelections: number[],
private endSelections: number[]
) {
}

Expand All @@ -36,13 +39,15 @@ export class InsertCellEdit implements IResourceUndoRedoElement {
}

this.editingDelegate.deleteCell(this.insertIndex);
this.editingDelegate.setSelections(this.beforedSelections);
}
redo(): void | Promise<void> {
if (!this.editingDelegate.insertCell) {
throw new Error('Notebook Insert Cell not implemented for Undo/Redo');
}

this.editingDelegate.insertCell(this.insertIndex, this.cell);
this.editingDelegate.setSelections(this.endSelections);
}
}

Expand All @@ -55,7 +60,9 @@ export class DeleteCellEdit implements IResourceUndoRedoElement {
public resource: URI,
private insertIndex: number,
cell: BaseCellViewModel,
private editingDelegate: ICellEditingDelegate
private editingDelegate: ICellEditingDelegate,
private beforedSelections: number[],
private endSelections: number[]
) {
this._rawCell = cell.model;

Expand All @@ -70,6 +77,7 @@ export class DeleteCellEdit implements IResourceUndoRedoElement {

const cell = this.editingDelegate.createCellViewModel(this._rawCell);
this.editingDelegate.insertCell(this.insertIndex, cell);
this.editingDelegate.setSelections(this.beforedSelections);
}

redo(): void | Promise<void> {
Expand All @@ -78,6 +86,7 @@ export class DeleteCellEdit implements IResourceUndoRedoElement {
}

this.editingDelegate.deleteCell(this.insertIndex);
this.editingDelegate.setSelections(this.endSelections);
}
}

Expand All @@ -89,7 +98,9 @@ export class MoveCellEdit implements IResourceUndoRedoElement {
public resource: URI,
private fromIndex: number,
private toIndex: number,
private editingDelegate: ICellEditingDelegate
private editingDelegate: ICellEditingDelegate,
private beforedSelections: number[],
private endSelections: number[]
) {
}

Expand All @@ -99,6 +110,7 @@ export class MoveCellEdit implements IResourceUndoRedoElement {
}

this.editingDelegate.moveCell(this.toIndex, this.fromIndex);
this.editingDelegate.setSelections(this.beforedSelections);
}

redo(): void | Promise<void> {
Expand All @@ -107,6 +119,7 @@ export class MoveCellEdit implements IResourceUndoRedoElement {
}

this.editingDelegate.moveCell(this.fromIndex, this.toIndex);
this.editingDelegate.setSelections(this.endSelections);
}
}

Expand All @@ -116,7 +129,9 @@ export class SpliceCellsEdit implements IResourceUndoRedoElement {
constructor(
public resource: URI,
private diffs: [number, CellViewModel[], CellViewModel[]][],
private editingDelegate: ICellEditingDelegate
private editingDelegate: ICellEditingDelegate,
private beforeHandles: number[],
private endHandles: number[]
) {
}

Expand All @@ -134,6 +149,7 @@ export class SpliceCellsEdit implements IResourceUndoRedoElement {
this.editingDelegate.insertCell!(diff[0], cell);
});
});
this.editingDelegate.setSelections(this.beforeHandles);
}

redo(): void | Promise<void> {
Expand All @@ -151,5 +167,6 @@ export class SpliceCellsEdit implements IResourceUndoRedoElement {
});
});

this.editingDelegate.setSelections(this.endHandles);
}
}
Expand Up @@ -127,6 +127,20 @@ function _normalizeOptions(options: IModelDecorationOptions): ModelDecorationOpt
return ModelDecorationOptions.createDynamic(options);
}

function selectionsEqual(a: number[], b: number[]) {
if (a.length !== b.length) {
return false;
}

for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}

return true;
}

let MODEL_ID = 0;


Expand Down Expand Up @@ -197,15 +211,24 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
return this._layoutInfo;
}

private readonly _onDidChangeSelection = new Emitter<void>();
get onDidChangeSelection(): Event<void> { return this._onDidChangeSelection.event; }

private _selections: number[] = [];

get selections() {
get selectionHandles() {
return this._selections;
}

set selections(selections: number[]) {
set selectionHandles(selections: number[]) {
selections = selections.sort();
if (selectionsEqual(selections, this.selectionHandles)) {
return;
}

this._selections = selections;
this._model.notebook.selections = selections;
this._onDidChangeSelection.fire();
}

private _decorationsTree = new DecorationsTree();
Expand Down Expand Up @@ -257,10 +280,39 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
splices: diffs
});

let endSelectionHandles: number[] = [];
if (this.selectionHandles.length) {
const primaryHandle = this.selectionHandles[0];
const primarySelectionIndex = this._viewCells.indexOf(this.getCellByHandle(primaryHandle)!);
endSelectionHandles = [primaryHandle];
let delta = 0;

for (let i = 0; i < diffs.length; i++) {
const diff = diffs[0];
if (diff[0] + diff[1] <= primarySelectionIndex) {
delta += diff[2].length - diff[1];
continue;
}

if (diff[0] > primarySelectionIndex) {
endSelectionHandles = [primaryHandle];
break;
}

if (diff[0] + diff[1] > primaryHandle) {
endSelectionHandles = [this._viewCells[diff[0] + delta].handle];
break;
}
}
}

this.undoService.pushElement(new SpliceCellsEdit(this.uri, undoDiff, {
insertCell: this._insertCellDelegate.bind(this),
deleteCell: this._deleteCellDelegate.bind(this)
}));
deleteCell: this._deleteCellDelegate.bind(this),
setSelections: this._setSelectionsDelegate.bind(this)
}, this.selectionHandles, endSelectionHandles));

this.selectionHandles = endSelectionHandles;
}));

this._register(this._model.notebook.onDidChangeMetadata(e => {
Expand Down Expand Up @@ -379,6 +431,10 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
});
}

getCellByHandle(handle: number) {
return this._handleToViewCellMapping.get(handle);
}

getCellIndex(cell: ICellViewModel) {
return this._viewCells.indexOf(cell as CellViewModel);
}
Expand Down Expand Up @@ -517,17 +573,23 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
this._onDidChangeViewCells.fire({ synchronous: true, splices: [[deleteIndex, 1, []]] });
}

private _setSelectionsDelegate(selections: number[]) {
this.selectionHandles = selections;
}

createCell(index: number, source: string[], language: string, type: CellKind, synchronous: boolean) {
const cell = this._model.notebook.createCellTextModel(source, language, type, [], undefined);
let newCell: CellViewModel = createCellViewModel(this.instantiationService, this, cell);
this._viewCells!.splice(index, 0, newCell);
this._handleToViewCellMapping.set(newCell.handle, newCell);
this._model.insertCell(cell, index);
this._localStore.add(newCell);

this.undoService.pushElement(new InsertCellEdit(this.uri, index, newCell, {
insertCell: this._insertCellDelegate.bind(this),
deleteCell: this._deleteCellDelegate.bind(this)
}));
deleteCell: this._deleteCellDelegate.bind(this),
setSelections: this._setSelectionsDelegate.bind(this)
}, this.selectionHandles, this.selectionHandles));

this._decorationsTree.acceptReplace(index, 0, 1, true);
this._onDidChangeViewCells.fire({ synchronous: synchronous, splices: [[index, 0, [newCell]]] });
Expand All @@ -543,29 +605,51 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
this._localStore.add(newCell);
this.undoService.pushElement(new InsertCellEdit(this.uri, index, newCell, {
insertCell: this._insertCellDelegate.bind(this),
deleteCell: this._deleteCellDelegate.bind(this)
}));
deleteCell: this._deleteCellDelegate.bind(this),
setSelections: this._setSelectionsDelegate.bind(this)
}, this.selectionHandles, this.selectionHandles));

this._decorationsTree.acceptReplace(index, 0, 1, true);
this._onDidChangeViewCells.fire({ synchronous: synchronous, splices: [[index, 0, [newCell]]] });
return newCell;
}

deleteCell(index: number, synchronous: boolean) {
const primarySelectionIndex = this.selectionHandles.length ? this._viewCells.indexOf(this.getCellByHandle(this.selectionHandles[0])!) : null;

let viewCell = this._viewCells[index];
this._viewCells.splice(index, 1);
this._handleToViewCellMapping.delete(viewCell.handle);

this._model.deleteCell(index);

let endSelections: number[] = [];
if (this.selectionHandles.length) {
const primarySelectionHandle = this.selectionHandles[0];

if (index === primarySelectionIndex) {
if (primarySelectionIndex < this.length - 1) {
endSelections = [this._viewCells[primarySelectionIndex + 1].handle];
} else if (primarySelectionIndex === this.length - 1 && this.length > 1) {
endSelections = [this._viewCells[primarySelectionIndex - 1].handle];
} else {
endSelections = [];
}
} else {
endSelections = [primarySelectionHandle];
}
}

this.undoService.pushElement(new DeleteCellEdit(this.uri, index, viewCell, {
insertCell: this._insertCellDelegate.bind(this),
deleteCell: this._deleteCellDelegate.bind(this),
createCellViewModel: (cell: NotebookCellTextModel) => {
return createCellViewModel(this.instantiationService, this, cell);
}
}));
},
setSelections: this._setSelectionsDelegate.bind(this)
}, this.selectionHandles, endSelections));

this.selectionHandles = endSelections;

this._decorationsTree.acceptReplace(index, 1, 0, true);

Expand All @@ -589,10 +673,13 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
this.undoService.pushElement(new MoveCellEdit(this.uri, index, newIdx, {
moveCell: (fromIndex: number, toIndex: number) => {
this.moveCellToIdx(fromIndex, toIndex, true, false);
}
}));
},
setSelections: this._setSelectionsDelegate.bind(this)
}, this.selectionHandles, this.selectionHandles));
}

this.selectionHandles = this.selectionHandles;

this._onDidChangeViewCells.fire({ synchronous: synchronous, splices: [[index, 1, []]] });
this._onDidChangeViewCells.fire({ synchronous: synchronous, splices: [[newIdx, 0, [viewCell]]] });

Expand Down

0 comments on commit ca16acf

Please sign in to comment.