GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(web): read the diff scroll anchor after the new viewport renders · pingdotgg/t3code@d1477cf

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/web/src/components/diffs
    • useCodeViewScrollRestore.test.ts
    • useCodeViewScrollRestore.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/web/src/components/diffs/useCodeViewScrollRestore.test.ts‎

Copy file name to clipboardExpand all lines: apps/web/src/components/diffs/useCodeViewScrollRestore.test.ts
+49-7Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CodeViewScrollTarget } from "@pierre/diffs";
2-
import { describe, expect, it } from "vite-plus/test";
2+
import { describe, expect, it, vi } from "vite-plus/test";
33

44
import { createCodeViewScrollRestore } from "./useCodeViewScrollRestore";
55

@@ -9,10 +9,13 @@ const LINE_HEIGHT = 20;
99
function viewer(tops: Record<string, number>, collapsed: readonly string[] = []) {
1010
const items = new Map(Object.entries(tops));
1111
const instance = {
12+
scrollTop: 0,
13+
rendered: null as string[] | null,
1214
getHeight: () => 500,
15+
getScrollTop: () => instance.scrollTop,
1316
getTopForItem: (id: string) => items.get(id),
1417
getRenderedItems: () =>
15-
[...items.keys()].map((id) => ({
18+
(instance.rendered ?? [...items.keys()]).map((id) => ({
1619
id,
1720
instance: {
1821
getNumericScrollAnchor: (localTop: number) => {
@@ -46,13 +49,32 @@ function mount(
4649
return restore;
4750
}
4851

52+
function setup() {
53+
const frames: FrameRequestCallback[] = [];
54+
vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => frames.push(callback));
55+
const runFrames = () => {
56+
while (frames.length > 0) frames.shift()?.(0);
57+
};
58+
const scroll = (
59+
restore: ReturnType<typeof mount>,
60+
view: ReturnType<typeof viewer>,
61+
top: number,
62+
) => {
63+
view.instance.scrollTop = top;
64+
restore.onScroll(top, view.instance);
65+
runFrames();
66+
};
67+
return { runFrames, scroll };
68+
}
69+
4970
describe("createCodeViewScrollRestore", () => {
5071
it("puts a remounted viewer back on its scope's file and line, not its pixel offset", () => {
72+
const { scroll } = setup();
5173
const first = viewer({ a: 0, b: 4000, c: 9000 });
5274
// 104px into file b: line 7 is the first under the sticky header, 16px below its edge.
53-
mount("thread-a", first).onScroll(4104, first.instance);
75+
scroll(mount("thread-a", first), first, 4104);
5476
const other = viewer({ a: 0, b: 4000 });
55-
mount("thread-b", other).onScroll(1200, other.instance);
77+
scroll(mount("thread-b", other), other, 1200);
5678

5779
// The remounted viewer estimates file a shorter, so file b starts elsewhere.
5880
const again = viewer({ a: 0, b: 2500, c: 6000 });
@@ -62,16 +84,35 @@ describe("createCodeViewScrollRestore", () => {
6284
]);
6385
});
6486

87+
it("reads the anchor from the viewport Pierre renders after the scroll, not the one before", () => {
88+
const { runFrames } = setup();
89+
const first = viewer({ a: 0, b: 4000, c: 8000 });
90+
first.instance.rendered = ["a"];
91+
const restore = mount("thread-e", first);
92+
// A scrollbar jump notifies before the viewer renders file c.
93+
first.instance.scrollTop = 8104;
94+
restore.onScroll(8104, first.instance);
95+
first.instance.rendered = ["c"];
96+
runFrames();
97+
98+
const again = viewer({ a: 0, b: 2500, c: 6000 });
99+
mount("thread-e", again);
100+
expect(again.scrolls).toEqual([
101+
{ type: "line", id: "c", align: "start", lineNumber: 7, offset: 16 },
102+
]);
103+
});
104+
65105
it("loads more files until the anchored file has loaded, then falls back to the file", () => {
106+
const { scroll } = setup();
66107
const first = viewer({ a: 0, b: 4000 });
67-
mount("thread-c", first).onScroll(4104, first.instance);
108+
scroll(mount("thread-c", first), first, 4104);
68109

69110
const loading = viewer({ a: 0 });
70111
let batches = 0;
71112
const restore = mount("thread-c", loading, false, () => {
72113
batches += 1;
73114
});
74-
restore.onScroll(0, loading.instance);
115+
scroll(restore, loading, 0);
75116
restore.flush();
76117
expect(loading.scrolls).toEqual([]);
77118
expect(batches).toBe(2);
@@ -92,9 +133,10 @@ describe("createCodeViewScrollRestore", () => {
92133
});
93134

94135
it("forgets a position that a reveal or collapsing every file replaced", () => {
136+
const { scroll } = setup();
95137
const first = viewer({ a: 0, b: 4000 });
96138
const restore = mount("thread-d", first);
97-
restore.onScroll(4104, first.instance);
139+
scroll(restore, first, 4104);
98140
restore.discard();
99141

100142
const again = viewer({ a: 0, b: 4000 });
Collapse file

‎apps/web/src/components/diffs/useCodeViewScrollRestore.ts‎

Copy file name to clipboardExpand all lines: apps/web/src/components/diffs/useCodeViewScrollRestore.ts
+16-5Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useEffect, useState } from "react";
33

44
interface Viewer {
55
getHeight(): number;
6+
getScrollTop(): number;
67
getRenderedItems(): readonly {
78
id: string;
89
instance: {
@@ -71,6 +72,15 @@ export function createCodeViewScrollRestore() {
7172
let viewer: Viewer | undefined;
7273
let handle: ScrollRestoreHandle | null = null;
7374
let pending: ScrollAnchor | null = null;
75+
let captureQueued = false;
76+
const capture = (source: object) => {
77+
captureQueued = false;
78+
if (!handle || source !== viewer || pending !== null || scopeKey === null) return;
79+
const scrollTop = viewer.getScrollTop();
80+
const anchor = anchorAt(viewer, scrollTop);
81+
if (anchor) anchorByScopeKey.set(scopeKey, anchor);
82+
else if (scrollTop <= 0) anchorByScopeKey.delete(scopeKey);
83+
};
7484
const flush = () => {
7585
if (pending === null || !viewer || !handle || viewer.getHeight() === 0) return;
7686
const item = handle.getItem(pending.id);
@@ -103,11 +113,12 @@ export function createCodeViewScrollRestore() {
103113
}
104114
flush();
105115
},
106-
onScroll(scrollTop: number, source: object) {
107-
if (!viewer || source !== viewer || pending !== null || scopeKey === null) return;
108-
const anchor = anchorAt(viewer, scrollTop);
109-
if (anchor) anchorByScopeKey.set(scopeKey, anchor);
110-
else anchorByScopeKey.delete(scopeKey);
116+
onScroll(_scrollTop: number, source: object) {
117+
if (source !== viewer || pending !== null || captureQueued) return;
118+
captureQueued = true;
119+
// Pierre notifies before it renders the new viewport in the next frame, so the rendered
120+
// items are read in the frame after that.
121+
requestAnimationFrame(() => requestAnimationFrame(() => capture(source)));
111122
},
112123
/** Something else positions the viewer, such as a file reveal or collapsing every file. */
113124
discard() {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.