Skip to content

Commit 7362b25

Browse files
author
David Kutugata
authored
added commands for 'dd' (#7313)
* added commands for 'dd', 'ctrl + enter', 'alt + enter', 'a', 'b', 'j', 'k' to behave just like jupyterLabs. * added news file * removed the addEmptyCell function, it was redundant. * Added parameter to insertCell to check if the cell being inserted should be monaco or not
1 parent bb6899c commit 7362b25

File tree

4 files changed

+89
-18
lines changed

4 files changed

+89
-18
lines changed

news/2 Fixes/7229.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added commands for 'dd', 'ctrl + enter', 'alt + enter', 'a', 'b', 'j', 'k' in the native Editor to behave just like JupyterLabs.

src/datascience-ui/interactive-common/mainStateController.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ export class MainStateController implements IMessageHandler {
701701
this.insertCell(cell);
702702
}
703703

704-
protected insertCell(cell: ICell, position?: number): ICellViewModel | undefined {
704+
protected insertCell(cell: ICell, position?: number, isMonaco?: boolean): ICellViewModel | undefined {
705705
if (cell) {
706706
const showInputs = getSettings().showCellInputCode;
707707
const collapseInputs = getSettings().collapseCellInputCodeByDefault;
@@ -711,6 +711,10 @@ export class MainStateController implements IMessageHandler {
711711
cellVM = this.alterCellVM(cellVM, showInputs, !collapseInputs);
712712

713713
if (cellVM) {
714+
if (isMonaco) {
715+
cellVM.useQuickEdit = false;
716+
}
717+
714718
const newList = [...this.state.cellVMs];
715719
// Make sure to use the same array so our entire state doesn't update
716720
if (position && position >= 0) {

src/datascience-ui/native-editor/nativeEditor.tsx

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class NativeEditor extends React.Component<INativeEditorProps, IMainState
4040
private stateController: NativeEditorStateController;
4141
private initialCellDivs: (HTMLDivElement | null)[] = [];
4242
private debounceUpdateVisibleCells = debounce(this.updateVisibleCells.bind(this), 100);
43+
private pressedDOnce = false;
4344

4445
constructor(props: INativeEditorProps) {
4546
super(props);
@@ -281,68 +282,114 @@ export class NativeEditor extends React.Component<INativeEditorProps, IMainState
281282
}
282283
}
283284

284-
// tslint:disable-next-line: cyclomatic-complexity
285-
private keyDownCell = (cellId: string, e: IKeyboardEvent) => {
285+
// tslint:disable-next-line: cyclomatic-complexity max-func-body-length
286+
private keyDownCell = async (cellId: string, e: IKeyboardEvent) => {
286287
switch (e.code) {
287288
case 'ArrowUp':
289+
this.pressedDOnce = false;
288290
if (this.state.focusedCell === cellId && e.editorInfo && e.editorInfo.isFirstLine && !e.editorInfo.isSuggesting) {
289291
this.arrowUpFromCell(cellId, e);
290292
} else if (!this.state.focusedCell) {
291293
this.arrowUpFromCell(cellId, e);
292294
}
293295
break;
294-
295296
case 'ArrowDown':
297+
this.pressedDOnce = false;
296298
if (this.state.focusedCell === cellId && e.editorInfo && e.editorInfo.isLastLine && !e.editorInfo.isSuggesting) {
297299
this.arrowDownFromCell(cellId, e);
298300
} else if (!this.state.focusedCell) {
299301
this.arrowDownFromCell(cellId, e);
300302
}
301303
break;
302-
303304
case 'Escape':
305+
this.pressedDOnce = false;
304306
if (this.state.focusedCell && e.editorInfo && !e.editorInfo.isSuggesting) {
305307
this.escapeCell(this.state.focusedCell, e);
306308
}
307309
break;
308-
309310
case 'y':
311+
this.pressedDOnce = false;
310312
if (!this.state.focusedCell && this.state.selectedCell) {
311313
e.stopPropagation();
312314
this.stateController.changeCellType(this.state.selectedCell, 'code');
313315
}
314316
break;
315-
316317
case 'm':
318+
this.pressedDOnce = false;
317319
if (!this.state.focusedCell && this.state.selectedCell) {
318320
e.stopPropagation();
319321
this.stateController.changeCellType(this.state.selectedCell, 'markdown');
320322
}
321323
break;
322-
323324
case 'l':
325+
this.pressedDOnce = false;
324326
if (!this.state.focusedCell && this.state.selectedCell) {
325327
e.stopPropagation();
326328
this.stateController.toggleLineNumbers(this.state.selectedCell);
327329
}
328330
break;
329-
330331
case 'o':
332+
this.pressedDOnce = false;
331333
if (!this.state.focusedCell && this.state.selectedCell) {
332334
e.stopPropagation();
333335
this.stateController.toggleOutput(this.state.selectedCell);
334336
}
335337
break;
336-
337338
case 'Enter':
339+
this.pressedDOnce = false;
338340
if (e.shiftKey) {
339-
this.submitCell(cellId, e);
341+
this.submitCell(cellId, e, true);
342+
} else if (e.ctrlKey) {
343+
this.submitCell(cellId, e, false);
344+
} else if (e.altKey) {
345+
this.submitCell(cellId, e, false);
346+
this.stateController.insertBelow(cellId, true);
340347
} else {
341348
this.enterCell(cellId, e);
342349
}
343350
break;
344-
351+
case 'd':
352+
if (this.pressedDOnce) {
353+
this.stateController.deleteCell(cellId);
354+
this.pressedDOnce = false;
355+
} else {
356+
this.pressedDOnce = true;
357+
}
358+
break;
359+
case 'a':
360+
this.pressedDOnce = false;
361+
if (this.state.focusedCell === cellId && e.editorInfo && e.editorInfo.isLastLine && !e.editorInfo.isSuggesting) {
362+
this.stateController.insertAbove(cellId, true);
363+
} else if (!this.state.focusedCell) {
364+
this.stateController.insertAbove(cellId, true);
365+
}
366+
break;
367+
case 'b':
368+
this.pressedDOnce = false;
369+
if (this.state.focusedCell === cellId && e.editorInfo && e.editorInfo.isLastLine && !e.editorInfo.isSuggesting) {
370+
this.stateController.insertBelow(cellId, true);
371+
} else if (!this.state.focusedCell) {
372+
this.stateController.insertBelow(cellId, true);
373+
}
374+
break;
375+
case 'j':
376+
this.pressedDOnce = false;
377+
if (this.state.focusedCell === cellId && e.editorInfo && e.editorInfo.isFirstLine && !e.editorInfo.isSuggesting) {
378+
this.arrowUpFromCell(cellId, e);
379+
} else if (!this.state.focusedCell) {
380+
this.arrowUpFromCell(cellId, e);
381+
}
382+
break;
383+
case 'k':
384+
this.pressedDOnce = false;
385+
if (this.state.focusedCell === cellId && e.editorInfo && e.editorInfo.isFirstLine && !e.editorInfo.isSuggesting) {
386+
this.arrowDownFromCell(cellId, e);
387+
} else if (!this.state.focusedCell) {
388+
this.arrowDownFromCell(cellId, e);
389+
}
390+
break;
345391
default:
392+
this.pressedDOnce = false;
346393
break;
347394
}
348395
}
@@ -361,7 +408,7 @@ export class NativeEditor extends React.Component<INativeEditorProps, IMainState
361408
}
362409
}
363410

364-
private submitCell = (cellId: string, e: IKeyboardEvent) => {
411+
private submitCell = (cellId: string, e: IKeyboardEvent, moveToNextCell: boolean) => {
365412
let content: string | undefined ;
366413
const cellVM = this.findCellViewModel(cellId);
367414

@@ -382,7 +429,7 @@ export class NativeEditor extends React.Component<INativeEditorProps, IMainState
382429
}
383430

384431
// If this is not the edit cell, move to our next cell
385-
if (cellId !== Identifiers.EditCellId) {
432+
if (cellId !== Identifiers.EditCellId && moveToNextCell) {
386433
const nextCell = this.getNextCellId(cellId);
387434
if (nextCell) {
388435
this.stateController.selectCell(nextCell, undefined);
@@ -439,6 +486,7 @@ export class NativeEditor extends React.Component<INativeEditorProps, IMainState
439486
}
440487

441488
private clickCell = (cellId: string) => {
489+
this.pressedDOnce = false;
442490
const focusedCell = cellId === this.state.focusedCell ? cellId : undefined;
443491
this.stateController.selectCell(cellId, focusedCell);
444492
}
@@ -472,6 +520,24 @@ export class NativeEditor extends React.Component<INativeEditorProps, IMainState
472520
// }
473521
// }
474522

523+
// private pasteFromClipboard = (cellId: string) => {
524+
// const editedCells = this.state.cellVMs;
525+
// const index = editedCells.findIndex(x => x.cell.id === cellId) + 1;
526+
527+
// if (index > -1) {
528+
// const textArea = document.createElement('textarea');
529+
// document.body.appendChild(textArea);
530+
// textArea.select();
531+
// document.execCommand('Paste');
532+
// editedCells[index].cell.data.source = textArea.value.split(/\r?\n/);
533+
// textArea.remove();
534+
// }
535+
536+
// this.setState({
537+
// cellVMs: editedCells
538+
// });
539+
// }
540+
475541
private moveCellUp = (cellId?: string) => {
476542
if (this.contentPanelRef.current && cellId) {
477543
const wasFocused = this.state.focusedCell;

src/datascience-ui/native-editor/nativeEditorStateController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,19 @@ export class NativeEditorStateController extends MainStateController {
110110
}
111111
}
112112

113-
public insertAbove = (cellId?: string) => {
113+
public insertAbove = (cellId?: string, isMonaco?: boolean) => {
114114
const cells = this.getState().cellVMs;
115115
const index = cells.findIndex(cvm => cvm.cell.id === cellId);
116116
if (index >= 0) {
117-
this.insertCell(createEmptyCell(uuid(), null), index);
117+
this.insertCell(createEmptyCell(uuid(), null), index, isMonaco);
118118
}
119119
}
120120

121-
public insertBelow = (cellId?: string) => {
121+
public insertBelow = (cellId?: string, isMonaco?: boolean) => {
122122
const cells = this.getState().cellVMs;
123123
const index = cells.findIndex(cvm => cvm.cell.id === cellId);
124124
if (index >= 0) {
125-
this.insertCell(createEmptyCell(uuid(), null), index + 1);
125+
this.insertCell(createEmptyCell(uuid(), null), index + 1, isMonaco);
126126
}
127127
}
128128

0 commit comments

Comments
 (0)