Skip to content

Commit d76c943

Browse files
committed
fix scroll-into view when using keyboard to navigate directory
1 parent 152c78a commit d76c943

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

frontend/app/view/preview/directorypreview.tsx

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,15 @@ function DirectoryTable({
386386
useLayoutEffect(() => {
387387
const rows = table.getRowModel()?.flatRows;
388388
let foundParentDir = false;
389-
389+
390390
for (const row of rows) {
391391
if (row.getValue("name") == "..") {
392392
row.pin("top");
393393
foundParentDir = true;
394394
break;
395395
}
396396
}
397-
397+
398398
// If we didn't find the ".." row, reset the pinning to avoid stale references
399399
if (!foundParentDir) {
400400
table.resetRowPinning();
@@ -520,34 +520,39 @@ function TableBody({
520520
}: TableBodyProps) {
521521
const dummyLineRef = useRef<HTMLDivElement>();
522522
const warningBoxRef = useRef<HTMLDivElement>();
523-
const rowRefs = useRef<HTMLDivElement[]>([]);
524523
const conn = useAtomValue(model.connection);
525524
const setErrorMsg = useSetAtom(model.errorMsgAtom);
526525

527526
useEffect(() => {
528-
if (focusIndex !== null && rowRefs.current[focusIndex] && bodyRef.current && osRef) {
529-
const viewport = osRef.osInstance().elements().viewport;
530-
const viewportHeight = viewport.offsetHeight;
531-
const rowElement = rowRefs.current[focusIndex];
532-
const rowRect = rowElement.getBoundingClientRect();
533-
const parentRect = viewport.getBoundingClientRect();
534-
const viewportScrollTop = viewport.scrollTop;
535-
const rowTopRelativeToViewport = rowRect.top - parentRect.top + viewport.scrollTop;
536-
const rowBottomRelativeToViewport = rowRect.bottom - parentRect.top + viewport.scrollTop;
537-
if (rowTopRelativeToViewport - 30 < viewportScrollTop) {
538-
// Row is above the visible area
539-
let topVal = rowTopRelativeToViewport - 30;
540-
if (topVal < 0) {
541-
topVal = 0;
542-
}
543-
viewport.scrollTo({ top: topVal });
544-
} else if (rowBottomRelativeToViewport + 5 > viewportScrollTop + viewportHeight) {
545-
// Row is below the visible area
546-
const topVal = rowBottomRelativeToViewport - viewportHeight + 5;
547-
viewport.scrollTo({ top: topVal });
527+
if (focusIndex === null || !bodyRef.current || !osRef) {
528+
return;
529+
}
530+
531+
const rowElement = bodyRef.current.querySelector(`[data-rowindex="${focusIndex}"]`) as HTMLDivElement;
532+
if (!rowElement) {
533+
return;
534+
}
535+
536+
const viewport = osRef.osInstance().elements().viewport;
537+
const viewportHeight = viewport.offsetHeight;
538+
const rowRect = rowElement.getBoundingClientRect();
539+
const parentRect = viewport.getBoundingClientRect();
540+
const viewportScrollTop = viewport.scrollTop;
541+
const rowTopRelativeToViewport = rowRect.top - parentRect.top + viewport.scrollTop;
542+
const rowBottomRelativeToViewport = rowRect.bottom - parentRect.top + viewport.scrollTop;
543+
544+
if (rowTopRelativeToViewport - 30 < viewportScrollTop) {
545+
// Row is above the visible area
546+
let topVal = rowTopRelativeToViewport - 30;
547+
if (topVal < 0) {
548+
topVal = 0;
548549
}
550+
viewport.scrollTo({ top: topVal });
551+
} else if (rowBottomRelativeToViewport + 5 > viewportScrollTop + viewportHeight) {
552+
// Row is below the visible area
553+
const topVal = rowBottomRelativeToViewport - viewportHeight + 5;
554+
viewport.scrollTo({ top: topVal });
549555
}
550-
// setIndexChangedFromClick(false);
551556
}, [focusIndex]);
552557

553558
const handleFileContextMenu = useCallback(
@@ -730,6 +735,7 @@ const TableRow = React.forwardRef(function ({
730735
return (
731736
<div
732737
className={clsx("dir-table-body-row", { focused: focusIndex === idx })}
738+
data-rowindex={idx}
733739
onDoubleClick={() => {
734740
const newFileName = row.getValue("path") as string;
735741
model.goHistory(newFileName);

0 commit comments

Comments
 (0)