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

Align to vscode notebook commands #13645

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ export namespace NotebookCommands {
});

export const CUT_SELECTED_CELL = Command.toDefaultLocalizedCommand({
id: 'notebook.cut-selected-cell',
id: 'notebook.cell.cut',
category: 'Notebook',
});

export const COPY_SELECTED_CELL = Command.toDefaultLocalizedCommand({
id: 'notebook.copy-selected-cell',
id: 'notebook.cell.copy',
category: 'Notebook',
});

export const PASTE_CELL = Command.toDefaultLocalizedCommand({
id: 'notebook.paste-cell',
id: 'notebook.cell.paste',
category: 'Notebook',
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,24 @@ export namespace NotebookCellCommands {
label: 'Change Cell to Mardown'
});

export const COLLAPSE_CELL_OUTPUT = Command.toDefaultLocalizedCommand({
id: 'notebook.cell.collapseCellOutput',
export const TOGGLE_CELL_OUTPUT = Command.toDefaultLocalizedCommand({
id: 'notebook.cell.toggleOutputs',
category: 'Notebook',
label: 'Collapse Cell Output',
});

export const EXPAND_CELL_OUTPUT = Command.toDefaultLocalizedCommand({
id: 'notebook.cell.expandCellOutput',
category: 'Notebook',
label: 'Expand Cell Output',
});

export const CHANGE_CELL_LANGUAGE = Command.toDefaultLocalizedCommand({
id: 'notebook.cell.changeLanguage',
category: 'Notebook',
label: 'Change Cell Language',
});

export const TOGGLE_LINE_NUMBERS = Command.toDefaultLocalizedCommand({
id: 'notebook.cell.toggleLineNumbers',
category: 'Notebook',
label: 'Show Cell Line Numbers',
});

}

@injectable()
Expand Down Expand Up @@ -351,20 +351,11 @@ export class NotebookCellActionContribution implements MenuContribution, Command
changeCellType(notebookModel, cell, CellKind.Markup);
}));

commands.registerCommand(NotebookCellCommands.COLLAPSE_CELL_OUTPUT, {
execute: () => {
const selectedCell = this.notebookEditorWidgetService.focusedEditor?.model?.selectedCell;
if (selectedCell) {
selectedCell.outputVisible = false;
}
}
});

commands.registerCommand(NotebookCellCommands.EXPAND_CELL_OUTPUT, {
commands.registerCommand(NotebookCellCommands.TOGGLE_CELL_OUTPUT, {
execute: () => {
const selectedCell = this.notebookEditorWidgetService.focusedEditor?.model?.selectedCell;
if (selectedCell) {
selectedCell.outputVisible = true;
selectedCell.outputVisible = !selectedCell.outputVisible;
}
}
});
Expand All @@ -387,6 +378,16 @@ export class NotebookCellActionContribution implements MenuContribution, Command
}
});

commands.registerCommand(NotebookCellCommands.TOGGLE_LINE_NUMBERS, {
execute: () => {
const selectedCell = this.notebookEditorWidgetService.focusedEditor?.model?.selectedCell;
if (selectedCell) {
const currentLineNumber = selectedCell.editorOptions?.lineNumbers;
selectedCell.editorOptions = { ...selectedCell.editorOptions, lineNumbers: !currentLineNumber || currentLineNumber === 'off' ? 'on' : 'off' };
}
}
});

}

