浏览代码

Revert "Don’t activate the notebook widget when getting it as the current widget."

This reverts commit ed11dc9960bac0f3bd2f2870a3e6865d82cdb6c4.

Changing the behavior of getting the current widget this close to release is an risky thing. We can take a look at it after the release to more thoroughly examine the ramifications.
Jason Grout 6 年之前
父节点
当前提交
895a566eec
共有 1 个文件被更改,包括 71 次插入67 次删除
  1. 71 67
      packages/notebook-extension/src/index.ts

+ 71 - 67
packages/notebook-extension/src/index.ts

@@ -690,12 +690,16 @@ function activateNotebookHandler(app: JupyterLab, mainMenu: IMainMenu, palette:
 function addCommands(app: JupyterLab, services: ServiceManager, tracker: NotebookTracker): void {
   const { commands, shell } = app;
 
-  /**
-   * Get the current NotebookPanel widget
-   */
   // Get the current widget and activate unless the args specify otherwise.
-  function getCurrent(): NotebookPanel | null {
-    return tracker.currentWidget;
+  function getCurrent(args: ReadonlyJSONObject): NotebookPanel | null {
+    const widget = tracker.currentWidget;
+    const activate = args['activate'] !== false;
+
+    if (activate && widget) {
+      shell.activateById(widget.id);
+    }
+
+    return widget;
   }
 
   /**
@@ -726,7 +730,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.runAndAdvance, {
     label: 'Run Selected Cells',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { context, content } = current;
@@ -739,7 +743,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.run, {
     label: 'Run Selected Cells and Don\'t Advance',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { context, content } = current;
@@ -752,7 +756,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.runAndInsert, {
     label: 'Run Selected Cells and Insert Below',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { context, content } = current;
@@ -765,7 +769,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.runInConsole, {
     label: 'Run Selected Text or Current Line in Console',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { context, content } = current;
@@ -823,7 +827,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.runAll, {
     label: 'Run All Cells',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { context, content } = current;
@@ -836,7 +840,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.runAllAbove, {
     label: 'Run All Above Selected Cell',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { context, content } = current;
@@ -854,7 +858,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.runAllBelow, {
     label: 'Run Selected Cell and All Below',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { context, content } = current;
@@ -873,7 +877,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.restart, {
     label: 'Restart Kernel…',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return current.session.restart();
@@ -884,7 +888,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.closeAndShutdown, {
     label: 'Close and Shutdown',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (!current) {
         return;
@@ -908,7 +912,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.trust, {
     label: () => 'Trust Notebook',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { context, content } = current;
@@ -925,7 +929,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
         return (args['isPalette'] ? 'Export Notebook to ' : '') + formatLabel;
     },
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (!current) {
         return;
@@ -955,7 +959,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.restartClear, {
     label: 'Restart Kernel and Clear All Outputs…',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { content, session } = current;
@@ -969,7 +973,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.restartRunAll, {
     label: 'Restart Kernel and Run All Cells…',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         const { context, content, session } = current;
@@ -987,7 +991,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.clearAllOutputs, {
     label: 'Clear All Outputs',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.clearAllOutputs(current.content);
@@ -998,7 +1002,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.clearOutputs, {
     label: 'Clear Outputs',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.clearOutputs(current.content);
@@ -1009,7 +1013,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.interrupt, {
     label: 'Interrupt Kernel',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (!current) {
         return;
@@ -1026,7 +1030,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.toCode, {
     label: 'Change to Code Cell Type',
     execute: args => {
-      const current = getCurrent({ activate: !!args['activate'] });
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.changeCellType(current.content, 'code');
@@ -1037,7 +1041,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.toMarkdown, {
     label: 'Change to Markdown Cell Type',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.changeCellType(current.content, 'markdown');
@@ -1048,7 +1052,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.toRaw, {
     label: 'Change to Raw Cell Type',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.changeCellType(current.content, 'raw');
@@ -1059,7 +1063,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.cut, {
     label: 'Cut Cells',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.cut(current.content);
@@ -1070,7 +1074,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.copy, {
     label: 'Copy Cells',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.copy(current.content);
@@ -1081,7 +1085,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.pasteBelow, {
     label: 'Paste Cells Below',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.paste(current.content, 'below');
@@ -1092,7 +1096,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.pasteAbove, {
     label: 'Paste Cells Above',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.paste(current.content, 'above');
@@ -1103,7 +1107,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.pasteAndReplace, {
     label: 'Paste Cells and Replace',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.paste(current.content, 'replace');
@@ -1114,7 +1118,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.deleteCell, {
     label: 'Delete Cells',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.deleteCells(current.content);
@@ -1125,7 +1129,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.split, {
     label: 'Split Cell',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.splitCell(current.content);
@@ -1136,7 +1140,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.merge, {
     label: 'Merge Selected Cells',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.mergeCells(current.content);
@@ -1147,7 +1151,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.insertAbove, {
     label: 'Insert Cell Above',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.insertAbove(current.content);
@@ -1158,7 +1162,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.insertBelow, {
     label: 'Insert Cell Below',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.insertBelow(current.content);
@@ -1169,7 +1173,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.selectAbove, {
     label: 'Select Cell Above',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.selectAbove(current.content);
@@ -1180,7 +1184,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.selectBelow, {
     label: 'Select Cell Below',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.selectBelow(current.content);
@@ -1191,7 +1195,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.extendAbove, {
     label: 'Extend Selection Above',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.extendSelectionAbove(current.content);
@@ -1202,7 +1206,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.extendBelow, {
     label: 'Extend Selection Below',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.extendSelectionBelow(current.content);
@@ -1213,7 +1217,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.selectAll, {
     label: 'Select All Cells',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.selectAll(current.content);
@@ -1224,7 +1228,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.deselectAll, {
     label: 'Deselect All Cells',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.deselectAll(current.content);
@@ -1235,7 +1239,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.moveUp, {
     label: 'Move Cells Up',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.moveUp(current.content);
@@ -1246,7 +1250,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.moveDown, {
     label: 'Move Cells Down',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.moveDown(current.content);
@@ -1257,7 +1261,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.toggleAllLines, {
     label: 'Toggle All Line Numbers',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.toggleAllLineNumbers(current.content);
@@ -1268,7 +1272,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.commandMode, {
     label: 'Enter Command Mode',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         current.content.mode = 'command';
@@ -1279,7 +1283,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.editMode, {
     label: 'Enter Edit Mode',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         current.content.mode = 'edit';
@@ -1290,7 +1294,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.undoCellAction, {
     label: 'Undo Cell Operation',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.undo(current.content);
@@ -1301,7 +1305,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.redoCellAction, {
     label: 'Redo Cell Operation',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.redo(current.content);
@@ -1312,7 +1316,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.changeKernel, {
     label: 'Change Kernel…',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return current.context.session.selectKernel();
@@ -1323,7 +1327,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.reconnectToKernel, {
     label: 'Reconnect To Kernel',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (!current) {
         return;
@@ -1363,7 +1367,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.createConsole, {
     label: 'New Console for Notebook',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent({ ...args, activate: false });
       const widget = tracker.currentWidget;
 
       if (!current || !widget) {
@@ -1385,7 +1389,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.markdown1, {
     label: 'Change to Heading 1',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.setMarkdownHeader(current.content, 1);
@@ -1396,7 +1400,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.markdown2, {
     label: 'Change to Heading 2',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.setMarkdownHeader(current.content, 2);
@@ -1407,7 +1411,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.markdown3, {
     label: 'Change to Heading 3',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.setMarkdownHeader(current.content, 3);
@@ -1418,7 +1422,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.markdown4, {
     label: 'Change to Heading 4',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.setMarkdownHeader(current.content, 4);
@@ -1429,7 +1433,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.markdown5, {
     label: 'Change to Heading 5',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.setMarkdownHeader(current.content, 5);
@@ -1440,7 +1444,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.markdown6, {
     label: 'Change to Heading 6',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.setMarkdownHeader(current.content, 6);
@@ -1451,7 +1455,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.hideCode, {
     label: 'Collapse Selected Code',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.hideCode(current.content);
@@ -1462,7 +1466,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.showCode, {
     label: 'Expand Selected Code',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.showCode(current.content);
@@ -1473,7 +1477,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.hideAllCode, {
     label: 'Collapse All Code',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.hideAllCode(current.content);
@@ -1484,7 +1488,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.showAllCode, {
     label: 'Expand All Code',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.showAllCode(current.content);
@@ -1495,7 +1499,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.hideOutput, {
     label: 'Collapse Selected Outputs',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.hideOutput(current.content);
@@ -1506,7 +1510,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.showOutput, {
     label: 'Expand Selected Outputs',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.showOutput(current.content);
@@ -1517,7 +1521,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.hideAllOutputs, {
     label: 'Collapse All Outputs',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.hideAllOutputs(current.content);
@@ -1528,7 +1532,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.showAllOutputs, {
     label: 'Expand All Outputs',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.showAllOutputs(current.content);
@@ -1539,7 +1543,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.enableOutputScrolling, {
     label: 'Enable Scrolling for Outputs',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.enableOutputScrolling(current.content);
@@ -1550,7 +1554,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.disableOutputScrolling, {
     label: 'Disable Scrolling for Outputs',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         return NotebookActions.disableOutputScrolling(current.content);
@@ -1561,7 +1565,7 @@ function addCommands(app: JupyterLab, services: ServiceManager, tracker: Noteboo
   commands.addCommand(CommandIDs.saveWithView, {
     label: 'Save Notebook with View State',
     execute: args => {
-      const current = getCurrent();
+      const current = getCurrent(args);
 
       if (current) {
         NotebookActions.persistViewState(current.content);