Skip to content

Commit bf17628

Browse files
committed
fix(pdf-viewer): read live displayMode in requestFitToContent, guard tiny measurements
currentDisplayMode defaults to "inline" and handleHostContextChanged only writes it `if (ctx.displayMode)`. If the host's initial context omits the field — or the update lands one tick after a renderPage() — requestFitToContent measures a near-empty pageWrapper and sends ~85px (toolbar + padding only), shrinking a fullscreen iframe to a sliver. Workaround was exit + re-enter fullscreen to force a context update. Read app.getHostContext().displayMode directly (always fresh), and separately refuse to send when pageWrapper measures smaller than the toolbar — that's never a real layout, just an early render.
1 parent 27dca05 commit bf17628

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

examples/pdf-server/src/mcp-app.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,14 @@ async function computeFitToWidthScale(): Promise<number | null> {
287287
* Only applies in inline mode - fullscreen mode uses scrolling.
288288
*/
289289
function requestFitToContent() {
290-
if (currentDisplayMode === "fullscreen") {
291-
return; // Fullscreen uses scrolling
290+
// Read the host's current state, not our cached currentDisplayMode.
291+
// currentDisplayMode defaults to "inline" and handleHostContextChanged
292+
// only updates it `if (ctx.displayMode)` — if the host omits the field
293+
// or the update lands one tick late, the cached value lies. We've seen
294+
// this measure a near-empty pageWrapper (~85px = toolbar + padding) and
295+
// shrink a fullscreen iframe to a sliver.
296+
if (app.getHostContext()?.displayMode === "fullscreen") {
297+
return;
292298
}
293299

294300
const canvasHeight = canvasEl.height;
@@ -324,6 +330,16 @@ function requestFitToContent() {
324330
// In inline mode (this function early-returns for fullscreen) the side panel is hidden
325331
const totalWidth = pageWrapperEl.offsetWidth + BUFFER;
326332

333+
// pageWrapper measuring ≈ 0 means the canvas hasn't laid out yet (early
334+
// render, hidden ancestor, etc). Sending toolbar-height-only would shrink
335+
// the iframe to a sliver. The next renderPage() will measure correctly.
336+
if (pageWrapperHeight < toolbarHeight) {
337+
log.info(
338+
`requestFitToContent: pageWrapper ${pageWrapperHeight}px < toolbar ${toolbarHeight}px — skipping`,
339+
);
340+
return;
341+
}
342+
327343
app.sendSizeChanged({ width: totalWidth, height: totalHeight });
328344
}
329345

0 commit comments

Comments
 (0)