From b216b43c2fd922a34a492e45e2ebcb0e51f5de58 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Mon, 18 Mar 2024 00:37:02 +0100 Subject: [PATCH] Shift+PageUp/Down to select NB output contents (#207962) --- .../browser/contrib/navigation/arrow.ts | 2 ++ .../browser/view/renderers/webviewPreloads.ts | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/navigation/arrow.ts b/src/vs/workbench/contrib/notebook/browser/contrib/navigation/arrow.ts index 52eea945ec68b..cb9e490f91d7a 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/navigation/arrow.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/navigation/arrow.ts @@ -358,6 +358,7 @@ registerAction2(class extends NotebookCellAction { NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.has(InputFocusedContextKey), EditorContextKeys.editorTextFocus, + NOTEBOOK_OUTPUT_FOCUSED.negate(), // Webview handles Shift+PageUp for selection of output contents ), primary: KeyMod.Shift | KeyCode.PageUp, weight: NOTEBOOK_EDITOR_WIDGET_ACTION_WEIGHT @@ -406,6 +407,7 @@ registerAction2(class extends NotebookCellAction { NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.has(InputFocusedContextKey), EditorContextKeys.editorTextFocus, + NOTEBOOK_OUTPUT_FOCUSED.negate(), // Webview handles Shift+PageDown for selection of output contents ), primary: KeyMod.Shift | KeyCode.PageDown, weight: NOTEBOOK_EDITOR_WIDGET_ACTION_WEIGHT diff --git a/src/vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts b/src/vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts index 81b19159bb60f..5781cdb6a3fe3 100644 --- a/src/vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts +++ b/src/vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads.ts @@ -286,6 +286,38 @@ async function webviewPreloads(ctx: PreloadContext) { }; + const onPageUpDownSelectionHandler = (e: KeyboardEvent) => { + if (!lastFocusedOutput?.id || !e.shiftKey) { + return; + } + // We want to handle just `Shift + PageUp/PageDown` & `Shift + Cmd + ArrowUp/ArrowDown` (for mac) + if (!(e.code === 'PageUp' || e.code === 'PageDown') && !(e.metaKey && (e.code === 'ArrowDown' || e.code === 'ArrowUp'))) { + return; + } + const outputContainer = window.document.getElementById(lastFocusedOutput.id); + const selection = window.getSelection(); + if (!outputContainer || !selection?.anchorNode) { + return; + } + + // These should change the scroll position, not adjust the selected cell in the notebook + e.stopPropagation(); // We don't want the notebook to handle this. + e.preventDefault(); // We will handle selection. + + const { anchorNode, anchorOffset } = selection; + const range = document.createRange(); + if (e.code === 'PageDown' || e.code === 'ArrowDown') { + range.setStart(anchorNode, anchorOffset); + range.setEnd(outputContainer, 1); + } + else { + range.setStart(outputContainer, 0); + range.setEnd(anchorNode, anchorOffset); + } + selection.removeAllRanges(); + selection.addRange(range); + }; + const handleDataUrl = async (data: string | ArrayBuffer | null, downloadName: string) => { postNotebookMessage('clicked-data-url', { data, @@ -310,6 +342,7 @@ async function webviewPreloads(ctx: PreloadContext) { window.document.body.addEventListener('click', handleInnerClick); window.document.body.addEventListener('focusin', checkOutputInputFocus); window.document.body.addEventListener('focusout', handleOutputFocusOut); + window.document.body.addEventListener('keydown', onPageUpDownSelectionHandler); interface RendererContext extends rendererApi.RendererContext { readonly onDidChangeSettings: Event;