From b811a179bfae1ab27142cab31d08e6c5de8dc0f9 Mon Sep 17 00:00:00 2001 From: Jason Grout Date: Fri, 11 Oct 2019 23:18:42 -0700 Subject: [PATCH] Backport PR #7177: Added the Selection of cells till Top and Bottom --- examples/notebook/src/commands.ts | 10 +++++++ .../notebook-extension/schema/tracker.json | 10 +++++++ packages/notebook-extension/src/index.ts | 28 +++++++++++++++++++ packages/notebook/src/actions.tsx | 26 ++++++++++++++--- tests/test-notebook/src/actions.spec.ts | 14 ++++++++++ 5 files changed, 84 insertions(+), 4 deletions(-) diff --git a/examples/notebook/src/commands.ts b/examples/notebook/src/commands.ts index 568c435e76e1..30ba8608b1c0 100644 --- a/examples/notebook/src/commands.ts +++ b/examples/notebook/src/commands.ts @@ -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', @@ -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) diff --git a/packages/notebook-extension/schema/tracker.json b/packages/notebook-extension/schema/tracker.json index 0688b0e88ce5..bc75cb91b87b 100644 --- a/packages/notebook-extension/schema/tracker.json +++ b/packages/notebook-extension/schema/tracker.json @@ -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"], diff --git a/packages/notebook-extension/src/index.ts b/packages/notebook-extension/src/index.ts index 95886e4699df..6dc045b62971 100644 --- a/packages/notebook-extension/src/index.ts +++ b/packages/notebook-extension/src/index.ts @@ -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'; @@ -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 => { @@ -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 => { @@ -1922,7 +1948,9 @@ function populatePalette( CommandIDs.selectAbove, CommandIDs.selectBelow, CommandIDs.extendAbove, + CommandIDs.extendTop, CommandIDs.extendBelow, + CommandIDs.extendBottom, CommandIDs.moveDown, CommandIDs.moveUp, CommandIDs.undoCellAction, diff --git a/packages/notebook/src/actions.tsx b/packages/notebook/src/actions.tsx index 1a532c880e9c..cf87328a6530 100644 --- a/packages/notebook/src/actions.tsx +++ b/packages/notebook/src/actions.tsx @@ -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; } @@ -736,7 +740,12 @@ 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); } @@ -744,12 +753,16 @@ export namespace NotebookActions { * 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; } @@ -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); } diff --git a/tests/test-notebook/src/actions.spec.ts b/tests/test-notebook/src/actions.spec.ts index 547071b529a6..ce8fcd5e1a90 100644 --- a/tests/test-notebook/src/actions.spec.ts +++ b/tests/test-notebook/src/actions.spec.ts @@ -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); @@ -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);