Skip to content

Commit

Permalink
Merge pull request #7177 from bw-space/master
Browse files Browse the repository at this point in the history
Added the Selection of cells till Top and Bottom
  • Loading branch information
jasongrout committed Oct 12, 2019
2 parents e04e3af + 5e33ad4 commit c9d9a39
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
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 @@ -1466,6 +1470,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 @@ -1477,6 +1492,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 @@ -1922,7 +1948,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 @@ -918,6 +918,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 @@ -970,6 +978,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

0 comments on commit c9d9a39

Please sign in to comment.