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
10 changes: 10 additions & 0 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 @@ -166,10 +168,18 @@ export const SetupCommands = (
label: 'Extend Above',
execute: () => NotebookActions.extendSelectionAbove(nbWidget.content)
});
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)
});
commands.addCommand(cmdIds.extendBottom, {
label: 'Extend to Bottom',
execute: () => NotebookActions.extendSelectionBelow(nbWidget.content, true)
});
commands.addCommand(cmdIds.merge, {
label: 'Merge Cells',
execute: () => NotebookActions.mergeCells(nbWidget.content)
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 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 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
14 changes: 14 additions & 0 deletions tests/test-notebook/src/actions.spec.ts
Expand Up @@ -916,6 +916,14 @@ describe('@jupyterlab/notebook', () => {
expect(widget.isSelected(widget.widgets[0])).to.equal(true);
});

it('should extend the selection to the topmost cell', () => {
widget.activeCellIndex = 1;
NotebookActions.extendSelectionAbove(widget, true);
for (let i = widget.activeCellIndex; i >= 0; i--) {
expect(widget.isSelected(widget.widgets[i])).to.equal(true);
}
});

it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.extendSelectionAbove(widget);
Expand Down Expand Up @@ -968,6 +976,12 @@ describe('@jupyterlab/notebook', () => {
expect(widget.isSelected(widget.widgets[1])).to.equal(true);
});

it('should extend the selection the bottomost cell', () => {
NotebookActions.extendSelectionBelow(widget, true);
for (let i = widget.activeCellIndex; i < widget.widgets.length; i++) {
expect(widget.isSelected(widget.widgets[i])).to.equal(true);
}
});
it('should be a no-op if there is no model', () => {
widget.model = null;
NotebookActions.extendSelectionBelow(widget);
Expand Down