Skip to content

Commit

Permalink
Merge pull request #6356 from saulshanabrook/collapsed
Browse files Browse the repository at this point in the history
Skip collapsed cells when selecting
  • Loading branch information
blink1073 committed May 15, 2019
2 parents b58e1dd + 8e8ce16 commit 223c63e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
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

0 comments on commit 223c63e

Please sign in to comment.