дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
fix(editor): keep a horizontally scrolled view in place while line-end hints refresh
Inline blame, line-end lenses and plugin messages draw from a zero-width anchor: they widen the scroller but not the content, and most of them leave on an edit and return a moment later. While they were away the scroll range shrank and the browser pulled a horizontally scrolled view back to the left, so the text jumped sideways as the user typed. A 1px spacer at the visible right edge, moved with every scroll, keeps the range from shrinking under the view without adding scroll room. Fixes #6624
Цей коміт міститься в:
1 джерело
16c2613cc0
коміт
c4b7f2a670
3 files changed
+104
No files matched your search
@@ -0,0 +1,43 @@
|
||||
// @vitest-environment jsdom
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { horizontalScrollHold } from "./horizontalScrollHold";
|
||||
|
||||
const views: EditorView[] = [];
|
||||
afterEach(() => views.splice(0).forEach((v) => v.destroy()));
|
||||
|
||||
function mount(): EditorView {
|
||||
const parent = document.createElement("div");
|
||||
document.body.append(parent);
|
||||
const view = new EditorView({
|
||||
state: EditorState.create({ doc: "x".repeat(200), extensions: horizontalScrollHold }),
|
||||
parent,
|
||||
});
|
||||
views.push(view);
|
||||
return view;
|
||||
}
|
||||
|
||||
// A line-end hint that leaves on an edit shrinks the scroll range; the spacer at the visible right
|
||||
// edge keeps a horizontally scrolled view where it is instead of letting the browser pull it left.
|
||||
it("keeps a spacer at the visible right edge as the view scrolls", () => {
|
||||
const view = mount();
|
||||
const scroller = view.scrollDOM;
|
||||
const spacer = scroller.querySelector<HTMLElement>(":scope > .cm-openidea-scrollHold")!;
|
||||
expect(spacer.getAttribute("aria-hidden")).toBe("true");
|
||||
Object.defineProperty(scroller, "clientWidth", { configurable: true, value: 500 });
|
||||
scroller.scrollLeft = 40;
|
||||
scroller.dispatchEvent(new Event("scroll"));
|
||||
expect(spacer.style.left).toBe("539px");
|
||||
scroller.scrollLeft = 0;
|
||||
scroller.dispatchEvent(new Event("scroll"));
|
||||
expect(spacer.style.left).toBe("499px");
|
||||
});
|
||||
|
||||
it("removes the spacer with the editor", () => {
|
||||
const view = mount();
|
||||
const scroller = view.scrollDOM;
|
||||
view.destroy();
|
||||
views.splice(views.indexOf(view), 1);
|
||||
expect(scroller.querySelector(".cm-openidea-scrollHold")).toBeNull();
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
import { EditorView, ViewPlugin, type ViewUpdate } from "@codemirror/view";
|
||||
|
||||
/**
|
||||
* Line-end hints (inline blame, line-end lenses, plugin messages) draw from a zero-width anchor,
|
||||
* so they widen the scroller without widening the content, and most of them leave on an edit and
|
||||
* come back a moment later. While they are away the scroll range shrinks and the browser pulls a
|
||||
* horizontally scrolled view back to the left: the text jumps sideways as the user types.
|
||||
* A 1px spacer at the visible right edge keeps the range from shrinking under the view; it moves
|
||||
* with the view and never adds scroll room of its own.
|
||||
*/
|
||||
export const horizontalScrollHold = [
|
||||
ViewPlugin.fromClass(
|
||||
class {
|
||||
readonly spacer: HTMLElement;
|
||||
private readonly onScroll = () => this.place();
|
||||
|
||||
constructor(readonly view: EditorView) {
|
||||
this.spacer = document.createElement("div");
|
||||
this.spacer.className = "cm-openidea-scrollHold";
|
||||
this.spacer.setAttribute("aria-hidden", "true");
|
||||
view.scrollDOM.appendChild(this.spacer);
|
||||
view.scrollDOM.addEventListener("scroll", this.onScroll, { passive: true });
|
||||
this.place();
|
||||
}
|
||||
|
||||
update(u: ViewUpdate) {
|
||||
if (u.geometryChanged) this.view.requestMeasure({ read: () => this.edge(), write: (x) => this.put(x) });
|
||||
}
|
||||
|
||||
private edge(): number {
|
||||
const { scrollLeft, clientWidth } = this.view.scrollDOM;
|
||||
return Math.max(0, scrollLeft + clientWidth - 1);
|
||||
}
|
||||
|
||||
private put(x: number) {
|
||||
this.spacer.style.left = `${x}px`;
|
||||
}
|
||||
|
||||
private place() {
|
||||
this.put(this.edge());
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.view.scrollDOM.removeEventListener("scroll", this.onScroll);
|
||||
this.spacer.remove();
|
||||
}
|
||||
},
|
||||
),
|
||||
EditorView.baseTheme({
|
||||
".cm-openidea-scrollHold": {
|
||||
position: "absolute",
|
||||
top: "0",
|
||||
width: "1px",
|
||||
height: "1px",
|
||||
pointerEvents: "none",
|
||||
visibility: "hidden",
|
||||
},
|
||||
}),
|
||||
];
|
||||
@@ -58,6 +58,7 @@ import { gutterLineDrag } from "./gutterDrag";
|
||||
import { overwriteField } from "./overwrite";
|
||||
import { imeSyllableSpaces } from "./imeCommit";
|
||||
import { stickySelectionField } from "./stickySelection";
|
||||
import { horizontalScrollHold } from "./horizontalScrollHold";
|
||||
import { relativeLineNumbers } from "./relativeLineNumbers";
|
||||
import { clipboardDocumentPath, clipboardFilesExtension } from "./clipboard";
|
||||
import { primarySelectionYield } from "./primarySelection";
|
||||
@@ -404,6 +405,7 @@ export function baseExtensions(o: SetupOptions): Extension {
|
||||
overwriteField,
|
||||
imeSyllableSpaces,
|
||||
stickySelectionField,
|
||||
horizontalScrollHold,
|
||||
gutterLineDrag,
|
||||
clipboardDocumentPath.of(o.ctx.path),
|
||||
clipboardFilesExtension,
|
||||
|
||||
Reference in new issue
Заблокувати користувача