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>
68 lines
2.6 KiB
TypeScript
68 lines
2.6 KiB
TypeScript
import {
|
|
IMAGE_SNIPPET,
|
|
INFOBOX_SNIPPET,
|
|
TABLE_SNIPPET,
|
|
THREE_COLUMN_SNIPPET,
|
|
TWO_COLUMN_SNIPPET,
|
|
createFormatUpdate,
|
|
createIndentUpdate,
|
|
createLinePrefixUpdate,
|
|
createListContinueUpdate,
|
|
createReferenceUpdate,
|
|
createSnippetUpdate,
|
|
createWrappedUpdate,
|
|
type BodySelection,
|
|
type BodyUpdate,
|
|
} from "@/lib/wiki/editorFormatting";
|
|
|
|
/**
|
|
* Pure markdown editing commands shared by the wiki toolbar and the editor
|
|
* keymap. Each takes the current document plus selection and returns the
|
|
* resulting document plus where the selection should land, or null when the
|
|
* command does not apply (for example Enter outside a list item).
|
|
*
|
|
* Keeping these independent of CodeMirror means the same functions back the
|
|
* toolbar buttons, the keyboard shortcuts and the unit tests.
|
|
*/
|
|
export type MarkdownCommand = (body: string, selection: BodySelection) => BodyUpdate | null;
|
|
|
|
function wrap(prefix: string, suffix: string, placeholder: string): MarkdownCommand {
|
|
return (body, selection) => createWrappedUpdate(body, selection, { prefix, suffix, placeholder });
|
|
}
|
|
|
|
function prefixLines(prefix: string): MarkdownCommand {
|
|
return (body, selection) => createLinePrefixUpdate(body, selection, prefix);
|
|
}
|
|
|
|
function snippet(insertion: string, placeholder?: string): MarkdownCommand {
|
|
return (body, selection) => createSnippetUpdate(body, selection, { insertion, placeholder });
|
|
}
|
|
|
|
function reference(definitionSuffix: string): MarkdownCommand {
|
|
return (body, selection) => createReferenceUpdate(body, selection, { definitionSuffix });
|
|
}
|
|
|
|
export const markdownCommands = {
|
|
bold: wrap("**", "**", "bold text"),
|
|
italic: wrap("*", "*", "italic text"),
|
|
strikethrough: wrap("~~", "~~", "strikethrough text"),
|
|
inlineCode: wrap("`", "`", "code"),
|
|
codeBlock: wrap("```\n", "\n```", "code block"),
|
|
heading: prefixLines("## "),
|
|
bulletList: prefixLines("- "),
|
|
numberedList: prefixLines("1. "),
|
|
quote: prefixLines("> "),
|
|
format: (body, selection) => createFormatUpdate(body, selection),
|
|
indent: (body, selection) => createIndentUpdate(body, selection, false),
|
|
outdent: (body, selection) => createIndentUpdate(body, selection, true),
|
|
listContinue: createListContinueUpdate,
|
|
table: snippet(TABLE_SNIPPET, "Header"),
|
|
captionedImage: snippet(IMAGE_SNIPPET, "Alt text"),
|
|
infobox: snippet(INFOBOX_SNIPPET, "Infobox title"),
|
|
twoColumns: snippet(TWO_COLUMN_SNIPPET, "Left column content"),
|
|
threeColumns: snippet(THREE_COLUMN_SNIPPET, "Left column content"),
|
|
footnote: reference(""),
|
|
citation: reference('Author, "Title", <URL>'),
|
|
} satisfies Record<string, MarkdownCommand>;
|
|
|
|
export type MarkdownCommandName = keyof typeof markdownCommands;
|