@@ -286,6 +286,38 @@ async function webviewPreloads(ctx: PreloadContext) {
286286
287287 } ;
288288
289+ const onPageUpDownSelectionHandler = ( e : KeyboardEvent ) => {
290+ if ( ! lastFocusedOutput ?. id || ! e . shiftKey ) {
291+ return ;
292+ }
293+ // We want to handle just `Shift + PageUp/PageDown` & `Shift + Cmd + ArrowUp/ArrowDown` (for mac)
294+ if ( ! ( e . code === 'PageUp' || e . code === 'PageDown' ) && ! ( e . metaKey && ( e . code === 'ArrowDown' || e . code === 'ArrowUp' ) ) ) {
295+ return ;
296+ }
297+ const outputContainer = window . document . getElementById ( lastFocusedOutput . id ) ;
298+ const selection = window . getSelection ( ) ;
299+ if ( ! outputContainer || ! selection ?. anchorNode ) {
300+ return ;
301+ }
302+
303+ // These should change the scroll position, not adjust the selected cell in the notebook
304+ e . stopPropagation ( ) ; // We don't want the notebook to handle this.
305+ e . preventDefault ( ) ; // We will handle selection.
306+
307+ const { anchorNode, anchorOffset } = selection ;
308+ const range = document . createRange ( ) ;
309+ if ( e . code === 'PageDown' || e . code === 'ArrowDown' ) {
310+ range . setStart ( anchorNode , anchorOffset ) ;
311+ range . setEnd ( outputContainer , 1 ) ;
312+ }
313+ else {
314+ range . setStart ( outputContainer , 0 ) ;
315+ range . setEnd ( anchorNode , anchorOffset ) ;
316+ }
317+ selection . removeAllRanges ( ) ;
318+ selection . addRange ( range ) ;
319+ } ;
320+
289321 const handleDataUrl = async ( data : string | ArrayBuffer | null , downloadName : string ) => {
290322 postNotebookMessage < webviewMessages . IClickedDataUrlMessage > ( 'clicked-data-url' , {
291323 data,
@@ -310,6 +342,7 @@ async function webviewPreloads(ctx: PreloadContext) {
310342 window . document . body . addEventListener ( 'click' , handleInnerClick ) ;
311343 window . document . body . addEventListener ( 'focusin' , checkOutputInputFocus ) ;
312344 window . document . body . addEventListener ( 'focusout' , handleOutputFocusOut ) ;
345+ window . document . body . addEventListener ( 'keydown' , onPageUpDownSelectionHandler ) ;
313346
314347 interface RendererContext extends rendererApi . RendererContext < unknown > {
315348 readonly onDidChangeSettings : Event < RenderOptions > ;
0 commit comments