Skip to content

Commit

Permalink
fix directory scroll into view (#1901)
Browse files Browse the repository at this point in the history
  • Loading branch information
sawka authored Feb 5, 2025
1 parent affd846 commit 3c5ff51
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions frontend/app/view/preview/directorypreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { quote as shellQuote } from "shell-quote";
import { debounce } from "throttle-debounce";
import "./directorypreview.scss";

const PageJumpSize = 20;

declare module "@tanstack/react-table" {
interface TableMeta<TData extends RowData> {
updateName: (path: string) => void;
Expand Down Expand Up @@ -493,18 +495,21 @@ function TableBody({
const viewportHeight = viewport.offsetHeight;
const rowElement = rowRefs.current[focusIndex];
const rowRect = rowElement.getBoundingClientRect();
const parentRect = bodyRef.current.getBoundingClientRect();
const parentRect = viewport.getBoundingClientRect();
const viewportScrollTop = viewport.scrollTop;

const rowTopRelativeToViewport = rowRect.top - parentRect.top;
const rowBottomRelativeToViewport = rowRect.bottom - parentRect.top;

if (rowTopRelativeToViewport < viewportScrollTop) {
const rowTopRelativeToViewport = rowRect.top - parentRect.top + viewport.scrollTop;
const rowBottomRelativeToViewport = rowRect.bottom - parentRect.top + viewport.scrollTop;
if (rowTopRelativeToViewport - 30 < viewportScrollTop) {
// Row is above the visible area
viewport.scrollTo({ top: rowTopRelativeToViewport });
} else if (rowBottomRelativeToViewport > viewportScrollTop + viewportHeight) {
let topVal = rowTopRelativeToViewport - 30;
if (topVal < 0) {
topVal = 0;
}
viewport.scrollTo({ top: topVal });
} else if (rowBottomRelativeToViewport + 5 > viewportScrollTop + viewportHeight) {
// Row is below the visible area
viewport.scrollTo({ top: rowBottomRelativeToViewport - viewportHeight });
const topVal = rowBottomRelativeToViewport - viewportHeight + 5;
viewport.scrollTo({ top: topVal });
}
}
// setIndexChangedFromClick(false);
Expand Down Expand Up @@ -772,6 +777,14 @@ function DirectoryPreview({ model }: DirectoryPreviewProps) {
setFocusIndex((idx) => Math.min(idx + 1, filteredData.length - 1));
return true;
}
if (checkKeyPressed(waveEvent, "PageUp")) {
setFocusIndex((idx) => Math.max(idx - PageJumpSize, 0));
return true;
}
if (checkKeyPressed(waveEvent, "PageDown")) {
setFocusIndex((idx) => Math.min(idx + PageJumpSize, filteredData.length - 1));
return true;
}
if (checkKeyPressed(waveEvent, "Enter")) {
if (filteredData.length == 0) {
return;
Expand Down

0 comments on commit 3c5ff51

Please sign in to comment.