Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai cc83c09e54 fix(diff): prevent wheel scroll jump-back loop in WebKit 2026-11-26 15:22:58 +08:00
3 змінених файлів з 192 додано та 10 видалено

No files matched your search

+83 -2
Переглянути файл
@@ -1,8 +1,9 @@
import { describe, expect, it, vi } from "vitest";
import { Chunk, MergeView } from "@codemirror/merge";
import { Text } from "@codemirror/state";
import type { EditorView } from "@codemirror/view";
import { EditorState, Text } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { bandAt, bandPath, transferY, PaddedPanes, UnalignedPanes, type Band } from "../diffScroll";
import { collapsedDiffLines } from "../collapsedDiffLines";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
@@ -177,6 +178,32 @@ describe("synchronized navigation scrolling", () => {
vi.useRealTimers();
}
});
it("keeps the wheel-scrolled pane in charge when the follower pane adjusts scrollTop on measure", () => {
vi.useFakeTimers();
const frame = vi.spyOn(window, "requestAnimationFrame").mockReturnValue(1);
const p = panes();
try {
p.b.scrollDOM.dispatchEvent(new Event("wheel"));
p.scroll(p.b, 1200);
expect(p.a.scrollDOM.scrollTop).toBe(1200);
// Repeated wheel events must not cancel native momentum with scrollTo(instant).
p.b.scrollDOM.dispatchEvent(new Event("wheel"));
expect(p.b.scrollDOM.scrollTo).not.toHaveBeenCalled();
// Follower shifts from CodeMirror height measurement must not yank the wheel source back.
p.scroll(p.a, 800);
expect(p.b.scrollDOM.scrollTop).toBe(1200);
expect(p.a.scrollDOM.scrollTop).toBe(1200);
// Pointer interaction on the other pane hands control back immediately.
p.a.scrollDOM.dispatchEvent(new Event("pointerdown"));
p.scroll(p.a, 400);
expect(p.b.scrollDOM.scrollTop).toBe(400);
} finally {
p.controller.destroy();
frame.mockRestore();
vi.useRealTimers();
}
});
});
describe("padded pane scrolling (aligned diff)", () => {
@@ -241,6 +268,60 @@ describe("padded pane scrolling (aligned diff)", () => {
vi.useRealTimers();
}
});
it("keeps the wheel-scrolled pane in charge when the follower shifts during layout", () => {
vi.useFakeTimers();
const p = panes();
try {
p.b.scrollDOM.dispatchEvent(new Event("wheel"));
p.scroll(p.b, 900);
expect(p.a.scrollDOM.scrollTop).toBe(900);
p.scroll(p.a, 500);
expect(p.b.scrollDOM.scrollTop).toBe(900);
expect(p.a.scrollDOM.scrollTop).toBe(900);
vi.advanceTimersByTime(150);
p.scroll(p.a, 200);
expect(p.b.scrollDOM.scrollTop).toBe(200);
} finally {
p.controller.destroy();
vi.useRealTimers();
}
});
});
describe("collapsed diff lines", () => {
it("defers button DOM replacement from docViewUpdate into CodeMirror's measure write phase", () => {
const parent = document.createElement("div");
document.body.appendChild(parent);
const view = new EditorView({
state: EditorState.create({ doc: "a\nb\nc", extensions: [collapsedDiffLines()] }),
parent,
});
const measureSpy = vi.spyOn(view, "requestMeasure");
try {
const row = document.createElement("div");
row.className = "cm-collapsedLines";
row.textContent = "10 unchanged lines";
view.contentDOM.appendChild(row);
const plugin = (
view as unknown as { plugins: Array<{ value: { docViewUpdate?: () => void } | null }> }
).plugins.find((p) => typeof p.value?.docViewUpdate === "function");
expect(plugin).toBeDefined();
measureSpy.mockClear();
plugin!.value!.docViewUpdate!();
// Must not mutate DOM synchronously during docViewUpdate (triggers WebKit scrollTop clamping).
expect(row.querySelector("button")).toBeNull();
expect(measureSpy).toHaveBeenCalledTimes(1);
const req = measureSpy.mock.calls[0]![0] as { write?: (v: unknown, view: EditorView) => void };
req.write?.(null, view);
expect(row.querySelector("button")).not.toBeNull();
expect(row.querySelector("button")?.textContent).toBe("10 unchanged lines");
} finally {
measureSpy.mockRestore();
view.destroy();
parent.remove();
}
});
});
describe("divider arrows", () => {
+7 -2
Переглянути файл
@@ -20,8 +20,13 @@ const collapseButtons = ViewPlugin.define((view) => {
row.replaceChildren(button);
}
};
view.requestMeasure({ read: () => null, write: enhance });
return { docViewUpdate: enhance };
// Defer DOM replacement into CodeMirror's measure write phase rather than mutating synchronously
// during docViewUpdate: in WebKit, inserting the inline SVG while updated viewport gaps have not
// flushed layout yet causes scrollTop to clamp backwards and trap wheel scrolling in a loop.
const measure = { key: enhance, read: () => null, write: enhance };
const schedule = () => view.requestMeasure(measure);
schedule();
return { docViewUpdate: schedule };
});
export function collapsedDiffLines(): Extension {
+102 -6
Переглянути файл
@@ -193,17 +193,25 @@ export interface DiffPanes {
* of the content, out of reach until the diff is scrolled to its end.
*/
export class PaddedPanes implements DiffPanes {
readonly extension: Extension = [];
readonly extension: Extension;
private mv: MergeView | null = null;
private sync: boolean;
private scrolling: Side | null = null;
private scrollTimer: ReturnType<typeof setTimeout> | undefined;
private wheelLeader: Side | null = null;
private wheelTimer: ReturnType<typeof setTimeout> | undefined;
/** The scroll position a pane was last moved to by the other one, to tell its echo from the user. */
private readonly moved: Record<Side, number | null> = { a: null, b: null };
private readonly cleanup: (() => void)[] = [];
constructor(sync: boolean) {
this.sync = sync;
this.extension = EditorView.updateListener.of((u) => {
if (u.heightChanged && !u.docChanged && this.mv) {
const side: Side | null = u.view === this.mv.a ? "a" : u.view === this.mv.b ? "b" : null;
if (side && this.moved[side] !== null) this.moved[side] = u.view.scrollDOM.scrollTop;
}
});
}
attach(mv: MergeView) {
@@ -211,8 +219,22 @@ export class PaddedPanes implements DiffPanes {
for (const side of ["a", "b"] as const) {
const el = this.view(side).scrollDOM;
const onScroll = () => this.scrolled(side);
const onWheel = () => {
this.interruptScroll();
this.leadWheel(side);
};
const onPointer = () => {
this.interruptScroll();
this.clearWheel();
};
el.addEventListener("scroll", onScroll, { passive: true });
this.cleanup.push(() => el.removeEventListener("scroll", onScroll));
el.addEventListener("wheel", onWheel, { passive: true });
el.addEventListener("pointerdown", onPointer);
this.cleanup.push(() => {
el.removeEventListener("scroll", onScroll);
el.removeEventListener("wheel", onWheel);
el.removeEventListener("pointerdown", onPointer);
});
}
this.resync();
}
@@ -225,12 +247,14 @@ export class PaddedPanes implements DiffPanes {
setSync(on: boolean) {
if (on === this.sync) return;
this.interruptScroll();
this.clearWheel();
this.sync = on;
this.resync();
}
scrollTo(side: Side, options: ScrollToOptions) {
this.interruptScroll();
this.clearWheel();
this.scrolling = this.sync ? side : null;
this.releaseScroll();
this.view(side).scrollDOM.scrollTo(options);
@@ -241,12 +265,14 @@ export class PaddedPanes implements DiffPanes {
* other pane's delayed reflow follows it instead of being mistaken for a user scroll pulling back.
*/
leadScroll(side: Side) {
this.clearWheel();
this.scrolling = this.sync ? side : null;
this.releaseScroll();
}
destroy() {
clearTimeout(this.scrollTimer);
clearTimeout(this.wheelTimer);
for (const f of this.cleanup) f();
this.mv = null;
}
@@ -262,6 +288,16 @@ export class PaddedPanes implements DiffPanes {
this.releaseScroll();
return;
}
if (this.wheelLeader) {
this.moved[from] = null;
if (from === this.wheelLeader) {
this.leadWheel(from);
if (this.sync) this.follow(from);
} else if (this.sync) {
this.follow(this.wheelLeader);
}
return;
}
const el = this.view(from).scrollDOM;
const moved = this.moved[from];
const echo = moved !== null && Math.abs(el.scrollTop - moved) <= 1;
@@ -269,6 +305,17 @@ export class PaddedPanes implements DiffPanes {
if (!echo && this.sync) this.follow(from);
}
private leadWheel(side: Side) {
this.wheelLeader = this.sync ? side : null;
clearTimeout(this.wheelTimer);
if (this.wheelLeader) this.wheelTimer = setTimeout(() => (this.wheelLeader = null), 150);
}
private clearWheel() {
this.wheelLeader = null;
clearTimeout(this.wheelTimer);
}
private follow(from: Side) {
const to: Side = from === "a" ? "b" : "a";
const el = this.view(to).scrollDOM;
@@ -314,6 +361,8 @@ export class UnalignedPanes implements DiffPanes {
private sync: boolean;
private scrolling: Side | null = null;
private scrollTimer: ReturnType<typeof setTimeout> | undefined;
private wheelLeader: Side | null = null;
private wheelTimer: ReturnType<typeof setTimeout> | undefined;
private frame = -1;
private resize: ResizeObserver | null = null;
private readonly cleanup: (() => void)[] = [];
@@ -326,7 +375,19 @@ export class UnalignedPanes implements DiffPanes {
opts.initialRatio != null && Number.isFinite(opts.initialRatio)
? Math.min(MAX_RATIO, Math.max(MIN_RATIO, opts.initialRatio))
: loadRatio();
this.extension = [gapMarks, changedLineGutters, EditorView.updateListener.of(() => this.schedule())];
this.extension = [
gapMarks,
changedLineGutters,
EditorView.updateListener.of((u) => {
// When CodeMirror's measure adjusts a follower pane's scrollTop after line heights resolve,
// update the expected echo so the layout adjustment is not mistaken for a user scroll.
if (u.heightChanged && !u.docChanged && this.mv) {
const side: Side | null = u.view === this.mv.a ? "a" : u.view === this.mv.b ? "b" : null;
if (side && this.moved[side].top !== null) this.moved[side].top = u.view.scrollDOM.scrollTop;
}
this.schedule();
}),
];
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);
@@ -349,13 +410,20 @@ export class UnalignedPanes implements DiffPanes {
for (const side of ["a", "b"] as const) {
const el = this.view(side).scrollDOM;
const onScroll = () => this.scrolled(side);
const onPointer = () => this.interruptScroll();
const onWheel = () => {
this.interruptScroll();
this.leadWheel(side);
};
const onPointer = () => {
this.interruptScroll();
this.clearWheel();
};
el.addEventListener("scroll", onScroll, { passive: true });
el.addEventListener("wheel", onPointer, { passive: true });
el.addEventListener("wheel", onWheel, { passive: true });
el.addEventListener("pointerdown", onPointer);
this.cleanup.push(() => {
el.removeEventListener("scroll", onScroll);
el.removeEventListener("wheel", onPointer);
el.removeEventListener("wheel", onWheel);
el.removeEventListener("pointerdown", onPointer);
});
}
@@ -428,6 +496,7 @@ export class UnalignedPanes implements DiffPanes {
const onWheel = (e: WheelEvent) => {
e.preventDefault();
this.interruptScroll();
this.leadWheel("b");
mv.b.scrollDOM.scrollBy(e.deltaX, e.deltaY);
};
this.divider.addEventListener("wheel", onWheel, { passive: false });
@@ -459,17 +528,30 @@ export class UnalignedPanes implements DiffPanes {
setSync(on: boolean) {
if (on === this.sync) return;
this.interruptScroll();
this.clearWheel();
this.sync = on;
this.resync();
}
scrollTo(side: Side, options: ScrollToOptions) {
this.interruptScroll();
this.clearWheel();
this.scrolling = this.sync ? side : null;
this.releaseScroll();
this.view(side).scrollDOM.scrollTo(options);
}
private leadWheel(side: Side) {
this.wheelLeader = this.sync ? side : null;
clearTimeout(this.wheelTimer);
if (this.wheelLeader) this.wheelTimer = setTimeout(() => (this.wheelLeader = null), 150);
}
private clearWheel() {
this.wheelLeader = null;
clearTimeout(this.wheelTimer);
}
private releaseScroll() {
clearTimeout(this.scrollTimer);
if (this.scrolling) this.scrollTimer = setTimeout(() => (this.scrolling = null), 150);
@@ -487,6 +569,7 @@ export class UnalignedPanes implements DiffPanes {
destroy() {
clearTimeout(this.scrollTimer);
clearTimeout(this.wheelTimer);
if (this.frame >= 0) cancelAnimationFrame(this.frame);
this.resize?.disconnect();
for (const f of this.cleanup) f();
@@ -518,6 +601,19 @@ export class UnalignedPanes implements DiffPanes {
this.schedule();
return;
}
if (this.wheelLeader) {
// Keep the actively wheel-scrolled pane in charge so follower measure adjustments never pull it back.
const moved = this.moved[side];
moved.top = moved.left = null;
if (side === this.wheelLeader) {
this.leadWheel(side);
if (this.sync) this.follow(side);
} else if (this.sync) {
this.follow(this.wheelLeader);
}
this.schedule();
return;
}
const el = this.view(side).scrollDOM;
const moved = this.moved[side];
const echo =