cut aligns with delete cells.

This commit is contained in:
rebornix 2021-04-01 11:52:55 -07:00
parent ae6d1a7328
commit e2a0fe60b2
No known key found for this signature in database
GPG key ID: 181FC90D15393C20
2 changed files with 67 additions and 0 deletions

View file

@ -156,6 +156,23 @@ export function runCutCells(accessor: ServicesAccessor, editor: INotebookEditor,
}
}
const focus = viewModel.getFocus();
const containingSelection = selections.find(selection => selection.start <= focus.start && focus.end <= selection.end);
if (!containingSelection) {
// focus is out of any selection, we should only cut this cell
const targetCell = viewModel.cellAt(focus.start)!;
clipboardService.writeText(targetCell.getText());
const newFocus = focus.end === viewModel.length ? { start: focus.start - 1, end: focus.end - 1 } : focus;
const newSelections = selections.map(selection => (selection.end <= focus.start ? selection : { start: selection.start - 1, end: selection.end - 1 }));
viewModel.notebookDocument.applyEdits([
{ editType: CellEditType.Replace, index: focus.start, count: 1, cells: [] }
], true, { kind: SelectionStateType.Index, focus: viewModel.getFocus(), selections: selections }, () => ({ kind: SelectionStateType.Index, focus: newFocus, selections: newSelections }), undefined, true);
notebookService.setToCopy([targetCell.model], false);
return true;
}
const selectionRanges = expandCellRangesWithHiddenCells(editor, viewModel, viewModel.getSelections());
const selectedCells = cellRangeToViewCells(viewModel, selectionRanges);

View file

@ -251,4 +251,54 @@ suite('Notebook Clipboard', () => {
assert.deepStrictEqual(viewModel.getSelections(), [{ start: 1, end: 3 }]);
});
});
test('cut focus cell still works if the focus is not part of any selection', async () => {
await withTestNotebook(
[
['# header 1', 'markdown', CellKind.Markdown, [], {}],
['paragraph 1', 'markdown', CellKind.Markdown, [], {}],
['paragraph 2', 'markdown', CellKind.Markdown, [], {}],
['paragraph 3', 'markdown', CellKind.Markdown, [], {}],
],
async (editor, accessor) => {
accessor.stub(INotebookService, new class extends mock<INotebookService>() {
setToCopy() { }
getToCopy() {
return { items: [], isCopy: true };
}
});
const viewModel = editor.viewModel;
viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 0, end: 1 }, selections: [{ start: 2, end: 4 }] }, 'model');
assert.ok(runCutCells(accessor, editor, undefined));
assert.strictEqual(viewModel.length, 3);
assert.deepStrictEqual(viewModel.getFocus(), { start: 0, end: 1 });
assert.deepStrictEqual(viewModel.getSelections(), [{ start: 1, end: 3 }]);
});
});
test('cut focus cell still works if the focus is not part of any selection 2', async () => {
await withTestNotebook(
[
['# header 1', 'markdown', CellKind.Markdown, [], {}],
['paragraph 1', 'markdown', CellKind.Markdown, [], {}],
['paragraph 2', 'markdown', CellKind.Markdown, [], {}],
['paragraph 3', 'markdown', CellKind.Markdown, [], {}],
],
async (editor, accessor) => {
accessor.stub(INotebookService, new class extends mock<INotebookService>() {
setToCopy() { }
getToCopy() {
return { items: [], isCopy: true };
}
});
const viewModel = editor.viewModel;
viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 3, end: 4 }, selections: [{ start: 0, end: 2 }] }, 'model');
assert.ok(runCutCells(accessor, editor, undefined));
assert.strictEqual(viewModel.length, 3);
assert.deepStrictEqual(viewModel.getFocus(), { start: 2, end: 3 });
assert.deepStrictEqual(viewModel.getSelections(), [{ start: 0, end: 2 }]);
});
});
});