Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Render all markdown cells" command #6029

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/mainmenu-extension/src/index.ts
Expand Up @@ -92,6 +92,8 @@ export namespace CommandIDs {

export const runAll = 'runmenu:run-all';

export const runAllMarkdown = 'runmenu:run-all-markdown';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this ID should be here at all -- there is no 'runmenu:run-all-markdown' command defined as far as I see.


export const restartAndRunAll = 'runmenu:restart-and-run-all';

export const runAbove = 'runmenu:run-above';
Expand Down Expand Up @@ -674,7 +676,6 @@ export function createRunMenu(app: JupyterFrontEnd, menu: RunMenu): void {
isEnabled: Private.delegateEnabled(app, menu.codeRunners, 'runAll'),
execute: Private.delegateExecute(app, menu.codeRunners, 'runAll')
});

commands.addCommand(CommandIDs.restartAndRunAll, {
label: () => {
const noun = Private.delegateLabel(app, menu.codeRunners, 'noun');
Expand All @@ -693,11 +694,13 @@ export function createRunMenu(app: JupyterFrontEnd, menu: RunMenu): void {
execute: Private.delegateExecute(app, menu.codeRunners, 'restartAndRunAll')
});

const runAllGroup = [CommandIDs.runAll, CommandIDs.restartAndRunAll].map(
command => {
return { command };
}
);
const runAllGroup = [
CommandIDs.runAll,
CommandIDs.restartAndRunAll,
CommandIDs.runAllMarkdown
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be removed.

].map(command => {
return { command };
});

menu.addGroup([{ command: CommandIDs.run }], 0);
menu.addGroup(runAllGroup, 999);
Expand Down
23 changes: 23 additions & 0 deletions packages/notebook-extension/src/index.ts
Expand Up @@ -119,6 +119,8 @@ namespace CommandIDs {

export const runAllBelow = 'notebook:run-all-below';

export const renderAllMarkdown = 'notebook:render-all-markdown';

export const toCode = 'notebook:change-cell-to-code';

export const toMarkdown = 'notebook:change-cell-to-markdown';
Expand Down Expand Up @@ -1037,6 +1039,17 @@ function addCommands(
);
}
});
commands.addCommand(CommandIDs.renderAllMarkdown, {
label: 'Render All Markdown Cells',
execute: args => {
const current = getCurrent(args);
if (current) {
const { context, content } = current;
return NotebookActions.renderAllMarkdown(content, context.session);
}
},
isEnabled
});
commands.addCommand(CommandIDs.restart, {
label: 'Restart Kernel…',
execute: args => {
Expand Down Expand Up @@ -1788,6 +1801,7 @@ function populatePalette(
CommandIDs.restartClear,
CommandIDs.restartRunAll,
CommandIDs.runAll,
CommandIDs.renderAllMarkdown,
CommandIDs.runAllAbove,
CommandIDs.runAllBelow,
CommandIDs.selectAll,
Expand Down Expand Up @@ -2073,6 +2087,14 @@ function populateMenus(
}
} as IRunMenu.ICodeRunner<NotebookPanel>);

// Add a renderAllMarkdown group to the run menu.
const renderAllMarkdown = [
CommandIDs.renderAllMarkdown,
CommandIDs.run,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove run and runInConsole here? The first is redundant with existing menu items, and the second should probably have its own discussion. I think it's fine for renderAllMarkdown to have its own menu group.

CommandIDs.runInConsole
].map(command => {
return { command };
});
// Add a run+insert and run+don't advance group to the run menu.
const runExtras = [
CommandIDs.runAndInsert,
Expand Down Expand Up @@ -2132,6 +2154,7 @@ function populateMenus(
mainMenu.editMenu.addGroup(splitMergeGroup, 9);
mainMenu.runMenu.addGroup(runExtras, 10);
mainMenu.runMenu.addGroup(runAboveBelowGroup, 11);
mainMenu.runMenu.addGroup(renderAllMarkdown, 12);

// Add kernel information to the application help menu.
mainMenu.helpMenu.kernelUsers.add({
Expand Down
26 changes: 26 additions & 0 deletions packages/notebook/src/actions.tsx
Expand Up @@ -526,6 +526,32 @@ export namespace NotebookActions {
return promise;
}

export function renderAllMarkdown(
notebook: Notebook,
session?: IClientSession
): Promise<boolean> {
if (!notebook.model || !notebook.activeCell) {
return Promise.resolve(false);
}
const previousIndex = notebook.activeCellIndex;
const state = Private.getState(notebook);
notebook.widgets.forEach((child, index) => {
if (child.model.type === 'markdown') {
notebook.select(child);
// This is to make sure that the activeCell
// does not get executed
notebook.activeCellIndex = index;
}
});
if (notebook.activeCell.model.type !== 'markdown') {
return Promise.resolve(true);
}
const promise = Private.runSelected(notebook, session);
notebook.activeCellIndex = previousIndex;
Private.handleRunState(notebook, state, true);
return promise;
}

/**
* Run all of the cells before the currently active cell (exclusive).
*
Expand Down