Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ export default function App() {
* 那一行有什么特别。
*/
const [cursor, setCursor] = useState(-1);
/** 流量页的滚动层。键盘选中一行之后,在它里面找到那一行滚进视野 */
const listRef = useRef<HTMLDivElement>(null);
const [tab, setTab] = useState<Surface>("dashboard");
/** 从客户端页点了某把密钥:密钥页打开时定位到那一行 */
const [focusKey, setFocusKey] = useState<string | null>(null);
Expand Down Expand Up @@ -559,7 +561,8 @@ export default function App() {
* **「用鼠标一行行点太慢」**,而 Requests 是主战场。
*
* 两个边界:在输入框里打字时不接管方向键(否则光标动不了);
* 抽屉开着时 `↑↓` 也不动,那时用户在看详情而不是挑行。
* 抽屉开着时 `↑↓` 也不动,那时用户在看详情而不是挑行。请求和会话的
* 抽屉都算:选中的行会滚进视野,漏掉一个,背后的列表就跟着光标滚走了。
*/
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
Expand Down Expand Up @@ -611,23 +614,37 @@ export default function App() {
setOpen(null);
return;
}
if (open !== null) return;
if (open !== null || openSession !== null) return;
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
e.preventDefault();
setCursor((c) => {
const next = e.key === "ArrowDown" ? c + 1 : c - 1;
// 第一次按方向键从第一行开始,而不是从「上一行」跳到末尾
if (c < 0) return e.key === "ArrowDown" ? 0 : 0;
return Math.max(0, Math.min(rows.length - 1, next));
});
const step = e.key === "ArrowDown" ? 1 : -1;
// 第一次按方向键从第一行开始,而不是从「上一行」跳到末尾
const next =
cursor < 0 ? 0 : Math.max(0, Math.min(rows.length - 1, cursor + step));
setCursor(next);
/*
**选中的那一行要一直看得见。**方向键自带的滚动上面拦掉了,不补的话
按过可视区的下沿,高亮就跑到屏幕外面去了。只滚刚好够的距离
(`nearest`);让开吸顶的表头、不横着滚,靠的是行上的 scroll-margin,
见 `Row`。光标到了头、没动,也照样滚:用滚轮翻走之后,按一下方向键
就回到选中的那一行。

**按 id 找行,不按下标。**归组时表里的顺序不是 `rows` 的顺序,折起来
的组里的行也不在表里 —— 那时找不到,就不滚。
*/
const id = rows[next]?.id;
if (id !== undefined)
listRef.current
?.querySelector(`[data-row="${id}"]`)
?.scrollIntoView({ block: "nearest" });
} else if (e.key === "Enter" && cursor >= 0 && rows[cursor]) {
e.preventDefault();
setOpen(rows[cursor].id);
}
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [tab, rows, cursor, open]);
}, [tab, rows, cursor, open, openSession]);

// 换页时把抽屉关掉 —— 它是请求页的东西,留在别的页上没有意义,
// 而那时 Esc 也不接管了(键盘那个 effect 只在请求页挂)
Expand Down Expand Up @@ -1261,7 +1278,7 @@ export default function App() {
onChanged={() => setNudge((n) => n + 1)}
/>
) : (
<div className="min-h-0 flex-1 overflow-auto">
<div ref={listRef} className="min-h-0 flex-1 overflow-auto">
{/*
详情走**浮层**,不拆栏。

Expand Down
16 changes: 16 additions & 0 deletions src/traffic/RequestTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,28 @@ function Row({
]}
>
<TableRow
data-row={r.id}
onClick={() => {
onSelect();
onOpen(r.id);
}}
className={
"cursor-pointer border-b border-neutral-100 hover:bg-neutral-50 dark:border-neutral-900 dark:hover:bg-neutral-900 " +
/*
这两样是给键盘导航的 `scrollIntoView` 用的(见 App)。

**上边让出吸顶的表头**,不然往上翻时这一行停在表头
底下。36px 是表头的高:排序钮 24px,加「状态」那一格
的 `py-1.5`。

**左右各放出一整屏宽,横向就不会滚。**窗口窄、表横着
滚的时候,一行横跨整张表,总有一截在视野外,`nearest`
会为它横着滚:Chromium 把表推开 20px,左右边距没了;
WebKit 从最右一下跳回最左。只放出外面那块的 `px-5`
在 WebKit 里不够 —— 它把滚动宽度向上取整,滚到最右时
行边差零点几像素够不着可视区的右沿。
*/
"scroll-mt-9 scroll-mx-[100vw] " +
(selected
? "bg-neutral-100 dark:bg-neutral-800"
: fresh
Expand Down
Loading