提交 3f3dcf16 编写于 作者: R Rob Lourens

Add commands to convert cell to markdown/code

上级 e814a196
......@@ -69,7 +69,7 @@ registerAction2(class extends Action2 {
if (nextCell) {
editor.focusNotebookCell(nextCell, false);
} else {
editor.insertEmptyNotebookCell(activeCell, CellKind.Code, 'below');
editor.insertNotebookCell(activeCell, CellKind.Code, 'below');
}
}
});
......@@ -99,7 +99,7 @@ registerAction2(class extends Action2 {
return;
}
editor.insertEmptyNotebookCell(activeCell, CellKind.Code, 'below');
editor.insertNotebookCell(activeCell, CellKind.Code, 'below');
}
});
......@@ -254,6 +254,41 @@ MenuRegistry.appendMenuItem(MenuId.EditorTitle, {
when: NOTEBOOK_EDITOR_FOCUSED
});
registerAction2(class extends Action2 {
constructor() {
super({
id: 'workbench.action.changeCellToCode',
title: 'Change Cell to Code',
keybinding: {
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.not(InputFocusedContextKey)),
primary: KeyCode.KEY_Y,
weight: KeybindingWeight.WorkbenchContrib
}
});
}
async run(accessor: ServicesAccessor): Promise<void> {
return changeActiveCellToKind(CellKind.Code, accessor);
}
});
registerAction2(class extends Action2 {
constructor() {
super({
id: 'workbench.action.changeCellToMarkdown',
title: 'Change Cell to Markdown',
keybinding: {
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.not(InputFocusedContextKey)),
primary: KeyCode.KEY_M,
weight: KeybindingWeight.WorkbenchContrib
}
});
}
async run(accessor: ServicesAccessor): Promise<void> {
return changeActiveCellToKind(CellKind.Markdown, accessor);
}
});
function getActiveNotebookEditor(editorService: IEditorService): NotebookEditor | undefined {
const activeEditorPane = editorService.activeEditorPane as NotebookEditor | undefined;
......@@ -294,3 +329,35 @@ function runActiveCell(accessor: ServicesAccessor): CellViewModel | undefined {
return activeCell;
}
function changeActiveCellToKind(kind: CellKind, accessor: ServicesAccessor): void {
const editorService = accessor.get(IEditorService);
const editor = getActiveNotebookEditor(editorService);
if (!editor) {
return;
}
const activeCell = editor.getActiveCell();
if (!activeCell) {
return;
}
if (activeCell.cellKind === kind) {
return;
}
const text = activeCell.getText();
editor.insertNotebookCell(activeCell, kind, 'below', text);
const idx = editor.viewModel?.getViewCellIndex(activeCell);
if (typeof idx !== 'number') {
return;
}
const newCell = editor.viewModel?.viewCells[idx + 1];
if (!newCell) {
return;
}
editor.focusNotebookCell(newCell, false);
editor.deleteNotebookCell(activeCell);
}
......@@ -46,7 +46,7 @@ export interface INotebookEditor {
/**
* Insert a new cell around `cell`
*/
insertEmptyNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise<void>;
insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below', initialText?: string): Promise<void>;
/**
* Delete a cell from the notebook
......
......@@ -534,12 +534,13 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor {
});
}
async insertEmptyNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise<void> {
async insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below', initialText: string = ''): Promise<void> {
const newLanguages = this.notebookViewModel!.languages;
const language = newLanguages && newLanguages.length ? newLanguages[0] : 'markdown';
const index = this.notebookViewModel!.getViewCellIndex(cell);
const insertIndex = direction === 'above' ? index : index + 1;
const newModeCell = await this.notebookService.createNotebookCell(this.notebookViewModel!.viewType, this.notebookViewModel!.uri, insertIndex, language, type);
newModeCell!.source = initialText.split(/\r?\n/g);
const newCell = this.notebookViewModel!.insertCell(insertIndex, newModeCell!);
this.list?.splice(insertIndex, 0, [newCell]);
......
......@@ -89,7 +89,7 @@ class AbstractCellRenderer {
undefined,
true,
async () => {
await this.notebookEditor.insertEmptyNotebookCell(element, CellKind.Code, 'above');
await this.notebookEditor.insertNotebookCell(element, CellKind.Code, 'above');
}
);
actions.push(insertAbove);
......@@ -100,7 +100,7 @@ class AbstractCellRenderer {
undefined,
true,
async () => {
await this.notebookEditor.insertEmptyNotebookCell(element, CellKind.Code, 'below');
await this.notebookEditor.insertNotebookCell(element, CellKind.Code, 'below');
}
);
actions.push(insertBelow);
......@@ -111,7 +111,7 @@ class AbstractCellRenderer {
undefined,
true,
async () => {
await this.notebookEditor.insertEmptyNotebookCell(element, CellKind.Markdown, 'above');
await this.notebookEditor.insertNotebookCell(element, CellKind.Markdown, 'above');
}
);
actions.push(insertMarkdownAbove);
......@@ -122,7 +122,7 @@ class AbstractCellRenderer {
undefined,
true,
async () => {
await this.notebookEditor.insertEmptyNotebookCell(element, CellKind.Markdown, 'below');
await this.notebookEditor.insertNotebookCell(element, CellKind.Markdown, 'below');
}
);
actions.push(insertMarkdownBelow);
......
......@@ -134,7 +134,7 @@ export class TestNotebookEditor implements INotebookEditor {
revealInCenterIfOutsideViewport(cell: CellViewModel): void {
throw new Error('Method not implemented.');
}
async insertEmptyNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise<void> {
async insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise<void> {
// throw new Error('Method not implemented.');
}
deleteNotebookCell(cell: CellViewModel): void {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册