Skip to content

Commit

Permalink
Align to vscode notebook commands (#13645)
Browse files Browse the repository at this point in the history
* aligned commands to vscodes commands
this makes more keybindings available.
Also implements to notbookOutputInputFocused context key

Signed-off-by: Jonah Iden <jonah.iden@typefox.io>

* fixed type and forgotten dispose of emitter

Signed-off-by: Jonah Iden <jonah.iden@typefox.io>

* fixed lint

Signed-off-by: Jonah Iden <jonah.iden@typefox.io>

---------

Signed-off-by: Jonah Iden <jonah.iden@typefox.io>
  • Loading branch information
jonah-iden authored Apr 30, 2024
1 parent 6a82f78 commit c1f222d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,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 @@ -74,6 +75,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
8 changes: 8 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 onDidChangeOutputInputFocusEmitter = new Emitter<boolean>();
readonly onDidChangeOutputInputFocus = this.onDidChangeOutputInputFocusEmitter.event;

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

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

override dispose(): void {
this.notebookContextManager.dispose();
this.onDidChangeModelEmitter.dispose();
this.onDidPostKernelMessageEmitter.dispose();
this.onDidReceiveKernelMessageEmitter.dispose();
this.onPostRendererMessageEmitter.dispose();
this.onDidChangeOutputInputFocusEmitter.dispose();
this.viewportService.dispose();
this._model?.dispose();
super.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 @@ -286,6 +286,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;
}

0 comments on commit c1f222d

Please sign in to comment.