Extend NotebookMultiCellAction and move into contrib

This commit is contained in:
Joyce Er 2021-08-19 15:30:33 +00:00 committed by GitHub
parent cec2f28bf3
commit 51c240d75b
2 changed files with 46 additions and 40 deletions

View file

@ -71,6 +71,7 @@ const EXECUTE_CELL_INSERT_BELOW = 'notebook.cell.executeAndInsertBelow';
const EXECUTE_CELL_AND_BELOW = 'notebook.cell.executeCellAndBelow';
const EXECUTE_CELLS_ABOVE = 'notebook.cell.executeCellsAbove';
const CLEAR_CELL_OUTPUTS_COMMAND_ID = 'notebook.cell.clearOutputs';
const TOGGLE_CELL_OUTPUTS_COMMAND_ID = 'notebook.cell.toggleOutputs';
const CENTER_ACTIVE_CELL = 'notebook.centerActiveCell';
const COLLAPSE_CELL_INPUT_COMMAND_ID = 'notebook.cell.collapseCellInput';
@ -691,6 +692,46 @@ registerAction2(class CancelExecuteCell extends NotebookMultiCellAction<INoteboo
}
});
registerAction2(class extends NotebookMultiCellAction<INotebookActionContext> {
constructor() {
super({
id: TOGGLE_CELL_OUTPUTS_COMMAND_ID,
precondition: NOTEBOOK_CELL_LIST_FOCUSED,
title: localize('notebookActions.toggleOutputs', "Toggle Outputs"),
description: {
description: localize('notebookActions.toggleOutputs', "Toggle Outputs"),
args: cellExecutionArgs
}
});
}
parseArgs(accessor: ServicesAccessor, ...args: any[]): INotebookActionContext | undefined {
return parseMultiCellExecutionArgs(accessor, ...args);
}
async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise<void> {
const textModel = context.notebookEditor.viewModel.notebookDocument;
let cells: ICellViewModel[] = [];
if (context.ui && context.cell) {
cells = [context.cell];
} else if (context.selectedCells) {
cells = [...context.selectedCells];
} else {
cells = [...context.notebookEditor.viewModel.getCells()];
}
const edits: ICellEditOperation[] = [];
for (const cell of cells) {
const index = textModel.cells.indexOf(cell.model);
if (index >= 0) {
edits.push({ editType: CellEditType.Metadata, index, metadata: { ...cell.metadata, outputCollapsed: !cell.metadata.outputCollapsed } });
}
}
textModel.applyEdits(edits, true, undefined, () => undefined, undefined);
}
});
export class DeleteCellAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,

View file

@ -13,25 +13,24 @@ import { MarshalledId } from 'vs/base/common/marshalling';
import { Schemas } from 'vs/base/common/network';
import * as nls from 'vs/nls';
import { createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { Action2, IMenuService, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
import { IMenuService, MenuId } from 'vs/platform/actions/common/actions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { ThemeIcon } from 'vs/platform/theme/common/themeService';
import { IExtensionsViewPaneContainer, VIEWLET_ID as EXTENSION_VIEWLET_ID } from 'vs/workbench/contrib/extensions/common/extensions';
import { INotebookCellActionContext, NOTEBOOK_ACTIONS_CATEGORY } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions';
import { CodeCellRenderTemplate, getNotebookEditorFromEditorPane, ICellOutputViewModel, ICellViewModel, IInsetRenderOutput, INotebookEditor, IRenderOutput, JUPYTER_EXTENSION_ID, NOTEBOOK_EDITOR_FOCUSED, RenderOutputType } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { INotebookCellActionContext } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions';
import { CodeCellRenderTemplate, ICellOutputViewModel, ICellViewModel, IInsetRenderOutput, INotebookEditor, IRenderOutput, JUPYTER_EXTENSION_ID, RenderOutputType } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { mimetypeIcon } from 'vs/workbench/contrib/notebook/browser/notebookIcons';
import { getResizesObserver } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellWidgets';
import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { BUILTIN_RENDERER_ID, CellEditType, CellUri, ICellEditOperation, IOrderedMimeType, NotebookCellOutputsSplice, RENDERER_NOT_AVAILABLE } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { BUILTIN_RENDERER_ID, CellUri, IOrderedMimeType, NotebookCellOutputsSplice, RENDERER_NOT_AVAILABLE } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookKernel } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
@ -853,37 +852,3 @@ const JUPYTER_RENDERER_MIMETYPES = [
'application/vnd.jupyter.widget-view+json',
'application/vnd.code.notebook.error'
];
registerAction2(class extends Action2 {
constructor() {
super({
id: 'notebook.toggleOutputs',
title: nls.localize('notebookActions.toggleOutputs', 'Toggle Outputs'),
precondition: NOTEBOOK_EDITOR_FOCUSED,
f1: false,
category: NOTEBOOK_ACTIONS_CATEGORY
});
}
async run(accessor: ServicesAccessor) {
const editorService = accessor.get(IEditorService);
const editor = getNotebookEditorFromEditorPane(editorService.activeEditorPane);
const viewModel = editor?.viewModel;
const textModel = viewModel?.notebookDocument;
if (!textModel) {
return;
}
const cells = viewModel.getCells() ?? [];
const edits: ICellEditOperation[] = [];
for (const cell of cells) {
const index = textModel.cells.indexOf(cell.model);
if (index >= 0) {
edits.push({ editType: CellEditType.Metadata, index, metadata: { ...cell.metadata, outputCollapsed: !cell.metadata.outputCollapsed } });
}
}
textModel.applyEdits(edits, true, undefined, () => undefined, undefined);
}
});