Browse Source

Backport PR #10729 on branch 3.2.x (Toc: Run nested code cells directly from markdown headings) (#11375)

* Backport PR #10729: Toc: Run nested code cells directly from markdown headings

* Lint

* Restart CI
Jessica Xu 3 years ago
parent
commit
ba558dc443

+ 1 - 0
packages/toc-extension/package.json

@@ -41,6 +41,7 @@
   },
   "dependencies": {
     "@jupyterlab/application": "^3.2.1",
+    "@jupyterlab/cells": "^3.2.1",
     "@jupyterlab/docmanager": "^3.2.1",
     "@jupyterlab/fileeditor": "^3.2.1",
     "@jupyterlab/markdownviewer": "^3.2.1",

+ 30 - 2
packages/toc-extension/src/index.ts

@@ -29,7 +29,8 @@ import {
 } from '@jupyterlab/toc';
 import { ITranslator } from '@jupyterlab/translation';
 import { tocIcon } from '@jupyterlab/ui-components';
-import { runNestedCodeCells } from '@jupyterlab/toc';
+import { INotebookHeading } from '@jupyterlab/toc';
+import { CodeCell, MarkdownCell } from '@jupyterlab/cells';
 
 /**
  * The command IDs used by TOC item.
@@ -88,7 +89,34 @@ async function activateTOC(
 
   app.commands.addCommand(CommandIDs.runCells, {
     execute: args => {
-      return runNestedCodeCells(toc.headings, toc.activeEntry);
+      const panel = notebookTracker.currentWidget;
+      if (panel == null) {
+        return;
+      }
+
+      const cells = panel.content.widgets;
+      if (cells === undefined) {
+        return;
+      }
+
+      const activeCell = (toc.activeEntry as INotebookHeading).cellRef;
+
+      if (activeCell instanceof MarkdownCell) {
+        let level = activeCell.headingInfo.level;
+        for (let i = cells.indexOf(activeCell) + 1; i < cells.length; i++) {
+          const cell = cells[i];
+          if (cell instanceof MarkdownCell && cell.headingInfo.level <= level) {
+            break;
+          }
+          if (cell instanceof CodeCell) {
+            void CodeCell.execute(cell, panel.sessionContext);
+          }
+        }
+      } else {
+        if (activeCell instanceof CodeCell) {
+          void CodeCell.execute(activeCell, panel.sessionContext);
+        }
+      }
     },
     label: trans.__('Run Cell(s)')
   });

+ 1 - 0
packages/toc-extension/style/index.css

@@ -8,6 +8,7 @@
 @import url('~@jupyterlab/rendermime/style/index.css');
 @import url('~@jupyterlab/application/style/index.css');
 @import url('~@jupyterlab/docmanager/style/index.css');
+@import url('~@jupyterlab/cells/style/index.css');
 @import url('~@jupyterlab/fileeditor/style/index.css');
 @import url('~@jupyterlab/markdownviewer/style/index.css');
 @import url('~@jupyterlab/notebook/style/index.css');

+ 1 - 0
packages/toc-extension/style/index.js

@@ -8,6 +8,7 @@ import '@jupyterlab/ui-components/style/index.js';
 import '@jupyterlab/rendermime/style/index.js';
 import '@jupyterlab/application/style/index.js';
 import '@jupyterlab/docmanager/style/index.js';
+import '@jupyterlab/cells/style/index.js';
 import '@jupyterlab/fileeditor/style/index.js';
 import '@jupyterlab/markdownviewer/style/index.js';
 import '@jupyterlab/notebook/style/index.js';

+ 3 - 0
packages/toc-extension/tsconfig.json

@@ -9,6 +9,9 @@
     {
       "path": "../application"
     },
+    {
+      "path": "../cells"
+    },
     {
       "path": "../docmanager"
     },

+ 1 - 0
packages/toc/src/utils/headings.ts

@@ -150,3 +150,4 @@ export { IHeading };
 export { INumberedHeading };
 export { INotebookHeading };
 export { runNestedCodeCells };
+export { isNotebookHeading };