Skip to content

Commit c1f222d

Browse files
authored
Align to vscode notebook commands (#13645)
* aligned commands to vscodes commands this makes more keybindings available. Also implements to notbookOutputInputFocused context key Signed-off-by: Jonah Iden <[email protected]> * fixed type and forgotten dispose of emitter Signed-off-by: Jonah Iden <[email protected]> * fixed lint Signed-off-by: Jonah Iden <[email protected]> --------- Signed-off-by: Jonah Iden <[email protected]>
1 parent 6a82f78 commit c1f222d

File tree

8 files changed

+58
-23
lines changed

8 files changed

+58
-23
lines changed

packages/notebook/src/browser/contributions/notebook-actions-contribution.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ export namespace NotebookCommands {
7070
});
7171

7272
export const CUT_SELECTED_CELL = Command.toDefaultLocalizedCommand({
73-
id: 'notebook.cut-selected-cell',
73+
id: 'notebook.cell.cut',
7474
category: 'Notebook',
7575
});
7676

7777
export const COPY_SELECTED_CELL = Command.toDefaultLocalizedCommand({
78-
id: 'notebook.copy-selected-cell',
78+
id: 'notebook.cell.copy',
7979
category: 'Notebook',
8080
});
8181

8282
export const PASTE_CELL = Command.toDefaultLocalizedCommand({
83-
id: 'notebook.paste-cell',
83+
id: 'notebook.cell.paste',
8484
category: 'Notebook',
8585
});
8686
}

packages/notebook/src/browser/contributions/notebook-cell-actions-contribution.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,24 @@ export namespace NotebookCellCommands {
120120
label: 'Change Cell to Mardown'
121121
});
122122

123-
export const COLLAPSE_CELL_OUTPUT = Command.toDefaultLocalizedCommand({
124-
id: 'notebook.cell.collapseCellOutput',
123+
export const TOGGLE_CELL_OUTPUT = Command.toDefaultLocalizedCommand({
124+
id: 'notebook.cell.toggleOutputs',
125125
category: 'Notebook',
126126
label: 'Collapse Cell Output',
127127
});
128128

129-
export const EXPAND_CELL_OUTPUT = Command.toDefaultLocalizedCommand({
130-
id: 'notebook.cell.expandCellOutput',
131-
category: 'Notebook',
132-
label: 'Expand Cell Output',
133-
});
134-
135129
export const CHANGE_CELL_LANGUAGE = Command.toDefaultLocalizedCommand({
136130
id: 'notebook.cell.changeLanguage',
137131
category: 'Notebook',
138132
label: 'Change Cell Language',
139133
});
140134

135+
export const TOGGLE_LINE_NUMBERS = Command.toDefaultLocalizedCommand({
136+
id: 'notebook.cell.toggleLineNumbers',
137+
category: 'Notebook',
138+
label: 'Show Cell Line Numbers',
139+
});
140+
141141
}
142142

143143
@injectable()
@@ -351,20 +351,11 @@ export class NotebookCellActionContribution implements MenuContribution, Command
351351
changeCellType(notebookModel, cell, CellKind.Markup);
352352
}));
353353

354-
commands.registerCommand(NotebookCellCommands.COLLAPSE_CELL_OUTPUT, {
355-
execute: () => {
356-
const selectedCell = this.notebookEditorWidgetService.focusedEditor?.model?.selectedCell;
357-
if (selectedCell) {
358-
selectedCell.outputVisible = false;
359-
}
360-
}
361-
});
362-
363-
commands.registerCommand(NotebookCellCommands.EXPAND_CELL_OUTPUT, {
354+
commands.registerCommand(NotebookCellCommands.TOGGLE_CELL_OUTPUT, {
364355
execute: () => {
365356
const selectedCell = this.notebookEditorWidgetService.focusedEditor?.model?.selectedCell;
366357
if (selectedCell) {
367-
selectedCell.outputVisible = true;
358+
selectedCell.outputVisible = !selectedCell.outputVisible;
368359
}
369360
}
370361
});
@@ -387,6 +378,16 @@ export class NotebookCellActionContribution implements MenuContribution, Command
387378
}
388379
});
389380

