The body editor painted highlighted markdown in a div behind a transparent-text textarea, so the painted text and the caret were two different layout engines. Any wrap-point difference (borders, scrollbar gutters, engine rounding, trailing newlines) shifted the overlay by whole lines once content soft-wrapped, making clicks land on the wrong glyph and the document end feel unreachable. The CSS patch that preceded this reduced the drift but could not remove the class of bug. CodeMirror 6 owns text and caret together, so the failure mode is gone by construction rather than by maintenance. The body remains a markdown string, so prepareMarkdown, ::: directives, wikilinks, templates, revisions and the preview pipeline are untouched, with no data migration. - WikiMarkdownEditor: EditorView lifecycle, theme and markdown HighlightStyle bound to the app's CSS tokens, history, line wrapping, IME-safe keymap. - markdownCommands: one pure command table shared by toolbar buttons and keys. - editorChange: toolbar updates dispatch only the differing span, keeping undo granular and selections outside the edit intact. - Removes WikiEditorHighlightLayer, editorMetrics, lib/wiki/editorHighlight, useEditorHistory and the now-dead textarea selection reader. - Adds e2e proof: glyph-precise clicks across wrapped paragraphs, list items, image markdown and scrolled documents land at the exact clicked offset. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
176 lines
6.6 KiB
TypeScript
176 lines
6.6 KiB
TypeScript
import { expect, test, type Page } from "@playwright/test";
|
|
|
|
/**
|
|
* Regression guard for the wiki body editor's caret geometry.
|
|
*
|
|
* The previous implementation painted the text in an overlay <div> while a
|
|
* transparent <textarea> owned the caret, so any wrap-point difference between
|
|
* the two layers put the caret on the wrong line as soon as lines soft-wrapped
|
|
* (and it was worst right after an image markdown token was inserted). These
|
|
* tests click a real glyph and assert the typed character lands at that exact
|
|
* document offset, which is only true when the editor owns both the text and
|
|
* the caret.
|
|
*/
|
|
|
|
const SAMPLE = [
|
|
"Introduction paragraph with enough words to wrap across the editor column at least twice so a click can target a continuation line inside it.",
|
|
"",
|
|
"Second paragraph with its own wrapping behaviour and a distinctive platypus.",
|
|
"",
|
|
"- list item one with a few words",
|
|
"- list item two with a few words",
|
|
"- list item three with a few words",
|
|
"",
|
|
"",
|
|
"",
|
|
].join("\n");
|
|
|
|
async function login(page: Page) {
|
|
await page.goto("/login");
|
|
await page.getByLabel("Username").fill("dev");
|
|
await page.getByLabel("Password").fill("Test123");
|
|
await page.getByRole("button", { name: "Log in" }).click();
|
|
await page.waitForURL((url) => !url.pathname.startsWith("/login"), { timeout: 30_000 });
|
|
}
|
|
|
|
/** Document text as CodeMirror currently holds it (lines joined with newlines). */
|
|
async function readDoc(page: Page): Promise<string> {
|
|
return page.evaluate(() =>
|
|
Array.from(document.querySelectorAll(".cm-content .cm-line"))
|
|
.map((line) => line.textContent ?? "")
|
|
.join("\n"),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Screen centre of a character (plus its document offset), located through the
|
|
* rendered DOM, exactly like a human clicking that glyph would.
|
|
*/
|
|
async function locateCharacter(
|
|
page: Page,
|
|
needle: string,
|
|
offsetInNeedle: number,
|
|
): Promise<{ x: number; y: number; index: number }> {
|
|
const target = await page.evaluate(
|
|
({ needle: text, offsetInNeedle: offset }) => {
|
|
const lines = Array.from(document.querySelectorAll(".cm-content .cm-line"));
|
|
const joined = lines.map((line) => line.textContent ?? "").join("\n");
|
|
const index = joined.indexOf(text);
|
|
if (index < 0) throw new Error(`"${text}" not found in document`);
|
|
const absolute = index + offset;
|
|
|
|
// Map the absolute document offset onto (line, offset inside line).
|
|
let remaining = absolute;
|
|
for (const line of lines) {
|
|
const content = line.textContent ?? "";
|
|
if (remaining <= content.length) {
|
|
// Walk the line's text nodes (syntax highlighting splits them).
|
|
const walker = document.createTreeWalker(line, NodeFilter.SHOW_TEXT);
|
|
let seen = 0;
|
|
let node = walker.nextNode() as Text | null;
|
|
while (node) {
|
|
const length = node.textContent?.length ?? 0;
|
|
if (remaining <= seen + length) {
|
|
const local = Math.min(remaining - seen, Math.max(length - 1, 0));
|
|
const range = document.createRange();
|
|
range.setStart(node, local);
|
|
range.setEnd(node, Math.min(local + 1, length));
|
|
const rect = range.getBoundingClientRect();
|
|
return {
|
|
x: rect.left + rect.width / 2,
|
|
y: rect.top + rect.height / 2,
|
|
index: absolute,
|
|
};
|
|
}
|
|
seen += length;
|
|
node = walker.nextNode() as Text | null;
|
|
}
|
|
const fallback = line.getBoundingClientRect();
|
|
return { x: fallback.left + 4, y: fallback.top + 8, index: absolute };
|
|
}
|
|
remaining -= content.length + 1; // + newline separator
|
|
}
|
|
throw new Error("offset outside document");
|
|
},
|
|
{ needle, offsetInNeedle },
|
|
);
|
|
|
|
const clipped = await page.evaluate(
|
|
({ x, y }) => ({ x, y, width: window.innerWidth, height: window.innerHeight }),
|
|
{ x: target.x, y: target.y },
|
|
);
|
|
expect(clipped.x).toBeGreaterThan(0);
|
|
expect(clipped.y).toBeGreaterThan(0);
|
|
return target;
|
|
}
|
|
|
|
async function clickAndMark(page: Page, needle: string, offset: number, marker: string) {
|
|
const spot = await locateCharacter(page, needle, offset);
|
|
await page.mouse.click(spot.x, spot.y);
|
|
await page.keyboard.type(marker);
|
|
const doc = await readDoc(page);
|
|
const markerIndex = doc.indexOf(marker);
|
|
expect(
|
|
markerIndex,
|
|
`marker ${marker} landed at ${markerIndex}, clicked offset was ${spot.index}`,
|
|
).toBe(spot.index);
|
|
return doc;
|
|
}
|
|
|
|
test.describe("wiki editor caret geometry", () => {
|
|
test.beforeEach(async ({ page }) => {
|
|
await page.addInitScript(() => {
|
|
window.localStorage.setItem("ptf:wiki-standards-accepted", "1");
|
|
});
|
|
await login(page);
|
|
});
|
|
|
|
test("clicks land on the glyph under the pointer across wrapped lines, lists and long tokens", async ({
|
|
page,
|
|
}) => {
|
|
await page.goto("/wiki/new");
|
|
const editor = page.locator(".cm-content");
|
|
await expect(editor).toBeVisible({ timeout: 60_000 });
|
|
|
|
await editor.click();
|
|
await page.keyboard.insertText(SAMPLE);
|
|
await expect(page.locator(".cm-content .cm-line").first()).toContainText("Introduction");
|
|
|
|
// 1. A glyph on a soft-wrapped continuation line of the first paragraph.
|
|
await clickAndMark(page, "editor column", 2, "ZQ1");
|
|
|
|
// 2. A glyph inside the second list item (line-based structure).
|
|
await clickAndMark(page, "item two", 4, "ZQ2");
|
|
|
|
// 3. A glyph inside the image markdown URL token.
|
|
await clickAndMark(page, "outpost_diagram", 3, "ZQ3");
|
|
|
|
// 4. The end of the document must be reachable and editable.
|
|
await page.locator(".cm-content").click();
|
|
await page.keyboard.press("Control+End");
|
|
await page.keyboard.type("ENDMARK");
|
|
const finalDoc = await readDoc(page);
|
|
expect(finalDoc.trimEnd().endsWith("ENDMARK")).toBe(true);
|
|
});
|
|
|
|
test("clicks stay aligned after scrolling a long document", async ({ page }) => {
|
|
await page.goto("/wiki/new");
|
|
const editor = page.locator(".cm-content");
|
|
await expect(editor).toBeVisible({ timeout: 60_000 });
|
|
|
|
const long = Array.from(
|
|
{ length: 60 },
|
|
(_, i) => `Paragraph ${i + 1} with wrapping words ${"and more ".repeat(6)}`,
|
|
).join("\n\n");
|
|
await editor.click();
|
|
await page.keyboard.insertText(long);
|
|
|
|
await page.locator(".cm-scroller").evaluate((el) => {
|
|
el.scrollTop = el.scrollHeight;
|
|
});
|
|
await page.waitForTimeout(250);
|
|
|
|
const lastParagraph = "Paragraph 60 with wrapping words";
|
|
await clickAndMark(page, lastParagraph, 12, "ZQ4");
|
|
});
|
|
});
|