提交 2b646152 编写于 作者: R Rob Lourens

Remove MenuItemActions to simplify action registration code

上级 a0d07dcf
...@@ -241,7 +241,7 @@ export class MainThreadNotebookController implements IMainNotebookController { ...@@ -241,7 +241,7 @@ export class MainThreadNotebookController implements IMainNotebookController {
async createRawCell(uri: URI, index: number, language: string, type: CellKind): Promise<NotebookCellTextModel | undefined> { async createRawCell(uri: URI, index: number, language: string, type: CellKind): Promise<NotebookCellTextModel | undefined> {
let cell = await this._proxy.$createEmptyCell(this._viewType, uri, index, language, type); let cell = await this._proxy.$createEmptyCell(this._viewType, uri, index, language, type);
if (cell) { if (cell) {
let mainCell = new NotebookCellTextModel(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs); let mainCell = new NotebookCellTextModel(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs, cell.metadata);
return mainCell; return mainCell;
} }
......
...@@ -580,7 +580,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN ...@@ -580,7 +580,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
let editor = this._editors.get(URI.revive(uri).toString()); let editor = this._editors.get(URI.revive(uri).toString());
let document = this._documents.get(URI.revive(uri).toString()); let document = this._documents.get(URI.revive(uri).toString());
let rawCell = editor?.editor.createCell('', language, type, [], undefined) as ExtHostCell; let rawCell = editor?.editor.createCell('', language, type, [], { editable: true }) as ExtHostCell;
document?.insertCell(index, rawCell!); document?.insertCell(index, rawCell!);
let allDocuments = this._documentsAndEditors.allDocuments(); let allDocuments = this._documentsAndEditors.allDocuments();
......
...@@ -26,5 +26,10 @@ export const EDITOR_BOTTOM_PADDING = 8; ...@@ -26,5 +26,10 @@ export const EDITOR_BOTTOM_PADDING = 8;
export const EDITOR_TOOLBAR_HEIGHT = 22; export const EDITOR_TOOLBAR_HEIGHT = 22;
export const RUN_BUTTON_WIDTH = 20; export const RUN_BUTTON_WIDTH = 20;
// Context Keys // Cell context keys
export const NOTEBOOK_CELL_TYPE_CONTEXT_KEY = 'notebookCellType'; export const NOTEBOOK_CELL_TYPE_CONTEXT_KEY = 'notebookCellType';
export const NOTEBOOK_CELL_EDITABLE_CONTEXT_KEY = 'notebookCellEditable';
export const NOTEBOOK_CELL_MARKDOWN_EDIT_MODE_CONTEXT_KEY = 'notebookCellMarkdownEditMode';
// Notebook context keys
export const NOTEBOOK_EDITABLE_CONTEXT_KEY = 'notebookEditable';
...@@ -11,12 +11,21 @@ import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/commo ...@@ -11,12 +11,21 @@ import { ContextKeyExpr, IContextKeyService } from 'vs/platform/contextkey/commo
import { InputFocusedContext, InputFocusedContextKey, IsDevelopmentContext } from 'vs/platform/contextkey/common/contextkeys'; import { InputFocusedContext, InputFocusedContextKey, IsDevelopmentContext } from 'vs/platform/contextkey/common/contextkeys';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry'; import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { COPY_CELL_DOWN_COMMAND_ID, COPY_CELL_UP_COMMAND_ID, DELETE_CELL_COMMAND_ID, EDIT_CELL_COMMAND_ID, EXECUTE_CELL_COMMAND_ID, INSERT_CODE_CELL_ABOVE_COMMAND_ID, INSERT_CODE_CELL_BELOW_COMMAND_ID, INSERT_MARKDOWN_CELL_ABOVE_COMMAND_ID, INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID, MOVE_CELL_DOWN_COMMAND_ID, MOVE_CELL_UP_COMMAND_ID, SAVE_CELL_COMMAND_ID } from 'vs/workbench/contrib/notebook/browser/constants'; import { COPY_CELL_DOWN_COMMAND_ID, COPY_CELL_UP_COMMAND_ID, DELETE_CELL_COMMAND_ID, EDIT_CELL_COMMAND_ID, EXECUTE_CELL_COMMAND_ID, INSERT_CODE_CELL_ABOVE_COMMAND_ID, INSERT_CODE_CELL_BELOW_COMMAND_ID, INSERT_MARKDOWN_CELL_ABOVE_COMMAND_ID, INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID, MOVE_CELL_DOWN_COMMAND_ID, MOVE_CELL_UP_COMMAND_ID, SAVE_CELL_COMMAND_ID, NOTEBOOK_CELL_TYPE_CONTEXT_KEY, NOTEBOOK_EDITABLE_CONTEXT_KEY, NOTEBOOK_CELL_EDITABLE_CONTEXT_KEY } from 'vs/workbench/contrib/notebook/browser/constants';
import { CellRenderTemplate, CellEditState, ICellViewModel, INotebookEditor, KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED, NOTEBOOK_EDITOR_FOCUSED, CellRunState } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; import { CellRenderTemplate, CellEditState, ICellViewModel, INotebookEditor, KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED, NOTEBOOK_EDITOR_FOCUSED, CellRunState } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService'; import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
import { CellKind, NOTEBOOK_EDITOR_CURSOR_BOUNDARY } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { CellKind, NOTEBOOK_EDITOR_CURSOR_BOUNDARY } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
const enum CellToolbarOrder {
MoveCellUp,
MoveCellDown,
EditCell,
SaveCell,
InsertCell,
DeleteCell
}
registerAction2(class extends Action2 { registerAction2(class extends Action2 {
constructor() { constructor() {
super({ super({
...@@ -29,7 +38,8 @@ registerAction2(class extends Action2 { ...@@ -29,7 +38,8 @@ registerAction2(class extends Action2 {
primary: KeyMod.WinCtrl | KeyMod.Alt | KeyCode.Enter primary: KeyMod.WinCtrl | KeyMod.Alt | KeyCode.Enter
}, },
weight: KeybindingWeight.WorkbenchContrib weight: KeybindingWeight.WorkbenchContrib
} },
icon: { id: 'codicon/play' }
}); });
} }
...@@ -417,7 +427,18 @@ registerAction2(class extends InsertCellCommand { ...@@ -417,7 +427,18 @@ registerAction2(class extends InsertCellCommand {
super( super(
{ {
id: INSERT_CODE_CELL_BELOW_COMMAND_ID, id: INSERT_CODE_CELL_BELOW_COMMAND_ID,
title: localize('notebookActions.insertCodeCellBelow', "Insert Code Cell Below") title: localize('notebookActions.insertCodeCellBelow', "Insert Code Cell Below"),
icon: { id: 'codicon/add' },
menu: {
id: MenuId.NotebookCellTitle,
order: CellToolbarOrder.InsertCell,
alt: {
id: INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID,
title: localize('notebookActions.insertMarkdownCellBelow', "Insert Markdown Cell Below"),
icon: { id: 'codicon/add' },
},
when: ContextKeyExpr.equals(NOTEBOOK_EDITABLE_CONTEXT_KEY, true)
}
}, },
CellKind.Code, CellKind.Code,
'below'); 'below');
...@@ -441,89 +462,13 @@ registerAction2(class extends InsertCellCommand { ...@@ -441,89 +462,13 @@ registerAction2(class extends InsertCellCommand {
super( super(
{ {
id: INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID, id: INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID,
title: localize('notebookActions.insertMarkdownCellBelow', "Insert Markdown Cell Below"), title: localize('notebookActions.insertMarkdownCellBelow', "Insert Markdown Cell Below")
}, },
CellKind.Markdown, CellKind.Markdown,
'below'); 'below');
} }
}); });
export class InsertCodeCellAboveAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService commandService: ICommandService
) {
super(
{
id: INSERT_CODE_CELL_ABOVE_COMMAND_ID,
title: localize('notebookActions.insertCodeCellAbove', "Insert Code Cell Above"),
icon: { id: 'codicon/add' }
},
undefined,
{ shouldForwardArgs: true },
contextKeyService,
commandService);
}
}
export class InsertCodeCellBelowAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService commandService: ICommandService
) {
super(
{
id: INSERT_CODE_CELL_BELOW_COMMAND_ID,
title: localize('notebookActions.insertCodeCellBelow', "Insert Code Cell Below"),
icon: { id: 'codicon/add' }
},
{
id: INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID,
title: localize('notebookActions.insertMarkdownCellBelow', "Insert Markdown Cell Below"),
icon: { id: 'codicon/add' }
},
{ shouldForwardArgs: true },
contextKeyService,
commandService);
}
}
export class InsertMarkdownCellAboveAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService commandService: ICommandService
) {
super(
{
id: INSERT_MARKDOWN_CELL_ABOVE_COMMAND_ID,
title: localize('notebookActions.insertMarkdownCellAbove', "Insert Markdown Cell Above"),
icon: { id: 'codicon/add' }
},
undefined,
{ shouldForwardArgs: true },
contextKeyService,
commandService);
}
}
export class InsertMarkdownCellBelowAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService commandService: ICommandService
) {
super(
{
id: INSERT_MARKDOWN_CELL_BELOW_COMMAND_ID,
title: localize('notebookActions.insertMarkdownCellBelow', "Insert Markdown Cell Below"),
icon: { id: 'codicon/add' }
},
undefined,
{ shouldForwardArgs: true },
contextKeyService,
commandService);
}
}
registerAction2(class extends Action2 { registerAction2(class extends Action2 {
constructor() { constructor() {
super( super(
...@@ -534,7 +479,15 @@ registerAction2(class extends Action2 { ...@@ -534,7 +479,15 @@ registerAction2(class extends Action2 {
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.not(InputFocusedContextKey)), when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.not(InputFocusedContextKey)),
primary: KeyCode.Enter, primary: KeyCode.Enter,
weight: KeybindingWeight.WorkbenchContrib weight: KeybindingWeight.WorkbenchContrib
} },
menu: {
id: MenuId.NotebookCellTitle,
when: ContextKeyExpr.and(
ContextKeyExpr.equals(NOTEBOOK_CELL_TYPE_CONTEXT_KEY, 'markdown'),
ContextKeyExpr.equals(NOTEBOOK_CELL_EDITABLE_CONTEXT_KEY, true)),
order: CellToolbarOrder.EditCell
},
icon: { id: 'codicon/pencil' }
}); });
} }
...@@ -550,30 +503,20 @@ registerAction2(class extends Action2 { ...@@ -550,30 +503,20 @@ registerAction2(class extends Action2 {
} }
}); });
export class EditCellAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService commandService: ICommandService
) {
super(
{
id: EDIT_CELL_COMMAND_ID,
title: localize('notebookActions.editCell', "Edit Cell"),
icon: { id: 'codicon/pencil' }
},
undefined,
{ shouldForwardArgs: true },
contextKeyService,
commandService);
}
}
registerAction2(class extends Action2 { registerAction2(class extends Action2 {
constructor() { constructor() {
super( super(
{ {
id: SAVE_CELL_COMMAND_ID, id: SAVE_CELL_COMMAND_ID,
title: localize('notebookActions.saveCell', "Save Cell") title: localize('notebookActions.saveCell', "Save Cell"),
menu: {
id: MenuId.NotebookCellTitle,
when: ContextKeyExpr.and(
ContextKeyExpr.equals(NOTEBOOK_CELL_TYPE_CONTEXT_KEY, 'markdown'),
ContextKeyExpr.equals(NOTEBOOK_CELL_EDITABLE_CONTEXT_KEY, true)),
order: CellToolbarOrder.SaveCell
},
icon: { id: 'codicon/save' }
}); });
} }
...@@ -589,30 +532,19 @@ registerAction2(class extends Action2 { ...@@ -589,30 +532,19 @@ registerAction2(class extends Action2 {
} }
}); });
export class SaveCellAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService commandService: ICommandService
) {
super(
{
id: SAVE_CELL_COMMAND_ID,
title: localize('notebookActions.saveCell', "Save Cell"),
icon: { id: 'codicon/save' }
},
undefined,
{ shouldForwardArgs: true },
contextKeyService,
commandService);
}
}
registerAction2(class extends Action2 { registerAction2(class extends Action2 {
constructor() { constructor() {
super( super(
{ {
id: DELETE_CELL_COMMAND_ID, id: DELETE_CELL_COMMAND_ID,
title: localize('notebookActions.deleteCell', "Delete Cell") title: localize('notebookActions.deleteCell', "Delete Cell"),
menu: {
id: MenuId.NotebookCellTitle,
order: CellToolbarOrder.DeleteCell,
when: ContextKeyExpr.equals(NOTEBOOK_EDITABLE_CONTEXT_KEY, true)
},
icon: { id: 'codicon/x' }
}); });
} }
...@@ -628,26 +560,6 @@ registerAction2(class extends Action2 { ...@@ -628,26 +560,6 @@ registerAction2(class extends Action2 {
} }
}); });
export class DeleteCellAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService commandService: ICommandService
) {
super(
{
id: DELETE_CELL_COMMAND_ID,
title: localize('notebookActions.deleteCell', "Delete Cell"),
icon: { id: 'codicon/x' }
},
undefined,
{ shouldForwardArgs: true },
contextKeyService,
commandService);
this.class = 'codicon-x';
}
}
async function moveCell(context: INotebookCellActionContext, direction: 'up' | 'down'): Promise<void> { async function moveCell(context: INotebookCellActionContext, direction: 'up' | 'down'): Promise<void> {
direction === 'up' ? direction === 'up' ?
context.notebookEditor.moveCellUp(context.cell) : context.notebookEditor.moveCellUp(context.cell) :
...@@ -665,7 +577,18 @@ registerAction2(class extends Action2 { ...@@ -665,7 +577,18 @@ registerAction2(class extends Action2 {
super( super(
{ {
id: MOVE_CELL_UP_COMMAND_ID, id: MOVE_CELL_UP_COMMAND_ID,
title: localize('notebookActions.moveCellUp', "Move Cell Up") title: localize('notebookActions.moveCellUp', "Move Cell Up"),
icon: { id: 'codicon/arrow-up' },
menu: {
id: MenuId.NotebookCellTitle,
order: CellToolbarOrder.MoveCellUp,
alt: {
id: COPY_CELL_UP_COMMAND_ID,
title: localize('notebookActions.copyCellUp', "Copy Cell Up"),
icon: { id: 'codicon/arrow-up' }
},
when: ContextKeyExpr.equals(NOTEBOOK_EDITABLE_CONTEXT_KEY, true)
},
}); });
} }
...@@ -681,34 +604,23 @@ registerAction2(class extends Action2 { ...@@ -681,34 +604,23 @@ registerAction2(class extends Action2 {
} }
}); });
export class MoveCellUpAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService commandService: ICommandService
) {
super(
{
id: MOVE_CELL_UP_COMMAND_ID,
title: localize('notebookActions.moveCellUp', "Move Cell Up"),
icon: { id: 'codicon/arrow-up' }
},
{
id: COPY_CELL_UP_COMMAND_ID,
title: localize('notebookActions.copyCellUp', "Copy Cell Up"),
icon: { id: 'codicon/arrow-up' }
},
{ shouldForwardArgs: true },
contextKeyService,
commandService);
}
}
registerAction2(class extends Action2 { registerAction2(class extends Action2 {
constructor() { constructor() {
super( super(
{ {
id: MOVE_CELL_DOWN_COMMAND_ID, id: MOVE_CELL_DOWN_COMMAND_ID,
title: localize('notebookActions.moveCellDown', "Move Cell Down") title: localize('notebookActions.moveCellDown', "Move Cell Down"),
icon: { id: 'codicon/arrow-down' },
menu: {
id: MenuId.NotebookCellTitle,
order: CellToolbarOrder.MoveCellDown,
alt: {
id: COPY_CELL_DOWN_COMMAND_ID,
title: localize('notebookActions.copyCellDown', "Copy Cell Down"),
icon: { id: 'codicon/arrow-down' }
},
when: ContextKeyExpr.equals(NOTEBOOK_EDITABLE_CONTEXT_KEY, true)
},
}); });
} }
...@@ -724,30 +636,6 @@ registerAction2(class extends Action2 { ...@@ -724,30 +636,6 @@ registerAction2(class extends Action2 {
} }
}); });
export class MoveCellDownAction extends MenuItemAction {
constructor(
@IContextKeyService contextKeyService: IContextKeyService,
@ICommandService commandService: ICommandService
) {
super(
{
id: MOVE_CELL_DOWN_COMMAND_ID,
title: localize('notebookActions.moveCellDown', "Move Cell Down"),
icon: { id: 'codicon/arrow-down' }
},
{
id: COPY_CELL_DOWN_COMMAND_ID,
title: localize('notebookActions.copyCellDown', "Copy Cell Down"),
icon: { id: 'codicon/arrow-down' }
},
{ shouldForwardArgs: true },
contextKeyService,
commandService);
this.class = 'codicon-arrow-down';
}
}
registerAction2(class extends Action2 { registerAction2(class extends Action2 {
constructor() { constructor() {
super( super(
......
...@@ -16,7 +16,7 @@ import { FindMatch } from 'vs/editor/common/model'; ...@@ -16,7 +16,7 @@ import { FindMatch } from 'vs/editor/common/model';
import { RawContextKey } from 'vs/platform/contextkey/common/contextkey'; import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer'; import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
import { IModelDecorationsChangeAccessor, NotebookViewModel, CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { IModelDecorationsChangeAccessor, NotebookViewModel, CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { CellKind, IOutput, IRenderOutput } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { CellKind, IOutput, IRenderOutput, NotebookCellMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
export const KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED = new RawContextKey<boolean>('notebookFindWidgetFocused', false); export const KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED = new RawContextKey<boolean>('notebookFindWidgetFocused', false);
...@@ -73,6 +73,7 @@ export interface ICellViewModel { ...@@ -73,6 +73,7 @@ export interface ICellViewModel {
runState: CellRunState; runState: CellRunState;
focusMode: CellFocusMode; focusMode: CellFocusMode;
getText(): string; getText(): string;
metadata: NotebookCellMetadata | undefined;
} }
export interface INotebookEditor { export interface INotebookEditor {
......
...@@ -53,7 +53,7 @@ export class NotebookEditorModel extends EditorModel { ...@@ -53,7 +53,7 @@ export class NotebookEditorModel extends EditorModel {
let notebook = this.getNotebook(); let notebook = this.getNotebook();
if (notebook) { if (notebook) {
let mainCell = new NotebookCellTextModel(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs); let mainCell = new NotebookCellTextModel(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs, cell.metadata);
this.notebook.insertNewCell(index, mainCell); this.notebook.insertNewCell(index, mainCell);
this._dirty = true; this._dirty = true;
this._onDidChangeDirty.fire(); this._onDidChangeDirty.fire();
......
...@@ -18,22 +18,22 @@ import { BareFontInfo } from 'vs/editor/common/config/fontInfo'; ...@@ -18,22 +18,22 @@ import { BareFontInfo } from 'vs/editor/common/config/fontInfo';
import { ContextAwareMenuEntryActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem'; import { ContextAwareMenuEntryActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { MenuItemAction } from 'vs/platform/actions/common/actions'; import { MenuItemAction } from 'vs/platform/actions/common/actions';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView'; import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { INotificationService } from 'vs/platform/notification/common/notification'; import { INotificationService } from 'vs/platform/notification/common/notification';
import { EDITOR_BOTTOM_PADDING, EDITOR_TOOLBAR_HEIGHT, EDITOR_TOP_PADDING, NOTEBOOK_CELL_TYPE_CONTEXT_KEY } from 'vs/workbench/contrib/notebook/browser/constants'; import { EDITOR_BOTTOM_PADDING, EDITOR_TOOLBAR_HEIGHT, EDITOR_TOP_PADDING, NOTEBOOK_CELL_EDITABLE_CONTEXT_KEY, NOTEBOOK_CELL_TYPE_CONTEXT_KEY, NOTEBOOK_EDITABLE_CONTEXT_KEY } from 'vs/workbench/contrib/notebook/browser/constants';
import { DeleteCellAction, EditCellAction, ExecuteCellAction, INotebookCellActionContext, InsertCodeCellBelowAction, MoveCellDownAction, MoveCellUpAction, SaveCellAction, InsertCodeCellAboveAction, InsertMarkdownCellAboveAction, InsertMarkdownCellBelowAction } from 'vs/workbench/contrib/notebook/browser/contrib/notebookActions'; import { CellRenderTemplate, CellRunState, ICellViewModel, INotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { CellRenderTemplate, ICellViewModel, INotebookEditor, CellRunState } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; import { CellMenus } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellMenus';
import { CodeCell } from 'vs/workbench/contrib/notebook/browser/view/renderers/codeCell'; import { CodeCell } from 'vs/workbench/contrib/notebook/browser/view/renderers/codeCell';
import { StatefullMarkdownCell } from 'vs/workbench/contrib/notebook/browser/view/renderers/markdownCell'; import { StatefullMarkdownCell } from 'vs/workbench/contrib/notebook/browser/view/renderers/markdownCell';
import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel';
import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel';
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookCellActionContext, ExecuteCellAction } from 'vs/workbench/contrib/notebook/browser/contrib/notebookActions';
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar'; import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { CellMenus } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellMenus';
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel';
import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel';
const $ = DOM.$; const $ = DOM.$;
...@@ -121,37 +121,21 @@ abstract class AbstractCellRenderer { ...@@ -121,37 +121,21 @@ abstract class AbstractCellRenderer {
return menu; return menu;
} }
abstract getCellToolbarActions(element: CellViewModel): IAction[]; getCellToolbarActions(scopedContextKeyService: IContextKeyService): IAction[] {
const viewModel = this.notebookEditor.viewModel;
showContextMenu(listIndex: number | undefined, element: CellViewModel, x: number, y: number) {
const actions: IAction[] = [ if (!viewModel) {
this.instantiationService.createInstance(InsertCodeCellAboveAction), return [];
this.instantiationService.createInstance(InsertCodeCellBelowAction), }
this.instantiationService.createInstance(InsertMarkdownCellAboveAction),
this.instantiationService.createInstance(InsertMarkdownCellBelowAction), const menu = this.createMenu().getCellTitleActions(scopedContextKeyService);
]; const actions: IAction[] = [];
actions.push(...this.getAdditionalContextMenuActions()); for (let [, menuActions] of menu.getActions({ shouldForwardArgs: true })) {
actions.push(...[ actions.push(...menuActions);
this.instantiationService.createInstance(DeleteCellAction) }
]);
this.contextMenuService.showContextMenu({
getAnchor: () => {
return {
x,
y
};
},
getActions: () => actions,
getActionsContext: () => <INotebookCellActionContext>{
cell: element,
notebookEditor: this.notebookEditor
},
autoSelectFirstItem: false
});
}
abstract getAdditionalContextMenuActions(): IAction[]; return actions;
}
} }
export class MarkdownCellRenderer extends AbstractCellRenderer implements IListRenderer<MarkdownCellViewModel, CellRenderTemplate> { export class MarkdownCellRenderer extends AbstractCellRenderer implements IListRenderer<MarkdownCellViewModel, CellRenderTemplate> {
...@@ -165,7 +149,7 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR ...@@ -165,7 +149,7 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR
@IContextMenuService contextMenuService: IContextMenuService, @IContextMenuService contextMenuService: IContextMenuService,
@IKeybindingService keybindingService: IKeybindingService, @IKeybindingService keybindingService: IKeybindingService,
@INotificationService notificationService: INotificationService, @INotificationService notificationService: INotificationService,
@IContextKeyService contextKeyService: IContextKeyService @IContextKeyService contextKeyService: IContextKeyService,
) { ) {
super(instantiationService, notehookEditor, contextMenuService, configurationService, keybindingService, notificationService, contextKeyService, 'markdown'); super(instantiationService, notehookEditor, contextMenuService, configurationService, keybindingService, notificationService, contextKeyService, 'markdown');
} }
...@@ -219,7 +203,10 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR ...@@ -219,7 +203,10 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR
const contextKeyService = this.contextKeyService.createScoped(templateData.container); const contextKeyService = this.contextKeyService.createScoped(templateData.container);
contextKeyService.createKey(NOTEBOOK_CELL_TYPE_CONTEXT_KEY, 'markdown'); contextKeyService.createKey(NOTEBOOK_CELL_TYPE_CONTEXT_KEY, 'markdown');
const toolbarActions = this.getCellToolbarActions(element); contextKeyService.createKey(NOTEBOOK_CELL_EDITABLE_CONTEXT_KEY, element.metadata?.editable);
contextKeyService.createKey(NOTEBOOK_EDITABLE_CONTEXT_KEY, this.notebookEditor.viewModel?.metadata?.editable);
const toolbarActions = this.getCellToolbarActions(contextKeyService);
templateData.toolbar!.setActions(toolbarActions)(); templateData.toolbar!.setActions(toolbarActions)();
if (templateData.focusIndicator) { if (templateData.focusIndicator) {
...@@ -238,51 +225,6 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR ...@@ -238,51 +225,6 @@ export class MarkdownCellRenderer extends AbstractCellRenderer implements IListR
}; };
} }
getCellToolbarActions(element: MarkdownCellViewModel): IAction[] {
const viewModel = this.notebookEditor.viewModel;
if (!viewModel) {
return [];
}
const menu = this.createMenu().getCellTitleActions(this.contextKeyService);
const actions: IAction[] = [];
for (let [, actions] of menu.getActions({ shouldForwardArgs: true })) {
actions.push(...actions);
}
const metadata = viewModel.metadata;
if (!metadata || metadata.editable) {
actions.push(
this.instantiationService.createInstance(MoveCellUpAction),
this.instantiationService.createInstance(MoveCellDownAction),
this.instantiationService.createInstance(InsertCodeCellBelowAction)
);
}
const cellMetadata = element.metadata;
if (!cellMetadata || cellMetadata.editable) {
actions.push(
this.instantiationService.createInstance(EditCellAction),
this.instantiationService.createInstance(SaveCellAction)
);
}
if (!metadata || metadata.editable) {
this.instantiationService.createInstance(DeleteCellAction);
}
return actions;
}
getAdditionalContextMenuActions(): IAction[] {
return [
this.instantiationService.createInstance(EditCellAction),
this.instantiationService.createInstance(SaveCellAction),
];
}
disposeTemplate(templateData: CellRenderTemplate): void { disposeTemplate(templateData: CellRenderTemplate): void {
// throw nerendererw Error('Method not implemented.'); // throw nerendererw Error('Method not implemented.');
...@@ -307,7 +249,7 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende ...@@ -307,7 +249,7 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
@IInstantiationService instantiationService: IInstantiationService, @IInstantiationService instantiationService: IInstantiationService,
@IKeybindingService keybindingService: IKeybindingService, @IKeybindingService keybindingService: IKeybindingService,
@INotificationService notificationService: INotificationService, @INotificationService notificationService: INotificationService,
@IContextKeyService contextKeyService: IContextKeyService @IContextKeyService contextKeyService: IContextKeyService,
) { ) {
super(instantiationService, notebookEditor, contextMenuService, configurationService, keybindingService, notificationService, contextKeyService, 'python'); super(instantiationService, notebookEditor, contextMenuService, configurationService, keybindingService, notificationService, contextKeyService, 'python');
} }
...@@ -319,12 +261,6 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende ...@@ -319,12 +261,6 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
renderTemplate(container: HTMLElement): CellRenderTemplate { renderTemplate(container: HTMLElement): CellRenderTemplate {
const disposables = new DisposableStore(); const disposables = new DisposableStore();
const toolbar = this.createToolbar(container); const toolbar = this.createToolbar(container);
toolbar.setActions([
this.instantiationService.createInstance(MoveCellUpAction),
this.instantiationService.createInstance(MoveCellDownAction),
this.instantiationService.createInstance(InsertCodeCellBelowAction),
this.instantiationService.createInstance(DeleteCellAction)
])();
disposables.add(toolbar); disposables.add(toolbar);
const cellContainer = DOM.append(container, $('.cell.code')); const cellContainer = DOM.append(container, $('.cell.code'));
...@@ -393,7 +329,7 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende ...@@ -393,7 +329,7 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
elementDisposable?.add(element.onDidChangeCellRunState(() => { elementDisposable?.add(element.onDidChangeCellRunState(() => {
if (element.runState === CellRunState.Running) { if (element.runState === CellRunState.Running) {
templateData.progressBar?.infinite().show(); templateData.progressBar?.infinite().show(500);
} else { } else {
templateData.progressBar?.hide(); templateData.progressBar?.hide();
} }
...@@ -408,7 +344,10 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende ...@@ -408,7 +344,10 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
const contextKeyService = this.contextKeyService.createScoped(templateData.container); const contextKeyService = this.contextKeyService.createScoped(templateData.container);
contextKeyService.createKey(NOTEBOOK_CELL_TYPE_CONTEXT_KEY, 'code'); contextKeyService.createKey(NOTEBOOK_CELL_TYPE_CONTEXT_KEY, 'code');
const toolbarActions = this.getCellToolbarActions(element); contextKeyService.createKey(NOTEBOOK_CELL_EDITABLE_CONTEXT_KEY, element.metadata?.editable);
contextKeyService.createKey(NOTEBOOK_EDITABLE_CONTEXT_KEY, this.notebookEditor.viewModel?.metadata?.editable);
const toolbarActions = this.getCellToolbarActions(contextKeyService);
templateData.toolbar!.setActions(toolbarActions)(); templateData.toolbar!.setActions(toolbarActions)();
templateData.toolbar!.context = toolbarContext; templateData.toolbar!.context = toolbarContext;
templateData.runToolbar!.context = toolbarContext; templateData.runToolbar!.context = toolbarContext;
...@@ -422,39 +361,6 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende ...@@ -422,39 +361,6 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
} }
} }
getCellToolbarActions(element: CodeCellViewModel): IAction[] {
const viewModel = this.notebookEditor.viewModel;
if (!viewModel) {
return [];
}
const menu = this.createMenu().getCellTitleActions(this.contextKeyService);
const actions: IAction[] = [];
for (let [, actions] of menu.getActions({ shouldForwardArgs: true })) {
actions.push(...actions);
}
const metadata = viewModel.metadata;
if (!metadata || metadata.editable) {
actions.push(
this.instantiationService.createInstance(MoveCellUpAction),
this.instantiationService.createInstance(MoveCellDownAction),
this.instantiationService.createInstance(InsertCodeCellBelowAction),
this.instantiationService.createInstance(DeleteCellAction)
);
}
return actions;
}
getAdditionalContextMenuActions(): IAction[] {
return [];
}
disposeTemplate(templateData: CellRenderTemplate): void { disposeTemplate(templateData: CellRenderTemplate): void {
templateData.disposables.clear(); templateData.disposables.clear();
} }
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from 'vs/base/common/event'; import { Emitter, Event } from 'vs/base/common/event';
import { ICell, IOutput, NotebookCellOutputsSplice, CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { ICell, IOutput, NotebookCellOutputsSplice, CellKind, NotebookCellMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { PieceTreeTextBufferFactory, PieceTreeTextBufferBuilder } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder'; import { PieceTreeTextBufferFactory, PieceTreeTextBufferBuilder } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder';
import { URI } from 'vs/base/common/uri'; import { URI } from 'vs/base/common/uri';
...@@ -38,7 +38,8 @@ export class NotebookCellTextModel implements ICell { ...@@ -38,7 +38,8 @@ export class NotebookCellTextModel implements ICell {
private _source: string[], private _source: string[],
public language: string, public language: string,
public cellKind: CellKind, public cellKind: CellKind,
outputs: IOutput[] outputs: IOutput[],
public readonly metadata: NotebookCellMetadata | undefined
) { ) {
this._outputs = outputs; this._outputs = outputs;
} }
......
...@@ -72,7 +72,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel ...@@ -72,7 +72,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
splices.reverse().forEach(splice => { splices.reverse().forEach(splice => {
let cellDtos = splice[2]; let cellDtos = splice[2];
let newCells = cellDtos.map(cell => { let newCells = cellDtos.map(cell => {
let mainCell = new NotebookCellTextModel(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs || []); let mainCell = new NotebookCellTextModel(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs || [], cell.metadata);
this._mapping.set(cell.handle, mainCell); this._mapping.set(cell.handle, mainCell);
let dirtyStateListener = mainCell.onDidChangeContent(() => { let dirtyStateListener = mainCell.onDidChangeContent(() => {
this._onDidChangeContent.fire(); this._onDidChangeContent.fire();
......
...@@ -33,14 +33,16 @@ suite('NotebookViewModel', () => { ...@@ -33,14 +33,16 @@ suite('NotebookViewModel', () => {
blukEditService, blukEditService,
undoRedoService, undoRedoService,
[ [
[['var a = 1;'], 'javascript', CellKind.Code, []], [['var a = 1;'], 'javascript', CellKind.Code, [], { editable: true }],
[['var b = 2;'], 'javascript', CellKind.Code, []] [['var b = 2;'], 'javascript', CellKind.Code, [], { editable: false }]
], ],
(editor, viewModel) => { (editor, viewModel) => {
const cell = viewModel.insertCell(1, new TestCell(viewModel.viewType, 0, ['var c = 3;'], 'javascript', CellKind.Code, []), true); const cell = viewModel.insertCell(1, new TestCell(viewModel.viewType, 0, ['var c = 3;'], 'javascript', CellKind.Code, []), true);
assert.equal(viewModel.viewCells.length, 3); assert.equal(viewModel.viewCells.length, 3);
assert.equal(viewModel.notebookDocument.cells.length, 3); assert.equal(viewModel.notebookDocument.cells.length, 3);
assert.equal(viewModel.getViewCellIndex(cell), 1); assert.equal(viewModel.getViewCellIndex(cell), 1);
assert.equal(viewModel.viewCells[0].metadata?.editable, true);
assert.equal(viewModel.viewCells[0].metadata?.editable, false);
viewModel.deleteCell(1, true); viewModel.deleteCell(1, true);
assert.equal(viewModel.viewCells.length, 2); assert.equal(viewModel.viewCells.length, 2);
...@@ -56,8 +58,8 @@ suite('NotebookViewModel', () => { ...@@ -56,8 +58,8 @@ suite('NotebookViewModel', () => {
blukEditService, blukEditService,
undoRedoService, undoRedoService,
[ [
[['var a = 1;'], 'javascript', CellKind.Code, []], [['var a = 1;'], 'javascript', CellKind.Code, [], { editable: true }],
[['var b = 2;'], 'javascript', CellKind.Code, []] [['var b = 2;'], 'javascript', CellKind.Code, [], { editable: true }]
], ],
(editor, viewModel) => { (editor, viewModel) => {
const firstViewCell = viewModel.viewCells[0]; const firstViewCell = viewModel.viewCells[0];
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
import { Emitter, Event } from 'vs/base/common/event'; import { Emitter, Event } from 'vs/base/common/event';
import { URI } from 'vs/base/common/uri'; import { URI } from 'vs/base/common/uri';
import { PieceTreeTextBufferFactory } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder'; import { PieceTreeTextBufferFactory } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder';
import { CellKind, ICell, IOutput, NotebookCellOutputsSplice, CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { CellKind, ICell, IOutput, NotebookCellOutputsSplice, CellUri, NotebookCellMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { NotebookViewModel, IModelDecorationsChangeAccessor, CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { NotebookViewModel, IModelDecorationsChangeAccessor, CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput'; import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
...@@ -187,12 +187,12 @@ export class TestNotebookEditor implements INotebookEditor { ...@@ -187,12 +187,12 @@ export class TestNotebookEditor implements INotebookEditor {
// return createCellViewModel(instantiationService, viewType, notebookHandle, mockCell); // return createCellViewModel(instantiationService, viewType, notebookHandle, mockCell);
// } // }
export function withTestNotebook(instantiationService: IInstantiationService, blukEditService: IBulkEditService, undoRedoService: IUndoRedoService, cells: [string[], string, CellKind, IOutput[]][], callback: (editor: TestNotebookEditor, viewModel: NotebookViewModel) => void) { export function withTestNotebook(instantiationService: IInstantiationService, blukEditService: IBulkEditService, undoRedoService: IUndoRedoService, cells: [string[], string, CellKind, IOutput[], NotebookCellMetadata][], callback: (editor: TestNotebookEditor, viewModel: NotebookViewModel) => void) {
const viewType = 'notebook'; const viewType = 'notebook';
const editor = new TestNotebookEditor(); const editor = new TestNotebookEditor();
const notebook = new NotebookTextModel(0, viewType, URI.parse('test')); const notebook = new NotebookTextModel(0, viewType, URI.parse('test'));
notebook.cells = cells.map((cell, index) => { notebook.cells = cells.map((cell, index) => {
return new NotebookCellTextModel(notebook.uri, index, cell[0], cell[1], cell[2], cell[3]); return new NotebookCellTextModel(notebook.uri, index, cell[0], cell[1], cell[2], cell[3], cell[4]);
}); });
const model = new NotebookEditorModel(notebook); const model = new NotebookEditorModel(notebook);
const viewModel = new NotebookViewModel(viewType, model, instantiationService, blukEditService, undoRedoService); const viewModel = new NotebookViewModel(viewType, model, instantiationService, blukEditService, undoRedoService);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册