Skip to content
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

Skip collapsed cells when selecting #6356

Merged
merged 3 commits into from May 15, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 32 additions & 3 deletions packages/notebook/src/actions.tsx
Expand Up @@ -644,6 +644,7 @@ export namespace NotebookActions {
* #### Notes
* The widget mode will be preserved.
* This is a no-op if the first cell is the active cell.
* This will skip any collapsed cells.
* The existing selection will be cleared.
*/
export function selectAbove(notebook: Notebook): void {
Expand All @@ -654,9 +655,22 @@ export namespace NotebookActions {
return;
}

let possibleNextCell = notebook.activeCellIndex - 1;

// find first non hidden cell above current cell
if (notebook.mode === 'edit') {
while (notebook.widgets[possibleNextCell].inputHidden) {
// If we are at the top cell, we cannot change selection.
if (possibleNextCell === 0) {
return;
}
possibleNextCell -= 1;
}
}

const state = Private.getState(notebook);

notebook.activeCellIndex -= 1;
notebook.activeCellIndex = possibleNextCell;
notebook.deselectAll();
Private.handleState(notebook, state, true);
}
Expand All @@ -669,19 +683,34 @@ export namespace NotebookActions {
* #### Notes
* The widget mode will be preserved.
* This is a no-op if the last cell is the active cell.
* This will skip any collapsed cells.
* The existing selection will be cleared.
*/
export function selectBelow(notebook: Notebook): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
if (notebook.activeCellIndex === notebook.widgets.length - 1) {
const maxCellIndex = notebook.widgets.length - 1;
if (notebook.activeCellIndex === maxCellIndex) {
return;
}

let possibleNextCell = notebook.activeCellIndex + 1;

// find first non hidden cell below current cell
if (notebook.mode === 'edit') {
while (notebook.widgets[possibleNextCell].inputHidden) {
// If we are at the bottom cell, we cannot change selection.
if (possibleNextCell === maxCellIndex) {
return;
}
possibleNextCell += 1;
}
}

const state = Private.getState(notebook);

notebook.activeCellIndex += 1;
notebook.activeCellIndex = possibleNextCell;
notebook.deselectAll();
Private.handleState(notebook, state, true);
}
Expand Down
58 changes: 57 additions & 1 deletion tests/test-notebook/src/actions.spec.ts
Expand Up @@ -797,7 +797,7 @@ describe('@jupyterlab/notebook', () => {
}).timeout(60000); // Allow for slower CI
});

describe('#selectAbove(`)', () => {
describe('#selectAbove()', () => {
it('should select the cell above the active cell', () => {
widget.activeCellIndex = 1;
NotebookActions.selectAbove(widget);
Expand All @@ -823,6 +823,34 @@ describe('@jupyterlab/notebook', () => {
NotebookActions.selectAbove(widget);
expect(widget.mode).to.equal('edit');
});

it('should skip collapsed cells in edit mode', () => {
widget.activeCellIndex = 3;
widget.mode = 'edit';
widget.widgets[1].inputHidden = true;
widget.widgets[2].inputHidden = true;
widget.widgets[3].inputHidden = false;
NotebookActions.selectAbove(widget);
expect(widget.activeCellIndex).to.equal(0);
});

it('should not change if in edit mode and no non-collapsed cells above', () => {
widget.activeCellIndex = 1;
widget.mode = 'edit';
widget.widgets[0].inputHidden = true;
NotebookActions.selectAbove(widget);
expect(widget.activeCellIndex).to.equal(1);
});

it('should not skip collapsed cells and in command mode', () => {
widget.activeCellIndex = 3;
widget.mode = 'command';
widget.widgets[1].inputHidden = true;
widget.widgets[2].inputHidden = true;
widget.widgets[3].inputHidden = false;
NotebookActions.selectAbove(widget);
expect(widget.activeCellIndex).to.equal(2);
});
});

describe('#selectBelow()', () => {
Expand Down Expand Up @@ -851,6 +879,34 @@ describe('@jupyterlab/notebook', () => {
NotebookActions.selectBelow(widget);
expect(widget.mode).to.equal('edit');
});

it('should skip collapsed cells in edit mode', () => {
widget.activeCellIndex = 0;
widget.mode = 'edit';
widget.widgets[1].inputHidden = true;
widget.widgets[2].inputHidden = true;
widget.widgets[3].inputHidden = false;
NotebookActions.selectBelow(widget);
expect(widget.activeCellIndex).to.equal(3);
});

it('should not change if in edit mode and no non-collapsed cells below', () => {
widget.activeCellIndex = widget.widgets.length - 2;
widget.mode = 'edit';
widget.widgets[widget.widgets.length - 1].inputHidden = true;
NotebookActions.selectBelow(widget);
expect(widget.activeCellIndex).to.equal(widget.widgets.length - 2);
});

it('should not skip collapsed cells and in command mode', () => {
widget.activeCellIndex = 0;
widget.mode = 'command';
widget.widgets[1].inputHidden = true;
widget.widgets[2].inputHidden = true;
widget.widgets[3].inputHidden = false;
NotebookActions.selectBelow(widget);
expect(widget.activeCellIndex).to.equal(1);
});
});

describe('#extendSelectionAbove()', () => {
Expand Down