fix(wiki): align highlight overlay and textarea wrapping so caret/click match painted text
The markdown overlay shared the wrapper's inset-0 box with the transparent-text textarea, but its inner div added a 1px transparent border while the textarea is border-0, shrinking its text width by ~2px; Firefox/Safari classic scrollbars reserve another ~15px inside the scrolled textarea that the overflow-hidden overlay never sees. Either gutter makes long unbreakable tokens (e.g. image markdown URLs) wrap differently between the two layers, so the overlay text drifts vertically under the caret/click math and editing feels like it's one or more lines off right after inserting an image. Compose both layers' classes from a shared WIKI_EDITOR_METRICS constant, drop the overlay border, hide the textarea scrollbar so its content width matches the overlay in every browser, and lock class parity with a jsdom RTL spec. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
34c0d5681f
commit
e15b0923ad
4 changed files with 121 additions and 5 deletions
|
|
@ -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",
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -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<HTMLDivElement> {
|
||||
readonly body: string;
|
||||
|
|
@ -10,9 +11,11 @@ interface WikiEditorHighlightLayerProps extends HTMLAttributes<HTMLDivElement> {
|
|||
|
||||
/**
|
||||
* 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}
|
||||
>
|
||||
<div className="whitespace-pre-wrap break-words border border-transparent px-3 py-2 font-mono text-sm text-foreground/90">
|
||||
<div className={cn(WIKI_EDITOR_METRICS, "whitespace-pre-wrap break-words text-foreground/90")}>
|
||||
{nodes}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
25
src/components/frontend/wiki/editorMetrics.ts
Normal file
25
src/components/frontend/wiki/editorMetrics.ts
Normal file
|
|
@ -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 (``, 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";
|
||||
79
tests/int/wiki-editor-metrics.int.spec.ts
Normal file
79
tests/int/wiki-editor-metrics.int.spec.ts
Normal file
|
|
@ -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<string> {
|
||||
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<HTMLTextAreaElement>();
|
||||
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);
|
||||
}
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue