diff --git a/packages/notebook/src/actions.tsx b/packages/notebook/src/actions.tsx index 0ba677f0d80e..a3108458dcbb 100644 --- a/packages/notebook/src/actions.tsx +++ b/packages/notebook/src/actions.tsx @@ -555,6 +555,8 @@ export namespace NotebookActions { * @param translator - The application translator. * * #### Notes + * The existing selection will be preserved. + * The mode will be changed to command. * An execution error will prevent the remaining code cells from executing. * All markdown cells will be rendered. */ @@ -728,6 +730,7 @@ export namespace NotebookActions { } const state = Private.getState(notebook); + const lastIndex = notebook.widgets.length; const promise = Private.runCells( notebook, @@ -737,6 +740,9 @@ export namespace NotebookActions { translator ); + notebook.activeCellIndex = lastIndex; + notebook.deselectAll(); + Private.handleRunState(notebook, state, true); return promise; } @@ -800,6 +806,8 @@ export namespace NotebookActions { translator ); + notebook.deselectAll(); + Private.handleRunState(notebook, state, true); return promise; } @@ -2223,6 +2231,8 @@ namespace Private { translator?: ITranslator ): Promise { const lastCell = cells[-1]; + notebook.mode = 'command'; + return Promise.all( cells.map(cell => runCell(notebook, cell, sessionContext, sessionDialogs, translator) diff --git a/packages/notebook/test/actions.spec.ts b/packages/notebook/test/actions.spec.ts index c7f0e8d29ddf..b91ac65391c7 100644 --- a/packages/notebook/test/actions.spec.ts +++ b/packages/notebook/test/actions.spec.ts @@ -1000,6 +1000,36 @@ describe('@jupyterlab/notebook', () => { }); }); + describe('#runCells()', () => { + beforeEach(() => { + // Make sure all cells have valid code. + widget.widgets[2].model.sharedModel.setSource('a = 1'); + }); + + it('should change to command mode', async () => { + widget.mode = 'edit'; + const result = await NotebookActions.runCells( + widget, + [widget.widgets[2]], + sessionContext + ); + expect(result).toBe(true); + expect(widget.mode).toBe('command'); + }); + + it('should preserve the existing selection', async () => { + const next = widget.widgets[2]; + widget.select(next); + const result = await NotebookActions.runCells( + widget, + [widget.widgets[1]], + sessionContext + ); + expect(result).toBe(true); + expect(widget.isSelected(widget.widgets[2])).toBe(true); + }); + }); + describe('#runAll()', () => { beforeEach(() => { // Make sure all cells have valid code.