Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipper): turn off scroll snapping during a full-page screenshot

Elian Doran114684818d66
diff --git a/apps/web-clipper/floating_elements.spec.ts b/apps/web-clipper/floating_elements.spec.ts
index ab50022a46..87983514a3 100644
--- a/apps/web-clipper/floating_elements.spec.ts
+++ b/apps/web-clipper/floating_elements.spec.ts
@@ -44,7 +44,7 @@ describe("setFloatingElements", () => {
expect(styleOf(".gone")).toBeNull();
});
- it("hides the scrollbars until the page is restored", () => {
+ it("hides the scrollbars and turns off scroll snapping until the page is restored", () => {
const scrollbarStyles = () => [ ...document.querySelectorAll("style") ]
.filter((style) => style.textContent?.includes("scrollbar-width: none !important"));
@@ -52,6 +52,7 @@ describe("setFloatingElements", () => {
setFloatingElements("first");
expect(scrollbarStyles()).toHaveLength(1);
expect(scrollbarStyles()[0]?.textContent).toContain("html::-webkit-scrollbar");
+ expect(scrollbarStyles()[0]?.textContent).toContain("scroll-snap-type: none !important");
setFloatingElements("restore");
expect(scrollbarStyles()).toHaveLength(0);
diff --git a/apps/web-clipper/floating_elements.ts b/apps/web-clipper/floating_elements.ts
index 0e8a39a5b0..08b340b876 100644
--- a/apps/web-clipper/floating_elements.ts
+++ b/apps/web-clipper/floating_elements.ts
@@ -1,7 +1,8 @@
/**
* The steps of a full-page screenshot that change the page:
- * - `first`, before the first capture, hides the scrollbars, pins sticky elements where they are
- * in the page and hides the fixed elements that are not at the top of the viewport;
+ * - `first`, before the first capture, hides the scrollbars, turns off scroll snapping, pins sticky
+ * elements where they are in the page and hides the fixed elements that are not at the top of the
+ * viewport;
* - `rest`, after the first capture, hides the fixed elements at the top too, so a header appears
* once, at the top of the screenshot;
* - `restore` puts back every inline style the other steps changed.
@@ -23,12 +24,13 @@ const PINNED_STICKY: [ string, string ][] = [
const HIDDEN: [ string, string ][] = [ [ "display", "none" ] ];
/**
- * Hides the page's scrollbars, which would otherwise be in every capture. The page can still be
- * scrolled. `scrollbar-width` is for Firefox and Chrome 121 or later, `::-webkit-scrollbar` for
- * older Chrome.
+ * Hides the page's scrollbars, which would otherwise be in every capture, and turns off scroll
+ * snapping, which can move the page past the position asked for and leave rows uncaptured. The page
+ * can still be scrolled. `scrollbar-width` is for Firefox and Chrome 121 or later,
+ * `::-webkit-scrollbar` for older Chrome.
*/
-const HIDE_SCROLLBARS_CSS = `
- html, body { scrollbar-width: none !important; }
+const CAPTURE_CSS = `
+ html, body { scrollbar-width: none !important; scroll-snap-type: none !important; }
html::-webkit-scrollbar, body::-webkit-scrollbar { display: none !important; }`;
interface SavedStyle {
@@ -42,7 +44,7 @@ interface SavedStyle {
const saved = new Map<HTMLElement, SavedStyle>();
let topFixedElements: HTMLElement[] = [];
let restoreTimer: ReturnType<typeof setTimeout> | undefined;
-let scrollbarStyle: HTMLStyleElement | undefined;
+let captureStyle: HTMLStyleElement | undefined;
export function setFloatingElements(step: FloatingElementsStep) {
if (step === "first") {
@@ -60,9 +62,9 @@ export function setFloatingElements(step: FloatingElementsStep) {
}
function prepareFirstCapture() {
- scrollbarStyle = document.createElement("style");
- scrollbarStyle.textContent = HIDE_SCROLLBARS_CSS;
- document.documentElement.append(scrollbarStyle);
+ captureStyle = document.createElement("style");
+ captureStyle.textContent = CAPTURE_CSS;
+ document.documentElement.append(captureStyle);
for (const element of document.body.querySelectorAll<HTMLElement>("*")) {
const style = getComputedStyle(element);
@@ -114,8 +116,8 @@ function override(element: HTMLElement, properties: [ string, string ][]) {
function restoreFloatingElements() {
clearTimeout(restoreTimer);
restoreTimer = undefined;
- scrollbarStyle?.remove();
- scrollbarStyle = undefined;
+ captureStyle?.remove();
+ captureStyle = undefined;
for (const [ element, original ] of saved) {
for (const [ name, { value, priority } ] of original.properties) {