protected editableCellCommandHandler(execute: (notebookModel: NotebookModel, cell: NotebookCellModel, output?: NotebookCellOutputModel) => void): CommandHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED = 'notebookFindWidg
export const NOTEBOOK_EDITOR_FOCUSED = 'notebookEditorFocused';
export const NOTEBOOK_CELL_LIST_FOCUSED = 'notebookCellListFocused';
export const NOTEBOOK_OUTPUT_FOCUSED = 'notebookOutputFocused';
export const NOTEBOOK_OUTPUT_INPUT_FOCUSED = 'notebookOutputInputFocused';
export const NOTEBOOK_EDITOR_EDITABLE = 'notebookEditable';
export const NOTEBOOK_HAS_RUNNING_CELL = 'notebookHasRunningCell';
export const NOTEBOOK_USE_CONSOLIDATED_OUTPUT_BUTTON = 'notebookUseConsolidatedOutputButton';
Expand Down Expand Up @@ -71,6 +72,7 @@ export namespace NotebookContextKeys {
service.createKey(NOTEBOOK_EDITOR_FOCUSED, false);
service.createKey(NOTEBOOK_CELL_LIST_FOCUSED, false);
service.createKey(NOTEBOOK_OUTPUT_FOCUSED, false);
service.createKey(NOTEBOOK_OUTPUT_INPUT_FOCUSED, false);
service.createKey(NOTEBOOK_EDITOR_EDITABLE, true);
service.createKey(NOTEBOOK_HAS_RUNNING_CELL, false);
service.createKey(NOTEBOOK_USE_CONSOLIDATED_OUTPUT_BUTTON, false);
Expand Down
7 changes: 7 additions & 0 deletions packages/notebook/src/browser/notebook-editor-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export class NotebookEditorWidget extends ReactWidget implements Navigatable, Sa
protected readonly onDidReceiveKernelMessageEmitter = new Emitter<unknown>();
readonly onDidReceiveKernelMessage = this.onDidReceiveKernelMessageEmitter.event;

protected readonly onDidChangeOutputInputFocuEmitter = new Emitter<boolean>();
jonah-iden marked this conversation as resolved.
Show resolved Hide resolved
readonly onDidChangeOutputInputFocus = this.onDidChangeOutputInputFocuEmitter.event;

protected readonly renderers = new Map<CellKind, CellRenderer>();
protected _model?: NotebookModel;
protected _ready: Deferred<NotebookModel> = new Deferred();
Expand Down Expand Up @@ -251,6 +254,10 @@ export class NotebookEditorWidget extends ReactWidget implements Navigatable, Sa
this.onDidReceiveKernelMessageEmitter.fire(message);
}

outputInputFocusChanged(focused: boolean): void {
this.onDidChangeOutputInputFocuEmitter.fire(focused);
}

override dispose(): void {
this.notebookContextManager.dispose();
this.onDidChangeModelEmitter.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
NOTEBOOK_CELL_EXECUTING, NOTEBOOK_CELL_EXECUTION_STATE,
NOTEBOOK_CELL_FOCUSED, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE,
NOTEBOOK_CELL_TYPE, NOTEBOOK_HAS_OUTPUTS, NOTEBOOK_KERNEL, NOTEBOOK_KERNEL_SELECTED,
NOTEBOOK_OUTPUT_INPUT_FOCUSED,
NOTEBOOK_VIEW_TYPE
} from '../contributions/notebook-context-keys';
import { NotebookEditorWidget } from '../notebook-editor-widget';
Expand Down Expand Up @@ -101,6 +102,11 @@ export class NotebookContextManager {

widget.model?.onDidChangeSelectedCell(e => this.selectedCellChanged(e));

widget.onDidChangeOutputInputFocus(focus => {
this.scopedStore.setContext(NOTEBOOK_OUTPUT_INPUT_FOCUSED, focus);
this.onDidChangeContextEmitter.fire(this.createContextKeyChangedEvent([NOTEBOOK_OUTPUT_INPUT_FOCUSED]));
});

this.onDidChangeContextEmitter.fire(this.createContextKeyChangedEvent([NOTEBOOK_VIEW_TYPE, NOTEBOOK_KERNEL_SELECTED, NOTEBOOK_KERNEL]));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ export class CellOutputWebviewImpl implements CellOutputWebview, Disposable {
// console.log('from webview customKernelMessage ', JSON.stringify(message.message));
this.editor.recieveKernelMessage(message.message);
break;
case 'inputFocusChanged':
this.editor?.outputInputFocusChanged(message.focused);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,5 +581,15 @@ export async function outputWebviewPreload(ctx: PreloadContext): Promise<void> {
return this.originalAppendChild(node);
};

const focusChange = (event: FocusEvent, focus: boolean) => {
if (event.target instanceof HTMLInputElement) {
theia.postMessage({ type: 'inputFocusChanged', focused: focus } as webviewCommunication.InputFocusChange);
}
};

window.addEventListener('focusin', (event: FocusEvent) => focusChange(event, true));

window.addEventListener('focusout', (event: FocusEvent) => focusChange(event, false));

theia.postMessage(<webviewCommunication.WebviewInitialized>{ type: 'initialized' });
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ export interface WheelMessage {
readonly deltaX: number;
}

export type FromWebviewMessage = WebviewInitialized | OnDidRenderOutput | WheelMessage | CustomRendererMessage | KernelMessage;
export interface InputFocusChange {
readonly type: 'inputFocusChanged';
readonly focused: boolean;
}

export type FromWebviewMessage = WebviewInitialized | OnDidRenderOutput | WheelMessage | CustomRendererMessage | KernelMessage | InputFocusChange;

export interface Output {
id: string
Expand All @@ -88,3 +93,4 @@ export interface OutputItem {
readonly mime: string;
readonly data: Uint8Array;
}

Loading