Skip to content
TypeScript

fix(web): make styled composer Markdown markers editable (#30421)

shivamaf54fe234426
diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx
index c59724cccf6..925319680d4 100644
--- a/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx
+++ b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx
@@ -9,13 +9,15 @@ import {
import type { ServerProviderSkill } from "@t3tools/contracts";
import type { Editor } from "@tiptap/core";
+import { redoDepth, undoDepth } from "@tiptap/pm/history";
+import { NodeSelection } from "@tiptap/pm/state";
import { createElement, createRef, useEffect, useState } from "react";
import { describe, expect, it, vi } from "vite-plus/test";
import { userEvent } from "vite-plus/test/browser";
import { act, fakeTimers, render } from "../../test/render";
-import { serializeEditorDoc } from "~/composer-rich-text-doc";
+import { flatToMarkdown, pmToFlat, serializeEditorDoc } from "~/composer-rich-text-doc";
import { isMacPlatform } from "~/lib/utils";
import {
ComposerPromptEditorTiptap as ComposerPromptEditor,
@@ -149,24 +151,67 @@ it.each([
expect(textWithMark(editor, "code")).toEqual(code);
});
-it("bolds with Mod+B only when text is selected", async () => {
- await using screen = await mountEditor("ship it");
- const { editor } = screen;
- const modB = isMacPlatform(navigator.platform) ? "{Meta>}b{/Meta}" : "{Control>}b{/Control}";
- await act(async () => {
- editor.commands.focus("end");
- });
- await vi.waitFor(() => expect(document.activeElement).toBe(editor.view.dom));
- await userEvent.keyboard(modB);
- // Without the guard, Tiptap stores bold for the next typed text.
- expect(editor.state.storedMarks).toBeNull();
+it.each([
+ { word: "ship", prefix: "", source: ["**ship**"] },
+ { word: "path\\", prefix: "", source: [] },
+ { word: "2*3", prefix: "", source: [] },
+ { word: "b", prefix: "*", source: [] },
+ { word: "word", prefix: "\\\\*", source: [] },
+ { word: "word", prefix: "\\\\**", source: [] },
+ { word: "word", prefix: "\\", source: [] },
+ { word: "word", prefix: "\\\\", source: ["**word**"] },
+ { word: "word", prefix: "\\\\\\", source: [] },
+ {
+ word: "word",
+ prefix: "`path\\`",
+ prefixText: "path\\",
+ source: ["**word**"],
+ code: ["path\\"],
+ },
+])(
+ "bolds $word after '$prefix' with Mod+B only when text is selected",
+ async ({ word, prefix, prefixText = prefix, source, code = [] }) => {
+ const value = `${prefix}${word} it`;
+ await using screen = await mountEditor(value);
+ const { editor } = screen;
+ const modB = isMacPlatform(navigator.platform) ? "{Meta>}b{/Meta}" : "{Control>}b{/Control}";
+ await act(async () => {
+ editor.commands.focus("end");
+ });
+ await vi.waitFor(() => expect(document.activeElement).toBe(editor.view.dom));
+ await userEvent.keyboard(modB);
+ // Without the guard, Tiptap stores bold for the next typed text.
+ expect(editor.state.storedMarks).toBeNull();
- await act(async () => {
- editor.commands.setTextSelection({ from: 1, to: 5 });
- });
- await userEvent.keyboard(modB);
- await vi.waitFor(() => expect(textWithMark(editor, "bold")).toEqual(["ship"]));
-});
+ await act(async () => {
+ editor.commands.setTextSelection({
+ from: prefixText.length + 1,
+ to: prefixText.length + word.length + 1,
+ });
+ });
+ await userEvent.keyboard(modB);
+ await vi.waitFor(() =>
+ expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}**${word}** it`),
+ );
+ expect(textWithMark(editor, "markdownSource")).toEqual(source);
+ const depth = undoDepth(editor.state);
+ await userEvent.keyboard("{End}");
+ expect(serializeEditorDoc(editor.state.doc).value).toBe(`${prefix}**${word}** it`);
+ expect(textWithMark(editor, "bold")).toEqual([word]);
+ expect(textWithMark(editor, "code")).toEqual(code);
+ expect(undoDepth(editor.state)).toBe(depth);
+ await act(async () => {
+ editor.commands.setTextSelection({
+ from: prefixText.length + 1,
+ to: prefixText.length + word.length + 1,
+ });
+ });
+
+ // Whether the selection spans shown markers or not, Mod+B removes the bold.
+ await userEvent.keyboard(modB);
+ await vi.waitFor(() => expect(serializeEditorDoc(editor.state.doc).value).toBe(value));
+ },
+);
it.each([false, true])(
"composes an umlaut without wrapping the preedit text (rich text: %s)",
@@ -199,6 +244,62 @@ it("lets composition replace selected text without surrounding it", async () =>
expect(serializeEditorDoc(editor.state.doc).value).toBe('"');
});
+it.each([
+ { at: "inside bold", selection: 5, composed: "**bo漢ld** after", bold: ["bo漢ld"] },
+ {
+ at: "over the opening markers",
+ selection: { from: 1, to: 3 },
+ composed: "漢bold** after",
+ bold: [],
+ },
+ { at: "after the closing markers", selection: 9, composed: "**bold**漢 after", bold: ["bold"] },
+])("undoes composition $at in revealed Markdown through the earlier paste", async (row) => {
+ await using screen = await mountEditor("");
+ const { editor } = screen;
+ const value = () => serializeEditorDoc(editor.state.doc).value;
+ await act(async () => {
+ editor.view.focus();
+ const clipboardData = new DataTransfer();
+ clipboardData.setData("text/plain", "**bold** after");
+ editor.view.dom.dispatchEvent(new ClipboardEvent("paste", { clipboardData, cancelable: true }));
+ editor.commands.insertContent("!");
+ editor.commands.undo();
+ editor.commands.setTextSelection(3);
+ });
+ expect(value()).toBe("**bold** after");
+ expect(textWithMark(editor, "markdownSource")).toEqual(["**bold**"]);
+ await act(async () => {
+ editor.commands.setTextSelection(row.selection);
+ // A composition canceled before it edits anything keeps the redo and adds no undo.
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionstart"));
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionend", { data: "" }));
+ });
+ expect([undoDepth(editor.state), redoDepth(editor.state)]).toEqual([1, 1]);
+ await act(async () => {
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionstart"));
+ // ProseMirror numbers each composition; the canceled one was 1.
+ editor.view.dispatch(editor.state.tr.insertText("漢").setMeta("composition", 2));
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionend", { data: "漢" }));
+ editor.commands.setTextSelection(editor.state.doc.content.size - 1);
+ });
+ expect(value()).toBe(row.composed);
+ await act(async () => {
+ editor.commands.undo();
+ });
+ expect(value()).toBe("**bold** after");
+ await act(async () => {
+ editor.commands.undo();
+ });
+ expect(value()).toBe("");
+ await act(async () => {
+ editor.commands.redo();
+ editor.commands.redo();
+ editor.commands.setTextSelection(editor.state.doc.content.size - 1);
+ });
+ expect(value()).toBe(row.composed);
+ expect(textWithMark(editor, "bold")).toEqual(row.bold);
+});
+
it("keeps unknown dollar names editable when restoring and pasting a draft", async () => {
const value = "echo $HOME $PATH $anything $notaskill $20 $20k $babysit ";
await using screen = await mountEditor(value, true, [babysit]);
@@ -239,6 +340,256 @@ it("keeps the caret inside unknown dollar text across rich and plain remounts",
expect(serializeEditorDoc(editor.state.doc).value).toBe("echo $HOXYME now");
});
+async function focusAt(editor: Editor, position: number | "end") {
+ // ProseMirror rewrites the DOM caret 20ms after focus. Run that now, or it
+ // can undo the first key before ProseMirror reads the native move.
+ using _timers = fakeTimers();
+ await act(async () => {
+ editor.commands.focus(position);
+ });
+ await vi.waitFor(() => expect(document.activeElement).toBe(editor.view.dom));
+ vi.runOnlyPendingTimers();
+}
+
+describe("Markdown markers around styled text", () => {
+ it.each([
+ { keys: "{Backspace}", expected: "**** after" },
+ { keys: "X", expected: "**X** after" },
+ ])("keeps a styled chip selected for $keys when showing source", async ({ keys, expected }) => {
+ await using screen = await mountEditor("$babysit after", true, [babysit]);
+ const { editor } = screen;
+ await focusAt(editor, "end");
+ await act(async () => {
+ editor.view.dispatch(editor.state.tr.addMark(1, 2, editor.schema.marks.bold!.create()));
+ });
+ expect(editor.state.doc.nodeAt(1)?.type.name).toBe("composer-skill");
+ await act(async () => {
+ editor.commands.setNodeSelection(1);
+ });
+ expect(editor.state.selection).toBeInstanceOf(NodeSelection);
+ expect((editor.state.selection as NodeSelection).node.type.name).toBe("composer-skill");
+ await userEvent.keyboard(keys);
+ await userEvent.keyboard("{End}");
+ await vi.waitFor(() => expect(serializeEditorDoc(editor.state.doc).value).toBe(expected));
+ });
+
+ it.each([
+ {
+ name: "deleting the opening markers removes bold",
+ value: "**bold** after",
+ caret: 1,
+ keys: "{Delete}{Delete}",
+ expected: "bold** after",
+ marks: {},
+ },
+ {
+ name: "turning ** into * makes it italic",
+ value: "**bold** after",
+ caret: 1,
+ keys: "{Delete}{ArrowRight}{ArrowRight}{ArrowRight}{ArrowRight}{ArrowRight}{Delete}",
+ expected: "*bold* after",
+ marks: { italic: ["bold"] },
+ },
+ {
+ name: "backspacing from the closing edge keeps italic",
+ value: "**bold** after",
+ caret: 1,
+ keys: async (editor: Editor) => {
+ await userEvent.keyboard("{Delete}");
+ await vi.waitFor(() => expect(editor.state.doc.textContent).toBe("*bold** after"));
+ for (let position = 2; position <= 8; position += 1) {
+ await userEvent.keyboard("{ArrowRight}");
+ await vi.waitFor(() => expect(editor.state.selection.from).toBe(position));
+ }
+ await userEvent.keyboard("{Backspace}");
+ await vi.waitFor(() =>
+ expect(serializeEditorDoc(editor.state.doc).value).toBe("*bold* after"),
+ );
+ },
+ expected: "*bold* after",
+ marks: { italic: ["bold"] },
+ },
+ {
+ name: "retyping deleted closing markers restores bold",
+ value: "**bold** after",
+ caret: 3,
+ keys: "{ArrowRight}{ArrowRight}{Delete}{Delete}**",
+ expected: "**bold** after",
+ marks: { bold: ["bold"] },
+ },
+ {
+ name: "deleting a backtick removes inline code",
+ value: "`code` after",
+ caret: 1,
+ keys: "{Delete}",
+ expected: "code` after",
+ marks: {},
+ },
+ {
+ name: "a line break inside bold keeps both halves bold",
+ value: "**bold** after",
+ caret: 3,
+ keys: "{Shift>}{Enter}{/Shift}",
+ expected: "**bo**\n**ld** after",
+ marks: { bold: ["bo", "ld"] },
+ },
+ {
+ name: "splitting a list item inside bold keeps both halves bold",
+ value: "- **bold** after",
+ caret: 5,
+ keys: "{Enter}",
+ expected: "- **bo**\n- **ld** after",
+ marks: { bold: ["bo", "ld"] },
+ },
+ ])("edits the markers as text: $name", async ({ value, caret, keys, expected, marks }) => {
+ await using screen = await mountEditor(value);
+ const { editor } = screen;
+ // Position 1 is the start of the styled word; 3 is two characters into it (5 in a list item).
+ await focusAt(editor, caret);
+ if (typeof keys === "string") await userEvent.keyboard(keys);
+ else await keys(editor);
+ await userEvent.keyboard("{End}");
+ await vi.waitFor(() => expect(serializeEditorDoc(editor.state.doc).value).toBe(expected));
+ for (const name of ["bold", "italic", "code"] as const) {
+ expect(textWithMark(editor, name)).toEqual(marks[name as keyof typeof marks] ?? []);
+ }
+ });
+
+ it("shows nested formatting as Markdown and restores it when the caret leaves", async () => {
+ const value = "**a *b* c** d";
+ await using screen = await mountEditor(value);
+ const { editor } = screen;
+ await focusAt(editor, "a b".length + 1);
+ expect(editor.state.doc.textContent).toBe(value);
+ expect(textWithMark(editor, "bold")).toEqual([]);
+ await userEvent.keyboard("{End}");
+ await vi.waitFor(() => expect(editor.state.doc.textContent).toBe("a b c d"));
+ expect(textWithMark(editor, "bold")).toEqual(["a ", "b", " c"]);
+ expect(textWithMark(editor, "italic")).toEqual(["b"]);
+ expect(serializeEditorDoc(editor.state.doc).value).toBe(value);
+ });
+
+ it("undoes a marker edit in one step without an extra step for showing markers", async () => {
+ await using screen = await mountEditor("**bold** after");
+ const { editor } = screen;
+ await focusAt(editor, "end");
+ await userEvent.keyboard("!");
+ await act(async () => {
+ editor.commands.setTextSelection(1);
+ });
+ await userEvent.keyboard("{Delete}{Delete}{End}");
+ const value = () => serializeEditorDoc(editor.state.doc).value;
+ await vi.waitFor(() => expect(value()).toBe("bold** after!"));
+ await act(async () => {
+ editor.commands.undo();
+ });
+ expect(value()).toBe("**bold** after!");
+ expect(textWithMark(editor, "markdownSource")).toEqual(["**bold**"]);
+ const end = () => editor.state.doc.content.size - 1;
+ // Undo restored the markers as text; leaving hides them without touching the text undo needs.
+ await act(async () => {
+ editor.commands.setTextSelection(end());
+ });
+ expect(value()).toBe("**bold** after!");
+ const hidden = editor.view.dom.querySelectorAll(".composer-rich-marker-hidden");
+ expect(Array.from(hidden, (marker) => marker.textContent)).toEqual(["**", "**"]);
+ await act(async () => {
+ editor.commands.undo();
+ });
+ expect(value()).toBe("**bold** after");
+ // Moving the caret into styled text keeps redo, and adds no undo step after it.
+ await act(async () => {
+ editor.commands.setTextSelection(3);
+ });
+ expect(editor.state.doc.textContent).toBe("**bold** after");
+ expect(textWithMark(editor, "markdownSource")).toEqual(["**bold**"]);
+ await act(async () => {
+ editor.commands.redo();
+ });
+ expect(value()).toBe("**bold** after!");
+ await act(async () => {
+ editor.commands.setTextSelection(1);
+ });
+ await act(async () => {
+ editor.commands.redo();
+ });
+ expect(value()).toBe("bold** after!");
+ expect(textWithMark(editor, "bold")).toEqual([]);
+ await act(async () => {
+ editor.commands.undo();
+ editor.commands.undo();
+ });
+ await act(async () => {
+ editor.commands.redo();
+ });
+ await act(async () => {
+ editor.commands.setTextSelection(editor.state.doc.content.size - 1);
+ });
+ await act(async () => {
+ editor.commands.undo();
+ });
+ // Undo puts the caret back inside the styled text.
+ expect(editor.state.doc.textContent).toBe("**bold** after");
+ await act(async () => {
+ editor.commands.setTextSelection(end());
+ });
+ expect(editor.state.doc.textContent).toBe("bold after");
+ expect(textWithMark(editor, "bold")).toEqual(["bold"]);
+ // After a completed redo, markers still follow the caret, and Delete edits the opening one.
+ await userEvent.keyboard("?");
+ await act(async () => {
+ editor.commands.undo();
+ });
+ await act(async () => {
+ editor.commands.redo();
+ });
+ expect(editor.state.doc.textContent).toBe("bold after?");
+ await act(async () => {
+ editor.commands.setTextSelection(1);
+ });
+ expect(editor.state.doc.textContent).toBe("**bold** after?");
+ expect(textWithMark(editor, "markdownSource")).toEqual(["**bold**"]);
+ await act(async () => {
+ editor.commands.setTextSelection(end());
+ });
+ expect(editor.state.doc.textContent).toBe("bold after?");
+ expect(textWithMark(editor, "bold")).toEqual(["bold"]);
+ await act(async () => {
+ editor.commands.setTextSelection(1);
+ });
+ await userEvent.keyboard("{Delete}");
+ expect(value()).toBe("*bold** after?");
+ await act(async () => {
+ editor.commands.undo();
+ });
+ expect(value()).toBe("**bold** after?");
+ expect(textWithMark(editor, "markdownSource")).toEqual(["**bold**"]);
+ await act(async () => {
+ editor.commands.setTextSelection(end());
+ });
+ expect(editor.state.doc.textContent).toBe("bold after?");
+ expect(textWithMark(editor, "bold")).toEqual(["bold"]);
+ });
+
+ it("keeps bold around a skill pasted inside it", async () => {
+ await using screen = await mountEditor("**helloworld** after", true, [babysit]);
+ const { editor } = screen;
+ await focusAt(editor, "hello".length + 1);
+ await act(async () => {
+ const clipboardData = new DataTransfer();
+ clipboardData.setData("text/plain", "$babysit");
+ editor.view.dom.dispatchEvent(
+ new ClipboardEvent("paste", { clipboardData, cancelable: true }),
+ );
+ });
+ await userEvent.keyboard("{End}");
+ await vi.waitFor(() =>
+ expect(serializeEditorDoc(editor.state.doc).value).toBe("**hello** $babysit **world** after"),
+ );
+ expect(textWithMark(editor, "bold")).toEqual(["hello", "world"]);
+ });
+});
+
const file = {
type: "file",
id: "file-1",
@@ -386,26 +737,26 @@ describe("an Arabic line", () => {
vi.runOnlyPendingTimers();
}
- it("leaves inline code through its start with ArrowRight, which points back", async () => {
- // Positions: اكتب at 1-5, sumBy at 6-11, الان at 12-16.
- await using screen = await mountEditor("اكتب `sumBy` الان");
+ it("walks back through inline code and its backticks with ArrowRight", async () => {
+ const value = "اكتب `sumBy` الان";
+ await using screen = await mountEditor(value);
const { editor } = screen;
await focusEnd(editor);
- const caret = () => ({
- head: editor.state.selection.head,
- inCode: (editor.state.storedMarks ?? editor.state.selection.$head.marks()).length > 0,
- });
- const visited = [caret()];
- while (visited.at(-1)!.head > 5) {
+ const offset = () => {
+ const map = serializeEditorDoc(editor.state.doc);
+ return flatToMarkdown(map, pmToFlat(map, editor.state.selection.head));
+ };
+ const visited = [offset()];
+ let sawSource = false;
+ while (visited.at(-1)! > "اكتب".length) {
const before = visited.at(-1)!;
await userEvent.keyboard("{ArrowRight}");
- await vi.waitFor(() => {
- const after = caret();
- expect(after.head < before.head || after.inCode !== before.inCode).toBe(true);
- });
- visited.push(caret());
+ await vi.waitFor(() => expect(offset()).toBe(before - 1));
+ sawSource ||= editor.state.doc.textContent === value;
+ visited.push(offset());
}
- expect(visited.some((stop) => stop.inCode)).toBe(true);
+ expect(sawSource).toBe(true);
+ expect(textWithMark(editor, "code")).toEqual(["sumBy"]);
});
it("shows the backticks on either side of inline code it is editing", async () => {
@@ -415,14 +766,17 @@ describe("an Arabic line", () => {
editor.commands.setTextSelection("اكتب su".length + 1);
});
const code = editor.view.dom.querySelector("code")!;
- const word = document.createRange();
- word.selectNodeContents([...code.childNodes].find((node) => node instanceof Text)!);
- const text = word.getBoundingClientRect();
- const [open, close] = [...editor.view.dom.querySelectorAll(".composer-rich-marker")].map(
- (marker) => marker.getBoundingClientRect(),
- );
- expect(open!.right).toBeLessThanOrEqual(text.left + 1);
- expect(close!.left).toBeGreaterThanOrEqual(text.right - 1);
+ const text = [...code.childNodes].find((node) => node instanceof Text)!;
+ expect(text.data).toBe("`sumBy`");
+ const rect = (from: number, to: number) => {
+ const range = document.createRange();
+ range.setStart(text, from);
+ range.setEnd(text, to);
+ return range.getBoundingClientRect();
+ };
+ const [open, word, close] = [rect(0, 1), rect(1, 6), rect(6, 7)];
+ expect(open.right).toBeLessThanOrEqual(word.left + 1);
+ expect(close.left).toBeGreaterThanOrEqual(word.right - 1);
});
it("starts the next line on the right", async () => {
diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx
index 3df1e476133..8c4698210b1 100644
--- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx
+++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx
@@ -12,7 +12,6 @@ import {
} from "@tiptap/core";
import { ReactNodeViewRenderer, NodeViewWrapper, type NodeViewProps } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
-import { type Node as ProseMirrorNode } from "@tiptap/pm/model";
import { newlineInCode, splitBlockKeepMarks } from "@tiptap/pm/commands";
import { isHistoryTransaction } from "@tiptap/pm/history";
import {
@@ -59,6 +58,11 @@ import {
} from "~/composer-logic";
import { useStableCallback } from "~/hooks/useStableCallback";
import { caretLineDirection, ComposerTextDirectionExtension } from "~/composer-text-direction";
+import {
+ collapseShownMarkdownSource,
+ ComposerMarkdownSourceExtension,
+} from "~/composer-markdown-source";
+import { MARKDOWN_SOURCE_MARK } from "~/composer-rich-text";
import {
collectComposerPromptInlineTokens,
selectionTouchesMentionBoundary,
@@ -69,7 +73,6 @@ import {
ComposerCodeBlockExtension,
ComposerListExtensions,
buildTiptapContent,
- caretTakesMarksBefore,
ComposerBoldSelectionOnlyExtension,
convertBulletItemToTask,
ComposerCodeExtension,
@@ -524,46 +527,6 @@ function ComposerContextReferenceNodeView({ node }: NodeViewProps) {
);
}
-type StyledRange = {
- from: number;
- to: number;
- markers: { at: number; side: number; text: string }[];
-};
-
-function collectStyledRanges(doc: ProseMirrorNode): StyledRange[] {
- const ranges: StyledRange[] = [];
- const map = serializeEditorDoc(doc);
- let range: StyledRange | null = null;
- let openLength = 0;
- for (const run of map.runs) {
- if (run.nodeName === "codeBlock") continue;
- if (run.openLen > 0) {
- range ??= { from: run.pmPos, to: run.pmPos, markers: [] };
- range.markers.push({
- at: run.pmPos,
- side: -1,
- text: map.value.slice(run.mdStart, run.mdStart + run.openLen),
- });
- }
- if (range === null) continue;
- range.to = run.pmPos + run.docLen;
- if (run.closeLen > 0) {
- const end = run.mdStart + run.mdLen;
- range.markers.push({
- at: range.to,
- side: -2,
- text: map.value.slice(end - run.closeLen, end),
- });
- }
- openLength += run.openLen - run.closeLen;
- if (openLength === 0) {
- ranges.push(range);
- range = null;
- }
- }
- return ranges;
-}
-
/** Wait for a character after '- ' so task markers reach their input rule before becoming bullets. */
function listMarkerInputRule(find: RegExp, listType: "bulletList" | "orderedList"): InputRule {
return new InputRule({
@@ -684,75 +647,41 @@ function isInCodeBlock(view: EditorView): boolean {
return view.state.selection.$from.parent.type.spec.code === true;
}
-const MarkerPluginKey = new PluginKey("composer-rich-markers");
+const ChipSelectionPluginKey = new PluginKey<DecorationSet>("composer-chip-range-selection");
-const ComposerMarkerPlugin = new Plugin({
- key: MarkerPluginKey,
- state: {
- init: (_, state) => decorationsForSelection(state),
- apply: (tr, old, _, state) =>
- tr.docChanged || tr.selectionSet || tr.storedMarksSet ? decorationsForSelection(state) : old,
- },
- props: {
- decorations(state) {
- return MarkerPluginKey.getState(state);
- },
+/** Highlights chips inside a text selection; their node views draw no native highlight. */
+const ComposerChipSelectionExtension = Extension.create({
+ name: "composer-chip-range-selection",
+ addProseMirrorPlugins() {
+ return [
+ new Plugin<DecorationSet>({
+ key: ChipSelectionPluginKey,
+ state: {
+ init: (_, state) => chipSelectionDecorations(state),
+ apply: (tr, old, _, state) =>
+ tr.docChanged || tr.selectionSet ? chipSelectionDecorations(state) : old,
+ },
+ props: {
+ decorations: (state) => ChipSelectionPluginKey.getState(state),
+ },
+ }),
+ ];
},
});
-function decorationsForSelection(state: EditorState): DecorationSet {
- const { doc, selection } = state;
- const caretSide = caretTakesMarksBefore(state) ? 3 : 0;
- const rtl = caretLineDirection(state) === "rtl";
+function chipSelectionDecorations({ doc, selection }: EditorState): DecorationSet {
+ if (selection.empty) return DecorationSet.empty;
const decorations: Decoration[] = [];
- if (!selection.empty) {
- doc.nodesBetween(selection.from, selection.to, (node, pos) => {
- if (node.type.name.startsWith("composer-")) {
- decorations.push(
- Decoration.node(pos, pos + node.nodeSize, { class: "composer-chip-range-selected" }),
- );
- return false;
- }
- return true;
- });
- }
- for (const range of collectStyledRanges(doc)) {
- const active = selection.empty
- ? selection.from >= range.from && selection.from <= range.to
- : selection.from < range.to && selection.to > range.from;
- if (!active) continue;
- for (const { at, side: baseSide, text } of range.markers) {
- const side = selection.empty && at === selection.from ? baseSide + caretSide : baseSide;
- const marker = document.createElement("span");
- marker.className = "composer-rich-marker";
- marker.textContent = text;
- marker.setAttribute("aria-hidden", "true");
- // Inline code is isolated left to right. In a right-to-left line its
- // opening backtick joins the closing one inside the isolate, or the line
- // would put both on the same side of the code.
- const codeMarks =
- rtl && baseSide === -1 && /^`+$/.test(text)
- ? doc.resolve(at).nodeAfter?.marks.filter((mark) => mark.type.spec.code)
- : undefined;
- decorations.push(
- Decoration.widget(at, marker, {
- side,
- key: `marker-${at}-${side}-${marker.textContent}`,
- ...(codeMarks?.length ? { marks: codeMarks } : {}),
- }),
- );
- }
- }
+ doc.nodesBetween(selection.from, selection.to, (node, pos) => {
+ if (!node.type.name.startsWith("composer-")) return true;
+ decorations.push(
+ Decoration.node(pos, pos + node.nodeSize, { class: "composer-chip-range-selected" }),
+ );
+ return false;
+ });
return DecorationSet.create(doc, decorations);
}
-const ComposerMarkersExtension = Extension.create({
- name: "composer-rich-markers",
- addProseMirrorPlugins() {
- return [ComposerMarkerPlugin];
- },
-});
-
type TiptapEditor = NonNullable<ReturnType<typeof useEditor>>;
function replaceSkillChips(
@@ -842,9 +771,10 @@ function createComposerExtensions(richText: boolean) {
ComposerSkillExtension,
ComposerCitationExtension,
ComposerContextReferenceExtension,
- ComposerMarkersExtension,
+ ComposerChipSelectionExtension,
...(richText
? [
+ ComposerMarkdownSourceExtension,
ComposerBoldSelectionOnlyExtension,
ComposerItalicExtension,
ComposerBoldExtension,
@@ -1291,7 +1221,11 @@ function ComposerPromptEditorTiptapInner(
return true;
}
event.preventDefault();
- if ((isTaskItem || isListItem) && instance && splitOrLiftListItem(instance)) {
+ if (
+ (isTaskItem || isListItem) &&
+ instance &&
+ splitOrLiftListItem(instance, collapseShownMarkdownSource)
+ ) {
return true;
}
if (
@@ -1303,6 +1237,15 @@ function ComposerPromptEditorTiptapInner(
) {
return true;
}
+ // Parse shown Markdown first, or the split leaves each half with one delimiter.
+ if (instance) {
+ return instance
+ .chain()
+ .command(collapseShownMarkdownSource)
+ .command(({ state, dispatch }) => splitBlockKeepMarks(state, dispatch))
+ .scrollIntoView()
+ .run();
+ }
return splitBlockKeepMarks(view.state, (tr) => {
view.dispatch(tr.scrollIntoView());
});
@@ -1330,11 +1273,12 @@ function ComposerPromptEditorTiptapInner(
return false;
}
// Do not wrap chips, atoms, or styled text; default replacement preserves marks.
+ // Shown Markdown source is plain text, so typed delimiters wrap it.
let touchesSpecial = false;
view.state.doc.nodesBetween(from, to, (node) => {
if (
(node.isAtom && node.isInline && !node.isText) ||
- (node.isText && node.marks.length > 0)
+ (node.isText && node.marks.some((mark) => mark.type.name !== MARKDOWN_SOURCE_MARK))
) {
touchesSpecial = true;
return false;
@@ -1368,6 +1312,22 @@ function ComposerPromptEditorTiptapInner(
);
return true;
}
+ // Shown Markdown source is parsed when the caret leaves, so paste one line into it as text.
+ const sourceType = view.state.schema.marks[MARKDOWN_SOURCE_MARK];
+ const { from: pasteFrom, to: pasteTo, $from: $pasteFrom } = view.state.selection;
+ if (
+ sourceType?.isInSet($pasteFrom.marks()) &&
+ !pastedText.includes("\n") &&
+ collectComposerPromptInlineTokens(`${pastedText}\n`).length === 0
+ ) {
+ view.dispatch(
+ markAsClipboardEdit(
+ view.state.tr.insertText(pastedText, pasteFrom, pasteTo),
+ "paste",
+ ).scrollIntoView(),
+ );
+ return true;
+ }
const importFragment = readCallbacks().importContextFragment;
let text =
!literalText && importFragment
@@ -1408,6 +1368,8 @@ function ComposerPromptEditorTiptapInner(
markAsClipboardEdit(tr, "paste");
return true;
})
+ // Pasted chips and lines would split shown Markdown, so parse it first.
+ .command(collapseShownMarkdownSource)
.insertContent(content)
.command(({ tr }) => {
// Move after a pasted rule so the next keystroke cannot replace it; add a trailing line
@@ -1562,9 +1524,8 @@ function ComposerPromptEditorTiptapInner(
},
);
}
+ selectMarkdownOffset(editor, normalizedExpandedCursor);
const map = serializeEditorDoc(editor.state.doc);
- const flat = markdownToFlat(map, normalizedExpandedCursor);
- editor.commands.setTextSelection(flatToPm(map, flat));
if (isFocused) scrollTiptapCaretIntoView(editor);
if (pendingCitation) {
citationRequestRef.current = null;
@@ -1633,8 +1594,7 @@ function ComposerPromptEditorTiptapInner(
const boundedCursor = clampEditorCursor(snapshotRef.current.value, nextCursor);
const expandedCursor =
exactExpandedCursor ?? expandEditorCursor(snapshotRef.current.value, boundedCursor);
- const map = serializeEditorDoc(editor.state.doc);
- editor.commands.setTextSelection(flatToPm(map, markdownToFlat(map, expandedCursor)));
+ selectMarkdownOffset(editor, expandedCursor);
scrollTiptapCaretIntoView(editor);
if (boundedCursor === snapshotRef.current.cursor) return;
snapshotRef.current = {
@@ -1852,6 +1812,17 @@ function insertMarkdownParagraphs(
insertContent(blocks as JSONContent[]);
}
+/** Placing the caret can show Markdown markers, so place it again in the shown source. */
+function selectMarkdownOffset(editor: TiptapEditor, offset: number): void {
+ for (let pass = 0; pass < 2; pass += 1) {
+ const map = serializeEditorDoc(editor.state.doc);
+ const pos = flatToPm(map, markdownToFlat(map, offset));
+ const { selection } = editor.state;
+ if (pass > 0 && selection.empty && selection.from === pos) return;
+ editor.commands.setTextSelection(pos);
+ }
+}
+
/** Native scrolling follows only real input; explicitly reveal programmatically placed carets. */
function scrollTiptapCaretIntoView(editor: TiptapEditor): void {
editor.view.dispatch(editor.state.tr.scrollIntoView());
diff --git a/apps/web/src/composer-markdown-source.ts b/apps/web/src/composer-markdown-source.ts
new file mode 100644
index 00000000000..11ac9fdb95b
--- /dev/null
+++ b/apps/web/src/composer-markdown-source.ts
@@ -0,0 +1,575 @@
+import { type Command, Extension, Mark as TiptapMark } from "@tiptap/core";
+import {
+ closeHistory,
+ isHistoryTransaction,
+ redo,
+ redoDepth,
+ undo,
+ undoDepth,
+} from "@tiptap/pm/history";
+import type { MarkType, Node as ProseMirrorNode } from "@tiptap/pm/model";
+import {
+ AllSelection,
+ type EditorState,
+ NodeSelection,
+ Plugin,
+ PluginKey,
+ type Selection,
+ TextSelection,
+ type Transaction,
+} from "@tiptap/pm/state";
+import { type Mapping, StepMap } from "@tiptap/pm/transform";
+import { Decoration, DecorationSet, type EditorView } from "@tiptap/pm/view";
+
+import {
+ inlineMarkdownUnitMarks,
+ MARKDOWN_SOURCE_MARK,
+ type RichTextMark,
+} from "~/composer-rich-text";
+import { caretTakesMarksBefore, serializeEditorDoc } from "~/composer-rich-text-doc";
+
+// Styled text the selection touches becomes source text with the source mark,
+// parsed back once the selection leaves. History steps only replay on the shape
+// they were recorded in, so the history records both rewrites. Recording would
+// clear redo after undo or redo, so until the next edit see `normalizeDetached`.
+
+const STYLE_MARKS: RichTextMark[] = ["bold", "italic", "strike", "code"];
+const PREVIEW_TAGS: Record<Exclude<RichTextMark, "code">, string> = {
+ bold: "strong",
+ italic: "em",
+ strike: "s",
+};
+const ATOM_TEXT = "";
+const DELIMITER = /[*_~`]/;
+
+type TextRange = { from: number; to: number };
+
+type StyledRange = TextRange & {
+ escapedOpener: boolean;
+ markers: { at: number; closing: boolean; text: string }[];
+};
+
+function collectStyledRanges(doc: ProseMirrorNode): StyledRange[] {
+ const ranges: StyledRange[] = [];
+ const map = serializeEditorDoc(doc);
+ let range: StyledRange | null = null;
+ let openLength = 0;
+ for (const run of map.runs) {
+ if (run.nodeName === "codeBlock") continue;
+ if (run.openLen > 0) {
+ let backslashes = 0;
+ while (map.value[run.mdStart - backslashes - 1] === "\\") backslashes += 1;
+ range ??= {
+ from: run.pmPos,
+ to: run.pmPos,
+ escapedOpener: backslashes % 2 !== 0,
+ markers: [],
+ };
+ range.markers.push({
+ at: run.pmPos,
+ closing: false,
+ text: map.value.slice(run.mdStart, run.mdStart + run.openLen),
+ });
+ }
+ if (range === null) continue;
+ range.to = run.pmPos + run.docLen;
+ if (run.closeLen > 0) {
+ const end = run.mdStart + run.mdLen;
+ range.markers.push({
+ at: range.to,
+ closing: true,
+ text: map.value.slice(end - run.closeLen, end),
+ });
+ }
+ openLength += run.openLen - run.closeLen;
+ if (openLength === 0) {
+ ranges.push(range);
+ range = null;
+ }
+ }
+ return ranges;
+}
+
+/** Whether the Markdown that reveal shows for `range` parses back to the same styles. */
+function revealsFaithfully(doc: ProseMirrorNode, range: StyledRange): boolean {
+ if (range.escapedOpener) return false;
+ const adjacent = withAdjacentDelimiters(doc, range);
+ if (adjacent.from !== range.from || adjacent.to !== range.to) return false;
+ // Parsing keeps escapes as text, so styled `path\` or `2*3` stays styled, unshown.
+ const markers = range.markers.toSorted(
+ (left, right) => left.at - right.at || Number(right.closing) - Number(left.closing),
+ );
+ const expected: Array<RichTextMark[] | null> = [];
+ let text = "";
+ let next = 0;
+ const addMarkers = (pos: number) => {
+ for (; next < markers.length && markers[next]!.at <= pos; next++) {
+ text += markers[next]!.text;
+ expected.push(...markers[next]!.text.split("").map(() => null));
+ }
+ };
+ doc.nodesBetween(range.from, range.to, (node, pos) => {
+ if (!node.isInline) return true;
+ const marks = STYLE_MARKS.filter((name) => node.marks.some((mark) => mark.type.name === name));
+ const from = Math.max(pos, range.from);
+ const content = node.isText ? node.text!.slice(from - pos, range.to - pos) : ATOM_TEXT;
+ for (let index = 0; index < content.length; index++) {
+ addMarkers(from + index);
+ text += content[index];
+ expected.push(marks);
+ }
+ return false;
+ });
+ addMarkers(range.to);
+ return inlineMarkdownUnitMarks(text).every((marks, index) => sameMarks(marks, expected[index]!));
+}
+
+function touches(selection: Selection, { from, to }: TextRange): boolean {
+ return selection.empty
+ ? selection.from >= from && selection.from <= to
+ : selection.from < to && selection.to > from;
+}
+
+function sourceRegions(doc: ProseMirrorNode, type: MarkType): TextRange[] {
+ const regions: TextRange[] = [];
+ doc.descendants((node, pos) => {
+ if (!node.isTextblock) return true;
+ let current: TextRange | null = null;
+ node.forEach((child, offset) => {
+ const start = pos + 1 + offset;
+ if (!type.isInSet(child.marks)) {
+ current = null;
+ } else if (current) {
+ current.to = start + child.nodeSize;
+ } else {
+ current = { from: start, to: start + child.nodeSize };
+ regions.push(current);
+ }
+ });
+ return false;
+ });
+ return regions;
+}
+
+/** Takes in delimiters typed beside the source, like a closing `**` retyped after its text. */
+function withAdjacentDelimiters(doc: ProseMirrorNode, region: TextRange): TextRange {
+ const before = doc.resolve(region.from).nodeBefore;
+ const after = doc.resolve(region.to).nodeAfter;
+ let from = region.from;
+ let to = region.to;
+ if (before?.isText && before.marks.length === 0) {
+ const text = before.text!;
+ let count = 0;
+ while (count < text.length && DELIMITER.test(text[text.length - 1 - count]!)) count += 1;
+ // Only an odd backslash run escapes the first adjacent delimiter.
+ let backslashes = 0;
+ while (text[text.length - 1 - count - backslashes] === "\\") backslashes += 1;
+ if (count > 0 && backslashes % 2 === 1) count -= 1;
+ from -= count;
+ }
+ if (after?.isText && after.marks.length === 0) {
+ const text = after.text!;
+ let count = 0;
+ while (count < text.length && DELIMITER.test(text[count]!)) count += 1;
+ to += count;
+ }
+ return { from, to };
+}
+
+function sameMarks(left: RichTextMark[] | null, right: RichTextMark[] | null): boolean {
+ if (left === null || right === null) return left === right;
+ return left.length === right.length && left.every((mark) => right.includes(mark));
+}
+
+/** Calls `visit` for each run of units that parse the same way. */
+function forEachUnitRun(
+ units: ReadonlyArray<RichTextMark[] | null>,
+ visit: (start: number, end: number, marks: RichTextMark[] | null) => void,
+): void {
+ let start = 0;
+ while (start < units.length) {
+ let end = start + 1;
+ while (end < units.length && sameMarks(units[end]!, units[start]!)) end += 1;
+ visit(start, end, units[start]!);
+ start = end;
+ }
+}
+
+function regionUnits(doc: ProseMirrorNode, { from, to }: TextRange) {
+ const text = doc.textBetween(from, to, undefined, ATOM_TEXT);
+ return { text, units: inlineMarkdownUnitMarks(text) };
+}
+
+function collapseRegion(tr: Transaction, region: TextRange, type: MarkType): void {
+ const { schema } = tr.doc.type;
+ const { units } = regionUnits(tr.doc, region);
+ tr.removeMark(region.from, region.to, type);
+ forEachUnitRun(units, (start, end, marks) => {
+ for (const name of marks ?? []) {
+ const markType = schema.marks[name];
+ if (markType) tr.addMark(region.from + start, region.from + end, markType.create());
+ }
+ });
+ const deletions: TextRange[] = [];
+ forEachUnitRun(units, (start, end, marks) => {
+ if (marks === null) deletions.push({ from: region.from + start, to: region.from + end });
+ });
+ for (const { from, to } of deletions.toReversed()) tr.delete(from, to);
+}
+
+/** Parses source the selection does not touch back into marks, in place. */
+function collapseMarkdownSource(
+ tr: Transaction,
+ keep: (region: TextRange) => boolean = () => false,
+): void {
+ const type = tr.doc.type.schema.marks[MARKDOWN_SOURCE_MARK];
+ if (!type) return;
+ const regions: TextRange[] = [];
+ for (const region of sourceRegions(tr.doc, type)) {
+ const extended = withAdjacentDelimiters(tr.doc, region);
+ const previous = regions.at(-1);
+ if (previous && extended.from <= previous.to) previous.to = Math.max(previous.to, extended.to);
+ else regions.push(extended);
+ }
+ for (const region of regions.toReversed()) {
+ if (keep(region)) tr.addMark(region.from, region.to, type.create());
+ else collapseRegion(tr, region, type);
+ }
+}
+
+function mapSelection(tr: Transaction, selection: Selection, map: (pos: number) => number) {
+ if (selection instanceof AllSelection) return new AllSelection(tr.doc);
+ if (selection instanceof NodeSelection) {
+ const pos = map(selection.from);
+ if (tr.doc.nodeAt(pos)) return NodeSelection.create(tr.doc, pos);
+ }
+ return TextSelection.create(tr.doc, map(selection.anchor), map(selection.head));
+}
+
+/** Parses all shown source first, so a command that splits it keeps its styles. */
+export const collapseShownMarkdownSource: Command = ({ tr }) => {
+ collapseMarkdownSource(tr);
+ return true;
+};
+
+function mapRange(mapping: Mapping, { from, to }: TextRange): TextRange {
+ return { from: mapping.map(from, 1), to: mapping.map(to, -1) };
+}
+
+/** Shows styled text the selection touches as Markdown, returning the source it added. */
+function revealStyledText(tr: Transaction, type: MarkType, typed: boolean, takesBefore: boolean) {
+ const { schema } = tr.doc.type;
+ const selection = tr.selection;
+ const ranges = collectStyledRanges(tr.doc).filter(
+ (range) => touches(selection, range) && revealsFaithfully(tr.doc, range),
+ );
+ const revealed: { range: TextRange; step: number }[] = [];
+ const insertions: { at: number; length: number; closing: boolean }[] = [];
+ for (const range of ranges.toReversed()) {
+ for (const name of STYLE_MARKS) {
+ const markType = schema.marks[name];
+ if (markType) tr.removeMark(range.from, range.to, markType);
+ }
+ let length = 0;
+ // Inserting from the end keeps earlier positions valid; at a shared
+ // position the opening marker goes in first so the closing one ends up before it.
+ const markers = range.markers.toSorted(
+ (left, right) => right.at - left.at || Number(left.closing) - Number(right.closing),
+ );
+ for (const { at, closing, text } of markers) {
+ tr.insert(at, schema.text(text, [type.create()]));
+ insertions.push({ at, length: text.length, closing });
+ length += text.length;
+ }
+ tr.addMark(range.from, range.to + length, type.create());
+ revealed.push({ range: { from: range.from, to: range.to + length }, step: tr.steps.length });
+ }
+ if (ranges.length === 0) return [];
+ // A caret that arrives at a styled edge stays outside its markers. One that
+ // just typed styled text stays inside them, so the next character continues it.
+ const map = (pos: number) => {
+ let next = pos;
+ for (const { at, length, closing } of insertions) {
+ const inside = typed && selection.empty && (closing ? takesBefore : !takesBefore);
+ if (at < pos || (at === pos && (selection instanceof NodeSelection || closing !== inside))) {
+ next += length;
+ }
+ }
+ return next;
+ };
+ tr.setSelection(mapSelection(tr, selection, map));
+ return revealed.map(({ range, step }) => mapRange(tr.mapping.slice(step), range));
+}
+
+/** Shows styled text the selection touches as Markdown, and parses source it left. */
+function normalizeMarkdownSource(state: EditorState, typed: boolean): Transaction | null {
+ const type = state.schema.marks[MARKDOWN_SOURCE_MARK];
+ if (!type) return null;
+ const tr = state.tr;
+ collapseMarkdownSource(tr, (region) => touches(state.selection, region));
+ revealStyledText(tr, type, typed, caretTakesMarksBefore(state));
+ return tr.docChanged ? tr : null;
+}
+
+/**
+ * After undo or redo, history items may point inside shown source, and a
+ * rewrite in the history would clear redo. So until the next recorded edit,
+ * rewrites stay out of it and only remove text they added: source the history
+ * knows is hidden instead of parsed, and source revealed here is parsed back.
+ */
+function normalizeDetached(state: EditorState, revealed: TextRange[]): Transaction | null {
+ const type = state.schema.marks[MARKDOWN_SOURCE_MARK];
+ if (!type) return null;
+ const tr = state.tr;
+ const kept: TextRange[] = [];
+ for (const region of revealed.toSorted((left, right) => right.from - left.from)) {
+ if (touches(state.selection, region)) kept.push(region);
+ else collapseRegion(tr, region, type);
+ }
+ const added = revealStyledText(tr, type, false, caretTakesMarksBefore(state));
+ if (!tr.docChanged) return null;
+ const shown = [...kept.map((region) => mapRange(tr.mapping, region)), ...added];
+ return tr.setMeta(markdownSourceKey, shown).setMeta("addToHistory", false);
+}
+
+/** Parses the source `normalizeDetached` revealed, leaving the document the history knows. */
+function collapseRevealed(state: EditorState, revealed: TextRange[]): Transaction {
+ const tr = state.tr;
+ const type = state.schema.marks[MARKDOWN_SOURCE_MARK];
+ if (type) {
+ for (const region of revealed.toSorted((left, right) => right.from - left.from)) {
+ collapseRegion(tr, region, type);
+ }
+ }
+ return tr.setMeta(markdownSourceKey, []).setMeta("addToHistory", false);
+}
+
+/** Replays `edit` after undoing `collapse`, so the history records the reveal with the edit. */
+function rebaseEdit(tr: Transaction, collapse: Transaction, edit: Transaction): Transaction {
+ for (let index = collapse.steps.length - 1; index >= 0; index--) {
+ tr.step(collapse.steps[index]!.invert(collapse.docs[index]!));
+ }
+ for (const step of edit.steps) tr.step(step);
+ tr.setSelection(edit.selection.map(tr.doc, StepMap.empty)).setTime(edit.time);
+ if (edit.storedMarksSet) tr.setStoredMarks(edit.storedMarks);
+ if (edit.scrolledIntoView) tr.scrollIntoView();
+ // Metadata has no public accessor, and grouping, paste rules and composition read it.
+ type WithMeta = { meta: Record<string, unknown> };
+ Object.assign((tr as unknown as WithMeta).meta, (edit as unknown as WithMeta).meta);
+ return tr;
+}
+
+const replayKey = new PluginKey("composer-markdown-source-replay");
+
+/**
+ * Replays the edit a request carries after the source plugin appended its collapse,
+ * in the request's one view update so an IME keeps its preedit. History ignores what
+ * is appended to an unrecorded transaction, so the request itself records nothing.
+ */
+const replayPlugin = new Plugin({
+ key: replayKey,
+ appendTransaction([request, collapse], _, state) {
+ const edit = request?.getMeta(replayKey) as Transaction | undefined;
+ // Appended transactions otherwise join the previous undo event.
+ return edit && collapse ? closeHistory(rebaseEdit(state.tr, collapse, edit)) : null;
+ },
+});
+
+function previewDecorations(
+ doc: ProseMirrorNode,
+ type: MarkType,
+ hidden: Selection | null,
+): DecorationSet {
+ const decorations: Decoration[] = [];
+ for (const region of sourceRegions(doc, type)) {
+ const hide = hidden !== null && !touches(hidden, region);
+ const { text, units } = regionUnits(doc, region);
+ const codeTicks = new Set<number>();
+ forEachUnitRun(units, (start, end, marks) => {
+ for (const name of marks ?? []) {
+ if (name !== "code") {
+ decorations.push(
+ Decoration.inline(region.from + start, region.from + end, {
+ nodeName: PREVIEW_TAGS[name],
+ }),
+ );
+ continue;
+ }
+ // One element holds the ticks and the code, so a right-to-left line
+ // keeps both ticks on their own sides of it.
+ let from = start;
+ let to = end;
+ if (units[from - 1] === null && text[from - 1] === "`") codeTicks.add((from -= 1));
+ if (units[to] === null && text[to] === "`") codeTicks.add(to++);
+ decorations.push(
+ Decoration.inline(region.from + from, region.from + to, { nodeName: "code", dir: "ltr" }),
+ );
+ }
+ });
+ forEachUnitRun(
+ units.map((marks, index) => (marks === null && (hide || !codeTicks.has(index)) ? null : [])),
+ (start, end, marks) => {
+ if (marks !== null) return;
+ decorations.push(
+ Decoration.inline(region.from + start, region.from + end, {
+ class: hide ? "composer-rich-marker-hidden" : "composer-rich-marker",
+ }),
+ );
+ },
+ );
+ }
+ return DecorationSet.create(doc, decorations);
+}
+
+type MarkdownSourceState = {
+ decorations: DecorationSet;
+ /** From an undo or redo until the next recorded edit; see `normalizeDetached`. */
+ detached: boolean;
+ /** Source `normalizeDetached` revealed, which the history knows as styled text. */
+ revealed: TextRange[];
+};
+
+const markdownSourceKey = new PluginKey<MarkdownSourceState>("composer-markdown-source");
+
+function markdownSourcePlugin(type: MarkType): Plugin<MarkdownSourceState> {
+ let view: EditorView | null = null;
+ return new Plugin<MarkdownSourceState>({
+ key: markdownSourceKey,
+ view(editorView) {
+ view = editorView;
+ return {
+ destroy() {
+ view = null;
+ },
+ };
+ },
+ state: {
+ init: (_, state) => ({
+ decorations: previewDecorations(state.doc, type, null),
+ detached: false,
+ revealed: [],
+ }),
+ apply: (tr, old, _, state) => {
+ const history = isHistoryTransaction(tr);
+ const detached =
+ history || (old.detached && !(tr.docChanged && tr.getMeta("addToHistory") !== false));
+ const own = tr.getMeta(markdownSourceKey) as TextRange[] | undefined;
+ const revealed =
+ !detached || history
+ ? []
+ : (own ??
+ (tr.docChanged
+ ? old.revealed.map((range) => mapRange(tr.mapping, range))
+ : old.revealed));
+ const redraw = tr.docChanged || detached !== old.detached || (detached && tr.selectionSet);
+ return {
+ decorations: redraw
+ ? previewDecorations(state.doc, type, detached ? state.selection : null)
+ : old.decorations,
+ detached,
+ revealed,
+ };
+ },
+ },
+ props: {
+ decorations: (state) => markdownSourceKey.getState(state)?.decorations,
+ },
+ appendTransaction(transactions, _, state) {
+ if (transactions[0]?.getMeta(replayKey)) {
+ return collapseRevealed(state, markdownSourceKey.getState(state)?.revealed ?? []);
+ }
+ // Rewriting text mid-composition would break the IME's preedit.
+ if (view?.composing) return null;
+ // Rewrites appended to an undo or redo join its history step, which leaves
+ // the other direction unmapped. The dispatch hook normalizes after it instead.
+ if (transactions.some((tr) => isHistoryTransaction(tr) || tr.getMeta(markdownSourceKey))) {
+ return null;
+ }
+ if (!transactions.some((tr) => tr.docChanged || tr.selectionSet)) return null;
+ const source = markdownSourceKey.getState(state);
+ if (source?.detached) return normalizeDetached(state, source.revealed);
+ const tr = normalizeMarkdownSource(
+ state,
+ transactions.some((item) => item.docChanged),
+ );
+ // With nothing to undo, a recorded rewrite would only add a no-op undo.
+ if (tr && undoDepth(state) === 0) tr.setMeta("addToHistory", false);
+ return tr;
+ },
+ });
+}
+
+/** Keeps source `normalizeDetached` revealed out of what undo, redo and edits see. */
+const MarkdownSourceHistoryExtension = Extension.create({
+ name: "composer-markdown-source-history",
+ // Runs after undo grouping, so a rebased edit keeps the grouping chosen for it.
+ priority: 1,
+ dispatchTransaction({ transaction, next }) {
+ const { editor } = this;
+ const revealed = markdownSourceKey.getState(editor.state)?.revealed ?? [];
+ if (isHistoryTransaction(transaction)) {
+ if (revealed.length === 0) {
+ next(transaction);
+ } else {
+ const undoing = redoDepth(editor.state.apply(transaction)) > redoDepth(editor.state);
+ next(collapseRevealed(editor.state, revealed));
+ (undoing ? undo : redo)(editor.state, next);
+ }
+ const source = markdownSourceKey.getState(editor.state);
+ const tr = editor.view.composing
+ ? null
+ : normalizeDetached(editor.state, source?.revealed ?? []);
+ if (tr) next(tr);
+ return;
+ }
+ const recordedEdit =
+ transaction.docChanged &&
+ transaction.getMeta("addToHistory") !== false &&
+ transaction.getMeta(markdownSourceKey) === undefined;
+ if (revealed.length === 0 || !recordedEdit) {
+ next(transaction);
+ return;
+ }
+ next(editor.state.tr.setMeta(replayKey, transaction));
+ },
+});
+
+export const ComposerMarkdownSourceExtension = TiptapMark.create({
+ name: MARKDOWN_SOURCE_MARK,
+ // Its shortcuts run ahead of the style marks' own.
+ priority: 1001,
+ inclusive: false,
+ parseHTML() {
+ return [{ tag: "span.composer-markdown-source" }];
+ },
+ renderHTML() {
+ return ["span", { class: "composer-markdown-source" }, 0];
+ },
+ addKeyboardShortcuts() {
+ // Shown markers are plain text, so toggling a style there would wrap them
+ // in another style. Parse the source first and toggle the result.
+ const toggle = (name: RichTextMark) => () => {
+ const { state } = this.editor;
+ if (state.selection.empty) return false;
+ const touched = sourceRegions(state.doc, this.type).some((region) =>
+ touches(state.selection, region),
+ );
+ if (!touched) return false;
+ return this.editor.chain().command(collapseShownMarkdownSource).toggleMark(name).run();
+ };
+ return {
+ "Mod-b": toggle("bold"),
+ "Mod-B": toggle("bold"),
+ "Mod-i": toggle("italic"),
+ "Mod-I": toggle("italic"),
+ "Mod-Shift-s": toggle("strike"),
+ "Mod-e": toggle("code"),
+ };
+ },
+ addExtensions() {
+ return [MarkdownSourceHistoryExtension];
+ },
+ addProseMirrorPlugins() {
+ return [markdownSourcePlugin(this.type), replayPlugin];
+ },
+});
diff --git a/apps/web/src/composer-rich-text-doc.ts b/apps/web/src/composer-rich-text-doc.ts
index ae0c9441625..95975bf2857 100644
--- a/apps/web/src/composer-rich-text-doc.ts
+++ b/apps/web/src/composer-rich-text-doc.ts
@@ -42,7 +42,12 @@ import { ReplaceStep } from "@tiptap/pm/transform";
import { splitPromptIntoComposerSegments } from "~/composer-editor-mentions";
import { nextOrderedMarkerText } from "~/composer-list-continuation";
-import { parseInlineMarkdown, RICH_TEXT_DELIMITERS, type RichTextMark } from "~/composer-rich-text";
+import {
+ MARKDOWN_SOURCE_MARK,
+ parseInlineMarkdown,
+ RICH_TEXT_DELIMITERS,
+ type RichTextMark,
+} from "~/composer-rich-text";
import { collectInlineContextIds } from "~/lib/composerContextReferences";
import { composerMathRanges } from "@t3tools/shared/composerInlineTokens";
@@ -90,6 +95,7 @@ function codePairCaretPlugin(type: MarkType) {
if (view.composing || from !== to || text !== "`") return false;
const $from = view.state.doc.resolve(from);
if (!$from.parent.isTextblock || $from.parent.type.spec.code) return false;
+ if (touchesMarkdownSource(view.state, from, to)) return false;
const before = $from.nodeBefore?.text ?? "";
if (closesBacktickPair($from)) {
view.dispatch(insert().setMeta(codePairKey, { from: from - 1, to: from + 1 }));
@@ -218,7 +224,10 @@ export const ComposerCodeExtension = Code.extend({
find: rule.find,
undoable: rule.undoable,
handler: (props) => {
- if (literalBackticksBefore(props.state.doc.resolve(props.range.from)) % 2 === 1) {
+ if (
+ literalBackticksBefore(props.state.doc.resolve(props.range.from)) % 2 === 1 ||
+ touchesMarkdownSource(props.state, props.range.from, props.range.to)
+ ) {
return null;
}
@@ -246,6 +255,18 @@ function literalBackticksBefore($pos: ResolvedPos): number {
return count;
}
+/**
+ * Whether `from`-`to`, or the caret at `from`, is in shown Markdown source.
+ * Typed delimiters there stay text until the caret leaves and the source is parsed.
+ */
+function touchesMarkdownSource(state: EditorState, from: number, to: number): boolean {
+ const type = state.schema.marks[MARKDOWN_SOURCE_MARK];
+ if (!type) return false;
+ if (from < to) return state.doc.rangeHasMark(from, to, type);
+ const $pos = state.doc.resolve(from);
+ return [$pos.nodeBefore, $pos.nodeAfter].some((node) => node && type.isInSet(node.marks));
+}
+
// Code permits nested emphasis, so Tiptap's exclusion check cannot protect literal delimiters.
function markInputRuleOutsideCode(find: RegExp, type: MarkType) {
const rule = markInputRule({ find, type });
@@ -257,6 +278,7 @@ function markInputRuleOutsideCode(find: RegExp, type: MarkType) {
const content = match[match.length - 1] ?? "";
const textStart = range.from + match[0].indexOf(content);
const textEnd = textStart + content.length;
+ if (touchesMarkdownSource(state, range.from, range.to)) return null;
if (
code &&
(state.doc.rangeHasMark(range.from + match[0].search(/\S/), textStart, code) ||
@@ -1206,7 +1228,8 @@ function renumberFollowingItems(
});
}
-export function splitOrLiftListItem(editor: Editor): boolean {
+/** `prepare` runs first in the same transaction, which the split then reads. */
+export function splitOrLiftListItem(editor: Editor, prepare: Command): boolean {
const $from = editor.state.selection.$from;
const itemType = listItemTypeAt($from);
if (!itemType) return false;
@@ -1221,7 +1244,7 @@ export function splitOrLiftListItem(editor: Editor): boolean {
if (moved) alignListItemWithSiblings(tr, itemType);
return moved;
};
- return editor.can().command(move) && editor.chain().command(move).run();
+ return editor.can().command(move) && editor.chain().command(prepare).command(move).run();
}
export function convertBulletItemToTask(
@@ -1639,15 +1662,20 @@ export function pmToFlat(map: RichDocMap, pmPos: number): number {
// Styled boundaries need two caret stops at one position, one for each side's marks.
+// Shown Markdown source has real characters at its edges, so it needs no extra stop.
+function styleMarks(marks: readonly Mark[]): readonly Mark[] {
+ return marks.filter((mark) => mark.type.name !== MARKDOWN_SOURCE_MARK);
+}
+
function styledEdge(state: EditorState) {
const { selection } = state;
if (!selection.empty) return null;
const { $from } = selection;
if (!$from.parent.inlineContent) return null;
- const before = $from.nodeBefore?.marks ?? Mark.none;
- const after = $from.nodeAfter?.marks ?? Mark.none;
+ const before = styleMarks($from.nodeBefore?.marks ?? Mark.none);
+ const after = styleMarks($from.nodeAfter?.marks ?? Mark.none);
if (Mark.sameSet(before, after)) return null;
- return { before, after, current: state.storedMarks ?? $from.marks() };
+ return { before, after, current: styleMarks(state.storedMarks ?? $from.marks()) };
}
export function caretTakesMarksBefore(state: EditorState): boolean {
diff --git a/apps/web/src/composer-rich-text.test.ts b/apps/web/src/composer-rich-text.test.ts
index 3fca4b7196c..f967cfd3a30 100644
--- a/apps/web/src/composer-rich-text.test.ts
+++ b/apps/web/src/composer-rich-text.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vite-plus/test";
-import { parseInlineMarkdown } from "./composer-rich-text";
+import { inlineMarkdownUnitMarks, parseInlineMarkdown } from "./composer-rich-text";
describe("composer rich text markdown", () => {
it("preserves unmatched markers, escaped markers, and identifiers", () => {
@@ -56,4 +56,22 @@ describe("composer rich text markdown", () => {
{ text: "**not bold**", marks: ["code"] },
]);
});
+
+ it("maps each source character to its marks, or null for a dropped delimiter", () => {
+ const bold = ["bold"];
+ expect(inlineMarkdownUnitMarks("**`*` w** \\*")).toEqual([
+ null,
+ null,
+ null,
+ ["bold", "code"],
+ null,
+ bold,
+ bold,
+ null,
+ null,
+ [],
+ [],
+ [],
+ ]);
+ });
});
diff --git a/apps/web/src/composer-rich-text.ts b/apps/web/src/composer-rich-text.ts
index 2a08f916365..49cca9f5ec9 100644
--- a/apps/web/src/composer-rich-text.ts
+++ b/apps/web/src/composer-rich-text.ts
@@ -12,29 +12,35 @@ export const RICH_TEXT_DELIMITERS: Record<RichTextMark, string> = {
code: "`",
};
-function pushSpan(spans: RichTextSpan[], text: string, marks: RichTextMark[]): void {
- if (!text) return;
- const last = spans[spans.length - 1];
- if (
- last &&
- last.marks.length === marks.length &&
- last.marks.every((mark, index) => mark === marks[index])
- ) {
- last.text += text;
- } else {
- spans.push({ text, marks });
- }
+/** A run of `text` from `start` to `end` that parses with `marks`. */
+interface SourcePiece {
+ start: number;
+ end: number;
+ marks: RichTextMark[];
}
-export function parseInlineMarkdown(text: string): RichTextSpan[] {
- const root: RichTextSpan[] = [];
- const stack: { delimiter: string; mark: RichTextMark; spans: RichTextSpan[] }[] = [];
- const current = () => stack.at(-1)?.spans ?? root;
+function sameMarks(left: readonly RichTextMark[], right: readonly RichTextMark[]): boolean {
+ return left.length === right.length && left.every((mark, index) => mark === right[index]);
+}
+
+// Pieces keep source offsets, so callers can map parsed text back onto the
+// characters it came from; parsing only drops delimiters.
+function parseInlineMarkdownPieces(text: string): SourcePiece[] {
+ const root: SourcePiece[] = [];
+ const stack: { delimiter: string; mark: RichTextMark; start: number; pieces: SourcePiece[] }[] =
+ [];
+ const push = (start: number, end: number, marks: RichTextMark[] = []) => {
+ if (end <= start) return;
+ const pieces = stack.at(-1)?.pieces ?? root;
+ const last = pieces.at(-1);
+ if (last?.end === start && sameMarks(last.marks, marks)) last.end = end;
+ else pieces.push({ start, end, marks });
+ };
let index = 0;
while (index < text.length) {
const char = text[index]!;
if (char === "\\") {
- pushSpan(current(), text.slice(index, index + 2), []);
+ push(index, Math.min(index + 2, text.length));
index += 2;
continue;
}
@@ -47,16 +53,16 @@ export function parseInlineMarkdown(text: string): RichTextSpan[] {
!text.slice(index, close).includes("\n") &&
text[close + 1] !== "`"
) {
- pushSpan(current(), text.slice(index + 1, close), ["code"]);
+ push(index + 1, close, ["code"]);
index = close + 1;
} else {
- pushSpan(current(), run, []);
+ push(index, index + run.length);
index += run.length;
}
continue;
}
if (char !== "*" && char !== "_" && char !== "~") {
- pushSpan(current(), char, []);
+ push(index, index + 1);
index += 1;
continue;
}
@@ -82,33 +88,54 @@ export function parseInlineMarkdown(text: string): RichTextSpan[] {
index + top.delimiter.length <= end
) {
stack.pop();
- for (const span of top.spans) pushSpan(current(), span.text, [top.mark, ...span.marks]);
+ for (const piece of top.pieces) push(piece.start, piece.end, [top.mark, ...piece.marks]);
index += top.delimiter.length;
} else if (canOpen && (char !== "~" || end - index >= 2)) {
const length = char === "~" || end - index >= 2 ? 2 : 1;
const mark = char === "~" ? "strike" : length === 2 ? "bold" : "italic";
// Prevent self-nesting, including alternate delimiters, to bound the stack by supported styles.
if (stack.some((frame) => frame.mark === mark)) {
- pushSpan(current(), text.slice(index, end), []);
+ push(index, end);
index = end;
continue;
}
- stack.push({
- delimiter: char.repeat(length),
- mark,
- spans: [],
- });
+ stack.push({ delimiter: char.repeat(length), mark, start: index, pieces: [] });
index += length;
} else {
- pushSpan(current(), text.slice(index, end), []);
+ push(index, end);
index = end;
}
}
}
while (stack.length > 0) {
const frame = stack.pop()!;
- pushSpan(current(), frame.delimiter, []);
- for (const span of frame.spans) pushSpan(current(), span.text, span.marks);
+ push(frame.start, frame.start + frame.delimiter.length);
+ for (const piece of frame.pieces) push(piece.start, piece.end, piece.marks);
}
return root;
}
+
+export function parseInlineMarkdown(text: string): RichTextSpan[] {
+ const spans: RichTextSpan[] = [];
+ for (const { start, end, marks } of parseInlineMarkdownPieces(text)) {
+ const last = spans.at(-1);
+ if (last && sameMarks(last.marks, marks)) {
+ last.text += text.slice(start, end);
+ } else {
+ spans.push({ text: text.slice(start, end), marks });
+ }
+ }
+ return spans;
+}
+
+/** Marks text the composer shows as editable Markdown source while the caret is in it. */
+export const MARKDOWN_SOURCE_MARK = "markdownSource";
+
+/** Marks for each UTF-16 unit of `text`, or null for a delimiter that parsing drops. */
+export function inlineMarkdownUnitMarks(text: string): Array<RichTextMark[] | null> {
+ const result: Array<RichTextMark[] | null> = Array.from({ length: text.length }, () => null);
+ for (const { start, end, marks } of parseInlineMarkdownPieces(text)) {
+ result.fill(marks, start, end);
+ }
+ return result;
+}
diff --git a/apps/web/src/index.css b/apps/web/src/index.css
index 15dd5e14d33..3f3e1c742f4 100644
--- a/apps/web/src/index.css
+++ b/apps/web/src/index.css
@@ -2446,14 +2446,18 @@ code {
padding: 0.05em 0.25em;
}
+/* Markdown delimiters shown, as editable text, around styled text with the caret in it. */
.composer-rich-marker {
color: var(--muted-foreground);
opacity: 0.75;
font-weight: 400;
font-style: normal;
text-decoration: none;
- user-select: none;
- pointer-events: none;
+}
+
+/* Delimiters of source restored by undo or redo, once the caret leaves it. */
+.composer-rich-marker-hidden {
+ display: none;
}
/* Selected chips (node selection from arrowing into or clicking a chip). */
diff --git a/docs/user/composer.md b/docs/user/composer.md
index aa670e0b4da..c236da3c82c 100644
--- a/docs/user/composer.md
+++ b/docs/user/composer.md
@@ -14,12 +14,19 @@ to keep a large paste editable in the composer instead.
## Formatting
-The composer writes Markdown and shows it styled as you type. Markers such as
-`**` stay in the text and show beside the styled words when your cursor is on
-them. Lines starting with `- `, `1. `, `- [ ] `, `> `, `# ` or `---` become lists,
-task lists, quotes, headings and rules; ` ``` ` followed by Enter opens a code
-block. What you typed is what the agent receives, markers and numbering
-included, and `#1234` without a space still looks up a pull request.
+The composer writes Markdown and shows it styled as you type. When your cursor
+is on styled text, its Markdown shows instead, such as `**bold**`, and the
+markers edit like any other text: delete them to remove the style, or change
+`**` to `*` for italic. The style updates when your cursor leaves. To write
+plain Markdown with no styling, turn off **Settings → Chat → Rich text
+composer**. Lines starting with `- `, `1. `, `- [ ] `, `> `, `# ` or `---`
+become lists, task lists, quotes, headings and rules; ` ``` ` followed by Enter
+opens a code block. What you typed is what the agent receives, markers and
+numbering included, and `#1234` without a space still looks up a pull request.
+
+Some styled text containing or touching literal Markdown characters, such as a
+bold `path\` or `2*3`, keeps its styling without showing markers. Use formatting
+shortcuts or turn off rich text to edit the formatting of those ranges.
Enter sends. In a list or quote, **Shift+Enter** continues it, and Shift+Enter
on an empty line moves out one level, then leaves it; **Tab** nests a list item