diff --git a/src/components/frontend/wiki/WikiEditorBody.tsx b/src/components/frontend/wiki/WikiEditorBody.tsx index 5eb34edd..73c414c2 100644 --- a/src/components/frontend/wiki/WikiEditorBody.tsx +++ b/src/components/frontend/wiki/WikiEditorBody.tsx @@ -25,6 +25,8 @@ import { import { toast } from "sonner"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; +import { cn } from "@/lib/utils"; +import { WIKI_EDITOR_METRICS, WIKI_EDITOR_SCROLLBAR_NONE } from "./editorMetrics"; import { WikiLinkDialog } from "@/components/frontend/wiki/WikiLinkDialog"; import { WikiEditorHighlightLayer } from "@/components/frontend/wiki/WikiEditorHighlightLayer"; import { WikiTemplateDialog } from "@/components/frontend/wiki/WikiTemplateDialog"; @@ -448,7 +450,14 @@ export function WikiEditorBody({ onKeyDown={handleBodyKeyDown} placeholder="Write the field guide entry in Markdown." disabled={pending} - className="absolute inset-0 h-full w-full resize-none border-0 bg-transparent font-mono text-sm text-transparent caret-foreground shadow-none selection:bg-primary/30 focus-visible:outline-none focus-visible:ring-0" + className={cn( + // Must stay identical to the overlay metrics (editorMetrics.ts), plus no + // scrollbar gutter: a reserved classic scrollbar (Firefox/Safari) would + // shrink the textarea's content width vs the overlay and re-introduce wrap drift. + WIKI_EDITOR_METRICS, + WIKI_EDITOR_SCROLLBAR_NONE, + "absolute inset-0 h-full w-full resize-none border-0 bg-transparent text-transparent caret-foreground shadow-none selection:bg-primary/30 focus-visible:outline-none focus-visible:ring-0", + )} /> diff --git a/src/components/frontend/wiki/WikiEditorHighlightLayer.tsx b/src/components/frontend/wiki/WikiEditorHighlightLayer.tsx index d1d2b89e..554a2568 100644 --- a/src/components/frontend/wiki/WikiEditorHighlightLayer.tsx +++ b/src/components/frontend/wiki/WikiEditorHighlightLayer.tsx @@ -3,6 +3,7 @@ import { forwardRef, useMemo, type HTMLAttributes, type ReactNode } from "react"; import { cn } from "@/lib/utils"; import { highlightMarkdownLines } from "@/lib/wiki/editorHighlight"; +import { WIKI_EDITOR_METRICS } from "./editorMetrics"; interface WikiEditorHighlightLayerProps extends HTMLAttributes { readonly body: string; @@ -10,9 +11,11 @@ interface WikiEditorHighlightLayerProps extends HTMLAttributes { /** * Colored text layer rendered underneath the editor textarea (which uses - * transparent text). Sizing must mirror the textarea exactly: same padding, - * font, and a transparent border so text starts at the same 1px + padding - * offset inside the shared wrapper's border. + * transparent text). Sizing must mirror the textarea exactly — identical + * metrics/padding (`WIKI_EDITOR_METRICS`) and NO border of its own: children + * of the editor wrapper are absolutely positioned, so `inset-0` already places + * them inside the wrapper border; an extra transparent border here would just + * shrink the overlay by 2px and desynchronize where long lines wrap. */ export const WikiEditorHighlightLayer = forwardRef< HTMLDivElement, @@ -41,7 +44,7 @@ export const WikiEditorHighlightLayer = forwardRef< className={cn("pointer-events-none absolute inset-0 overflow-hidden", className)} {...rest} > -
+
{nodes}
diff --git a/src/components/frontend/wiki/editorMetrics.ts b/src/components/frontend/wiki/editorMetrics.ts new file mode 100644 index 00000000..6df16463 --- /dev/null +++ b/src/components/frontend/wiki/editorMetrics.ts @@ -0,0 +1,25 @@ +/** + * Typography + box-model classes that MUST stay identical between the editor + * textarea and the painted highlight overlay underneath it. + * + * The overlay is positioned `absolute inset-0` over the textarea and paints the + * visible text while the textarea keeps `text-transparent`. If the two layers + * ever differ in font size, line height, padding or available content width, + * soft-wrapped lines break at different columns; every difference shifts the + * overlay text vertically against the caret/click geometry of the textarea + * below it. Inserting an image markdown token (`![alt](/media/...png)`, one + * giant unbreakable word) maximizes wrap-point sensitivity, which is why the + * drift becomes obvious right after an upload. Both components import these + * classes from here instead of duplicating them; keep new shared metrics here, + * not inline in either component (see tests/int/wiki-editor-metrics.int.spec.ts). + */ +export const WIKI_EDITOR_METRICS = "px-3 py-2 font-mono text-sm"; + +/** + * Classic scrollbars (Firefox, Safari, non-overlay Chrome) reserve layout width + * inside the scrolling textarea, shrinking its content width relative to the + * overflow-hidden overlay. Hiding the scrollbar keeps both boxes exactly the + * same width everywhere; wheel/keyboard scrolling and the existing + * caret-into-view logic still work without a draggable thumb. + */ +export const WIKI_EDITOR_SCROLLBAR_NONE = "[scrollbar-width:none] [&::-webkit-scrollbar]:hidden"; diff --git a/tests/int/wiki-editor-metrics.int.spec.ts b/tests/int/wiki-editor-metrics.int.spec.ts new file mode 100644 index 00000000..a0e34171 --- /dev/null +++ b/tests/int/wiki-editor-metrics.int.spec.ts @@ -0,0 +1,79 @@ +// @vitest-environment jsdom +import { createElement, createRef } from "react"; +import { cleanup, render } from "@testing-library/react"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { WikiEditorBody } from "@/components/frontend/wiki/WikiEditorBody"; +import { WIKI_EDITOR_METRICS, WIKI_EDITOR_SCROLLBAR_NONE } from "@/components/frontend/wiki/editorMetrics"; + +vi.mock("@/app/(frontend)/wiki/actions", () => ({ + uploadWikiImage: vi.fn(), +})); + +afterEach(cleanup); + +function tokens(className: string | null): Set { + return new Set((className ?? "").trim().split(/\s+/).filter(Boolean)); +} + +describe("wiki editor overlay/textarea parity", () => { + const body = "# Heading\n\nSome long line ".repeat(10); + + function renderEditor() { + const ref = createRef(); + const { container } = render( + createElement(WikiEditorBody, { + body, + bodyRef: ref, + existingPages: [], + linkDialogOpen: false, + onBodyChange: () => {}, + onLinkDialogChange: () => {}, + pending: false, + templates: {}, + }), + ); + const textarea = container.querySelector("textarea#wiki-body"); + const overlayInner = container.querySelector("div.pointer-events-none > div"); + return { textarea, overlayInner }; + } + + it("renders both layers", () => { + const { textarea, overlayInner } = renderEditor(); + expect(textarea).not.toBeNull(); + expect(overlayInner).not.toBeNull(); + }); + + it("shares identical padding/typography classes between textarea and overlay", () => { + const { textarea, overlayInner } = renderEditor(); + const shared = tokens(WIKI_EDITOR_METRICS); + expect(shared.size).toBeGreaterThan(0); + for (const layer of [textarea, overlayInner]) { + const cls = tokens(layer?.className ?? null); + for (const metric of shared) { + expect(cls.has(metric), `${layer?.id ?? "overlay"} missing "${metric}"`).toBe(true); + } + } + }); + + it("keeps the overlay borderless (a border shrinks its width and desyncs wrapping)", () => { + const { overlayInner } = renderEditor(); + const cls = tokens(overlayInner?.className ?? ""); + for (const clsName of cls) expect(clsName).not.toMatch(/^border/); + }); + + it("hides the textarea scrollbar so its gutter never shrinks the content width", () => { + const { textarea } = renderEditor(); + const cls = tokens(textarea?.className ?? ""); + for (const token of tokens(WIKI_EDITOR_SCROLLBAR_NONE)) { + expect(cls.has(token), `textarea missing "${token}"`).toBe(true); + } + }); + + it("keeps the textarea flush and borderless inside the shared wrapper", () => { + const { textarea } = renderEditor(); + const cls = tokens(textarea?.className ?? ""); + for (const clsName of ["absolute", "inset-0", "border-0"]) { + expect(cls.has(clsName), `textarea missing "${clsName}"`).toBe(true); + } + }); +});