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

Added the Selection of cells till Top and Bottom #7177

Merged
merged 8 commits into from Oct 12, 2019
14 changes: 12 additions & 2 deletions examples/notebook/src/commands.ts
Expand Up @@ -30,7 +30,9 @@ const cmdIds = {
selectAbove: 'notebook-cells:select-above',
selectBelow: 'notebook-cells:select-below',
extendAbove: 'notebook-cells:extend-above',
extendTop: 'notebook-cells:extend-top',
extendBelow: 'notebook-cells:extend-below',
extendBottom: 'notebook-cells:extend-bottom',
editMode: 'notebook:edit-mode',
merge: 'notebook-cells:merge',
split: 'notebook-cells:split',
Expand Down Expand Up @@ -164,11 +166,19 @@ export const SetupCommands = (
});
commands.addCommand(cmdIds.extendAbove, {
label: 'Extend Above',
execute: () => NotebookActions.extendSelectionAbove(nbWidget.content)
execute: () => NotebookActions.extendSelectionAbove(nbWidget.content, false)
jasongrout marked this conversation as resolved.
Show resolved Hide resolved
});
commands.addCommand(cmdIds.extendTop, {
label: 'Extend to Top',
execute: () => NotebookActions.extendSelectionAbove(nbWidget.content, true)
});
commands.addCommand(cmdIds.extendBelow, {
label: 'Extend Below',
execute: () => NotebookActions.extendSelectionBelow(nbWidget.content)
execute: () => NotebookActions.extendSelectionBelow(nbWidget.content, false)
jasongrout marked this conversation as resolved.
Show resolved Hide resolved
});
commands.addCommand(cmdIds.extendBottom, {
label: 'Extend to Bottom',
execute: () => NotebookActions.extendSelectionBelow(nbWidget.content, true)
});
commands.addCommand(cmdIds.merge, {
label: 'Merge Cells',
Expand Down
10 changes: 10 additions & 0 deletions packages/notebook-extension/schema/tracker.json
Expand Up @@ -87,11 +87,21 @@
"keys": ["Shift K"],
"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:extend-marked-cells-top",
"keys": ["Shift PageUp"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"keys": ["Shift PageUp"],
"keys": ["Shift Home"],

"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:extend-marked-cells-below",
"keys": ["Shift ArrowDown"],
"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:extend-marked-cells-bottom",
"keys": ["Shift PageDown"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"keys": ["Shift PageDown"],
"keys": ["Shift End"],

"selector": ".jp-Notebook:focus"
},
{
"command": "notebook:extend-marked-cells-below",
"keys": ["Shift J"],
Expand Down
28 changes: 28 additions & 0 deletions packages/notebook-extension/src/index.ts
Expand Up @@ -157,8 +157,12 @@ namespace CommandIDs {

export const extendAbove = 'notebook:extend-marked-cells-above';

export const extendTop = 'notebook:extend-marked-cells-top';

export const extendBelow = 'notebook:extend-marked-cells-below';

export const extendBottom = 'notebook:extend-marked-cells-bottom';

export const selectAll = 'notebook:select-all';

export const deselectAll = 'notebook:deselect-all';
Expand Down Expand Up @@ -1464,6 +1468,17 @@ function addCommands(
},
isEnabled
});
commands.addCommand(CommandIDs.extendTop, {
label: 'Extend Selection to Top',
execute: args => {
const current = getCurrent(args);

if (current) {
return NotebookActions.extendSelectionAbove(current.content, true);
}
},
isEnabled
});
commands.addCommand(CommandIDs.extendBelow, {
label: 'Extend Selection Below',
execute: args => {
Expand All @@ -1475,6 +1490,17 @@ function addCommands(
},
isEnabled
});
commands.addCommand(CommandIDs.extendBottom, {
label: 'Extend Selection to Bottom',
execute: args => {
const current = getCurrent(args);

if (current) {
return NotebookActions.extendSelectionBelow(current.content, true);
}
},
isEnabled
});
commands.addCommand(CommandIDs.selectAll, {
label: 'Select All Cells',
execute: args => {
Expand Down Expand Up @@ -1908,7 +1934,9 @@ function populatePalette(
CommandIDs.selectAbove,
CommandIDs.selectBelow,
CommandIDs.extendAbove,
CommandIDs.extendTop,
CommandIDs.extendBelow,
CommandIDs.extendBottom,
CommandIDs.moveDown,
CommandIDs.moveUp,
CommandIDs.undoCellAction,
Expand Down
26 changes: 22 additions & 4 deletions packages/notebook/src/actions.tsx
Expand Up @@ -719,12 +719,16 @@ export namespace NotebookActions {
* Extend the selection to the cell above.
*
* @param notebook - The target notebook widget.
* @param toTop - If true, denotes selection to extend to the top.
*
* #### Notes
* This is a no-op if the first cell is the active cell.
* The new cell will be activated.
*/
export function extendSelectionAbove(notebook: Notebook): void {
export function extendSelectionAbove(
notebook: Notebook,
toTop: boolean = false
): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
Expand All @@ -736,20 +740,29 @@ export namespace NotebookActions {
const state = Private.getState(notebook);

notebook.mode = 'command';
notebook.extendContiguousSelectionTo(notebook.activeCellIndex - 1);
// Check if toTop is true, if yes, selection is made to the top.
if (toTop) {
notebook.extendContiguousSelectionTo(0);
} else {
notebook.extendContiguousSelectionTo(notebook.activeCellIndex - 1);
}
Private.handleState(notebook, state, true);
}

/**
* Extend the selection to the cell below.
*
* @param notebook - The target notebook widget.
* @param toBottom - If true, denotes selection to extend to the bottom.
*
* #### Notes
* This is a no-op if the last cell is the active cell.
* The new cell will be activated.
*/
export function extendSelectionBelow(notebook: Notebook): void {
export function extendSelectionBelow(
notebook: Notebook,
toBottom: boolean = false
): void {
if (!notebook.model || !notebook.activeCell) {
return;
}
Expand All @@ -761,7 +774,12 @@ export namespace NotebookActions {
const state = Private.getState(notebook);

notebook.mode = 'command';
notebook.extendContiguousSelectionTo(notebook.activeCellIndex + 1);
// Check if toBottom is true, if yes selection is made to the bottom.
if (toBottom) {
notebook.extendContiguousSelectionTo(notebook.widgets.length - 1);
} else {
notebook.extendContiguousSelectionTo(notebook.activeCellIndex + 1);
}
Private.handleState(notebook, state, true);
}

Expand Down
44 changes: 22 additions & 22 deletions tests/test-notebook/src/actions.spec.ts
Expand Up @@ -912,79 +912,79 @@ describe('@jupyterlab/notebook', () => {
describe('#extendSelectionAbove()', () => {
it('should extend the selection to the cell above', () => {
widget.activeCellIndex = 1;
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
jasongrout marked this conversation as resolved.
Show resolved Hide resolved
expect(widget.isSelected(widget.widgets[0])).to.equal(true);
jasongrout marked this conversation as resolved.
Show resolved Hide resolved
});

it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
expect(widget.activeCellIndex).to.equal(-1);
});

it('should change to command mode if there is a selection', () => {
widget.mode = 'edit';
widget.activeCellIndex = 1;
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
expect(widget.mode).to.equal('command');
});

it('should not wrap around to the bottom', () => {
widget.mode = 'edit';
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
expect(widget.activeCellIndex).to.equal(0);
const last = widget.widgets[widget.widgets.length - 1];
expect(widget.isSelected(last)).to.equal(false);
expect(widget.mode).to.equal('edit');
});

it('should deselect the current cell if the cell above is selected', () => {
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
NotebookActions.extendSelectionBelow(widget, false);
const cell = widget.activeCell;
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
expect(widget.isSelected(cell)).to.equal(false);
});

it('should select only the first cell if we move from the second to first', () => {
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
const cell = widget.activeCell;
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
expect(widget.isSelected(cell)).to.equal(false);
expect(widget.activeCellIndex).to.equal(0);
});

it('should activate the cell', () => {
widget.activeCellIndex = 1;
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
expect(widget.activeCellIndex).to.equal(0);
});
});

describe('#extendSelectionBelow()', () => {
it('should extend the selection to the cell below', () => {
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
jasongrout marked this conversation as resolved.
Show resolved Hide resolved
expect(widget.isSelected(widget.widgets[0])).to.equal(true);
expect(widget.isSelected(widget.widgets[1])).to.equal(true);
});

it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
expect(widget.activeCellIndex).to.equal(-1);
});

it('should change to command mode if there is a selection', () => {
widget.mode = 'edit';
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
expect(widget.mode).to.equal('command');
});

it('should not wrap around to the top', () => {
const last = widget.widgets.length - 1;
widget.activeCellIndex = last;
widget.mode = 'edit';
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
expect(widget.activeCellIndex).to.equal(last);
expect(widget.isSelected(widget.widgets[0])).to.equal(false);
expect(widget.mode).to.equal('edit');
Expand All @@ -993,33 +993,33 @@ describe('@jupyterlab/notebook', () => {
it('should deselect the current cell if the cell below is selected', () => {
const last = widget.widgets.length - 1;
widget.activeCellIndex = last;
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
NotebookActions.extendSelectionAbove(widget, false);
const current = widget.activeCell;
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
expect(widget.isSelected(current)).to.equal(false);
});

it('should select only the last cell if we move from the second last to last', () => {
const last = widget.widgets.length - 1;
widget.activeCellIndex = last;
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
const current = widget.activeCell;
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
expect(widget.isSelected(current)).to.equal(false);
expect(widget.activeCellIndex).to.equal(last);
});

it('should activate the cell', () => {
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
expect(widget.activeCellIndex).to.equal(1);
});
});

describe('#moveUp()', () => {
it('should move the selected cells up', () => {
widget.activeCellIndex = 2;
NotebookActions.extendSelectionAbove(widget);
NotebookActions.extendSelectionAbove(widget, false);
NotebookActions.moveUp(widget);
expect(widget.isSelected(widget.widgets[0])).to.equal(true);
expect(widget.isSelected(widget.widgets[1])).to.equal(true);
Expand Down Expand Up @@ -1051,7 +1051,7 @@ describe('@jupyterlab/notebook', () => {

describe('#moveDown()', () => {
it('should move the selected cells down', () => {
NotebookActions.extendSelectionBelow(widget);
NotebookActions.extendSelectionBelow(widget, false);
NotebookActions.moveDown(widget);
expect(widget.isSelected(widget.widgets[0])).to.equal(false);
expect(widget.isSelected(widget.widgets[1])).to.equal(true);
Expand Down