fix(editor): align wrapped line numbers
This commit is contained in:
5 files changed
+70
-3
No files matched your search
+14
-1
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSqlCompletionThemeRules, editorDiagnosticColors, editorThemeAppearanceFor, resolveCustomThemeBackgrounds, resolveEditorTheme } from "@/lib/editor/editorThemes";
|
||||
import { buildEditorFontThemeRules, buildSqlCompletionThemeRules, editorDiagnosticColors, editorThemeAppearanceFor, resolveCustomThemeBackgrounds, resolveEditorTheme } from "@/lib/editor/editorThemes";
|
||||
import { DEFAULT_APP_CUSTOM_UI_COLORS, wcagContrastRatio, type AppThemePalette } from "@/lib/app/appTheme";
|
||||
import type { EditorTheme } from "@/stores/settingsStore";
|
||||
|
||||
@@ -163,3 +163,16 @@ describe("SQL completion theme", () => {
|
||||
expect(rules[".cm-completionDetail"]).toMatchObject({ flex: "1 1 0", minWidth: "0", textOverflow: "ellipsis" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("editor gutters", () => {
|
||||
it("anchors line numbers to the first visual row of wrapped lines", () => {
|
||||
const rules = buildEditorFontThemeRules();
|
||||
|
||||
expect(rules[".cm-lineNumbers .cm-gutterElement"]).toMatchObject({
|
||||
alignItems: "center",
|
||||
display: "flex",
|
||||
justifyContent: "flex-end",
|
||||
});
|
||||
expect(rules[".cm-lineNumbers .cm-gutterElement.cm-db-wrapped-line-number"]).toMatchObject({ alignItems: "flex-start" });
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,7 @@
|
||||
import { Compartment, EditorState } from "@codemirror/state";
|
||||
import { EditorView, GutterMarker, gutter, lineNumbers } from "@codemirror/view";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { buildQueryEditorLineNumbersExtension } from "@/lib/editor/queryEditorLineNumbers";
|
||||
import { buildQueryEditorLineNumbersExtension, isWrappedLineNumberGutter } from "@/lib/editor/queryEditorLineNumbers";
|
||||
|
||||
class StatementRunMarker extends GutterMarker {}
|
||||
|
||||
@@ -41,6 +41,12 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe("query editor line numbers", () => {
|
||||
it("only treats gutters taller than one visual row as wrapped", () => {
|
||||
expect(isWrappedLineNumberGutter(20.8, 20.8)).toBe(false);
|
||||
expect(isWrappedLineNumberGutter(21.6, 20.8)).toBe(false);
|
||||
expect(isWrappedLineNumberGutter(42, 20.8)).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps line selection behavior when enabled", () => {
|
||||
const { selectLine, view } = createEditor(true);
|
||||
const lineNumber = view.dom.querySelector<HTMLElement>(".cm-lineNumbers .cm-gutterElement");
|
||||
|
||||
@@ -818,6 +818,9 @@ export function buildEditorFontThemeRules(opts?: { fixedHeight?: boolean; scroll
|
||||
paddingRight: "8px",
|
||||
userSelect: "none",
|
||||
},
|
||||
".cm-lineNumbers .cm-gutterElement.cm-db-wrapped-line-number": {
|
||||
alignItems: "flex-start",
|
||||
},
|
||||
".cm-run-statement-gutter": {
|
||||
minWidth: "28px",
|
||||
},
|
||||
|
||||
@@ -4,6 +4,50 @@ import type { lineNumbers } from "@codemirror/view";
|
||||
type LineNumbersFactory = typeof lineNumbers;
|
||||
type LineNumbersConfig = NonNullable<Parameters<LineNumbersFactory>[0]>;
|
||||
|
||||
export const WRAPPED_LINE_NUMBER_GUTTER_CLASS = "cm-db-wrapped-line-number";
|
||||
|
||||
export function isWrappedLineNumberGutter(gutterHeight: number, lineHeight: number): boolean {
|
||||
return gutterHeight > lineHeight + 1;
|
||||
}
|
||||
|
||||
export function buildQueryEditorLineNumbersExtension(factory: LineNumbersFactory | null, enabled: boolean, config: LineNumbersConfig): Extension {
|
||||
return enabled && factory ? factory(config) : [];
|
||||
}
|
||||
|
||||
export function createQueryEditorLineNumberAlignmentExtension(ViewPlugin: typeof import("@codemirror/view").ViewPlugin): Extension {
|
||||
return ViewPlugin.fromClass(
|
||||
class {
|
||||
private measureScheduled = false;
|
||||
|
||||
constructor(view: import("@codemirror/view").EditorView) {
|
||||
this.scheduleMeasure(view);
|
||||
}
|
||||
|
||||
update(update: import("@codemirror/view").ViewUpdate) {
|
||||
if (update.docChanged || update.geometryChanged || update.viewportChanged) this.scheduleMeasure(update.view);
|
||||
}
|
||||
|
||||
docViewUpdate(view: import("@codemirror/view").EditorView) {
|
||||
this.scheduleMeasure(view);
|
||||
}
|
||||
|
||||
private scheduleMeasure(view: import("@codemirror/view").EditorView) {
|
||||
if (this.measureScheduled) return;
|
||||
this.measureScheduled = true;
|
||||
view.requestMeasure({
|
||||
read: (measuredView) => {
|
||||
const lineHeight = measuredView.defaultLineHeight;
|
||||
return [...measuredView.dom.querySelectorAll<HTMLElement>(".cm-lineNumbers .cm-gutterElement")].map((element) => ({
|
||||
element,
|
||||
wrapped: isWrappedLineNumberGutter(element.getBoundingClientRect().height, lineHeight),
|
||||
}));
|
||||
},
|
||||
write: (measurements) => {
|
||||
this.measureScheduled = false;
|
||||
for (const { element, wrapped } of measurements) element.classList.toggle(WRAPPED_LINE_NUMBER_GUTTER_CLASS, wrapped);
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user