diff --git a/e2e/mergeEditor.spec.ts b/e2e/mergeEditor.spec.ts index 89c7859cea..c6d025500f 100644 --- a/e2e/mergeEditor.spec.ts +++ b/e2e/mergeEditor.spec.ts @@ -103,6 +103,29 @@ test("merge controls and pane titles fit while resizing the result", async ({ pa await expect.poll(() => text(result(page))).toContain("\nmine\n"); }); +test("both merge dividers scroll all three panes and redraw their ribbons", async ({ page }) => { + await page.setViewportSize({ width: 980, height: 660 }); + const base = lines(...Array.from({ length: 200 }, (_, i) => `line ${i}`)); + await mountMerge(page, [base, base.replace("line 20\n", "ours\n"), base.replace("line 20\n", "theirs\n")]); + const host = page.locator("#merge-host"); + const scrollers = host.locator(".cm-scroller"); + const positions = () => scrollers.evaluateAll((panes) => panes.map((pane) => pane.scrollTop)); + const left = host.getByRole("separator", { name: "Resize left merge pane" }); + const right = host.getByRole("separator", { name: "Resize right merge pane" }); + const ribbons = host.locator(".cm-mergeBand"); + await expect(ribbons).toHaveCount(2); + const paths = () => ribbons.evaluateAll((bands) => bands.map((band) => band.getAttribute("d"))); + + for (const divider of [left, right]) { + const beforeScroll = await positions(); + const beforeRibbons = await paths(); + await divider.hover(); + await page.mouse.wheel(0, 100); + await expect.poll(positions).toEqual(beforeScroll.map((top) => top + 100)); + await expect.poll(async () => (await paths()).filter((path, i) => path !== beforeRibbons[i])).toHaveLength(2); + } +}); + /** * Capture the Git boundary, keeping real editor state, dialogs and Apply behavior. Apply writes * the file through its document, which the page keeps in memory. diff --git a/src/features/vcs/MergeEditor.tsx b/src/features/vcs/MergeEditor.tsx index 1952686710..31e3ccf404 100644 --- a/src/features/vcs/MergeEditor.tsx +++ b/src/features/vcs/MergeEditor.tsx @@ -18,6 +18,7 @@ import { firstLineOf, languageFor, viewerBase } from "./cm"; import { ideaDiffConfig } from "./diffOptions"; import { confirmDialog } from "./dialogs"; import { focusedDiffNavigator, setFocusedDiffNavigator, type DiffNavigator } from "./diffNavigator"; +import { bindDividerWheel } from "./diffScroll"; import { mergeDividers } from "./mergeDividers"; import { syncMergeScroll } from "./mergeScroll"; import { mergeTitles } from "./conflicts"; @@ -130,6 +131,8 @@ export function MergeEditor({ input }: { input: EditorInput }) { const theirsRef = useRef(null); const leftDividerRef = useRef(null); const rightDividerRef = useRef(null); + const leftHandleRef = useRef(null); + const rightHandleRef = useRef(null); const resultView = useRef(null); const oursView = useRef(null); const theirsView = useRef(null); @@ -243,10 +246,14 @@ export function MergeEditor({ input }: { input: EditorInput }) { } // Scroll the three panes together, lined up through their changes. const stopSync = syncMergeScroll({ ours, result, theirs }); + const stopWheel = [leftHandleRef.current, rightHandleRef.current].map((handle) => + handle ? bindDividerWheel(handle, result) : undefined, + ); const left = leftDividerRef.current; const right = rightDividerRef.current; const stopDividers = left && right ? mergeDividers({ ours, result, theirs }, { left, right }) : () => {}; return () => { + stopWheel.forEach((stop) => stop?.()); stopSync(); stopDividers(); ours.destroy(); @@ -680,7 +687,7 @@ export function MergeEditor({ input }: { input: EditorInput }) { onContextMenu={(e) => oursView.current && showSideMenu(e, oursView.current, "left")} /> - +
@@ -691,7 +698,11 @@ export function MergeEditor({ input }: { input: EditorInput }) { onContextMenu={(e) => resultView.current && showResultMenu(e, resultView.current)} /> - +
{ } }); + it.each([ + [3, WheelEvent.DOM_DELTA_LINE, 3 * LINE], + [1, WheelEvent.DOM_DELTA_PAGE, 500], + ])("scrolls both panes from the divider in wheel delta mode %i/%i", (delta, mode, pixels) => { + const frame = vi.spyOn(window, "requestAnimationFrame").mockReturnValue(1); + const p = panes(true, { right: 200 }); + try { + expect(p.wheel(p.divider, delta, mode)).toBe(true); + expect(p.b.scrollDOM.scrollTop).toBe(pixels); + p.b.scrollDOM.dispatchEvent(new Event("scroll")); + expect(p.a.scrollDOM.scrollTop).toBe(pixels); + } finally { + p.controller.destroy(); + frame.mockRestore(); + } + }); + it("scrolls the right pane over a left pane at its end (lines added at the end)", () => { const frame = vi.spyOn(window, "requestAnimationFrame").mockReturnValue(1); const p = panes(true, { left: 25, right: 200 }); diff --git a/src/features/vcs/diffScroll.ts b/src/features/vcs/diffScroll.ts index 04b242d66a..bc666c3971 100644 --- a/src/features/vcs/diffScroll.ts +++ b/src/features/vcs/diffScroll.ts @@ -170,6 +170,47 @@ function loadRatio(fallback = 50): number { return fallback; } +function verticalWheelPixels(view: EditorView, event: WheelEvent): number { + const mode = event.deltaMode; + const scale = + mode === WheelEvent.DOM_DELTA_LINE + ? view.defaultLineHeight + : mode === WheelEvent.DOM_DELTA_PAGE + ? view.scrollDOM.clientHeight + : 1; + return event.deltaY * scale; +} + +/** Routes a divider's wheel to its editor so pane synchronization can handle the scroll. */ +export function bindDividerWheel(divider: HTMLElement, view: EditorView, beforeScroll?: (event: WheelEvent) => void) { + const scroller = view.scrollDOM; + const onWheel = (event: WheelEvent) => { + if (event.defaultPrevented) return; + event.preventDefault(); + beforeScroll?.(event); + const mode = event.deltaMode; + const x = event.deltaX; + const y = verticalWheelPixels(view, event); + // Use offsets to avoid stale compositor positions in WebKit's scrollBy. + if (x) { + const scale = + mode === WheelEvent.DOM_DELTA_LINE + ? view.defaultCharacterWidth + : mode === WheelEvent.DOM_DELTA_PAGE + ? scroller.clientWidth + : 1; + scroller.scrollLeft += x * scale; + } + if (y) scroller.scrollTop += y; + }; + divider.setAttribute("data-wheel-scroll", ""); + divider.addEventListener("wheel", onWheel, { passive: false }); + return () => { + divider.removeEventListener("wheel", onWheel); + divider.removeAttribute("data-wheel-scroll"); + }; +} + /** * The scroll machinery of a side-by-side diff's panes: added to both editors, * attached to their MergeView, and torn down with the diff. @@ -395,7 +436,6 @@ export class UnalignedPanes implements DiffPanes { this.divider.className = "cm-diffDivider"; // A lone arrow is centred in the divider; a pair keeps to the two edges. this.divider.classList.toggle("cm-diffDivider-lone", !!opts.arrows && opts.arrows.toA !== opts.arrows.toB); - this.divider.setAttribute("data-wheel-scroll", ""); this.divider.setAttribute("role", "separator"); this.divider.setAttribute("aria-orientation", "vertical"); this.divider.setAttribute("aria-label", "Resize diff panes"); @@ -498,18 +538,13 @@ export class UnalignedPanes implements DiffPanes { this.divider.removeEventListener("dblclick", onDblClick); }); // The divider scrolls the right pane (and, when synchronized, through it the left one). - const onWheel = (e: WheelEvent) => { - e.preventDefault(); - this.interruptScroll(); - this.leadWheel("b"); - this.passOn("b", e); - // 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)); + this.cleanup.push( + bindDividerWheel(this.divider, mv.b, (event) => { + this.interruptScroll(); + this.leadWheel("b"); + this.passOn("b", event); + }), + ); if (typeof ResizeObserver !== "undefined") { this.resize = new ResizeObserver(() => this.schedule()); this.resize.observe(this.divider); @@ -615,7 +650,7 @@ export class UnalignedPanes implements DiffPanes { const el = view.scrollDOM; const to: Side = side === "a" ? "b" : "a"; const other = this.view(to).scrollDOM; - const dy = e.deltaMode === 1 ? e.deltaY * view.defaultLineHeight : e.deltaY; + const dy = verticalWheelPixels(view, e); const room = dy > 0 ? el.scrollHeight - el.clientHeight - el.scrollTop : el.scrollTop; const scroll = (by: number, dir: number) => { const before = other.scrollTop;