Skip to content

Commit

Permalink
smooth scroll via Arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
prasunanand committed Oct 29, 2024
1 parent 7549761 commit 0cb132a
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions ui/src/ide/editor/NotebookEditor.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -58,7 +58,9 @@ export default function NotebookEditor (props) {
nbformat_minor:0,
metadata: {}
})
const [blankCell, setBlankCell] = useState<ICell>({ execution_count: 0, source: '' })
const [focusedIndex, setFocusedIndex] = useState(0);
const divRefs = useRef<(HTMLDivElement | null)[]>([]); // Type the refs


const [client, setClient] = useState<IClient>({ send: () => { } })

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -433,6 +453,10 @@ export default function NotebookEditor (props) {
prevCell={prevCell}
nextCell={nextCell}
deleteCell={deleteCell}
focusedIndex = {focusedIndex}
setFocusedIndex={setFocusedIndex}
handleKeyDown={handleKeyDown}
divRefs={divRefs}
/>
)
}
Expand Down Expand Up @@ -486,7 +510,9 @@ function Cell (props) {
const [fileContents, setFileContents] = useState(cell.source[0])
if (cell.cell_type === 'markdown') {
return (
<div tabIndex={props.index} className='activeCell'>
<div tabIndex={props.index} className={props.index === props.focusedIndex ? 'activeCell': ''}
ref={(el) => (props.divRefs.current[props.index] = el)}
onKeyDown={props.handleKeyDown} onFocus={() => props.setFocusedIndex(props.index)}>
<Markdown rehypePlugins={[rehypeRaw]}>{fileContents}</Markdown>
</div>
)
Expand All @@ -506,7 +532,11 @@ function Cell (props) {
])

return (
<div tabIndex={props.index} className='single-line activeCell'>
<div tabIndex={props.index}
className={props.index === props.focusedIndex ? 'single-line activeCell': 'single-line'}
onKeyDown={props.handleKeyDown}
ref={(el) => (props.divRefs.current[props.index] = el)}
onFocus={() => props.setFocusedIndex(props.index)}>

<div className='serial-no'>[{cell.execution_count}]:</div>
<div className='inner-content'>
Expand Down

0 comments on commit 0cb132a

Please sign in to comment.