fix(markdown): keep the preview painted while scrolling (#6908)

Trackpad scrolling in the Markdown preview could leave a large part of it blank until the next scroll. The preview moves itself on each wheel event with scrollBy, and in WebKit a run of scrollBy calls can leave the compositor at an older, never-painted offset than the page's. Setting scrollTop/scrollLeft keeps them in step; the source editor's large wheel jumps and the diff divider's wheel did the same and now set offsets too.

A fenced code block overflowing by a pixel no longer counts as a vertical scroller, and a nested scroller keeps the wheel only while it can still move in that direction, so scrolling no longer stops over code blocks.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-30 10:31:45 +08:00
1 джерело 4ca46032c4
коміт e9f3aef5ae
6 files changed
+103 -35

No files matched your search

+38 -22
Переглянути файл
@@ -207,6 +207,9 @@ describe("synchronized navigation scrolling", () => {
});
describe("wheel over a pane that cannot scroll further", () => {
const movedBy = new WeakMap<Element, ReturnType<typeof vi.fn>>();
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();
+5 -2
Переглянути файл
@@ -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];
+10 -3
Переглянути файл
@@ -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 });
+11 -3
Переглянути файл
@@ -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 {
+3 -1
Переглянути файл
@@ -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);
};
+36 -4
Переглянути файл
@@ -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],
]);
});