From c7e14a0502766c55ef2495b283f0e1684e13ce49 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Mon, 30 Nov 2026 10:24:32 +0800 Subject: [PATCH] fix(markdown): keep the preview painted while scrolling Scrolling the Markdown preview with a trackpad could leave a large part of it blank until the next scroll. The preview moves itself on each wheel event, and a run of scrollBy calls can leave WebKit's compositor showing an older offset than the page's, over content it never painted. The preview, the source's large wheel jumps and the diff divider now set scrollTop/scrollLeft instead. A code block whose last line overflows it by a pixel no longer counts as a vertical scroller, which handed the whole gesture to that block and stopped the preview over it. A nested scroller keeps the wheel only while it can still move that way. --- src/features/vcs/__tests__/diffScroll.test.ts | 60 ++++++++++++------- src/features/vcs/diffScroll.ts | 7 ++- .../viewers/markdown/MarkdownEditor.tsx | 13 +++- src/features/viewers/markdown/live.test.ts | 14 ++++- src/features/viewers/markdown/live.ts | 4 +- .../viewers/markdown/previewScroll.test.tsx | 40 +++++++++++-- 6 files changed, 103 insertions(+), 35 deletions(-) diff --git a/src/features/vcs/__tests__/diffScroll.test.ts b/src/features/vcs/__tests__/diffScroll.test.ts index ee2a834755..0b61aed4bd 100644 --- a/src/features/vcs/__tests__/diffScroll.test.ts +++ b/src/features/vcs/__tests__/diffScroll.test.ts @@ -207,6 +207,9 @@ describe("synchronized navigation scrolling", () => { }); describe("wheel over a pane that cannot scroll further", () => { + const movedBy = new WeakMap>(); + const moved = (view: EditorView) => movedBy.get(view.scrollDOM)!; + let wheeling = false; // Lines deleted at the end: the left pane is long, the right one fits without scrolling. // `lead` lines come above those in the left pane only. function panes(sync = true, { left = 200, right = 25, lead = 0 } = {}) { @@ -220,9 +223,17 @@ describe("wheel over a pane that cannot scroll further", () => { const scrollDOM = dom.appendChild(document.createElement("div")); Object.defineProperty(scrollDOM, "scrollHeight", { value: lines * LINE }); Object.defineProperty(scrollDOM, "clientHeight", { value: 500 }); - scrollDOM.scrollBy = vi.fn((_x: number, y: number) => { - scrollDOM.scrollTop = Math.max(0, Math.min(scrollDOM.scrollTop + y, lines * LINE - 500)); - }) as unknown as typeof scrollDOM.scrollBy; + // A scroll made while handling a wheel is recorded as the distance asked for; any is clamped to the pane. + const moved = vi.fn(); + let top = 0; + Object.defineProperty(scrollDOM, "scrollTop", { + get: () => top, + set: (to: number) => { + if (wheeling) moved(0, to - top); + top = Math.max(0, Math.min(to, lines * LINE - 500)); + }, + }); + movedBy.set(scrollDOM, moved); return { dom, scrollDOM, @@ -243,7 +254,12 @@ describe("wheel over a pane that cannot scroll further", () => { controller.attach({ a, b, chunks: Chunk.build(a.state.doc, b.state.doc) } as unknown as MergeView); const wheel = (el: Element, deltaY: number, deltaMode = 0, init: WheelEventInit = {}) => { const e = new WheelEvent("wheel", { deltaY, deltaMode, cancelable: true, ...init }); - el.dispatchEvent(e); + wheeling = true; + try { + el.dispatchEvent(e); + } finally { + wheeling = false; + } return e.defaultPrevented; }; return { a, b, controller, wheel, divider: root.querySelector(".cm-diffDivider")! }; @@ -254,13 +270,13 @@ describe("wheel over a pane that cannot scroll further", () => { const p = panes(); try { expect(p.wheel(p.b.scrollDOM, 120)).toBe(false); - expect(p.a.scrollDOM.scrollBy).toHaveBeenLastCalledWith(0, 120); + expect(moved(p.a)).toHaveBeenLastCalledWith(0, 120); p.wheel(p.divider, 80); - expect(p.a.scrollDOM.scrollBy).toHaveBeenLastCalledWith(0, 80); + expect(moved(p.a)).toHaveBeenLastCalledWith(0, 80); // The left pane can still scroll down itself: nothing is passed on. - vi.mocked(p.b.scrollDOM.scrollBy).mockClear(); + moved(p.b).mockClear(); p.wheel(p.a.scrollDOM, 120); - expect(p.b.scrollDOM.scrollBy).not.toHaveBeenCalled(); + expect(moved(p.b)).not.toHaveBeenCalled(); } finally { p.controller.destroy(); frame.mockRestore(); @@ -272,7 +288,7 @@ describe("wheel over a pane that cannot scroll further", () => { const p = panes(true, { left: 25, right: 200 }); try { expect(p.wheel(p.a.scrollDOM, 120)).toBe(false); - expect(p.b.scrollDOM.scrollBy).toHaveBeenLastCalledWith(0, 120); + expect(moved(p.b)).toHaveBeenLastCalledWith(0, 120); p.b.scrollDOM.dispatchEvent(new Event("scroll")); expect(p.b.scrollDOM.scrollTop).toBe(120); } finally { @@ -290,11 +306,11 @@ describe("wheel over a pane that cannot scroll further", () => { expect(p.a.scrollDOM.scrollTop).toBe(3000); // The right pane can't scroll at all: the left one comes back by the wheel's own distance. expect(p.wheel(p.b.scrollDOM, -3, WheelEvent.DOM_DELTA_LINE)).toBe(false); - expect(p.a.scrollDOM.scrollBy).toHaveBeenLastCalledWith(0, -3 * LINE); + expect(moved(p.a)).toHaveBeenLastCalledWith(0, -3 * LINE); p.a.scrollDOM.dispatchEvent(new Event("scroll")); expect(p.a.scrollDOM.scrollTop).toBe(3000 - 3 * LINE); p.wheel(p.divider, -80); - expect(p.a.scrollDOM.scrollBy).toHaveBeenLastCalledWith(0, -80); + expect(moved(p.a)).toHaveBeenLastCalledWith(0, -80); p.a.scrollDOM.dispatchEvent(new Event("scroll")); expect(p.a.scrollDOM.scrollTop).toBe(3000 - 3 * LINE - 80); } finally { @@ -311,9 +327,9 @@ describe("wheel over a pane that cannot scroll further", () => { p.a.scrollDOM.dispatchEvent(new Event("scroll")); p.a.scrollDOM.scrollTop = 2000; p.a.scrollDOM.dispatchEvent(new Event("scroll")); - vi.mocked(p.a.scrollDOM.scrollBy).mockClear(); + moved(p.a).mockClear(); expect(p.wheel(p.b.scrollDOM, -120)).toBe(false); - expect(p.a.scrollDOM.scrollBy).toHaveBeenLastCalledWith(0, -120); + expect(moved(p.a)).toHaveBeenLastCalledWith(0, -120); } finally { p.controller.destroy(); frame.mockRestore(); @@ -334,12 +350,12 @@ describe("wheel over a pane that cannot scroll further", () => { p.b.scrollDOM.dispatchEvent(new Event("scroll")); p.a.scrollDOM.dispatchEvent(new Event("scroll")); expect(p.a.scrollDOM.scrollTop).toBeCloseTo(p.b.scrollDOM.scrollTop); - vi.mocked(p.a.scrollDOM.scrollBy).mockClear(); + moved(p.a).mockClear(); expect(p.wheel(p.b.scrollDOM, -120)).toBe(false); // Elsewhere again, by something other than a wheel passed on. p.a.scrollDOM.scrollTop = 3000; expect(p.wheel(p.b.scrollDOM, -120)).toBe(false); - expect(p.a.scrollDOM.scrollBy).not.toHaveBeenCalled(); + expect(moved(p.a)).not.toHaveBeenCalled(); } finally { p.controller.destroy(); frame.mockRestore(); @@ -360,7 +376,7 @@ describe("wheel over a pane that cannot scroll further", () => { } // The pointer moves to the left pane, which can still scroll: the wheel is its own. expect(p.wheel(p.a.scrollDOM, 120)).toBe(false); - expect(p.b.scrollDOM.scrollBy).not.toHaveBeenCalled(); + expect(moved(p.b)).not.toHaveBeenCalled(); expect(p.a.scrollDOM.scrollTop).toBe(mapped + 480); } finally { p.controller.destroy(); @@ -380,7 +396,7 @@ describe("wheel over a pane that cannot scroll further", () => { expect(p.a.scrollDOM.scrollTop).toBe(mapped + 100); // A wheel longer than what is left: the left pane stops where the right one will map it. p.wheel(p.b.scrollDOM, -120); - expect(p.a.scrollDOM.scrollBy).toHaveBeenLastCalledWith(0, -220); + expect(moved(p.a)).toHaveBeenLastCalledWith(0, -220); p.b.scrollDOM.scrollTop -= 120; // the native scroll p.b.scrollDOM.dispatchEvent(new Event("scroll")); p.a.scrollDOM.dispatchEvent(new Event("scroll")); @@ -399,8 +415,8 @@ describe("wheel over a pane that cannot scroll further", () => { try { for (let i = 0; i < 3; i++) expect(p.wheel(p.b.scrollDOM, 120)).toBe(false); expect(p.wheel(p.divider, 120)).toBe(true); - expect(p.b.scrollDOM.scrollBy).toHaveBeenLastCalledWith(0, 120); - expect(p.a.scrollDOM.scrollBy).not.toHaveBeenCalled(); + expect(moved(p.b)).toHaveBeenLastCalledWith(0, 120); + expect(moved(p.a)).not.toHaveBeenCalled(); } finally { p.controller.destroy(); frame.mockRestore(); @@ -417,7 +433,7 @@ describe("wheel over a pane that cannot scroll further", () => { expect(p.b.scrollDOM.scrollLeft).toBe(40); expect(p.a.scrollDOM.scrollLeft).toBe(40); p.wheel(p.b.scrollDOM, 120, 0, { shiftKey: true }); - expect(p.a.scrollDOM.scrollBy).not.toHaveBeenCalled(); + expect(moved(p.a)).not.toHaveBeenCalled(); } finally { p.controller.destroy(); frame.mockRestore(); @@ -429,7 +445,7 @@ describe("wheel over a pane that cannot scroll further", () => { const p = panes(); try { expect(p.wheel(p.b.scrollDOM, 120, 0, { deltaX: 10 })).toBe(false); - expect(p.a.scrollDOM.scrollBy).toHaveBeenLastCalledWith(0, 120); + expect(moved(p.a)).toHaveBeenLastCalledWith(0, 120); p.a.scrollDOM.dispatchEvent(new Event("scroll")); p.b.scrollDOM.scrollLeft = 10; // the native horizontal scroll p.b.scrollDOM.dispatchEvent(new Event("scroll")); @@ -530,7 +546,7 @@ describe("wheel over a pane that cannot scroll further", () => { p.wheel(p.b.scrollDOM, 120); p.a.scrollDOM.scrollTop = 3000; p.wheel(p.b.scrollDOM, -120); - expect(p.a.scrollDOM.scrollBy).not.toHaveBeenCalled(); + expect(moved(p.a)).not.toHaveBeenCalled(); } finally { p.controller.destroy(); frame.mockRestore(); diff --git a/src/features/vcs/diffScroll.ts b/src/features/vcs/diffScroll.ts index ce76544793..04b242d66a 100644 --- a/src/features/vcs/diffScroll.ts +++ b/src/features/vcs/diffScroll.ts @@ -503,7 +503,10 @@ export class UnalignedPanes implements DiffPanes { this.interruptScroll(); this.leadWheel("b"); this.passOn("b", e); - mv.b.scrollDOM.scrollBy(e.deltaX, e.deltaY); + // Offsets rather than scrollBy, which can leave WebKit's compositor at a stale, unpainted offset. + const scroller = mv.b.scrollDOM; + scroller.scrollLeft += e.deltaX; + scroller.scrollTop += e.deltaY; }; this.divider.addEventListener("wheel", onWheel, { passive: false }); this.cleanup.push(() => this.divider.removeEventListener("wheel", onWheel)); @@ -616,7 +619,7 @@ export class UnalignedPanes implements DiffPanes { const room = dy > 0 ? el.scrollHeight - el.clientHeight - el.scrollTop : el.scrollTop; const scroll = (by: number, dir: number) => { const before = other.scrollTop; - other.scrollBy(0, by); + other.scrollTop += by; if (other.scrollTop !== before) this.passed[to] = { top: other.scrollTop, dir }; }; const passed = this.passed[to]; diff --git a/src/features/viewers/markdown/MarkdownEditor.tsx b/src/features/viewers/markdown/MarkdownEditor.tsx index f88240fac0..12f75cb62d 100644 --- a/src/features/viewers/markdown/MarkdownEditor.tsx +++ b/src/features/viewers/markdown/MarkdownEditor.tsx @@ -865,17 +865,24 @@ function MarkdownPreview({ path, tabId, sync }: { path: string; tabId: string; s const max = root.scrollHeight - root.clientHeight; if (max <= root.clientHeight || (e.deltaY > 0 ? root.scrollTop >= max : root.scrollTop <= 0)) return; for (let el = e.target instanceof Element ? e.target : null; el && el !== root; el = el.parentElement) { - if (el.scrollHeight <= el.clientHeight && (!e.deltaX || el.scrollWidth <= el.clientWidth)) continue; + // A code block's last line can overflow it by a pixel; that doesn't make it a vertical scroller. + const vertical = + el.scrollHeight - el.clientHeight > 1 && + (e.deltaY > 0 ? el.scrollTop + el.clientHeight < el.scrollHeight - 1 : el.scrollTop > 0); + if (!vertical && (!e.deltaX || el.scrollWidth <= el.clientWidth)) continue; const style = getComputedStyle(el); if ( - (el.scrollHeight > el.clientHeight && /^(auto|scroll)$/.test(style.overflowY)) || + (vertical && /^(auto|scroll)$/.test(style.overflowY)) || (e.deltaX && el.scrollWidth > el.clientWidth && /^(auto|scroll)$/.test(style.overflowX)) ) return; } // WebKit decides on the first wheel event whether it can scroll ahead of paint for the entire gesture. e.preventDefault(); - root.scrollBy(e.deltaX, e.deltaY); + // Set the offsets rather than scrollBy: WebKit's compositor can fall behind a run of + // scrollBy calls and show a stale offset whose content was never painted. + root.scrollLeft += e.deltaX; + root.scrollTop += e.deltaY; }; root.addEventListener("wheel", onPreviewWheel, { passive: false }); root.addEventListener("wheel", onWheel, { capture: true, passive: false }); diff --git a/src/features/viewers/markdown/live.test.ts b/src/features/viewers/markdown/live.test.ts index 22882fd72e..54135871c2 100644 --- a/src/features/viewers/markdown/live.test.ts +++ b/src/features/viewers/markdown/live.test.ts @@ -95,8 +95,13 @@ describe("Markdown source scroll", () => { vi.spyOn(view, "viewportLineBlocks", "get").mockReturnValue([block]); const measure = vi.spyOn(view, "requestMeasure").mockImplementation(() => {}); const lineAtHeight = vi.spyOn(view, "lineBlockAtHeight").mockReturnValue(block); - const scrollBy = vi.fn(); - Object.defineProperty(view.scrollDOM, "scrollBy", { value: scrollBy }); + const scrolled: [string, number][] = []; + for (const side of ["scrollTop", "scrollLeft"] as const) + Object.defineProperty(view.scrollDOM, side, { + configurable: true, + get: () => 0, + set: (v: number) => scrolled.push([side, v]), + }); const dispose = installMarkdownSourceScroll(view.dom); const wheel = (init: WheelEventInit) => view.contentDOM.dispatchEvent(new WheelEvent("wheel", { bubbles: true, cancelable: true, ...init })); @@ -106,7 +111,10 @@ describe("Markdown source scroll", () => { expect(wheel({ deltaY: 100, ctrlKey: true })).toBe(true); expect(wheel({ deltaX: 100, deltaY: 10 })).toBe(true); expect(wheel({ deltaX: 3, deltaY: 100 })).toBe(false); - expect(scrollBy).toHaveBeenCalledExactlyOnceWith(3, 100); + expect(scrolled).toEqual([ + ["scrollLeft", 3], + ["scrollTop", 100], + ]); expect(measure).toHaveBeenCalledTimes(1); expect(lineAtHeight).toHaveBeenCalledExactlyOnceWith(100); } finally { diff --git a/src/features/viewers/markdown/live.ts b/src/features/viewers/markdown/live.ts index 2fb404660d..20d77f4474 100644 --- a/src/features/viewers/markdown/live.ts +++ b/src/features/viewers/markdown/live.ts @@ -230,7 +230,9 @@ export function installMarkdownSourceScroll(root: HTMLElement): () => void { // A native jump can expose a virtual gap before CodeMirror redraws. Move and measure together. event.preventDefault(); - scroller.scrollBy(event.deltaX, target - current); + // Offsets rather than scrollBy, which can leave WebKit's compositor at a stale offset. + scroller.scrollLeft += event.deltaX; + scroller.scrollTop = target; view.requestMeasure(); view.lineBlockAtHeight(nextTop); }; diff --git a/src/features/viewers/markdown/previewScroll.test.tsx b/src/features/viewers/markdown/previewScroll.test.tsx index 332be2b9c0..dadf4ee320 100644 --- a/src/features/viewers/markdown/previewScroll.test.tsx +++ b/src/features/viewers/markdown/previewScroll.test.tsx @@ -81,7 +81,7 @@ describe("preview-only scroll position", () => { second.unmount(); }); - it("handles a long preview gesture from its first pixel and leaves nested scrolling native", () => { + it("handles a long preview gesture from its first pixel and leaves nested scrolling native while it can move", () => { open(mdCtx); act(() => useMarkdownModes.setState((s) => ({ byTab: { ...s.byTab, t: "preview" } }))); render( @@ -96,10 +96,36 @@ describe("preview-only scroll position", () => { }); const scrollBy = vi.fn(); preview.scrollBy = scrollBy; + const scrolled: [number, number][] = []; + let left = 0; + let top = 0; + Object.defineProperties(preview, { + scrollLeft: { configurable: true, get: () => left, set: (v: number) => void (left = v) }, + scrollTop: { + configurable: true, + get: () => top, + set: (v: number) => { + scrolled.push([left, v]); + top = v; + }, + }, + }); const nested = document.createElement("pre"); nested.style.overflowY = "auto"; Object.defineProperties(nested, { clientHeight: { value: 30 }, scrollHeight: { value: 120 } }); preview.appendChild(nested); + const snug = document.createElement("pre"); + snug.style.overflowY = "auto"; + Object.defineProperties(snug, { clientHeight: { value: 75 }, scrollHeight: { value: 76 } }); + preview.appendChild(snug); + const scrolledDown = document.createElement("pre"); + scrolledDown.style.overflowY = "auto"; + Object.defineProperties(scrolledDown, { + clientHeight: { value: 30 }, + scrollHeight: { value: 120 }, + scrollTop: { value: 90 }, + }); + preview.appendChild(scrolledDown); const wheel = (target: Element, init: WheelEventInit) => target.dispatchEvent( new WheelEvent("wheel", { @@ -108,17 +134,23 @@ describe("preview-only scroll position", () => { ...init, }), ); - expect(wheel(preview, { deltaY: 1 })).toBe(false); expect(wheel(preview, { deltaY: -300 })).toBe(true); + expect(wheel(preview, { deltaY: 1 })).toBe(false); expect(wheel(preview, { deltaY: 300, deltaMode: 1 })).toBe(true); expect(wheel(preview, { deltaY: 300, deltaX: 400 })).toBe(true); expect(wheel(nested, { deltaY: 300 })).toBe(true); expect(wheel(preview, { deltaY: 300, deltaX: 1 })).toBe(false); + expect(wheel(snug, { deltaY: 10 })).toBe(false); + expect(wheel(scrolledDown, { deltaY: 10 })).toBe(false); + expect(wheel(scrolledDown, { deltaY: -10 })).toBe(true); Object.defineProperty(preview, "scrollHeight", { value: 650 }); expect(wheel(preview, { deltaY: 1 })).toBe(true); - expect(scrollBy.mock.calls).toEqual([ + expect(scrollBy).not.toHaveBeenCalled(); + expect(scrolled).toEqual([ [0, 1], - [1, 300], + [1, 301], + [1, 311], + [1, 321], ]); });