Skip to content

Commit cc1d991

Browse files
committed
fix(web): escape formatting at composer boundaries
Reset implicit typing marks when placing the caret, pasting, cutting, or restoring history. Keep normal typing inclusive so explicit formatting shortcuts continue working across keystrokes.
1 parent 3a9c1a6 commit cc1d991

3 files changed

Lines changed: 203 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
groupUndoByChangeKind,
6363
markAsClipboardEdit,
6464
} from "~/composer-undo-grouping";
65+
import { ComposerTypingMarksExtension } from "~/composer-typing-marks";
6566
import { collectInlineContextIds } from "~/lib/composerContextReferences";
6667
import { cn, isMacPlatform } from "~/lib/utils";
6768
import { basenameOfPath } from "~/pierre-icons";
@@ -832,6 +833,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
832833
ComposerMarkersExtension,
833834
...(richText
834835
? [
836+
ComposerTypingMarksExtension,
835837
ComposerCodeExtension,
836838
TaskList,
837839
ComposerTaskItemExtension.extend({
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import { getSchema } from "@tiptap/core";
2+
import StarterKit from "@tiptap/starter-kit";
3+
import { history, redo, undo } from "@tiptap/pm/history";
4+
import { EditorState, TextSelection } from "@tiptap/pm/state";
5+
import { describe, expect, it } from "vite-plus/test";
6+
7+
import { buildDocJson, ComposerCodeExtension, serializeEditorDoc } from "./composer-rich-text-doc";
8+
import { createComposerTypingMarksPlugin } from "./composer-typing-marks";
9+
10+
const schema = getSchema([StarterKit.configure({ code: false }), ComposerCodeExtension]);
11+
12+
function createState(value: string, cursor: number) {
13+
const doc = schema.nodeFromJSON(buildDocJson(value, () => ({ label: "", description: null })));
14+
return EditorState.create({
15+
doc,
16+
selection: TextSelection.create(doc, cursor),
17+
plugins: [createComposerTypingMarksPlugin(), history()],
18+
});
19+
}
20+
21+
function move(state: EditorState, cursor: number) {
22+
return state.applyTransaction(state.tr.setSelection(TextSelection.create(state.doc, cursor)))
23+
.state;
24+
}
25+
26+
function type(state: EditorState, text: string) {
27+
for (const character of text)
28+
state = state.applyTransaction(state.tr.insertText(character)).state;
29+
return state;
30+
}
31+
32+
function value(state: EditorState) {
33+
return serializeEditorDoc(state.doc).value;
34+
}
35+
36+
describe("composer typing marks after moving the caret", () => {
37+
it.each(["**b**", "*b*", "~~b~~", "`b`", "***b***", "**`b`**"])(
38+
"types plain text after navigating away from and back to %s",
39+
(markdown) => {
40+
let state = createState(markdown, 2);
41+
state = state.applyTransaction(state.tr.setStoredMarks([])).state;
42+
state = move(move(state, 1), 2);
43+
expect(value(type(state, " plain"))).toBe(`${markdown} plain`);
44+
},
45+
);
46+
47+
it("covers caret placement after a controlled rewrite, including an unchanged position", () => {
48+
let state = createState("x", 2);
49+
const content = schema.nodeFromJSON(
50+
buildDocJson("**b**", () => ({ label: "", description: null })),
51+
);
52+
state = state.applyTransaction(
53+
state.tr.replaceWith(0, state.doc.content.size, content.content),
54+
).state;
55+
state = move(state, 2);
56+
expect(value(type(state, " plain"))).toBe("**b** plain");
57+
});
58+
59+
it("resets typing marks after a paste and subsequent navigation", () => {
60+
let state = createState("", 1);
61+
const content = schema.nodeFromJSON(
62+
buildDocJson("**b**", () => ({ label: "", description: null })),
63+
);
64+
state = state.applyTransaction(
65+
state.tr.replaceSelectionWith(content.firstChild!, false).setMeta("uiEvent", "paste"),
66+
).state;
67+
expect(value(type(state, " plain"))).toBe("**b** plain");
68+
state = move(move(state, 1), 2);
69+
expect(value(type(state, " plain"))).toBe("**b** plain");
70+
});
71+
72+
it("continues the following span after pasting in the middle of a line", () => {
73+
let state = createState("tail", 1);
74+
state = state.applyTransaction(
75+
state.tr
76+
.insertText("b")
77+
.addMark(1, 2, schema.marks.bold!.create())
78+
.setMeta("uiEvent", "paste"),
79+
).state;
80+
expect(value(type(state, "xy"))).toBe("**b**xytail");
81+
});
82+
83+
it("types plain after cutting back to a closing boundary", () => {
84+
let state = createState("**b** plain", 8);
85+
state = state.applyTransaction(
86+
state.tr
87+
.setSelection(TextSelection.create(state.doc, 2, 8))
88+
// Tiptap's deleteSelection command uses deleteRange without ensureMarks.
89+
.deleteRange(2, 8)
90+
.setMeta("uiEvent", "cut"),
91+
).state;
92+
expect(value(state)).toBe("**b**");
93+
expect(value(type(state, " plain"))).toBe("**b** plain");
94+
});
95+
96+
it("preserves formatting while typing inside a span", () => {
97+
const state = move(createState("**bold**", 5), 3);
98+
expect(value(type(state, "xy"))).toBe("**boxyld**");
99+
});
100+
101+
it("adopts following text marks at a boundary", () => {
102+
const state = move(createState("**b***tail*", 6), 2);
103+
expect(value(type(state, "xy"))).toBe("**b***xytail*");
104+
});
105+
106+
it("matches the opening marker when placing the caret before formatted text", () => {
107+
const state = move(createState("x **bold**", 7), 3);
108+
expect(value(type(state, "xy"))).toBe("x **xybold**");
109+
});
110+
111+
it("preserves explicit formatting on and off over several keystrokes", () => {
112+
let state = move(createState("**b**", 2), 2);
113+
state = state.applyTransaction(state.tr.addStoredMark(schema.marks.bold!.create())).state;
114+
state = type(state, "xy");
115+
expect(value(state)).toBe("**bxy**");
116+
state = state.applyTransaction(state.tr.removeStoredMark(schema.marks.bold!)).state;
117+
expect(value(type(state, " plain"))).toBe("**bxy** plain");
118+
});
119+
120+
it("preserves an explicit mark decision in a selection transaction", () => {
121+
let state = createState("**b**", 1);
122+
state = state.applyTransaction(
123+
state.tr
124+
.setSelection(TextSelection.create(state.doc, 2))
125+
.addStoredMark(schema.marks.italic!.create()),
126+
).state;
127+
expect(value(type(state, "xy"))).toBe("**b*xy***");
128+
});
129+
130+
it("does not alter ranged selection replacement", () => {
131+
let state = createState("**bold**", 5);
132+
state = state.applyTransaction(
133+
state.tr.setSelection(TextSelection.create(state.doc, 2, 4)),
134+
).state;
135+
expect(value(type(state, "x"))).toBe("**bxd**");
136+
});
137+
138+
it("leaves normal typing transactions alone", () => {
139+
const state = createState("**b**", 2);
140+
expect(value(type(state, "xy"))).toBe("**bxy**");
141+
expect(state.applyTransaction(state.tr.scrollIntoView()).transactions).toHaveLength(1);
142+
});
143+
144+
it("adds no undo step and preserves undo/redo content and selection", () => {
145+
let state = move(move(createState("**b**", 2), 1), 2);
146+
state = type(state, " plain");
147+
expect(
148+
undo(state, (transaction) => {
149+
state = state.applyTransaction(transaction).state;
150+
}),
151+
).toBe(true);
152+
expect(value(state)).toBe("**b**");
153+
expect(state.selection.from).toBe(2);
154+
expect(value(type(state, " again"))).toBe("**b** again");
155+
expect(
156+
redo(state, (transaction) => {
157+
state = state.applyTransaction(transaction).state;
158+
}),
159+
).toBe(true);
160+
expect(value(state)).toBe("**b** plain");
161+
expect(state.selection.from).toBe(8);
162+
});
163+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Extension } from "@tiptap/core";
2+
import { isHistoryTransaction } from "@tiptap/pm/history";
3+
import { Mark } from "@tiptap/pm/model";
4+
import { Plugin } from "@tiptap/pm/state";
5+
6+
/**
7+
* Follow the text on the right when moving the caret, or type plain at a line end.
8+
* Keep inclusive marks during edits so explicit formatting shortcuts continue typing.
9+
*/
10+
export function createComposerTypingMarksPlugin() {
11+
return new Plugin({
12+
appendTransaction(transactions, _oldState, state) {
13+
if (
14+
!state.selection.empty ||
15+
state.storedMarks !== null ||
16+
!transactions.some(
17+
(transaction) =>
18+
(transaction.selectionSet && !transaction.docChanged) ||
19+
isHistoryTransaction(transaction) ||
20+
transaction.getMeta("uiEvent") === "paste" ||
21+
transaction.getMeta("uiEvent") === "cut",
22+
)
23+
) {
24+
return null;
25+
}
26+
const marks = state.selection.$from.nodeAfter?.marks ?? [];
27+
if (Mark.sameSet(marks, state.selection.$from.marks())) return null;
28+
return state.tr.setStoredMarks(marks);
29+
},
30+
});
31+
}
32+
33+
export const ComposerTypingMarksExtension = Extension.create({
34+
name: "composer-typing-marks",
35+
addProseMirrorPlugins() {
36+
return [createComposerTypingMarksPlugin()];
37+
},
38+
});

0 commit comments

Comments
 (0)