From 0cb132af3238fcfbf4be6c95a98169bdc4f1117f Mon Sep 17 00:00:00 2001 From: Prasun Anand Date: Tue, 29 Oct 2024 13:38:43 +0530 Subject: [PATCH] smooth scroll via Arrow keys --- ui/src/ide/editor/NotebookEditor.tsx | 38 +++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/ui/src/ide/editor/NotebookEditor.tsx b/ui/src/ide/editor/NotebookEditor.tsx index f8182e4..f5a0205 100644 --- a/ui/src/ide/editor/NotebookEditor.tsx +++ b/ui/src/ide/editor/NotebookEditor.tsx @@ -1,5 +1,5 @@ -import React, { useEffect, useState } from 'react' +import React, { useEffect, useState, useRef } from 'react' import CodeMirror from '@uiw/react-codemirror' import { python } from '@codemirror/lang-python' @@ -58,7 +58,9 @@ export default function NotebookEditor (props) { nbformat_minor:0, metadata: {} }) - const [blankCell, setBlankCell] = useState({ execution_count: 0, source: '' }) + const [focusedIndex, setFocusedIndex] = useState(0); + const divRefs = useRef<(HTMLDivElement | null)[]>([]); // Type the refs + const [client, setClient] = useState({ send: () => { } }) @@ -396,6 +398,24 @@ export default function NotebookEditor (props) { console.log("ReExecute Notebook") } + const handleKeyDown = (event) => { + if (event.key === 'ArrowDown') { + setFocusedIndex((prev) => { + const newIndex = Math.min(prev + 1, notebook.cells.length - 1); + divRefs.current[newIndex]?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); + return newIndex; + }); + event.preventDefault(); + } else if (event.key === 'ArrowUp') { + setFocusedIndex((prev) => { + const newIndex = Math.max(prev - 1, 0); + divRefs.current[newIndex]?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); + return newIndex; + }); + event.preventDefault(); + } + }; + return ( @@ -433,6 +453,10 @@ export default function NotebookEditor (props) { prevCell={prevCell} nextCell={nextCell} deleteCell={deleteCell} + focusedIndex = {focusedIndex} + setFocusedIndex={setFocusedIndex} + handleKeyDown={handleKeyDown} + divRefs={divRefs} /> ) } @@ -486,7 +510,9 @@ function Cell (props) { const [fileContents, setFileContents] = useState(cell.source[0]) if (cell.cell_type === 'markdown') { return ( -
+
(props.divRefs.current[props.index] = el)} + onKeyDown={props.handleKeyDown} onFocus={() => props.setFocusedIndex(props.index)}> {fileContents}
) @@ -506,7 +532,11 @@ function Cell (props) { ]) return ( -
+
(props.divRefs.current[props.index] = el)} + onFocus={() => props.setFocusedIndex(props.index)}>
[{cell.execution_count}]: