fix(editor): anchor wrapped line numbers via inline style
This commit is contained in:
1 parent
767cc809a1
commit
dd11e42748
4 files changed
+29
-9
No files matched your search
@@ -4,8 +4,6 @@ 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;
|
||||
}
|
||||
@@ -14,6 +12,13 @@ export function buildQueryEditorLineNumbersExtension(factory: LineNumbersFactory
|
||||
return enabled && factory ? factory(config) : [];
|
||||
}
|
||||
|
||||
export function setQueryEditorLineNumberAlignment(element: HTMLElement, wrapped: boolean): void {
|
||||
// CodeMirror rebuilds gutter class names as marker state changes. Keep this
|
||||
// measured layout state inline so selection and active-line updates cannot
|
||||
// restore the base centered alignment for a wrapped line.
|
||||
element.style.alignItems = wrapped ? "flex-start" : "";
|
||||
}
|
||||
|
||||
export function createQueryEditorLineNumberAlignmentExtension(ViewPlugin: typeof import("@codemirror/view").ViewPlugin): Extension {
|
||||
return ViewPlugin.fromClass(
|
||||
class {
|
||||
@@ -44,7 +49,7 @@ export function createQueryEditorLineNumberAlignmentExtension(ViewPlugin: typeof
|
||||
},
|
||||
write: (measurements) => {
|
||||
this.measureScheduled = false;
|
||||
for (const { element, wrapped } of measurements) element.classList.toggle(WRAPPED_LINE_NUMBER_GUTTER_CLASS, wrapped);
|
||||
for (const { element, wrapped } of measurements) setQueryEditorLineNumberAlignment(element, wrapped);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user