Skip to content

Commit

Permalink
Fix Pressing enter in console with console run keystroke set to enter…
Browse files Browse the repository at this point in the history
… creates a newline and runs (jupyterlab#15869)

* fix: cells editorOptions return extensions option

* fix: remove editorExtensions function

* fix: call preventDefault() only when Enter key is pressed in the console and INTERACTION_MODE=terminal

---------

Co-authored-by: yelipei <yelipei@qianxin.com>
  • Loading branch information
FoSuCloud and yelipei authored Mar 4, 2024
1 parent 3f98909 commit be1c486
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/cells/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export class Cell<T extends ICellModel = ICellModel> extends Widget {

// For cells disable searching with CodeMirror search panel.
this._editorConfig = { searchWithCM: false, ...options.editorConfig };
this._editorExtensions = options.editorExtensions ?? [];
this._placeholder = true;
this._inViewport = false;
this.placeholder = options.placeholder ?? true;
Expand Down Expand Up @@ -615,7 +616,7 @@ export class Cell<T extends ICellModel = ICellModel> extends Widget {
* @returns Editor options
*/
protected getEditorOptions(): InputArea.IOptions['editorOptions'] {
return { config: this.editorConfig };
return { config: this.editorConfig, extensions: this._editorExtensions };
}

/**
Expand Down Expand Up @@ -694,6 +695,7 @@ export class Cell<T extends ICellModel = ICellModel> extends Widget {
protected _displayChanged = new Signal<this, void>(this);

private _editorConfig: Record<string, any> = {};
private _editorExtensions: Extension[] = [];
private _input: InputArea | null;
private _inputHidden = false;
private _inputWrapper: Widget | null;
Expand Down
11 changes: 10 additions & 1 deletion packages/console/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ const JUPYTER_CELL_MIME = 'application/vnd.jupyter.cells';
* The data attribute added to a widget that can undo.
*/
const UNDOER = 'jpUndoer';
/**
* The data attribute Whether the console interaction mimics the notebook
* or terminal keyboard shortcuts.
*/
const INTERACTION_MODE = 'jpInteractionMode';

/**
* A widget containing a Jupyter console.
Expand Down Expand Up @@ -789,6 +794,7 @@ export class CodeConsole extends Widget {
* Create the options used to initialize a code cell widget.
*/
private _createCodeCellOptions(): CodeCell.IOptions {
const { node } = this;
const contentFactory = this.contentFactory;
const modelFactory = this.modelFactory;
const model = modelFactory.createCodeCell({});
Expand All @@ -798,7 +804,10 @@ export class CodeConsole extends Widget {
// Suppress the default "Enter" key handling.
const onKeyDown = EditorView.domEventHandlers({
keydown: (event: KeyboardEvent, view: EditorView) => {
if (event.keyCode === 13) {
if (
event.keyCode === 13 &&
node.dataset[INTERACTION_MODE] === 'terminal'
) {
event.preventDefault();
return true;
}
Expand Down

0 comments on commit be1c486

Please sign in to comment.