381+
commands.registerCommand(NotebookCellCommands.TOGGLE_LINE_NUMBERS, {
382+
execute: () => {
383+
const selectedCell = this.notebookEditorWidgetService.focusedEditor?.model?.selectedCell;
384+
if (selectedCell) {
385+
const currentLineNumber = selectedCell.editorOptions?.lineNumbers;
386+
selectedCell.editorOptions = { ...selectedCell.editorOptions, lineNumbers: !currentLineNumber || currentLineNumber === 'off' ? 'on' : 'off' };
387+
}
388+
}
389+
});
390+
390391
}
391392

392393
protected editableCellCommandHandler(execute: (notebookModel: NotebookModel, cell: NotebookCellModel, output?: NotebookCellOutputModel) => void): CommandHandler {

packages/notebook/src/browser/contributions/notebook-context-keys.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const KEYBINDING_CONTEXT_NOTEBOOK_FIND_WIDGET_FOCUSED = 'notebookFindWidg
3030
export const NOTEBOOK_EDITOR_FOCUSED = 'notebookEditorFocused';
3131
export const NOTEBOOK_CELL_LIST_FOCUSED = 'notebookCellListFocused';
3232
export const NOTEBOOK_OUTPUT_FOCUSED = 'notebookOutputFocused';
33+
export const NOTEBOOK_OUTPUT_INPUT_FOCUSED = 'notebookOutputInputFocused';
3334
export const NOTEBOOK_EDITOR_EDITABLE = 'notebookEditable';
3435
export const NOTEBOOK_HAS_RUNNING_CELL = 'notebookHasRunningCell';
3536
export const NOTEBOOK_USE_CONSOLIDATED_OUTPUT_BUTTON = 'notebookUseConsolidatedOutputButton';
@@ -74,6 +75,7 @@ export namespace NotebookContextKeys {
7475
service.createKey(NOTEBOOK_EDITOR_FOCUSED, false);
7576
service.createKey(NOTEBOOK_CELL_LIST_FOCUSED, false);
7677
service.createKey(NOTEBOOK_OUTPUT_FOCUSED, false);
78+
service.createKey(NOTEBOOK_OUTPUT_INPUT_FOCUSED, false);
7779
service.createKey(NOTEBOOK_EDITOR_EDITABLE, true);
7880
service.createKey(NOTEBOOK_HAS_RUNNING_CELL, false);
7981
service.createKey(NOTEBOOK_USE_CONSOLIDATED_OUTPUT_BUTTON, false);

packages/notebook/src/browser/notebook-editor-widget.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ export class NotebookEditorWidget extends ReactWidget implements Navigatable, Sa
120120
protected readonly onDidReceiveKernelMessageEmitter = new Emitter<unknown>();
121121
readonly onDidReceiveKernelMessage = this.onDidReceiveKernelMessageEmitter.event;
122122

123+
protected readonly onDidChangeOutputInputFocusEmitter = new Emitter<boolean>();
124+
readonly onDidChangeOutputInputFocus = this.onDidChangeOutputInputFocusEmitter.event;
125+
123126
protected readonly renderers = new Map<CellKind, CellRenderer>();
124127
protected _model?: NotebookModel;
125128
protected _ready: Deferred<NotebookModel> = new Deferred();
@@ -251,12 +254,17 @@ export class NotebookEditorWidget extends ReactWidget implements Navigatable, Sa
251254
this.onDidReceiveKernelMessageEmitter.fire(message);
252255
}
253256

257+
outputInputFocusChanged(focused: boolean): void {
258+
this.onDidChangeOutputInputFocusEmitter.fire(focused);
259+
}
260+
254261
override dispose(): void {
255262
this.notebookContextManager.dispose();
256263
this.onDidChangeModelEmitter.dispose();
257264
this.onDidPostKernelMessageEmitter.dispose();
258265
this.onDidReceiveKernelMessageEmitter.dispose();
259266
this.onPostRendererMessageEmitter.dispose();
267+
this.onDidChangeOutputInputFocusEmitter.dispose();
260268
this.viewportService.dispose();
261269
this._model?.dispose();
262270
super.dispose();

packages/notebook/src/browser/service/notebook-context-manager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
NOTEBOOK_CELL_EXECUTING, NOTEBOOK_CELL_EXECUTION_STATE,
2424
NOTEBOOK_CELL_FOCUSED, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE,
2525
NOTEBOOK_CELL_TYPE, NOTEBOOK_HAS_OUTPUTS, NOTEBOOK_KERNEL, NOTEBOOK_KERNEL_SELECTED,
26+
NOTEBOOK_OUTPUT_INPUT_FOCUSED,
2627
NOTEBOOK_VIEW_TYPE
2728
} from '../contributions/notebook-context-keys';
2829
import { NotebookEditorWidget } from '../notebook-editor-widget';
@@ -101,6 +102,11 @@ export class NotebookContextManager {
101102

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

105+
widget.onDidChangeOutputInputFocus(focus => {
106+
this.scopedStore.setContext(NOTEBOOK_OUTPUT_INPUT_FOCUSED, focus);
107+
this.onDidChangeContextEmitter.fire(this.createContextKeyChangedEvent([NOTEBOOK_OUTPUT_INPUT_FOCUSED]));
108+
});
109+
104110
this.onDidChangeContextEmitter.fire(this.createContextKeyChangedEvent([NOTEBOOK_VIEW_TYPE, NOTEBOOK_KERNEL_SELECTED, NOTEBOOK_KERNEL]));
105111
}
106112

packages/plugin-ext/src/main/browser/notebooks/renderers/cell-output-webview.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ export class CellOutputWebviewImpl implements CellOutputWebview, Disposable {
286286
// console.log('from webview customKernelMessage ', JSON.stringify(message.message));
287287
this.editor.recieveKernelMessage(message.message);
288288
break;
289+
case 'inputFocusChanged':
290+
this.editor?.outputInputFocusChanged(message.focused);
289291
}
290292
}
291293

