The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
fix(web): PDF pages no longer turn black while the panel is resized (#39105)
Co-authored-by: Anton M. <347046595+Anton-M-R@users.noreply.github.com>
Anton Metzner Rodriguez committed
Nov 19, 2026 at 14:21 UTC
2a411317d9b7341547e5acb3e9bceb7e6195c5a2
2 files changed
+53
-4
apps/web/src/components/files/PdfViewer.browser.test.tsx
+33
@@ -142,6 +142,39 @@ it("fits the width, then finds split and rotated text and jumps to its page", as
142
}
143
});
144
145
+it("keeps the painted page while its width changes, then repaints it without a blank frame", async () => {
146
+ const url = URL.createObjectURL(testPdf());
147
+ try {
148
+ const container = document.createElement("div");
149
+ container.style.cssText = "display:flex;width:500px;height:600px";
150
+ await using screen = await render(<PdfViewer src={url} title="Resized PDF" />, { container });
151
+ await expect.element(screen.getByText("1 / 2 pages"), { timeout: 10_000 }).toBeVisible();
152
+ const region = screen.container.querySelector<HTMLElement>('[role="region"]')!;
153
+ const canvas = region.querySelector<HTMLCanvasElement>('canvas[aria-label="Page 1"]')!;
154
+ const bitmapWidth = () =>
155
+ Math.ceil((region.clientWidth - 32) * Math.min(2, window.devicePixelRatio));
156
+ await vi.waitFor(() => expect(canvas.width).toBe(bitmapWidth()));
157
+ const painted = canvas.width;
158
+
159
+ // A canvas loses its pixels when its bitmap is resized, so read them as soon as that happens.
160
+ const repainted = new Promise<number[]>((resolve) =>
161
+ new MutationObserver(() =>
162
+ resolve(Array.from(canvas.getContext("2d")!.getImageData(0, 0, 1, 1).data)),
163
+ ).observe(canvas, { attributeFilter: ["width"] }),
164
+ );
165
+ const resized = new Promise((resolve) => new ResizeObserver(resolve).observe(container));
166
+ container.style.width = "400px";
167
+ await resized;
168
+ expect(canvas.getBoundingClientRect().width).toBe(region.clientWidth - 32);
169
+ expect(canvas.width).toBe(painted);
170
+
171
+ expect(await repainted).toEqual([255, 255, 255, 255]);
172
+ expect(canvas.width).toBe(bitmapWidth());
173
+ } finally {
174
+ URL.revokeObjectURL(url);
175
+ }
176
+});
177
+
178
it("jumps to the first hit while still counting and keeps later manual navigation", async () => {
179
setup(4);
180
const page = (text: string) => ({
apps/web/src/components/files/PdfViewer.tsx
+20
-4
@@ -60,6 +60,7 @@ function PdfPage(props: {
60
const gt = useGT();
61
const canvas = useRef<HTMLCanvasElement>(null);
62
const text = useRef<HTMLDivElement>(null);
63
+ const painted = useRef<PDFDocumentProxy>(null);
64
const { document: pdf, index, width, onSize, onError } = props;
65
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Render the PDF page canvas and text layer, cancelling tasks and releasing pdf.js page resources on cleanup.
66
useEffect(() => {
@@ -67,7 +68,11 @@ function PdfPage(props: {
68
let paint: ReturnType<Awaited<ReturnType<PDFDocumentProxy["getPage"]>>["render"]> | undefined;
69
let layer: TextLayer | undefined;
70
let loadedPage: Awaited<ReturnType<PDFDocumentProxy["getPage"]>> | undefined;
71
+ let timer: ReturnType<typeof setTimeout> | undefined;
72
void (async () => {
73
+ // A drag changes the width every frame. CSS scales the painted page until the width settles.
74
+ if (painted.current === pdf)
75
+ await new Promise((resolve) => (timer = setTimeout(resolve, 150)));
76
const page = await pdf.getPage(index + 1);
77
loadedPage = page;
78
if (cancelled || !canvas.current || !text.current) {
@@ -82,10 +87,12 @@ function PdfPage(props: {
87
window.devicePixelRatio || 1,
88
Math.sqrt((8 * 1024 * 1024) / (viewport.width * viewport.height)),
89
);
85
- canvas.current.width = Math.ceil(viewport.width * ratio);
86
- canvas.current.height = Math.ceil(viewport.height * ratio);
90
+ // Resizing a canvas blanks it, so the page paints off screen and replaces the old one whole.
91
+ const buffer = document.createElement("canvas");
92
+ buffer.width = Math.ceil(viewport.width * ratio);
93
+ buffer.height = Math.ceil(viewport.height * ratio);
94
paint = page.render({
88
- canvas: canvas.current,
95
+ canvas: buffer,
96
viewport,
97
transform: [ratio, 0, 0, ratio, 0, 0],
98
background: "white",
@@ -97,13 +104,22 @@ function PdfPage(props: {
104
viewport,
105
textContentSource: page.streamTextContent(),
106
});
100
- await Promise.all([paint.promise, layer.render()]);
107
+ const shown = paint.promise.then(() => {
108
+ if (cancelled || !canvas.current) return;
109
+ canvas.current.width = buffer.width;
110
+ canvas.current.height = buffer.height;
111
+ canvas.current.getContext("2d", { alpha: false })?.drawImage(buffer, 0, 0);
112
+ buffer.width = 0;
113
+ painted.current = pdf;
114
+ });
115
+ await Promise.all([shown, layer.render()]);
116
})().catch((cause: unknown) => {
117
if (!cancelled)
118
onError(thrownErrorMessage(cause, m) ?? m(msg("This PDF page could not render.")));
119
});
120
return () => {
121
cancelled = true;
122
+ clearTimeout(timer);
123
paint?.cancel();
124
layer?.cancel();
125
// Release operator lists when virtual pages leave the viewport.