Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipper): keep the preview fitted to the page as it rewraps

Elian Dorana37c5eba0771
diff --git a/apps/web-clipper/entrypoints/popup/main.tsx b/apps/web-clipper/entrypoints/popup/main.tsx
index 31e5fcbf3c..0c6a1aa0a2 100644
--- a/apps/web-clipper/entrypoints/popup/main.tsx
+++ b/apps/web-clipper/entrypoints/popup/main.tsx
@@ -90,6 +90,7 @@ const PREVIEW_STYLE = `
@media (prefers-color-scheme: dark) {
:root { --code-block-box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.6); }
}
+ html { overflow: hidden; }
body { margin: 8px; font: 12px/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
h1 { font-size: 1.3em; } h2 { font-size: 1.15em; } h3, h4, h5, h6 { font-size: 1em; }
img, video, svg { max-width: 100%; height: auto; }
@@ -489,14 +490,20 @@ function withDescription(labels: Record<string, string> | undefined, description
}
/**
- * Makes the preview as tall as the page it shows, so that the page scrolls in `.page-body` together
- * with the description above it. `allow-same-origin` lets the popup read the height, and gives the
- * frame the extension's origin. The sandbox must not get `allow-scripts` too: with both, the clipped
- * page's HTML runs as the extension, with its browser APIs.
+ * Keeps the preview as tall as the page it shows, so that the page scrolls in `.page-body` together
+ * with the description above it. The page grows after `load` when it rewraps beside the scrollbar of
+ * `.page-body` or a lazy image loads, so a `ResizeObserver` from the frame's own window refits it.
+ * `allow-same-origin` lets the popup read the height, and gives the frame the extension's origin.
+ * The sandbox must not get `allow-scripts` too: with both, the clipped page's HTML runs as the
+ * extension, with its browser APIs.
*/
function fitToContent(frame: HTMLIFrameElement) {
- const height = frame.contentDocument?.documentElement.scrollHeight;
- if (height) frame.style.height = `${height}px`;
+ const root = frame.contentDocument?.documentElement;
+ const view = frame.contentWindow as (Window & typeof globalThis) | null;
+ if (!root || !view) return;
+ new view.ResizeObserver(() => {
+ frame.style.height = `${Math.ceil(root.getBoundingClientRect().height)}px`;
+ }).observe(root);
}
/** The modes the page offers, the default first: the selection, then the article, then a note. */
diff --git a/apps/web-clipper/entrypoints/popup/popup.spec.tsx b/apps/web-clipper/entrypoints/popup/popup.spec.tsx
index 1796c3e59a..6c8768bc96 100644
--- a/apps/web-clipper/entrypoints/popup/popup.spec.tsx
+++ b/apps/web-clipper/entrypoints/popup/popup.spec.tsx
@@ -460,24 +460,37 @@ describe("popup", () => {
}
});
- it("sizes the preview to the page, so that it scrolls with the description", async () => {
+ it("keeps the preview as tall as the page, so that it scrolls with the description", async () => {
const frame = container.querySelector<HTMLIFrameElement>(".page-body iframe.page-content");
expect(frame).not.toBeNull();
if (!frame) return;
- Object.defineProperty(frame, "contentDocument", {
- configurable: true,
- value: { documentElement: { scrollHeight: 640 } }
- });
+ let height = 640;
+ const root = { getBoundingClientRect: () => ({ height }) };
+ let onResize: (() => void) | undefined;
+ const observe = vi.fn();
+ class ResizeObserver {
+ constructor(callback: () => void) { onResize = callback; }
+ observe = observe;
+ }
+ Object.defineProperty(frame, "contentDocument", { configurable: true, value: { documentElement: root } });
+ Object.defineProperty(frame, "contentWindow", { configurable: true, value: { ResizeObserver } });
await act(() => {
frame.dispatchEvent(new Event("load"));
});
+ expect(observe).toHaveBeenCalledWith(root);
+ onResize?.();
expect(frame.style.height).toBe("640px");
+ // The page grows when it rewraps beside the card's scrollbar, or when an image loads.
+ height = 712.4;
+ onResize?.();
+ expect(frame.style.height).toBe("713px");
+
Object.defineProperty(frame, "contentDocument", { configurable: true, value: null });
await act(() => {
frame.dispatchEvent(new Event("load"));
});
- expect(frame.style.height).toBe("640px");
+ expect(frame.style.height).toBe("713px");
});
it("keeps a draft bookmark while Trilium is briefly not found", async () => {