Skip to content

Commit ba558dc

Browse files
authored
Backport PR jupyterlab#10729 on branch 3.2.x (Toc: Run nested code cells directly from markdown headings) (jupyterlab#11375)
* Backport PR jupyterlab#10729: Toc: Run nested code cells directly from markdown headings * Lint * Restart CI
1 parent 0457aff commit ba558dc

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

packages/toc-extension/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"dependencies": {
4343
"@jupyterlab/application": "^3.2.1",
44+
"@jupyterlab/cells": "^3.2.1",
4445
"@jupyterlab/docmanager": "^3.2.1",
4546
"@jupyterlab/fileeditor": "^3.2.1",
4647
"@jupyterlab/markdownviewer": "^3.2.1",

packages/toc-extension/src/index.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import {
2929
} from '@jupyterlab/toc';
3030
import { ITranslator } from '@jupyterlab/translation';
3131
import { tocIcon } from '@jupyterlab/ui-components';
32-
import { runNestedCodeCells } from '@jupyterlab/toc';
32+
import { INotebookHeading } from '@jupyterlab/toc';
33+
import { CodeCell, MarkdownCell } from '@jupyterlab/cells';
3334

3435
/**
3536
* The command IDs used by TOC item.
@@ -88,7 +89,34 @@ async function activateTOC(
8889

8990
app.commands.addCommand(CommandIDs.runCells, {
9091
execute: args => {
91-
return runNestedCodeCells(toc.headings, toc.activeEntry);
92+
const panel = notebookTracker.currentWidget;
93+
if (panel == null) {
94+
return;
95+
}
96+
97+
const cells = panel.content.widgets;
98+
if (cells === undefined) {
99+
return;
100+
}
101+
102+
const activeCell = (toc.activeEntry as INotebookHeading).cellRef;
103+
104+
if (activeCell instanceof MarkdownCell) {
105+
let level = activeCell.headingInfo.level;
106+
for (let i = cells.indexOf(activeCell) + 1; i < cells.length; i++) {
107+
const cell = cells[i];
108+
if (cell instanceof MarkdownCell && cell.headingInfo.level <= level) {
109+
break;
110+
}
111+
if (cell instanceof CodeCell) {
112+
void CodeCell.execute(cell, panel.sessionContext);
113+
}
114+
}
115+
} else {
116+
if (activeCell instanceof CodeCell) {
117+
void CodeCell.execute(activeCell, panel.sessionContext);
118+
}
119+
}
92120
},
93121
label: trans.__('Run Cell(s)')
94122
});

packages/toc-extension/style/index.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@import url('~@jupyterlab/rendermime/style/index.css');
99
@import url('~@jupyterlab/application/style/index.css');
1010
@import url('~@jupyterlab/docmanager/style/index.css');
11+
@import url('~@jupyterlab/cells/style/index.css');
1112
@import url('~@jupyterlab/fileeditor/style/index.css');
1213
@import url('~@jupyterlab/markdownviewer/style/index.css');
1314
@import url('~@jupyterlab/notebook/style/index.css');

packages/toc-extension/style/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import '@jupyterlab/ui-components/style/index.js';
88
import '@jupyterlab/rendermime/style/index.js';
99
import '@jupyterlab/application/style/index.js';
1010
import '@jupyterlab/docmanager/style/index.js';
11+
import '@jupyterlab/cells/style/index.js';
1112
import '@jupyterlab/fileeditor/style/index.js';
1213
import '@jupyterlab/markdownviewer/style/index.js';
1314
import '@jupyterlab/notebook/style/index.js';

packages/toc-extension/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
{
1010
"path": "../application"
1111
},
12+
{
13+
"path": "../cells"
14+
},
1215
{
1316
"path": "../docmanager"
1417
},

packages/toc/src/utils/headings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,4 @@ export { IHeading };
150150
export { INumberedHeading };
151151
export { INotebookHeading };
152152
export { runNestedCodeCells };
153+
export { isNotebookHeading };

0 commit comments

Comments
 (0)