Skip to content

Commit 1653604

Browse files
committed
fix(web): type plain text after a bold paste
Pasting styled Markdown leaves its mark active at the caret, so the next keystroke extends the pasted styling. After insertion, use the marks of the following text or plain text at a line end for continued typing. Done by GPT-6-Sol in Codex.
1 parent 720490a commit 1653604

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
serializeEditorDoc,
5656
type SkillMeta,
5757
} from "~/composer-rich-text-doc";
58+
import { setTypingMarksAfterPaste } from "~/composer-paste-marks";
5859
import { collectInlineContextIds } from "~/lib/composerContextReferences";
5960
import { cn, isMacPlatform } from "~/lib/utils";
6061
import { basenameOfPath } from "~/pierre-icons";
@@ -971,6 +972,10 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
971972
insertMarkdownParagraphs(text, skillLabelFor, { styling: richText }, (content) => {
972973
editorInstance.commands.insertContent(content);
973974
});
975+
if (richText) {
976+
const resetMarks = setTypingMarksAfterPaste(editorInstance.state);
977+
if (resetMarks) editorInstance.view.dispatch(resetMarks);
978+
}
974979
scrollTiptapCaretIntoView(editorInstance);
975980
}
976981
return true;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Schema } from "@tiptap/pm/model";
2+
import { EditorState, TextSelection } from "@tiptap/pm/state";
3+
import { describe, expect, it } from "vite-plus/test";
4+
5+
import { serializeEditorDoc } from "./composer-rich-text-doc";
6+
import { setTypingMarksAfterPaste } from "./composer-paste-marks";
7+
8+
const schema = new Schema({
9+
nodes: {
10+
doc: { content: "block+" },
11+
paragraph: { content: "inline*", group: "block" },
12+
text: { group: "inline" },
13+
},
14+
marks: { bold: {}, italic: {} },
15+
});
16+
17+
describe("typing after pasted formatting", () => {
18+
it("continues in plain text after a bold paste at the end of a line", () => {
19+
const doc = schema.node("doc", null, [
20+
schema.node("paragraph", null, [schema.text("bold", [schema.marks.bold!.create()])]),
21+
]);
22+
const state = EditorState.create({ doc, selection: TextSelection.create(doc, 5) });
23+
24+
expect(serializeEditorDoc(state.apply(state.tr.insertText(" plain")).doc).value).toBe(
25+
"**bold plain**",
26+
);
27+
const continued = state.apply(setTypingMarksAfterPaste(state)!);
28+
expect(serializeEditorDoc(continued.apply(continued.tr.insertText(" plain")).doc).value).toBe(
29+
"**bold** plain",
30+
);
31+
});
32+
33+
it("uses the formatting of following text when pasting in the middle", () => {
34+
const doc = schema.node("doc", null, [
35+
schema.node("paragraph", null, [
36+
schema.text("bold", [schema.marks.bold!.create()]),
37+
schema.text("tail", [schema.marks.italic!.create()]),
38+
]),
39+
]);
40+
const state = EditorState.create({ doc, selection: TextSelection.create(doc, 5) });
41+
const continued = state.apply(setTypingMarksAfterPaste(state)!);
42+
const withText = continued.apply(continued.tr.insertText("x")).doc;
43+
44+
expect(withText.child(0).child(1).text).toBe("xtail");
45+
expect(
46+
withText
47+
.child(0)
48+
.child(1)
49+
.marks.map((mark) => mark.type.name),
50+
).toEqual(["italic"]);
51+
});
52+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { EditorState, Transaction } from "@tiptap/pm/state";
2+
3+
/** Continue after pasted formatting with the text on the right, or plain at a line end. */
4+
export function setTypingMarksAfterPaste(state: EditorState): Transaction | null {
5+
if (!state.selection.empty) return null;
6+
return state.tr.setStoredMarks(state.selection.$from.nodeAfter?.marks ?? []);
7+
}

0 commit comments

Comments
 (0)