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>
53 lines
2.1 KiB
TypeScript
53 lines
2.1 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { minimalEditorChange } from "@/lib/wiki/editorChange";
|
|
|
|
function apply(previous: string, change: ReturnType<typeof minimalEditorChange>): string {
|
|
return previous.slice(0, change.from) + change.insert + previous.slice(change.to);
|
|
}
|
|
|
|
describe("minimalEditorChange", () => {
|
|
it("returns a no-op change for identical documents", () => {
|
|
expect(minimalEditorChange("same", "same")).toEqual({ from: 0, to: 0, insert: "" });
|
|
});
|
|
|
|
it("only touches the appended tail", () => {
|
|
const change = minimalEditorChange("alpha", "alpha beta");
|
|
expect(change).toEqual({ from: 5, to: 5, insert: " beta" });
|
|
expect(apply("alpha", change)).toBe("alpha beta");
|
|
});
|
|
|
|
it("only touches the prepended head", () => {
|
|
const change = minimalEditorChange("world", "hello world");
|
|
expect(change.from).toBe(0);
|
|
expect(apply("world", change)).toBe("hello world");
|
|
});
|
|
|
|
it("replaces just the differing middle span", () => {
|
|
const previous = "line one\nline two\nline three";
|
|
const next = "line one\nline TWO\nline three";
|
|
const change = minimalEditorChange(previous, next);
|
|
expect(change).toEqual({ from: 14, to: 17, insert: "TWO" });
|
|
expect(apply(previous, change)).toBe(next);
|
|
});
|
|
|
|
it("handles full replacement and emptying", () => {
|
|
expect(apply("abc", minimalEditorChange("abc", ""))).toBe("");
|
|
expect(apply("", minimalEditorChange("", "abc"))).toBe("abc");
|
|
expect(apply("abc", minimalEditorChange("abc", "xyz"))).toBe("xyz");
|
|
});
|
|
|
|
it("keeps multi-byte characters intact", () => {
|
|
const previous = "café ☕ planned";
|
|
const next = "café ☕ shipped";
|
|
expect(apply(previous, minimalEditorChange(previous, next))).toBe(next);
|
|
});
|
|
|
|
it("round-trips a wrapping edit without touching surrounding text", () => {
|
|
const body = "some **bold** words here";
|
|
const update = "some ****bold**** words here";
|
|
const change = minimalEditorChange(body, update);
|
|
expect(change.from).toBe(7);
|
|
expect(change.insert.startsWith("**")).toBe(true);
|
|
expect(apply(body, change)).toBe(update);
|
|
});
|
|
});
|