packages/plugin-ext/src/main/browser/notebooks/renderers/output-webview-internal.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,5 +581,15 @@ export async function outputWebviewPreload(ctx: PreloadContext): Promise<void> {
581581
return this.originalAppendChild(node);
582582
};
583583

584+
const focusChange = (event: FocusEvent, focus: boolean) => {
585+
if (event.target instanceof HTMLInputElement) {
586+
theia.postMessage({ type: 'inputFocusChanged', focused: focus } as webviewCommunication.InputFocusChange);
587+
}
588+
};
589+
590+
window.addEventListener('focusin', (event: FocusEvent) => focusChange(event, true));
591+
592+
window.addEventListener('focusout', (event: FocusEvent) => focusChange(event, false));
593+
584594
theia.postMessage(<webviewCommunication.WebviewInitialized>{ type: 'initialized' });
585595
}

packages/plugin-ext/src/main/browser/notebooks/renderers/webview-communication.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ export interface WheelMessage {
7676
readonly deltaX: number;
7777
}
7878

79-
export type FromWebviewMessage = WebviewInitialized | OnDidRenderOutput | WheelMessage | CustomRendererMessage | KernelMessage;
79+
export interface InputFocusChange {
80+
readonly type: 'inputFocusChanged';
81+
readonly focused: boolean;
82+
}
83+
84+
export type FromWebviewMessage = WebviewInitialized | OnDidRenderOutput | WheelMessage | CustomRendererMessage | KernelMessage | InputFocusChange;
8085

8186
export interface Output {
8287
id: string
@@ -88,3 +93,4 @@ export interface OutputItem {
8893
readonly mime: string;
8994
readonly data: Uint8Array;
9095
}
96+

0 commit comments

Comments
 (0)